FileRepository.cpp
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 #include <sofa/helper/system/FileRepository.h>
00028 #include <sofa/helper/system/SetDirectory.h>
00029
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #ifndef WIN32
00033 #include <unistd.h>
00034 #else
00035 #include <windows.h>
00036 #include <direct.h>
00037 #endif
00038
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <iostream>
00042
00043 namespace sofa
00044 {
00045
00046 namespace helper
00047 {
00048
00049 namespace system
00050 {
00051
00052 #if defined (WIN32)
00053 FileRepository DataRepository("SOFA_DATA_PATH", "../share;../examples");
00054 #elif defined (__APPLE__)
00055 FileRepository DataRepository("SOFA_DATA_PATH", "../share:../examples:../Resources/examples:../Resources:../../../../examples:../../../../share");
00056 #else
00057 FileRepository DataRepository("SOFA_DATA_PATH", "../share:../examples");
00058 #endif
00059
00060 FileRepository::FileRepository(const char* envVar, const char* relativePath)
00061 {
00062 if (envVar != NULL && envVar[0]!='\0')
00063 {
00064 const char* envpath = getenv(envVar);
00065 if (envpath != NULL && envpath[0]!='\0')
00066 addFirstPath(envpath);
00067 }
00068 if (relativePath != NULL && relativePath[0]!='\0')
00069 {
00070 std::string path = relativePath;
00071 size_t p0 = 0;
00072 size_t p1;
00073 while ( p0 < path.size() )
00074 {
00075 p1 = path.find(entrySeparator(),p0);
00076 if (p1 == std::string::npos) p1 = path.size();
00077 if (p1>p0+1)
00078 {
00079 std::string p = path.substr(p0,p1-p0);
00080 addLastPath(SetDirectory::GetRelativeFromProcess(p.c_str()));
00081 }
00082 p0 = p1+1;
00083 }
00084 }
00085
00086 }
00087
00088 FileRepository::~FileRepository()
00089 {
00090 }
00091
00092 void FileRepository::addFirstPath(const std::string& path)
00093 {
00094 std::vector<std::string> entries;
00095 size_t p0 = 0;
00096 size_t p1;
00097 while ( p0 < path.size() )
00098 {
00099 p1 = path.find(entrySeparator(),p0);
00100 if (p1 == std::string::npos) p1 = path.size();
00101 if (p1>p0+1)
00102 {
00103 entries.push_back(path.substr(p0,p1-p0));
00104 }
00105 p0 = p1+1;
00106 }
00107 vpath.insert(vpath.begin(), entries.begin(), entries.end());
00108 }
00109
00110 void FileRepository::addLastPath(const std::string& path)
00111 {
00112 std::vector<std::string> entries;
00113 size_t p0 = 0;
00114 size_t p1;
00115 while ( p0 < path.size() )
00116 {
00117 p1 = path.find(entrySeparator(),p0);
00118 if (p1 == std::string::npos) p1 = path.size();
00119 if (p1>p0+1)
00120 {
00121 entries.push_back(path.substr(p0,p1-p0));
00122 }
00123 p0 = p1+1;
00124 }
00125 vpath.insert(vpath.end(), entries.begin(), entries.end());
00126
00127 }
00128
00129 std::string FileRepository::getFirstPath()
00130 {
00131 if (vpath.size() > 0)
00132 return vpath[0];
00133 else return "";
00134 }
00135
00136 bool FileRepository::findFileIn(std::string& filename, const std::string& path)
00137 {
00138 if (filename.empty()) return false;
00139 struct stat s;
00140 std::string newfname = SetDirectory::GetRelativeFromDir(filename.c_str(), path.c_str());
00141
00142 if (!stat(newfname.c_str(),&s))
00143 {
00144
00145
00146 filename = newfname;
00147 return true;
00148 }
00149 return false;
00150 }
00151
00152 bool FileRepository::findFile(std::string& filename, const std::string& basedir)
00153 {
00154 if (filename.empty()) return false;
00155 if (basedir.empty())
00156 {
00157 if (findFileIn(filename, SetDirectory::GetCurrentDir())) return true;
00158 }
00159 else
00160 {
00161 if (findFileIn(filename, SetDirectory::GetRelativeFromDir(basedir.c_str(),SetDirectory::GetCurrentDir().c_str()))) return true;
00162 }
00163 if (SetDirectory::IsAbsolute(filename)) return false;
00164 if (filename.substr(0,2)=="./" || filename.substr(0,3)=="../") return false;
00165 for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00166 if (findFileIn(filename, *it)) return true;
00167 std::cerr << "File "<<filename<<" NOT FOUND in "<<basedir;
00168 for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00169 std::cerr << ':'<<*it;
00170 std::cerr<<std::endl;
00171 return false;
00172 }
00173
00174 bool FileRepository::findFileFromFile(std::string& filename, const std::string& basefile)
00175 {
00176 return findFile(filename, SetDirectory::GetParentDir(basefile.c_str()));
00177 }
00178
00179 void FileRepository::print()
00180 {
00181 for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
00182 std::cout << *it << std::endl;
00183 }
00184
00185 }
00186
00187 }
00188
00189 }
00190