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
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "cxutils/filefinder.h"
00041 #include "cxutils/fileio.h"
00042 #include <algorithm>
00043 #include <string>
00044
00045 using namespace std;
00046 using namespace CxUtils;
00047
00053 FileFinder::FileFinder()
00054 {
00055 mRecursionFlag = true;
00056 }
00057
00058
00066 FileFinder::FileFinder(const std::string& iniFile)
00067 {
00068 mRecursionFlag = true;
00069 LoadIniFile(iniFile);
00070 }
00071
00072
00078 FileFinder::~FileFinder()
00079 {
00080 }
00081
00082
00092 bool FileFinder::AddResourcePath(const std::string& path)
00093 {
00094 if(FileIO::IsDir(path))
00095 {
00096 if(find(mResourcePaths.begin(), mResourcePaths.end(), path) == mResourcePaths.end())
00097 {
00098 mResourcePaths.push_back(path);
00099 }
00100 return true;
00101 }
00102 return false;
00103 }
00104
00105
00114 void FileFinder::LoadIniFile(const std::string& iniFile)
00115 {
00116 FILE* fin = FileIO::Open(iniFile, "r");
00117 if(fin)
00118 {
00119 string line;
00120 while(FileIO::ReadLine(fin, line))
00121 {
00122 replace(line.begin(), line.end(),'\\','/');
00123 if(line[line.size()-1] != '/')
00124 {
00125 line.append("/");
00126 }
00127 AddResourcePath(line);
00128 }
00129 fclose(fin);
00130 }
00131 }
00132
00133
00143 bool FileFinder::RemoveResourcePath(const std::string& path)
00144 {
00145 vector<string>::iterator pos = find(mResourcePaths.begin(), mResourcePaths.end(), path);
00146 if(pos != mResourcePaths.end())
00147 {
00148 mResourcePaths.erase(pos);
00149 return true;
00150 }
00151 return false;
00152 }
00153
00154
00160 bool FileFinder::GetRecursionFlag() const
00161 {
00162 return mRecursionFlag;
00163 }
00164
00165
00175 void FileFinder::SetRecursionFlag(const bool recursionFlag)
00176 {
00177 mRecursionFlag = recursionFlag;
00178 }
00179
00180
00190 std::string FileFinder::GetFilePath(const std::string& fileName) const
00191 {
00192 string filePath;
00193 std::string justName;
00194
00195 if(FileIO::FileExists(fileName))
00196 {
00197 return fileName;
00198 }
00199 std::string fcopy = fileName;
00200 for(unsigned int i = 0; i < (unsigned int)fcopy.size(); i++)
00201 {
00202 if(fcopy[i] == '\\')
00203 {
00204 fcopy[i] = '/';
00205 }
00206 if(fcopy[i] == '/')
00207 {
00208 justName = fcopy.substr(i+1,fcopy.size());
00209 }
00210
00211 }
00212 int found = -1;
00213 std::string trimmedUp = fcopy;
00214 while((found = trimmedUp.find("../")) != string::npos)
00215 {
00216 trimmedUp = trimmedUp.substr(found+3,fcopy.size());
00217 }
00218
00219
00220 found = -1;
00221 vector<string>::const_iterator curPath;
00222 for(curPath = mResourcePaths.begin(); curPath != mResourcePaths.end(); curPath++)
00223 {
00224 std::string trimmed = trimmedUp;
00225 do
00226 {
00227 trimmed = trimmed.substr(found+1,trimmed.size());
00228 string tempPath = (*curPath) + trimmed;
00229 if(FileIO::FileExists(tempPath))
00230 {
00231 return tempPath;
00232 }
00233 }while((found = trimmed.find('/')) != string::npos);
00234
00235
00236 if(mRecursionFlag)
00237 {
00238 GetFilePathHelper((*curPath), justName, filePath);
00239 }
00240 }
00241
00242 return filePath;
00243 }
00244
00245
00257 void FileFinder::GetFilePathHelper(const std::string& rootPath,
00258 const std::string& fileName,
00259 std::string &filePath) const
00260 {
00261 vector<string> subDir;
00262 vector<string>::iterator curDir;
00263 if(filePath.empty())
00264 {
00265 FileIO::GetDirectories(subDir, rootPath, true);
00266 }
00267 for(curDir = subDir.begin(); (curDir != subDir.end()) && filePath.empty(); curDir++)
00268 {
00269 std::string complete = rootPath + "/" +(*curDir) + "/" + fileName;
00270 if(FileIO::FileExists(complete))
00271 {
00272 filePath = complete;
00273 return;
00274 }
00275 GetFilePathHelper(rootPath+(*curDir), fileName, filePath);
00276
00277 }
00278 }
00279
00280