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 #include <vrj/vrjConfig.h>
00034
00035 #include <vpr/Util/FileUtils.h>
00036 #include <vrj/Util/Debug.h>
00037 #include <vrj/Util/FileIO.h>
00038
00039 namespace vrj
00040 {
00041
00042 std::vector<std::string> FileIO::mPaths;
00043
00044 void FileIO::addFilePath( const std::string& filepath )
00045 {
00046 std::string demangled = vpr::replaceEnvVars( filepath );
00047 mPaths.push_back( demangled );
00048 }
00049
00050
00051 bool FileIO::fileExists( const char* const name )
00052 {
00053 std::string stdstring_name = name;
00054 std::string demangled_name = demangleFileName( stdstring_name, "" );
00055 FILE* file = ::fopen( demangled_name.c_str(), "r" );
00056 if (file == NULL)
00057 {
00058 return false;
00059 }
00060
00061 else
00062 {
00063 ::fclose( file );
00064 return true;
00065 }
00066 }
00067
00068 bool FileIO::fileExists( const std::string& name )
00069 {
00070 return fileExists( name.c_str() );
00071 }
00072
00073 bool FileIO::fileExistsResolvePath( const char* const filename, std::string& realPath )
00074 {
00075 realPath = resolvePathForName( filename );
00076 return fileExists( realPath.c_str() );
00077 }
00078
00079 bool FileIO::fileExistsResolvePath( const std::string& filename, std::string& realPath )
00080 {
00081 return fileExistsResolvePath( filename.c_str(), realPath );
00082 }
00083
00084 std::string FileIO::resolvePathForName( const char* const filename )
00085 {
00086 std::string stdstring_name = filename;
00087 std::string demangled_name = demangleFileName( stdstring_name, "" );
00088
00089 for (unsigned int x = 0; x < FileIO::mPaths.size(); ++x)
00090 {
00091 std::string slash = "/";
00092 std::string temp = FileIO::mPaths[x] + slash + demangled_name;
00093
00094
00095 if (fileExists( temp ))
00096 {
00097 std::cout<<"Fixed path: "<<temp.c_str()<<"\n"<<std::flush;
00098 return temp;
00099 }
00100 }
00101
00102
00103
00104 return demangled_name;
00105 }
00106
00110 bool FileIO::isAbsolutePathName (const std::string& n)
00111 {
00112 #ifdef WIN32
00113 return ((n.length() > 0) && (n[0] == '\\'))
00114 || ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\'));
00115 #else
00116 return (n.length() > 0) && (n[0] == '/');
00117 #endif
00118 }
00119
00120
00121
00122 std::string FileIO::demangleFileName (const std::string& n, std::string parentfile)
00123 {
00124
00125 std::string fname = vpr::replaceEnvVars (n);
00126
00127 if (!isAbsolutePathName(fname))
00128 {
00129
00130
00131
00132
00133 int lastslash = 0;
00134 for (unsigned int i = 0; i < parentfile.length(); i++)
00135 {
00136 if (parentfile[i] == '/')
00137 lastslash = i;
00138 #ifdef WIN32
00139 if (parentfile[i] == '\\')
00140 lastslash = i;
00141 #endif
00142 }
00143 if (lastslash)
00144 {
00145 std::string s(parentfile, 0, lastslash+1);
00146 fname = s + n;
00147 }
00148 }
00149
00150 return fname;
00151 }
00152
00153 };