platformFileIO.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. if(cwd == NULL)
  277. cwd = Con::isCurrentScriptToolScript() ? Platform::getMainDotCsDir() : Platform::getCurrentDirectory();
  278. dStrncpy(buffer, cwd, size);
  279. buffer[size-1] = 0;
  280. const char* defaultDir = Con::getVariable("defaultGame");
  281. char *ptr = bspath;
  282. char *slash = NULL;
  283. char *endptr = buffer + dStrlen(buffer) - 1;
  284. do
  285. {
  286. slash = dStrchr(ptr, '/');
  287. if(slash)
  288. {
  289. *slash = 0;
  290. // Directory
  291. if(String::compare(ptr, "..") == 0)
  292. {
  293. // Parent
  294. endptr = dStrrchr(buffer, '/');
  295. if (endptr)
  296. *endptr-- = 0;
  297. }
  298. else if(String::compare(ptr, ".") == 0)
  299. {
  300. // Current dir
  301. }
  302. else if(String::compare(ptr, "~") == 0)
  303. {
  304. catPath(endptr, defaultDir, size - (endptr - buffer));
  305. endptr += dStrlen(endptr) - 1;
  306. }
  307. else if(endptr)
  308. {
  309. catPath(endptr, ptr, size - (endptr - buffer));
  310. endptr += dStrlen(endptr) - 1;
  311. }
  312. ptr = slash + 1;
  313. }
  314. else if(endptr)
  315. {
  316. // File
  317. catPath(endptr, ptr, size - (endptr - buffer));
  318. endptr += dStrlen(endptr) - 1;
  319. }
  320. } while(slash);
  321. return buffer;
  322. }
  323. bool Platform::isFullPath(const char *path)
  324. {
  325. // Quick way out
  326. if(path[0] == '/' || path[1] == ':')
  327. return true;
  328. return false;
  329. }
  330. //-----------------------------------------------------------------------------
  331. /// Return "fileName" stripped of its extension. Only extensions contained
  332. /// in "validExtensions" will be stripped from the filename.
  333. ///
  334. /// @note Extensions in "validExtension" should include the dot.
  335. String Platform::stripExtension( String fileName, Vector< String >& validExtensions )
  336. {
  337. // See if we have a valid extension to strip off
  338. String ext;
  339. S32 dotPos = fileName.find( '.', 0, String::Right );
  340. if( dotPos != String::NPos )
  341. ext = fileName.substr( dotPos );
  342. U32 numValidExt = validExtensions.size();
  343. if( ext.isNotEmpty() && numValidExt )
  344. {
  345. bool validExt = false;
  346. for( U32 i = 0; i < numValidExt; i++ )
  347. {
  348. if( ext.equal( validExtensions[ i ], String::NoCase ) )
  349. {
  350. validExt = true;
  351. break;
  352. }
  353. }
  354. if( !validExt )
  355. ext = String::EmptyString;
  356. }
  357. if( ext.isEmpty() )
  358. return fileName;
  359. else
  360. return fileName.substr( 0, fileName.length() - ext.length() );
  361. }
  362. //-----------------------------------------------------------------------------
  363. // TODO: wow really shouldn't be adding everything to the string table, use the string class!
  364. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  365. {
  366. // Make sure 'to' is a proper absolute path terminated with a forward slash.
  367. char buffer[ 2048 ];
  368. if( !to )
  369. {
  370. dSprintf( buffer, sizeof( buffer ), "%s/", Platform::getMainDotCsDir() );
  371. to = buffer;
  372. }
  373. else if( !Platform::isFullPath( to ) )
  374. {
  375. dSprintf( buffer, sizeof( buffer ), "%s/%s/", Platform::getMainDotCsDir(), to );
  376. makeCleanPathInPlace( buffer );
  377. to = buffer;
  378. }
  379. else if( to[ dStrlen( to ) - 1 ] != '/' )
  380. {
  381. U32 length = getMin( (U32)dStrlen( to ), sizeof( buffer ) - 2 );
  382. dMemcpy( buffer, to, length );
  383. buffer[ length ] = '/';
  384. buffer[ length + 1 ] = '\0';
  385. to = buffer;
  386. }
  387. // If 'path' isn't absolute, make it now. Let's us use a single
  388. // absolute/absolute merge path from here on.
  389. char buffer2[ 1024 ];
  390. if( !Platform::isFullPath( path ) )
  391. {
  392. dSprintf( buffer2, sizeof( buffer2 ), "%s/%s", Platform::getMainDotCsDir(), path );
  393. makeCleanPathInPlace( buffer2 );
  394. path = buffer2;
  395. }
  396. // First, find the common prefix and see where 'path' branches off from 'to'.
  397. const char *pathPtr, *toPtr, *branch = path;
  398. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  399. {
  400. if(*pathPtr == '/')
  401. branch = pathPtr;
  402. }
  403. // If there's no common part, the two paths are on different drives and
  404. // there's nothing we can do.
  405. if( pathPtr == path )
  406. return StringTable->insert( path );
  407. // If 'path' and 'to' are identical (minus trailing slash or so), we can just return './'.
  408. else if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  409. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  410. {
  411. char* bufPtr = buffer;
  412. *bufPtr ++ = '.';
  413. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  414. *bufPtr++ = '/';
  415. *bufPtr = 0;
  416. return StringTable->insert(buffer);
  417. }
  418. // If 'to' is a proper prefix of 'path', the remainder of 'path' is our relative path.
  419. else if( *toPtr == '\0' && toPtr[ -1 ] == '/' )
  420. return StringTable->insert( pathPtr );
  421. // Otherwise have to step up the remaining directories in 'to' and then
  422. // append the remainder of 'path'.
  423. else
  424. {
  425. // FIXME: This condition is clearly wrong
  426. if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0))
  427. branch = pathPtr;
  428. // Allocate a new temp so we aren't prone to buffer overruns.
  429. TempAlloc< char > temp( dStrlen( toPtr ) + dStrlen( branch ) + 1 );
  430. char* bufPtr = temp;
  431. // Figure out parent dirs
  432. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  433. {
  434. if(*toPtr == '/' && *(toPtr + 1) != 0)
  435. {
  436. *bufPtr++ = '.';
  437. *bufPtr++ = '.';
  438. *bufPtr++ = '/';
  439. }
  440. }
  441. *bufPtr = 0;
  442. // Copy the rest
  443. if(*branch)
  444. dStrcpy(bufPtr, branch + 1, temp.size - (bufPtr - temp.ptr));
  445. else
  446. *--bufPtr = 0;
  447. return StringTable->insert( temp );
  448. }
  449. }
  450. //-----------------------------------------------------------------------------
  451. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  452. {
  453. U32 len = dStrlen(base);
  454. if(dStrnicmp(path, base, len) == 0)
  455. {
  456. if(*(path + len) == '/') ++len;
  457. return StringTable->insert(path + len, true);
  458. }
  459. return NULL;
  460. }
  461. StringTableEntry Platform::stripBasePath(const char *path)
  462. {
  463. if(path == NULL)
  464. return NULL;
  465. StringTableEntry str = tryStripBasePath(path, Platform::getMainDotCsDir());
  466. if(str != NULL )
  467. return str;
  468. str = tryStripBasePath(path, Platform::getCurrentDirectory());
  469. if(str != NULL )
  470. return str;
  471. str = tryStripBasePath(path, Platform::getPrefsPath());
  472. if(str != NULL )
  473. return str;
  474. return path;
  475. }
  476. //-----------------------------------------------------------------------------
  477. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  478. {
  479. #ifndef TORQUE2D_TOOLS_FIXME
  480. return StringTable->insert(file ? file : "");
  481. #else
  482. char buf[1024];
  483. const char *company = Con::getVariable("$Game::CompanyName");
  484. if(company == NULL || *company == 0)
  485. company = "GarageGames";
  486. const char *appName = Con::getVariable("$Game::GameName");
  487. if(appName == NULL || *appName == 0)
  488. appName = TORQUE_APP_NAME;
  489. if(file)
  490. {
  491. if(dStrstr(file, ".."))
  492. {
  493. Con::errorf("getPrefsPath - filename cannot be relative");
  494. return NULL;
  495. }
  496. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  497. }
  498. else
  499. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  500. return StringTable->insert(buf, true);
  501. #endif
  502. }
  503. //-----------------------------------------------------------------------------
  504. DefineEngineFunction( getUserDataDirectory, const char *, (), , "getUserDataDirectory()")
  505. {
  506. return Platform::getUserDataDirectory();
  507. }
  508. DefineEngineFunction( getUserHomeDirectory, const char *, (), , "getUserHomeDirectory()")
  509. {
  510. return Platform::getUserHomeDirectory();
  511. }
  512. DefineEngineFunction(setMainDotCsDir, void, (const char* path), , "setMainDotCsDir()")
  513. {
  514. Platform::setMainDotCsDir(StringTable->insert(path));
  515. }