Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "FileFunctions.h"
00024 #include <vector>
00025 #include <string>
00026 #include <fstream>
00027 #include <cstdio>
00028 #include <algorithm>
00029
00030 #ifdef WIN32
00031 #include <direct.h>
00032 #include "dirent_win.h"
00033 #define mkdir(a) _mkdir(a)
00034 #else
00035 #include <dirent.h>
00036 #include <sys/stat.h>
00037 #define mkdir(a) mkdir(a, 0755)
00038 #endif
00039
00040 using namespace std;
00041 using namespace DUtils;
00042
00043 void FileFunctions::MkDir(const char *path)
00044 {
00045 mkdir(path);
00046 }
00047
00048 void FileFunctions::RmDir(const char *path)
00049 {
00050
00051 vector<string> files = FileFunctions::Dir(path, "");
00052 for(vector<string>::iterator it = files.begin(); it != files.end(); it++){
00053 remove(it->c_str());
00054 }
00055 rmdir(path);
00056 }
00057
00058 void FileFunctions::RmFile(const char *path)
00059 {
00060 remove(path);
00061 }
00062
00063 bool FileFunctions::FileExists(const char *filename)
00064 {
00065 std::fstream f(filename, ios::in);
00066
00067 if(f.is_open()){
00068 f.close();
00069 return true;
00070 }else
00071 return false;
00072 }
00073
00074 bool FileFunctions::DirExists(const char *path)
00075 {
00076 DIR *dirp;
00077 if((dirp = opendir(path)) != NULL){
00078 closedir(dirp);
00079 return true;
00080 }else
00081 return false;
00082 }
00083
00084 std::vector<std::string> FileFunctions::Dir(const char *path, const char *right,
00085 bool sorted)
00086 {
00087 DIR *dirp;
00088 struct dirent *entry;
00089 vector<string> ret;
00090
00091 if((dirp = opendir(path)) != NULL){
00092 while((entry = readdir(dirp)) != NULL){
00093 string name(entry->d_name);
00094 string r(right);
00095 if((name.length() >= r.length()) &&
00096 (name.substr(name.length() - r.length()).compare(r) == 0))
00097 {
00098 ret.push_back(string(path) + "/" + entry->d_name);
00099 }
00100 }
00101 closedir(dirp);
00102 }
00103
00104 if(sorted) sort(ret.begin(), ret.end());
00105
00106 return ret;
00107 }
00108 std::string FileFunctions::FileName(const std::string filepath)
00109 {
00110 string::size_type p = filepath.find_last_of('/');
00111 string::size_type p2 = filepath.find_last_of('\\');
00112 if(p2 != string::npos && p2 > p) p = p2;
00113 return filepath.substr(p+1);
00114 }
00115
00116 void FileFunctions::FileParts(const std::string filepath, std::string &path,
00117 std::string &filename, std::string &ext)
00118 {
00119 string::size_type p = filepath.find_last_of('/');
00120 string::size_type p2 = filepath.find_last_of('\\');
00121 if(p == string::npos || (p2 != string::npos && p2 > p)) p = p2;
00122
00123 std::string filext;
00124
00125 if(p == string::npos){
00126 path = "";
00127 filext = filepath;
00128 }else{
00129 path = filepath.substr(0, p);
00130 filext = filepath.substr(p+1);
00131 }
00132
00133 p = filext.find_last_of('.');
00134 if(p == string::npos){
00135 filename = filext;
00136 ext = "";
00137 }else{
00138 filename = filext.substr(0, p);
00139 ext = filext.substr(p+1);
00140 }
00141 }
00142