platformFileIO.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 "console/engineAPI.h"
  26. #include "core/stringTable.h"
  27. //-----------------------------------------------------------------------------
  28. StringTableEntry Platform::getTemporaryDirectory()
  29. {
  30. StringTableEntry path = osGetTemporaryDirectory();
  31. if(! Platform::isDirectory(path))
  32. path = Platform::getCurrentDirectory();
  33. return path;
  34. }
  35. DefineEngineFunction( getTemporaryDirectory, const char *, (), ,
  36. "@brief Returns the OS temporary directory, \"C:/Users/Mich/AppData/Local/Temp\" for example\n\n"
  37. "@note This can be useful to adhering to OS standards and practices, "
  38. "but not really used in Torque 3D right now.\n"
  39. "@note Be very careful when getting into OS level File I/O."
  40. "@return String containing path to OS temp directory\n"
  41. "@note This is legacy function brought over from TGB, and does not appear "
  42. "to have much use. Possibly deprecate?\n"
  43. "@ingroup FileSystem\n"
  44. "@internal")
  45. {
  46. return Platform::getTemporaryDirectory();
  47. }
  48. StringTableEntry Platform::getTemporaryFileName()
  49. {
  50. char buf[512];
  51. StringTableEntry path = Platform::getTemporaryDirectory();
  52. dSprintf(buf, sizeof(buf), "%s/tgb.%08x.%02x.tmp", path, Platform::getRealMilliseconds(), U32(Platform::getRandom() * 255));
  53. // [tom, 9/7/2006] This shouldn't be needed, but just in case
  54. if(Platform::isFile(buf))
  55. return Platform::getTemporaryFileName();
  56. return StringTable->insert(buf);
  57. }
  58. DefineEngineFunction( getTemporaryFileName, const char *, (), ,
  59. "@brief Creates a name and extension for a potential temporary file\n\n"
  60. "This does not create the actual file. It simply creates a random name "
  61. "for a file that does not exist.\n\n"
  62. "@note This is legacy function brought over from TGB, and does not appear "
  63. "to have much use. Possibly deprecate?\n"
  64. "@ingroup FileSystem\n"
  65. "@internal")
  66. {
  67. return Platform::getTemporaryFileName();
  68. }
  69. //-----------------------------------------------------------------------------
  70. static char filePathBuffer[1024];
  71. static bool deleteDirectoryRecusrive(const char* pPath)
  72. {
  73. // Sanity!
  74. AssertFatal(pPath != NULL, "Cannot delete directory that is NULL.");
  75. // Find directories.
  76. Vector<StringTableEntry> directories;
  77. if (!Platform::dumpDirectories(pPath, directories, 0))
  78. {
  79. // Warn.
  80. Con::warnf("Could not retrieve sub-directories of '%s'.", pPath);
  81. return false;
  82. }
  83. // Iterate directories.
  84. for (Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr)
  85. {
  86. // Fetch base path.
  87. StringTableEntry basePath = *basePathItr;
  88. // Skip if the base path.
  89. if (basePathItr == directories.begin() && String::compare(pPath, basePath) == 0)
  90. continue;
  91. // Delete any directories recursively.
  92. if (!deleteDirectoryRecusrive(basePath))
  93. return false;
  94. }
  95. // Find files.
  96. Vector<Platform::FileInfo> files;
  97. if (!Platform::dumpPath(pPath, files, 0))
  98. {
  99. // Warn.
  100. Con::warnf("Could not retrieve files for directory '%s'.", pPath);
  101. return false;
  102. }
  103. // Iterate files.
  104. for (Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr)
  105. {
  106. // Format file.
  107. dSprintf(filePathBuffer, sizeof(filePathBuffer), "%s/%s", fileItr->pFullPath, fileItr->pFileName);
  108. // Delete file.
  109. if (!Platform::fileDelete(filePathBuffer))
  110. {
  111. // Warn.
  112. Con::warnf("Could not delete file '%s'.", filePathBuffer);
  113. return false;
  114. }
  115. }
  116. // Delete the directory.
  117. if (!Platform::fileDelete(pPath))
  118. {
  119. // Warn.
  120. Con::warnf("Could not delete directory '%s'.", pPath);
  121. return false;
  122. }
  123. return true;
  124. }
  125. //-----------------------------------------------------------------------------
  126. bool Platform::deleteDirectory(const char* pPath)
  127. {
  128. // Sanity!
  129. AssertFatal(pPath != NULL, "Cannot delete directory that is NULL.");
  130. // Is the path a file?
  131. if (Platform::isFile(pPath))
  132. {
  133. // Yes, so warn.
  134. Con::warnf("Cannot delete directory '%s' as it specifies a file.", pPath);
  135. return false;
  136. }
  137. // Expand module location.
  138. char pathBuffer[1024];
  139. Con::expandPath(pathBuffer, sizeof(pathBuffer), pPath, NULL, true);
  140. // Delete directory recursively.
  141. return deleteDirectoryRecusrive(pathBuffer);
  142. }
  143. //-----------------------------------------------------------------------------
  144. static StringTableEntry sgMainCSDir = NULL;
  145. StringTableEntry Platform::getMainDotCsDir()
  146. {
  147. if(sgMainCSDir == NULL)
  148. sgMainCSDir = Platform::getExecutablePath();
  149. return sgMainCSDir;
  150. }
  151. void Platform::setMainDotCsDir(const char *dir)
  152. {
  153. sgMainCSDir = StringTable->insert(dir);
  154. }
  155. //-----------------------------------------------------------------------------
  156. typedef Vector<char*> CharVector;
  157. static CharVector gPlatformDirectoryExcludeList( __FILE__, __LINE__ );
  158. void Platform::addExcludedDirectory(const char *pDir)
  159. {
  160. gPlatformDirectoryExcludeList.push_back(dStrdup(pDir));
  161. }
  162. void Platform::clearExcludedDirectories()
  163. {
  164. while(gPlatformDirectoryExcludeList.size())
  165. {
  166. dFree(gPlatformDirectoryExcludeList.last());
  167. gPlatformDirectoryExcludeList.pop_back();
  168. }
  169. }
  170. bool Platform::isExcludedDirectory(const char *pDir)
  171. {
  172. for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++)
  173. if(!String::compare(pDir, *i))
  174. return true;
  175. return false;
  176. }
  177. //-----------------------------------------------------------------------------
  178. inline void catPath(char *dst, const char *src, U32 len)
  179. {
  180. if(*dst != '/')
  181. {
  182. ++dst; --len;
  183. *dst = '/';
  184. }
  185. ++dst; --len;
  186. dStrncpy(dst, src, len);
  187. dst[len - 1] = 0;
  188. }
  189. // converts the posix root path "/" to "c:/" for win32
  190. // FIXME: this is not ideal. the c: drive is not guaranteed to exist.
  191. #if defined(TORQUE_OS_WIN)
  192. static inline void _resolveLeadingSlash(char* buf, U32 size)
  193. {
  194. if(buf[0] != '/')
  195. return;
  196. AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long");
  197. dMemmove(buf + 2, buf, dStrlen(buf));
  198. buf[0] = 'c';
  199. buf[1] = ':';
  200. }
  201. #endif
  202. static void makeCleanPathInPlace( char* path )
  203. {
  204. U32 pathDepth = 0;
  205. char* fromPtr = path;
  206. char* toPtr = path;
  207. bool isAbsolute = false;
  208. if( *fromPtr == '/' )
  209. {
  210. fromPtr ++;
  211. toPtr ++;
  212. isAbsolute = true;
  213. }
  214. else if( fromPtr[ 0 ] != '\0' && fromPtr[ 1 ] == ':' )
  215. {
  216. toPtr += 3;
  217. fromPtr += 3;
  218. isAbsolute = true;
  219. }
  220. while( *fromPtr )
  221. {
  222. if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '.' && fromPtr[ 2 ] == '/' )
  223. {
  224. // Back up from '../'
  225. if( pathDepth > 0 )
  226. {
  227. pathDepth --;
  228. toPtr -= 2;
  229. while( toPtr >= path && *toPtr != '/' )
  230. toPtr --;
  231. toPtr ++;
  232. }
  233. else if( !isAbsolute )
  234. {
  235. dMemcpy( toPtr, fromPtr, 3 );
  236. toPtr += 3;
  237. }
  238. fromPtr += 3;
  239. }
  240. else if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '/' )
  241. {
  242. // Ignore.
  243. fromPtr += 2;
  244. }
  245. else
  246. {
  247. if( fromPtr[ 0 ] == '/' )
  248. pathDepth ++;
  249. *toPtr ++ = *fromPtr ++;
  250. }
  251. }
  252. *toPtr = '\0';
  253. }
  254. char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */)
  255. {
  256. char bspath[1024];
  257. dStrncpy(bspath, path, sizeof(bspath));
  258. bspath[sizeof(bspath)-1] = 0;
  259. for(S32 i = 0;i < dStrlen(bspath);++i)
  260. {
  261. if(bspath[i] == '\\')
  262. bspath[i] = '/';
  263. }
  264. if(Platform::isFullPath(bspath))
  265. {
  266. // Already a full path
  267. #if defined(TORQUE_OS_WIN)
  268. _resolveLeadingSlash(bspath, sizeof(bspath));
  269. #endif
  270. dStrncpy(buffer, bspath, size);
  271. buffer[size-1] = 0;
  272. return buffer;
  273. }
  274. // [rene, 05/05/2008] Based on overall file handling in Torque, it does not seem to make
  275. // that much sense to me to base things off the current working directory here.
  276. #ifndef TORQUE_SCECURE_VFS
  277. if(cwd == NULL)
  278. cwd = Con::isCurrentScriptToolScript() ? Platform::getMainDotCsDir() : Platform::getCurrentDirectory();
  279. #else
  280. if (cwd == NULL)
  281. cwd = "game:/";
  282. #endif
  283. dStrncpy(buffer, cwd, size);
  284. buffer[size-1] = 0;
  285. const char* defaultDir = Con::getVariable("defaultGame");
  286. char *ptr = bspath;
  287. char *slash = NULL;
  288. char *endptr = buffer + dStrlen(buffer) - 1;
  289. do
  290. {
  291. slash = dStrchr(ptr, '/');
  292. if(slash)
  293. {
  294. *slash = 0;
  295. // Directory
  296. if(String::compare(ptr, "..") == 0)
  297. {
  298. // Parent
  299. endptr = dStrrchr(buffer, '/');
  300. if (endptr)
  301. *endptr-- = 0;
  302. }
  303. else if(String::compare(ptr, ".") == 0)
  304. {
  305. // Current dir
  306. }
  307. else if(String::compare(ptr, "~") == 0)
  308. {
  309. catPath(endptr, defaultDir, size - (endptr - buffer));
  310. endptr += dStrlen(endptr) - 1;
  311. }
  312. else if(endptr)
  313. {
  314. catPath(endptr, ptr, size - (endptr - buffer));
  315. endptr += dStrlen(endptr) - 1;
  316. }
  317. ptr = slash + 1;
  318. }
  319. else if(endptr)
  320. {
  321. // File
  322. catPath(endptr, ptr, size - (endptr - buffer));
  323. endptr += dStrlen(endptr) - 1;
  324. }
  325. } while(slash);
  326. return buffer;
  327. }
  328. bool Platform::isFullPath(const char *path)
  329. {
  330. // Quick way out
  331. if(path[0] == '/' || path[1] == ':')
  332. return true;
  333. return false;
  334. }
  335. //-----------------------------------------------------------------------------
  336. /// Return "fileName" stripped of its extension. Only extensions contained
  337. /// in "validExtensions" will be stripped from the filename.
  338. ///
  339. /// @note Extensions in "validExtension" should include the dot.
  340. String Platform::stripExtension( String fileName, Vector< String >& validExtensions )
  341. {
  342. // See if we have a valid extension to strip off
  343. String ext;
  344. S32 dotPos = fileName.find( '.', 0, String::Right );
  345. if( dotPos != String::NPos )
  346. ext = fileName.substr( dotPos );
  347. U32 numValidExt = validExtensions.size();
  348. if( ext.isNotEmpty() && numValidExt )
  349. {
  350. bool validExt = false;
  351. for( U32 i = 0; i < numValidExt; i++ )
  352. {
  353. if( ext.equal( validExtensions[ i ], String::NoCase ) )
  354. {
  355. validExt = true;
  356. break;
  357. }
  358. }
  359. if( !validExt )
  360. ext = String::EmptyString;
  361. }
  362. if( ext.isEmpty() )
  363. return fileName;
  364. else
  365. return fileName.substr( 0, fileName.length() - ext.length() );
  366. }
  367. //-----------------------------------------------------------------------------
  368. // TODO: wow really shouldn't be adding everything to the string table, use the string class!
  369. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  370. {
  371. // Make sure 'to' is a proper absolute path terminated with a forward slash.
  372. char buffer[ 2048 ];
  373. if( !to )
  374. {
  375. dSprintf( buffer, sizeof( buffer ), "%s/", Platform::getMainDotCsDir() );
  376. to = buffer;
  377. }
  378. else if( !Platform::isFullPath( to ) )
  379. {
  380. dSprintf( buffer, sizeof( buffer ), "%s/%s/", Platform::getMainDotCsDir(), to );
  381. makeCleanPathInPlace( buffer );
  382. to = buffer;
  383. }
  384. else if( to[ dStrlen( to ) - 1 ] != '/' )
  385. {
  386. U32 length = getMin( (U32)dStrlen( to ), sizeof( buffer ) - 2 );
  387. dMemcpy( buffer, to, length );
  388. buffer[ length ] = '/';
  389. buffer[ length + 1 ] = '\0';
  390. to = buffer;
  391. }
  392. // If 'path' isn't absolute, make it now. Let's us use a single
  393. // absolute/absolute merge path from here on.
  394. char buffer2[ 1024 ];
  395. if( !Platform::isFullPath( path ) )
  396. {
  397. dSprintf( buffer2, sizeof( buffer2 ), "%s/%s", Platform::getMainDotCsDir(), path );
  398. makeCleanPathInPlace( buffer2 );
  399. path = buffer2;
  400. }
  401. // First, find the common prefix and see where 'path' branches off from 'to'.
  402. const char *pathPtr, *toPtr, *branch = path;
  403. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  404. {
  405. if(*pathPtr == '/')
  406. branch = pathPtr;
  407. }
  408. // If there's no common part, the two paths are on different drives and
  409. // there's nothing we can do.
  410. if( pathPtr == path )
  411. return StringTable->insert( path );
  412. // If 'path' and 'to' are identical (minus trailing slash or so), we can just return './'.
  413. else if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  414. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  415. {
  416. char* bufPtr = buffer;
  417. *bufPtr ++ = '.';
  418. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  419. *bufPtr++ = '/';
  420. *bufPtr = 0;
  421. return StringTable->insert(buffer);
  422. }
  423. // If 'to' is a proper prefix of 'path', the remainder of 'path' is our relative path.
  424. else if( *toPtr == '\0' && toPtr[ -1 ] == '/' )
  425. return StringTable->insert( pathPtr );
  426. // Otherwise have to step up the remaining directories in 'to' and then
  427. // append the remainder of 'path'.
  428. else
  429. {
  430. // FIXME: This condition is clearly wrong
  431. if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0))
  432. branch = pathPtr;
  433. // Allocate a new temp so we aren't prone to buffer overruns.
  434. TempAlloc< char > temp( dStrlen( toPtr ) + dStrlen( branch ) + 1 );
  435. char* bufPtr = temp;
  436. // Figure out parent dirs
  437. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  438. {
  439. if(*toPtr == '/' && *(toPtr + 1) != 0)
  440. {
  441. *bufPtr++ = '.';
  442. *bufPtr++ = '.';
  443. *bufPtr++ = '/';
  444. }
  445. }
  446. *bufPtr = 0;
  447. // Copy the rest
  448. if(*branch)
  449. dStrcpy(bufPtr, branch + 1, temp.size - (bufPtr - temp.ptr));
  450. else
  451. *--bufPtr = 0;
  452. return StringTable->insert( temp );
  453. }
  454. }
  455. //-----------------------------------------------------------------------------
  456. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  457. {
  458. U32 len = dStrlen(base);
  459. if(dStrnicmp(path, base, len) == 0)
  460. {
  461. if(*(path + len) == '/') ++len;
  462. return StringTable->insert(path + len, true);
  463. }
  464. return NULL;
  465. }
  466. StringTableEntry Platform::stripBasePath(const char *path)
  467. {
  468. if(path == NULL)
  469. return NULL;
  470. StringTableEntry str = tryStripBasePath(path, Platform::getMainDotCsDir());
  471. if(str != NULL )
  472. return str;
  473. str = tryStripBasePath(path, Platform::getCurrentDirectory());
  474. if(str != NULL )
  475. return str;
  476. str = tryStripBasePath(path, Platform::getPrefsPath());
  477. if(str != NULL )
  478. return str;
  479. return path;
  480. }
  481. //-----------------------------------------------------------------------------
  482. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  483. {
  484. #ifndef TORQUE2D_TOOLS_FIXME
  485. return StringTable->insert(file ? file : "");
  486. #else
  487. char buf[1024];
  488. const char *company = Con::getVariable("$Game::CompanyName");
  489. if(company == NULL || *company == 0)
  490. company = "GarageGames";
  491. const char *appName = Con::getVariable("$Game::GameName");
  492. if(appName == NULL || *appName == 0)
  493. appName = TORQUE_APP_NAME;
  494. if(file)
  495. {
  496. if(dStrstr(file, ".."))
  497. {
  498. Con::errorf("getPrefsPath - filename cannot be relative");
  499. return NULL;
  500. }
  501. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  502. }
  503. else
  504. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  505. return StringTable->insert(buf, true);
  506. #endif
  507. }
  508. //-----------------------------------------------------------------------------
  509. DefineEngineFunction( getUserDataDirectory, const char *, (), , "getUserDataDirectory()")
  510. {
  511. return Platform::getUserDataDirectory();
  512. }
  513. DefineEngineFunction( getUserHomeDirectory, const char *, (), , "getUserHomeDirectory()")
  514. {
  515. return Platform::getUserHomeDirectory();
  516. }
  517. DefineEngineFunction(setMainDotCsDir, void, (const char* path), , "setMainDotCsDir()")
  518. {
  519. Platform::setMainDotCsDir(StringTable->insert(path));
  520. }