Advertisement
Rapptz

FileIO.hpp

Feb 7th, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/stat.h>
  3. #include <dirent.h>
  4. #include <cstdio>
  5.  
  6. //wrapper class for POSIX
  7. namespace FileIO {
  8. bool fileExists(const std::string& filename) {
  9.     return access(filename.c_str(),F_OK) == 0;
  10. }
  11.  
  12. bool canAccess(const std::string& directory) {
  13.     return access(directory.c_str(),W_OK | R_OK) == 0;
  14. }
  15.  
  16. bool directoryExists(const std::string& directory) {
  17.     struct stat st;
  18.     if(stat(directory.c_str(),&st) == 0)
  19.         return true;
  20.     else
  21.         return false;
  22. }
  23. void createDirectory(const std::string& directory) {
  24.     if(directory.length() > 0)
  25.         mkdir(directory.c_str());
  26. }
  27.  
  28. void clearFolder(const std::string& folder) {
  29.     struct dirent* nextFile;
  30.     DIR* dir = opendir(folder.c_str());
  31.     char filepath[256];
  32.     while((nextFile = readdir(dir))) {
  33.         sprintf(filepath, "%s/%s",folder.c_str(),nextFile->d_name);
  34.         remove(filepath);
  35.     }
  36. }
  37.  
  38. void deleteFolder(const std::string& folder) {
  39.     clearFolder(folder);
  40.     remove(folder.c_str());
  41. }
  42.  
  43. std::vector<std::string> listDirectories(const std::string& directory) {
  44.     std::vector<std::string> files;
  45.     DIR* dir;
  46.     struct dirent* ent;
  47.     dir = opendir(directory.c_str());
  48.     while((ent = readdir(dir)) != NULL) {
  49.         const std::string fileName = ent->d_name;
  50.         if(fileName[0] == '.')
  51.             continue;
  52.         files.push_back(fileName);
  53.     }
  54.     closedir(dir);
  55.     return files;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement