Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <cstdio>
- //wrapper class for POSIX
- namespace FileIO {
- bool fileExists(const std::string& filename) {
- return access(filename.c_str(),F_OK) == 0;
- }
- bool canAccess(const std::string& directory) {
- return access(directory.c_str(),W_OK | R_OK) == 0;
- }
- bool directoryExists(const std::string& directory) {
- struct stat st;
- if(stat(directory.c_str(),&st) == 0)
- return true;
- else
- return false;
- }
- void createDirectory(const std::string& directory) {
- if(directory.length() > 0)
- mkdir(directory.c_str());
- }
- void clearFolder(const std::string& folder) {
- struct dirent* nextFile;
- DIR* dir = opendir(folder.c_str());
- char filepath[256];
- while((nextFile = readdir(dir))) {
- sprintf(filepath, "%s/%s",folder.c_str(),nextFile->d_name);
- remove(filepath);
- }
- }
- void deleteFolder(const std::string& folder) {
- clearFolder(folder);
- remove(folder.c_str());
- }
- std::vector<std::string> listDirectories(const std::string& directory) {
- std::vector<std::string> files;
- DIR* dir;
- struct dirent* ent;
- dir = opendir(directory.c_str());
- while((ent = readdir(dir)) != NULL) {
- const std::string fileName = ent->d_name;
- if(fileName[0] == '.')
- continue;
- files.push_back(fileName);
- }
- closedir(dir);
- return files;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement