| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
#include <Kernel/vjDebug.h> |
|---|
| 34 |
#include <Utils/vjFileIO.h> |
|---|
| 35 |
|
|---|
| 36 |
std::vector<std::string> vjFileIO::mPaths; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
bool vjFileIO::fileExists( const char* const name ) |
|---|
| 40 |
{ |
|---|
| 41 |
std::string stdstring_name = name; |
|---|
| 42 |
std::string demangled_name = demangleFileName( stdstring_name, "" ); |
|---|
| 43 |
FILE* file = ::fopen( demangled_name.c_str(), "r" ); |
|---|
| 44 |
if (file == NULL) |
|---|
| 45 |
{ |
|---|
| 46 |
return false; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
else |
|---|
| 50 |
{ |
|---|
| 51 |
::fclose( file ); |
|---|
| 52 |
return true; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
bool vjFileIO::fileExists( const std::string& name ) |
|---|
| 57 |
{ |
|---|
| 58 |
return fileExists( name.c_str() ); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
bool vjFileIO::fileExistsResolvePath( const char* const filename, std::string& realPath ) |
|---|
| 62 |
{ |
|---|
| 63 |
realPath = resolvePathForName( filename ); |
|---|
| 64 |
return fileExists( realPath.c_str() ); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
bool vjFileIO::fileExistsResolvePath( const std::string& filename, std::string& realPath ) |
|---|
| 68 |
{ |
|---|
| 69 |
return fileExistsResolvePath( filename.c_str(), realPath ); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
std::string vjFileIO::resolvePathForName( const char* const filename ) |
|---|
| 73 |
{ |
|---|
| 74 |
std::string stdstring_name = filename; |
|---|
| 75 |
std::string demangled_name = demangleFileName( stdstring_name, "" ); |
|---|
| 76 |
|
|---|
| 77 |
for (unsigned int x = 0; x < vjFileIO::mPaths.size(); ++x) |
|---|
| 78 |
{ |
|---|
| 79 |
std::string slash = "/"; |
|---|
| 80 |
std::string temp = vjFileIO::mPaths[x] + slash + demangled_name; |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
if (fileExists( temp )) |
|---|
| 84 |
{ |
|---|
| 85 |
std::cout<<"Fixed path: "<<temp.c_str()<<"\n"<<std::flush; |
|---|
| 86 |
return temp; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
return demangled_name; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
std::string vjFileIO::replaceEnvVars( const std::string& s ) |
|---|
| 100 |
{ |
|---|
| 101 |
unsigned int i, j; |
|---|
| 102 |
int lastpos = 0; |
|---|
| 103 |
std::string result = ""; |
|---|
| 104 |
for (i = 0; i < s.length(); i++) |
|---|
| 105 |
{ |
|---|
| 106 |
if (s[i] == '$') |
|---|
| 107 |
{ |
|---|
| 108 |
|
|---|
| 109 |
result += std::string(s, lastpos, i - lastpos); |
|---|
| 110 |
i++; |
|---|
| 111 |
if (s[i] == '{') |
|---|
| 112 |
{ |
|---|
| 113 |
|
|---|
| 114 |
for (j = i; j < s.length(); j++) |
|---|
| 115 |
if (s[j] == '}') |
|---|
| 116 |
break; |
|---|
| 117 |
std::string var(s,i+1,j-i-1); |
|---|
| 118 |
|
|---|
| 119 |
std::string res = getenv( var.c_str() ); |
|---|
| 120 |
if (res == "") |
|---|
| 121 |
{ |
|---|
| 122 |
vjDEBUG( vjDBG_ALL, 0 ) << clrOutNORM(clrYELLOW,"!!! WARNING: ENV VARIABLE NOT FOUND !!!:") << var.c_str() <<" does not exist in your environment, please set it... Your application's behaviour is now undefined!\n" << vjDEBUG_FLUSH; |
|---|
| 123 |
result += var; |
|---|
| 124 |
} |
|---|
| 125 |
else |
|---|
| 126 |
{ |
|---|
| 127 |
result += res; |
|---|
| 128 |
} |
|---|
| 129 |
i = j+1; |
|---|
| 130 |
lastpos = i; |
|---|
| 131 |
} |
|---|
| 132 |
else { |
|---|
| 133 |
for (j = i; j < s.length(); j++) |
|---|
| 134 |
if (s[j] == '/' || s[j] == '\\') |
|---|
| 135 |
break; |
|---|
| 136 |
std::string var(s,i,j-i); |
|---|
| 137 |
|
|---|
| 138 |
std::string res = getenv (var.c_str()); |
|---|
| 139 |
if (res == "") |
|---|
| 140 |
{ |
|---|
| 141 |
vjDEBUG( vjDBG_ALL, 0 ) << clrOutNORM(clrYELLOW,"!!! WARNING: ENV VARIABLE NOT FOUND !!!:") << var.c_str() <<" does not exist in your environment, please set it... Your application's behaviour is now undefined!\n" << vjDEBUG_FLUSH; |
|---|
| 142 |
result += var; |
|---|
| 143 |
} |
|---|
| 144 |
else |
|---|
| 145 |
{ |
|---|
| 146 |
result += res; |
|---|
| 147 |
} |
|---|
| 148 |
i = j; |
|---|
| 149 |
lastpos = i; |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
result += std::string(s, lastpos, s.length() - lastpos); |
|---|
| 154 |
return result; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
bool vjFileIO::isAbsolutePathName (const std::string& n) |
|---|
| 161 |
{ |
|---|
| 162 |
#ifdef WIN32 |
|---|
| 163 |
return ((n.length() > 0) && (n[0] == '\\')) |
|---|
| 164 |
|| ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\')); |
|---|
| 165 |
#else |
|---|
| 166 |
return (n.length() > 0) && (n[0] == '/'); |
|---|
| 167 |
#endif |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
std::string vjFileIO::demangleFileName (const std::string& n, std::string parentfile) |
|---|
| 173 |
{ |
|---|
| 174 |
|
|---|
| 175 |
std::string fname = replaceEnvVars (n); |
|---|
| 176 |
|
|---|
| 177 |
if (!isAbsolutePathName(fname)) |
|---|
| 178 |
{ |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
int lastslash = 0; |
|---|
| 184 |
for (unsigned int i = 0; i < parentfile.length(); i++) |
|---|
| 185 |
{ |
|---|
| 186 |
if (parentfile[i] == '/') |
|---|
| 187 |
lastslash = i; |
|---|
| 188 |
#ifdef WIN32 |
|---|
| 189 |
if (parentfile[i] == '\\') |
|---|
| 190 |
lastslash = i; |
|---|
| 191 |
#endif |
|---|
| 192 |
} |
|---|
| 193 |
if (lastslash) |
|---|
| 194 |
{ |
|---|
| 195 |
std::string s(parentfile, 0, lastslash+1); |
|---|
| 196 |
fname = s + n; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
return fname; |
|---|
| 201 |
} |
|---|