platformFileIO.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "util/tempAlloc.h"
  24. #include "console/console.h"
  25. #include "core/stringTable.h"
  26. //-----------------------------------------------------------------------------
  27. StringTableEntry Platform::getTemporaryDirectory()
  28. {
  29. StringTableEntry path = osGetTemporaryDirectory();
  30. if(! Platform::isDirectory(path))
  31. path = Platform::getCurrentDirectory();
  32. return path;
  33. }
  34. ConsoleFunction(getTemporaryDirectory, const char *, 1, 1, "()"
  35. "@brief Returns the OS temporary directory, \"C:/Users/Mich/AppData/Local/Temp\" for example\n\n"
  36. "@note This can be useful to adhering to OS standards and practices, "
  37. "but not really used in Torque 3D right now.\n"
  38. "@note Be very careful when getting into OS level File I/O."
  39. "@return String containing path to OS temp directory\n"
  40. "@note This is legacy function brought over from TGB, and does not appear "
  41. "to have much use. Possibly deprecate?\n"
  42. "@ingroup FileSystem\n"
  43. "@internal")
  44. {
  45. return Platform::getTemporaryDirectory();
  46. }
  47. StringTableEntry Platform::getTemporaryFileName()
  48. {
  49. char buf[512];
  50. StringTableEntry path = Platform::getTemporaryDirectory();
  51. dSprintf(buf, sizeof(buf), "%s/tgb.%08x.%02x.tmp", path, Platform::getRealMilliseconds(), U32(Platform::getRandom() * 255));
  52. // [tom, 9/7/2006] This shouldn't be needed, but just in case
  53. if(Platform::isFile(buf))
  54. return Platform::getTemporaryFileName();
  55. return StringTable->insert(buf);
  56. }
  57. ConsoleFunction(getTemporaryFileName, const char *, 1, 1, "()"
  58. "@brief Creates a name and extension for a potential temporary file\n\n"
  59. "This does not create the actual file. It simply creates a random name "
  60. "for a file that does not exist.\n\n"
  61. "@note This is legacy function brought over from TGB, and does not appear "
  62. "to have much use. Possibly deprecate?\n"
  63. "@ingroup FileSystem\n"
  64. "@internal")
  65. {
  66. return Platform::getTemporaryFileName();
  67. }
  68. //-----------------------------------------------------------------------------
  69. static StringTableEntry sgMainCSDir = NULL;
  70. StringTableEntry Platform::getMainDotCsDir()
  71. {
  72. if(sgMainCSDir == NULL)
  73. sgMainCSDir = Platform::getExecutablePath();
  74. return sgMainCSDir;
  75. }
  76. void Platform::setMainDotCsDir(const char *dir)
  77. {
  78. sgMainCSDir = StringTable->insert(dir);
  79. }
  80. //-----------------------------------------------------------------------------
  81. typedef Vector<char*> CharVector;
  82. static CharVector gPlatformDirectoryExcludeList( __FILE__, __LINE__ );
  83. void Platform::addExcludedDirectory(const char *pDir)
  84. {
  85. gPlatformDirectoryExcludeList.push_back(dStrdup(pDir));
  86. }
  87. void Platform::clearExcludedDirectories()
  88. {
  89. while(gPlatformDirectoryExcludeList.size())
  90. {
  91. dFree(gPlatformDirectoryExcludeList.last());
  92. gPlatformDirectoryExcludeList.pop_back();
  93. }
  94. }
  95. bool Platform::isExcludedDirectory(const char *pDir)
  96. {
  97. for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++)
  98. if(!dStrcmp(pDir, *i))
  99. return true;
  100. return false;
  101. }
  102. //-----------------------------------------------------------------------------
  103. inline void catPath(char *dst, const char *src, U32 len)
  104. {
  105. if(*dst != '/')
  106. {
  107. ++dst; --len;
  108. *dst = '/';
  109. }
  110. ++dst; --len;
  111. dStrncpy(dst, src, len);
  112. dst[len - 1] = 0;
  113. }
  114. // converts the posix root path "/" to "c:/" for win32
  115. // FIXME: this is not ideal. the c: drive is not guaranteed to exist.
  116. #if defined(TORQUE_OS_WIN)
  117. static inline void _resolveLeadingSlash(char* buf, U32 size)
  118. {
  119. if(buf[0] != '/')
  120. return;
  121. AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long");
  122. dMemmove(buf + 2, buf, dStrlen(buf));
  123. buf[0] = 'c';
  124. buf[1] = ':';
  125. }
  126. #endif
  127. static void makeCleanPathInPlace( char* path )
  128. {
  129. U32 pathDepth = 0;
  130. char* fromPtr = path;
  131. char* toPtr = path;
  132. bool isAbsolute = false;
  133. if( *fromPtr == '/' )
  134. {
  135. fromPtr ++;
  136. toPtr ++;
  137. isAbsolute = true;
  138. }
  139. else if( fromPtr[ 0 ] != '\0' && fromPtr[ 1 ] == ':' )
  140. {
  141. toPtr += 3;
  142. fromPtr += 3;
  143. isAbsolute = true;
  144. }
  145. while( *fromPtr )
  146. {
  147. if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '.' && fromPtr[ 2 ] == '/' )
  148. {
  149. // Back up from '../'
  150. if( pathDepth > 0 )
  151. {
  152. pathDepth --;
  153. toPtr -= 2;
  154. while( toPtr >= path && *toPtr != '/' )
  155. toPtr --;
  156. toPtr ++;
  157. }
  158. else if( !isAbsolute )
  159. {
  160. dMemcpy( toPtr, fromPtr, 3 );
  161. toPtr += 3;
  162. }
  163. fromPtr += 3;
  164. }
  165. else if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '/' )
  166. {
  167. // Ignore.
  168. fromPtr += 2;
  169. }
  170. else
  171. {
  172. if( fromPtr[ 0 ] == '/' )
  173. pathDepth ++;
  174. *toPtr ++ = *fromPtr ++;
  175. }
  176. }
  177. *toPtr = '\0';
  178. }
  179. char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */)
  180. {
  181. char bspath[1024];
  182. dStrncpy(bspath, path, sizeof(bspath));
  183. bspath[sizeof(bspath)-1] = 0;
  184. for(S32 i = 0;i < dStrlen(bspath);++i)
  185. {
  186. if(bspath[i] == '\\')
  187. bspath[i] = '/';
  188. }
  189. if(Platform::isFullPath(bspath))
  190. {
  191. // Already a full path
  192. #if defined(TORQUE_OS_WIN)
  193. _resolveLeadingSlash(bspath, sizeof(bspath));
  194. #endif
  195. dStrncpy(buffer, bspath, size);
  196. buffer[size-1] = 0;
  197. return buffer;
  198. }
  199. // [rene, 05/05/2008] Based on overall file handling in Torque, it does not seem to make
  200. // that much sense to me to base things off the current working directory here.
  201. if(cwd == NULL)
  202. cwd = Con::isCurrentScriptToolScript() ? Platform::getMainDotCsDir() : Platform::getCurrentDirectory();
  203. dStrncpy(buffer, cwd, size);
  204. buffer[size-1] = 0;
  205. const char* defaultDir = Con::getVariable("defaultGame");
  206. char *ptr = bspath;
  207. char *slash = NULL;
  208. char *endptr = buffer + dStrlen(buffer) - 1;
  209. do
  210. {
  211. slash = dStrchr(ptr, '/');
  212. if(slash)
  213. {
  214. *slash = 0;
  215. // Directory
  216. if(dStrcmp(ptr, "..") == 0)
  217. {
  218. // Parent
  219. endptr = dStrrchr(buffer, '/');
  220. if (endptr)
  221. *endptr-- = 0;
  222. }
  223. else if(dStrcmp(ptr, ".") == 0)
  224. {
  225. // Current dir
  226. }
  227. else if(dStrcmp(ptr, "~") == 0)
  228. {
  229. catPath(endptr, defaultDir, size - (endptr - buffer));
  230. endptr += dStrlen(endptr) - 1;
  231. }
  232. else if(endptr)
  233. {
  234. catPath(endptr, ptr, size - (endptr - buffer));
  235. endptr += dStrlen(endptr) - 1;
  236. }
  237. ptr = slash + 1;
  238. }
  239. else if(endptr)
  240. {
  241. // File
  242. catPath(endptr, ptr, size - (endptr - buffer));
  243. endptr += dStrlen(endptr) - 1;
  244. }
  245. } while(slash);
  246. return buffer;
  247. }
  248. bool Platform::isFullPath(const char *path)
  249. {
  250. // Quick way out
  251. if(path[0] == '/' || path[1] == ':')
  252. return true;
  253. return false;
  254. }
  255. //-----------------------------------------------------------------------------
  256. /// Return "fileName" stripped of its extension. Only extensions contained
  257. /// in "validExtensions" will be stripped from the filename.
  258. ///
  259. /// @note Extensions in "validExtension" should include the dot.
  260. String Platform::stripExtension( String fileName, Vector< String >& validExtensions )
  261. {
  262. // See if we have a valid extension to strip off
  263. String ext;
  264. S32 dotPos = fileName.find( '.', 0, String::Right );
  265. if( dotPos != String::NPos )
  266. ext = fileName.substr( dotPos );
  267. U32 numValidExt = validExtensions.size();
  268. if( ext.isNotEmpty() && numValidExt )
  269. {
  270. bool validExt = false;
  271. for( U32 i = 0; i < numValidExt; i++ )
  272. {
  273. if( ext.equal( validExtensions[ i ], String::NoCase ) )
  274. {
  275. validExt = true;
  276. break;
  277. }
  278. }
  279. if( !validExt )
  280. ext = String::EmptyString;
  281. }
  282. if( ext.isEmpty() )
  283. return fileName;
  284. else
  285. return fileName.substr( 0, fileName.length() - ext.length() );
  286. }
  287. //-----------------------------------------------------------------------------
  288. // TODO: wow really shouldn't be adding everything to the string table, use the string class!
  289. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  290. {
  291. // Make sure 'to' is a proper absolute path terminated with a forward slash.
  292. char buffer[ 2048 ];
  293. if( !to )
  294. {
  295. dSprintf( buffer, sizeof( buffer ), "%s/", Platform::getMainDotCsDir() );
  296. to = buffer;
  297. }
  298. else if( !Platform::isFullPath( to ) )
  299. {
  300. dSprintf( buffer, sizeof( buffer ), "%s/%s/", Platform::getMainDotCsDir(), to );
  301. makeCleanPathInPlace( buffer );
  302. to = buffer;
  303. }
  304. else if( to[ dStrlen( to ) - 1 ] != '/' )
  305. {
  306. U32 length = getMin( (U32)dStrlen( to ), sizeof( buffer ) - 2 );
  307. dMemcpy( buffer, to, length );
  308. buffer[ length ] = '/';
  309. buffer[ length + 1 ] = '\0';
  310. to = buffer;
  311. }
  312. // If 'path' isn't absolute, make it now. Let's us use a single
  313. // absolute/absolute merge path from here on.
  314. char buffer2[ 1024 ];
  315. if( !Platform::isFullPath( path ) )
  316. {
  317. dSprintf( buffer2, sizeof( buffer2 ), "%s/%s", Platform::getMainDotCsDir(), path );
  318. makeCleanPathInPlace( buffer2 );
  319. path = buffer2;
  320. }
  321. // First, find the common prefix and see where 'path' branches off from 'to'.
  322. const char *pathPtr, *toPtr, *branch = path;
  323. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  324. {
  325. if(*pathPtr == '/')
  326. branch = pathPtr;
  327. }
  328. // If there's no common part, the two paths are on different drives and
  329. // there's nothing we can do.
  330. if( pathPtr == path )
  331. return StringTable->insert( path );
  332. // If 'path' and 'to' are identical (minus trailing slash or so), we can just return './'.
  333. else if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  334. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  335. {
  336. char* bufPtr = buffer;
  337. *bufPtr ++ = '.';
  338. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  339. *bufPtr++ = '/';
  340. *bufPtr = 0;
  341. return StringTable->insert(buffer);
  342. }
  343. // If 'to' is a proper prefix of 'path', the remainder of 'path' is our relative path.
  344. else if( *toPtr == '\0' && toPtr[ -1 ] == '/' )
  345. return StringTable->insert( pathPtr );
  346. // Otherwise have to step up the remaining directories in 'to' and then
  347. // append the remainder of 'path'.
  348. else
  349. {
  350. if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0))
  351. branch = pathPtr;
  352. // Allocate a new temp so we aren't prone to buffer overruns.
  353. TempAlloc< char > temp( dStrlen( toPtr ) + dStrlen( branch ) + 1 );
  354. char* bufPtr = temp;
  355. // Figure out parent dirs
  356. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  357. {
  358. if(*toPtr == '/' && *(toPtr + 1) != 0)
  359. {
  360. *bufPtr++ = '.';
  361. *bufPtr++ = '.';
  362. *bufPtr++ = '/';
  363. }
  364. }
  365. *bufPtr = 0;
  366. // Copy the rest
  367. if(*branch)
  368. dStrcpy(bufPtr, branch + 1);
  369. else
  370. *--bufPtr = 0;
  371. return StringTable->insert( temp );
  372. }
  373. }
  374. //-----------------------------------------------------------------------------
  375. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  376. {
  377. U32 len = dStrlen(base);
  378. if(dStrnicmp(path, base, len) == 0)
  379. {
  380. if(*(path + len) == '/') ++len;
  381. return StringTable->insert(path + len, true);
  382. }
  383. return NULL;
  384. }
  385. StringTableEntry Platform::stripBasePath(const char *path)
  386. {
  387. if(path == NULL)
  388. return NULL;
  389. StringTableEntry str = tryStripBasePath(path, Platform::getMainDotCsDir());
  390. if(str != NULL )
  391. return str;
  392. str = tryStripBasePath(path, Platform::getCurrentDirectory());
  393. if(str != NULL )
  394. return str;
  395. str = tryStripBasePath(path, Platform::getPrefsPath());
  396. if(str != NULL )
  397. return str;
  398. return path;
  399. }
  400. //-----------------------------------------------------------------------------
  401. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  402. {
  403. #ifndef TORQUE2D_TOOLS_FIXME
  404. return StringTable->insert(file ? file : "");
  405. #else
  406. char buf[1024];
  407. const char *company = Con::getVariable("$Game::CompanyName");
  408. if(company == NULL || *company == 0)
  409. company = "GarageGames";
  410. const char *appName = Con::getVariable("$Game::GameName");
  411. if(appName == NULL || *appName == 0)
  412. appName = TORQUE_APP_NAME;
  413. if(file)
  414. {
  415. if(dStrstr(file, ".."))
  416. {
  417. Con::errorf("getPrefsPath - filename cannot be relative");
  418. return NULL;
  419. }
  420. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  421. }
  422. else
  423. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  424. return StringTable->insert(buf, true);
  425. #endif
  426. }
  427. //-----------------------------------------------------------------------------
  428. ConsoleFunction(getUserDataDirectory, const char*, 1, 1, "getUserDataDirectory()")
  429. {
  430. return Platform::getUserDataDirectory();
  431. }
  432. ConsoleFunction(getUserHomeDirectory, const char*, 1, 1, "getUserHomeDirectory()")
  433. {
  434. return Platform::getUserHomeDirectory();
  435. }