platformFileIO.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. DefineConsoleFunction( 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. DefineConsoleFunction( 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 StringTableEntry sgMainCSDir = NULL;
  71. StringTableEntry Platform::getMainDotCsDir()
  72. {
  73. if(sgMainCSDir == NULL)
  74. sgMainCSDir = Platform::getExecutablePath();
  75. return sgMainCSDir;
  76. }
  77. void Platform::setMainDotCsDir(const char *dir)
  78. {
  79. sgMainCSDir = StringTable->insert(dir);
  80. }
  81. //-----------------------------------------------------------------------------
  82. typedef Vector<char*> CharVector;
  83. static CharVector gPlatformDirectoryExcludeList( __FILE__, __LINE__ );
  84. void Platform::addExcludedDirectory(const char *pDir)
  85. {
  86. gPlatformDirectoryExcludeList.push_back(dStrdup(pDir));
  87. }
  88. void Platform::clearExcludedDirectories()
  89. {
  90. while(gPlatformDirectoryExcludeList.size())
  91. {
  92. dFree(gPlatformDirectoryExcludeList.last());
  93. gPlatformDirectoryExcludeList.pop_back();
  94. }
  95. }
  96. bool Platform::isExcludedDirectory(const char *pDir)
  97. {
  98. for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++)
  99. if(!dStrcmp(pDir, *i))
  100. return true;
  101. return false;
  102. }
  103. //-----------------------------------------------------------------------------
  104. inline void catPath(char *dst, const char *src, U32 len)
  105. {
  106. if(*dst != '/')
  107. {
  108. ++dst; --len;
  109. *dst = '/';
  110. }
  111. ++dst; --len;
  112. dStrncpy(dst, src, len);
  113. dst[len - 1] = 0;
  114. }
  115. // converts the posix root path "/" to "c:/" for win32
  116. // FIXME: this is not ideal. the c: drive is not guaranteed to exist.
  117. #if defined(TORQUE_OS_WIN)
  118. static inline void _resolveLeadingSlash(char* buf, U32 size)
  119. {
  120. if(buf[0] != '/')
  121. return;
  122. AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long");
  123. dMemmove(buf + 2, buf, dStrlen(buf));
  124. buf[0] = 'c';
  125. buf[1] = ':';
  126. }
  127. #endif
  128. static void makeCleanPathInPlace( char* path )
  129. {
  130. U32 pathDepth = 0;
  131. char* fromPtr = path;
  132. char* toPtr = path;
  133. bool isAbsolute = false;
  134. if( *fromPtr == '/' )
  135. {
  136. fromPtr ++;
  137. toPtr ++;
  138. isAbsolute = true;
  139. }
  140. else if( fromPtr[ 0 ] != '\0' && fromPtr[ 1 ] == ':' )
  141. {
  142. toPtr += 3;
  143. fromPtr += 3;
  144. isAbsolute = true;
  145. }
  146. while( *fromPtr )
  147. {
  148. if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '.' && fromPtr[ 2 ] == '/' )
  149. {
  150. // Back up from '../'
  151. if( pathDepth > 0 )
  152. {
  153. pathDepth --;
  154. toPtr -= 2;
  155. while( toPtr >= path && *toPtr != '/' )
  156. toPtr --;
  157. toPtr ++;
  158. }
  159. else if( !isAbsolute )
  160. {
  161. dMemcpy( toPtr, fromPtr, 3 );
  162. toPtr += 3;
  163. }
  164. fromPtr += 3;
  165. }
  166. else if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '/' )
  167. {
  168. // Ignore.
  169. fromPtr += 2;
  170. }
  171. else
  172. {
  173. if( fromPtr[ 0 ] == '/' )
  174. pathDepth ++;
  175. *toPtr ++ = *fromPtr ++;
  176. }
  177. }
  178. *toPtr = '\0';
  179. }
  180. char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */)
  181. {
  182. char bspath[1024];
  183. dStrncpy(bspath, path, sizeof(bspath));
  184. bspath[sizeof(bspath)-1] = 0;
  185. for(S32 i = 0;i < dStrlen(bspath);++i)
  186. {
  187. if(bspath[i] == '\\')
  188. bspath[i] = '/';
  189. }
  190. if(Platform::isFullPath(bspath))
  191. {
  192. // Already a full path
  193. #if defined(TORQUE_OS_WIN)
  194. _resolveLeadingSlash(bspath, sizeof(bspath));
  195. #endif
  196. dStrncpy(buffer, bspath, size);
  197. buffer[size-1] = 0;
  198. return buffer;
  199. }
  200. // [rene, 05/05/2008] Based on overall file handling in Torque, it does not seem to make
  201. // that much sense to me to base things off the current working directory here.
  202. if(cwd == NULL)
  203. cwd = Con::isCurrentScriptToolScript() ? Platform::getMainDotCsDir() : Platform::getCurrentDirectory();
  204. dStrncpy(buffer, cwd, size);
  205. buffer[size-1] = 0;
  206. const char* defaultDir = Con::getVariable("defaultGame");
  207. char *ptr = bspath;
  208. char *slash = NULL;
  209. char *endptr = buffer + dStrlen(buffer) - 1;
  210. do
  211. {
  212. slash = dStrchr(ptr, '/');
  213. if(slash)
  214. {
  215. *slash = 0;
  216. // Directory
  217. if(dStrcmp(ptr, "..") == 0)
  218. {
  219. // Parent
  220. endptr = dStrrchr(buffer, '/');
  221. if (endptr)
  222. *endptr-- = 0;
  223. }
  224. else if(dStrcmp(ptr, ".") == 0)
  225. {
  226. // Current dir
  227. }
  228. else if(dStrcmp(ptr, "~") == 0)
  229. {
  230. catPath(endptr, defaultDir, size - (endptr - buffer));
  231. endptr += dStrlen(endptr) - 1;
  232. }
  233. else if(endptr)
  234. {
  235. catPath(endptr, ptr, size - (endptr - buffer));
  236. endptr += dStrlen(endptr) - 1;
  237. }
  238. ptr = slash + 1;
  239. }
  240. else if(endptr)
  241. {
  242. // File
  243. catPath(endptr, ptr, size - (endptr - buffer));
  244. endptr += dStrlen(endptr) - 1;
  245. }
  246. } while(slash);
  247. return buffer;
  248. }
  249. bool Platform::isFullPath(const char *path)
  250. {
  251. // Quick way out
  252. if(path[0] == '/' || path[1] == ':')
  253. return true;
  254. return false;
  255. }
  256. //-----------------------------------------------------------------------------
  257. /// Return "fileName" stripped of its extension. Only extensions contained
  258. /// in "validExtensions" will be stripped from the filename.
  259. ///
  260. /// @note Extensions in "validExtension" should include the dot.
  261. String Platform::stripExtension( String fileName, Vector< String >& validExtensions )
  262. {
  263. // See if we have a valid extension to strip off
  264. String ext;
  265. S32 dotPos = fileName.find( '.', 0, String::Right );
  266. if( dotPos != String::NPos )
  267. ext = fileName.substr( dotPos );
  268. U32 numValidExt = validExtensions.size();
  269. if( ext.isNotEmpty() && numValidExt )
  270. {
  271. bool validExt = false;
  272. for( U32 i = 0; i < numValidExt; i++ )
  273. {
  274. if( ext.equal( validExtensions[ i ], String::NoCase ) )
  275. {
  276. validExt = true;
  277. break;
  278. }
  279. }
  280. if( !validExt )
  281. ext = String::EmptyString;
  282. }
  283. if( ext.isEmpty() )
  284. return fileName;
  285. else
  286. return fileName.substr( 0, fileName.length() - ext.length() );
  287. }
  288. //-----------------------------------------------------------------------------
  289. // TODO: wow really shouldn't be adding everything to the string table, use the string class!
  290. StringTableEntry Platform::makeRelativePathName(const char *path, const char *to)
  291. {
  292. // Make sure 'to' is a proper absolute path terminated with a forward slash.
  293. char buffer[ 2048 ];
  294. if( !to )
  295. {
  296. dSprintf( buffer, sizeof( buffer ), "%s/", Platform::getMainDotCsDir() );
  297. to = buffer;
  298. }
  299. else if( !Platform::isFullPath( to ) )
  300. {
  301. dSprintf( buffer, sizeof( buffer ), "%s/%s/", Platform::getMainDotCsDir(), to );
  302. makeCleanPathInPlace( buffer );
  303. to = buffer;
  304. }
  305. else if( to[ dStrlen( to ) - 1 ] != '/' )
  306. {
  307. U32 length = getMin( (U32)dStrlen( to ), sizeof( buffer ) - 2 );
  308. dMemcpy( buffer, to, length );
  309. buffer[ length ] = '/';
  310. buffer[ length + 1 ] = '\0';
  311. to = buffer;
  312. }
  313. // If 'path' isn't absolute, make it now. Let's us use a single
  314. // absolute/absolute merge path from here on.
  315. char buffer2[ 1024 ];
  316. if( !Platform::isFullPath( path ) )
  317. {
  318. dSprintf( buffer2, sizeof( buffer2 ), "%s/%s", Platform::getMainDotCsDir(), path );
  319. makeCleanPathInPlace( buffer2 );
  320. path = buffer2;
  321. }
  322. // First, find the common prefix and see where 'path' branches off from 'to'.
  323. const char *pathPtr, *toPtr, *branch = path;
  324. for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr)
  325. {
  326. if(*pathPtr == '/')
  327. branch = pathPtr;
  328. }
  329. // If there's no common part, the two paths are on different drives and
  330. // there's nothing we can do.
  331. if( pathPtr == path )
  332. return StringTable->insert( path );
  333. // If 'path' and 'to' are identical (minus trailing slash or so), we can just return './'.
  334. else if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) &&
  335. (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0)))
  336. {
  337. char* bufPtr = buffer;
  338. *bufPtr ++ = '.';
  339. if(*pathPtr == '/' || *(pathPtr - 1) == '/')
  340. *bufPtr++ = '/';
  341. *bufPtr = 0;
  342. return StringTable->insert(buffer);
  343. }
  344. // If 'to' is a proper prefix of 'path', the remainder of 'path' is our relative path.
  345. else if( *toPtr == '\0' && toPtr[ -1 ] == '/' )
  346. return StringTable->insert( pathPtr );
  347. // Otherwise have to step up the remaining directories in 'to' and then
  348. // append the remainder of 'path'.
  349. else
  350. {
  351. // FIXME: This condition is clearly wrong
  352. if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0))
  353. branch = pathPtr;
  354. // Allocate a new temp so we aren't prone to buffer overruns.
  355. TempAlloc< char > temp( dStrlen( toPtr ) + dStrlen( branch ) + 1 );
  356. char* bufPtr = temp;
  357. // Figure out parent dirs
  358. for(toPtr = to + (branch - path);*toPtr;++toPtr)
  359. {
  360. if(*toPtr == '/' && *(toPtr + 1) != 0)
  361. {
  362. *bufPtr++ = '.';
  363. *bufPtr++ = '.';
  364. *bufPtr++ = '/';
  365. }
  366. }
  367. *bufPtr = 0;
  368. // Copy the rest
  369. if(*branch)
  370. dStrcpy(bufPtr, branch + 1);
  371. else
  372. *--bufPtr = 0;
  373. return StringTable->insert( temp );
  374. }
  375. }
  376. //-----------------------------------------------------------------------------
  377. static StringTableEntry tryStripBasePath(const char *path, const char *base)
  378. {
  379. U32 len = dStrlen(base);
  380. if(dStrnicmp(path, base, len) == 0)
  381. {
  382. if(*(path + len) == '/') ++len;
  383. return StringTable->insert(path + len, true);
  384. }
  385. return NULL;
  386. }
  387. StringTableEntry Platform::stripBasePath(const char *path)
  388. {
  389. if(path == NULL)
  390. return NULL;
  391. StringTableEntry str = tryStripBasePath(path, Platform::getMainDotCsDir());
  392. if(str != NULL )
  393. return str;
  394. str = tryStripBasePath(path, Platform::getCurrentDirectory());
  395. if(str != NULL )
  396. return str;
  397. str = tryStripBasePath(path, Platform::getPrefsPath());
  398. if(str != NULL )
  399. return str;
  400. return path;
  401. }
  402. //-----------------------------------------------------------------------------
  403. StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */)
  404. {
  405. #ifndef TORQUE2D_TOOLS_FIXME
  406. return StringTable->insert(file ? file : "");
  407. #else
  408. char buf[1024];
  409. const char *company = Con::getVariable("$Game::CompanyName");
  410. if(company == NULL || *company == 0)
  411. company = "GarageGames";
  412. const char *appName = Con::getVariable("$Game::GameName");
  413. if(appName == NULL || *appName == 0)
  414. appName = TORQUE_APP_NAME;
  415. if(file)
  416. {
  417. if(dStrstr(file, ".."))
  418. {
  419. Con::errorf("getPrefsPath - filename cannot be relative");
  420. return NULL;
  421. }
  422. dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file);
  423. }
  424. else
  425. dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName);
  426. return StringTable->insert(buf, true);
  427. #endif
  428. }
  429. //-----------------------------------------------------------------------------
  430. DefineConsoleFunction( getUserDataDirectory, const char *, (), , "getUserDataDirectory()")
  431. {
  432. return Platform::getUserDataDirectory();
  433. }
  434. DefineConsoleFunction( getUserHomeDirectory, const char *, (), , "getUserHomeDirectory()")
  435. {
  436. return Platform::getUserHomeDirectory();
  437. }