winFileio.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 "platformWin32/platformWin32.h"
  24. #include "core/fileio.h"
  25. #include "core/util/tVector.h"
  26. #include "core/stringTable.h"
  27. #include "console/console.h"
  28. #include "core/strings/unicode.h"
  29. #include "util/tempAlloc.h"
  30. #include "core/util/safeDelete.h"
  31. #include "core/volume.h"
  32. // Microsoft VC++ has this POSIX header in the wrong directory
  33. #if defined(TORQUE_COMPILER_VISUALC)
  34. #include <sys/utime.h>
  35. #elif defined (TORQUE_COMPILER_GCC)
  36. #include <time.h>
  37. #include <sys/utime.h>
  38. #else
  39. #include <utime.h>
  40. #endif
  41. StringTableEntry Platform::createPlatformFriendlyFilename( const char *filename )
  42. {
  43. return StringTable->insert( filename );
  44. }
  45. //-----------------------------------------------------------------------------
  46. bool dFileDelete(const char * name)
  47. {
  48. AssertFatal( name != NULL, "dFileDelete - NULL file name" );
  49. TempAlloc< TCHAR > buf( dStrlen( name ) + 1 );
  50. #ifdef UNICODE
  51. convertUTF8toUTF16( name, buf, buf.size );
  52. #else
  53. dStrcpy( buf, name );
  54. #endif
  55. backslash( buf );
  56. if( Platform::isFile( name ) )
  57. return DeleteFile( buf );
  58. else
  59. return RemoveDirectory( buf );
  60. }
  61. bool dFileRename(const char *oldName, const char *newName)
  62. {
  63. AssertFatal( oldName != NULL && newName != NULL, "dFileRename - NULL file name" );
  64. TempAlloc< TCHAR > oldf( dStrlen( oldName ) + 1 );
  65. TempAlloc< TCHAR > newf( dStrlen( newName ) + 1 );
  66. #ifdef UNICODE
  67. convertUTF8toUTF16( oldName, oldf, oldf.size );
  68. convertUTF8toUTF16( newName, newf, newf.size );
  69. #else
  70. dStrcpy(oldf, oldName);
  71. dStrcpy(newf, newName);
  72. #endif
  73. backslash(oldf);
  74. backslash(newf);
  75. return MoveFile( oldf, newf );
  76. }
  77. bool dFileTouch(const char * name)
  78. {
  79. AssertFatal( name != NULL, "dFileTouch - NULL file name" );
  80. TempAlloc< TCHAR > buf( dStrlen( name ) + 1 );
  81. #ifdef UNICODE
  82. convertUTF8toUTF16( name, buf, buf.size );
  83. #else
  84. dStrcpy( buf, name );
  85. #endif
  86. backslash( buf );
  87. FILETIME ftime;
  88. GetSystemTimeAsFileTime( &ftime );
  89. HANDLE handle = CreateFile( buf, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  90. NULL, OPEN_EXISTING, 0, NULL );
  91. if( handle == INVALID_HANDLE_VALUE )
  92. return false;
  93. bool result = SetFileTime( handle, NULL, NULL, &ftime );
  94. CloseHandle( handle );
  95. return result;
  96. };
  97. bool dPathCopy(const char *fromName, const char *toName, bool nooverwrite)
  98. {
  99. AssertFatal( fromName != NULL && toName != NULL, "dPathCopy - NULL file name" );
  100. TempAlloc< TCHAR > from( dStrlen( fromName ) + 1 );
  101. TempAlloc< TCHAR > to( dStrlen( toName ) + 1 );
  102. #ifdef UNICODE
  103. convertUTF8toUTF16( fromName, from, from.size );
  104. convertUTF8toUTF16( toName, to, to.size );
  105. #else
  106. dStrcpy( from, fromName );
  107. dStrcpy( to, toName );
  108. #endif
  109. backslash( from );
  110. backslash( to );
  111. // Copy File
  112. if (Platform::isFile(fromName))
  113. return CopyFile( from, to, nooverwrite );
  114. // Copy Path
  115. else if (Platform::isDirectory(fromName))
  116. {
  117. // If the destination path exists and we don't want to overwrite, return.
  118. if ((Platform::isDirectory(toName) || Platform::isFile(toName)) && nooverwrite)
  119. return false;
  120. Vector<StringTableEntry> directoryInfo;
  121. Platform::dumpDirectories(fromName, directoryInfo, -1);
  122. Vector<Platform::FileInfo> fileInfo;
  123. Platform::dumpPath(fromName, fileInfo);
  124. Platform::clearExcludedDirectories();
  125. TempAlloc< char > tempBuf( to.size * 3 + MAX_PATH * 3 );
  126. // Create all the directories.
  127. for (S32 i = 0; i < directoryInfo.size(); i++)
  128. {
  129. const char* fromDir = directoryInfo[i];
  130. char* toDir = tempBuf;
  131. Platform::makeFullPathName(fromDir + dStrlen(fromName) + (dStricmp(fromDir, fromName) ? 1 : 0), tempBuf, tempBuf.size, toName);
  132. if(*(toDir + dStrlen(toDir) - 1) != '/')
  133. dStrcat(toDir, "/");
  134. forwardslash(toDir);
  135. if (!Platform::createPath(toDir))
  136. {
  137. //TODO: New directory should be deleted here.
  138. return false;
  139. }
  140. }
  141. TempAlloc< char > tempBuf1( from.size * 3 + MAX_PATH * 3 );
  142. #ifdef UNICODE
  143. TempAlloc< WCHAR > wtempBuf( tempBuf.size / 3 );
  144. TempAlloc< WCHAR > wtempBuf1( tempBuf1.size / 3 );
  145. #endif
  146. for (S32 i = 0; i < fileInfo.size(); i++)
  147. {
  148. char* fromFile = tempBuf1;
  149. dSprintf( tempBuf1, tempBuf1.size, "%s/%s", fileInfo[i].pFullPath, fileInfo[i].pFileName);
  150. char* toFile = tempBuf;
  151. Platform::makeFullPathName(fileInfo[i].pFullPath + dStrlen(fromName) + (dStricmp(fileInfo[i].pFullPath, fromName) ? 1 : 0), tempBuf, tempBuf.size, toName);
  152. dStrcat(toFile, "/");
  153. dStrcat(toFile, fileInfo[i].pFileName);
  154. backslash(fromFile);
  155. backslash(toFile);
  156. #ifdef UNICODE
  157. convertUTF8toUTF16( tempBuf, wtempBuf, wtempBuf.size );
  158. convertUTF8toUTF16( tempBuf1, wtempBuf1, wtempBuf1.size );
  159. WCHAR* f = wtempBuf1;
  160. WCHAR* t = wtempBuf;
  161. #else
  162. char *f = (char*)fromFile;
  163. char *t = (char*)toFile;
  164. #endif
  165. if (!::CopyFile(f, t, nooverwrite))
  166. {
  167. // New directory should be deleted here.
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. return false;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Constructors & Destructor
  177. //-----------------------------------------------------------------------------
  178. //-----------------------------------------------------------------------------
  179. // After construction, the currentStatus will be Closed and the capabilities
  180. // will be 0.
  181. //-----------------------------------------------------------------------------
  182. File::File()
  183. : currentStatus(Closed), capability(0)
  184. {
  185. AssertFatal(sizeof(HANDLE) == sizeof(void *), "File::File: cannot cast void* to HANDLE");
  186. handle = (void *)INVALID_HANDLE_VALUE;
  187. }
  188. //-----------------------------------------------------------------------------
  189. // insert a copy constructor here... (currently disabled)
  190. //-----------------------------------------------------------------------------
  191. //-----------------------------------------------------------------------------
  192. // Destructor
  193. //-----------------------------------------------------------------------------
  194. File::~File()
  195. {
  196. close();
  197. handle = (void *)INVALID_HANDLE_VALUE;
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  201. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  202. // true.
  203. //
  204. // Sets capability appropriate to the openMode.
  205. // Returns the currentStatus of the file.
  206. //-----------------------------------------------------------------------------
  207. File::FileStatus File::open(const char *filename, const AccessMode openMode)
  208. {
  209. AssertFatal(NULL != filename, "File::open: NULL fname");
  210. AssertWarn(INVALID_HANDLE_VALUE == (HANDLE)handle, "File::open: handle already valid");
  211. TempAlloc< TCHAR > fname( dStrlen( filename ) + 1 );
  212. #ifdef UNICODE
  213. convertUTF8toUTF16( filename, fname, fname.size );
  214. #else
  215. dStrcpy(fname, filename);
  216. #endif
  217. backslash( fname );
  218. // Close the file if it was already open...
  219. if (Closed != currentStatus)
  220. close();
  221. // create the appropriate type of file...
  222. switch (openMode)
  223. {
  224. case Read:
  225. handle = (void *)CreateFile(fname,
  226. GENERIC_READ,
  227. FILE_SHARE_READ,
  228. NULL,
  229. OPEN_EXISTING,
  230. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  231. NULL);
  232. break;
  233. case Write:
  234. handle = (void *)CreateFile(fname,
  235. GENERIC_WRITE,
  236. 0,
  237. NULL,
  238. CREATE_ALWAYS,
  239. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  240. NULL);
  241. break;
  242. case ReadWrite:
  243. handle = (void *)CreateFile(fname,
  244. GENERIC_WRITE | GENERIC_READ,
  245. 0,
  246. NULL,
  247. OPEN_ALWAYS,
  248. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  249. NULL);
  250. break;
  251. case WriteAppend:
  252. handle = (void *)CreateFile(fname,
  253. GENERIC_WRITE,
  254. 0,
  255. NULL,
  256. OPEN_ALWAYS,
  257. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  258. NULL);
  259. break;
  260. default:
  261. AssertFatal(false, "File::open: bad access mode"); // impossible
  262. }
  263. if (INVALID_HANDLE_VALUE == (HANDLE)handle) // handle not created successfully
  264. {
  265. return setStatus();
  266. }
  267. else
  268. {
  269. // successfully created file, so set the file capabilities...
  270. switch (openMode)
  271. {
  272. case Read:
  273. capability = U32(FileRead);
  274. break;
  275. case Write:
  276. case WriteAppend:
  277. capability = U32(FileWrite);
  278. break;
  279. case ReadWrite:
  280. capability = U32(FileRead) |
  281. U32(FileWrite);
  282. break;
  283. default:
  284. AssertFatal(false, "File::open: bad access mode");
  285. }
  286. return currentStatus = Ok; // success!
  287. }
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Get the current position of the file pointer.
  291. //-----------------------------------------------------------------------------
  292. U32 File::getPosition() const
  293. {
  294. AssertFatal(Closed != currentStatus, "File::getPosition: file closed");
  295. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getPosition: invalid file handle");
  296. return SetFilePointer((HANDLE)handle,
  297. 0, // how far to move
  298. NULL, // pointer to high word
  299. FILE_CURRENT); // from what point
  300. }
  301. //-----------------------------------------------------------------------------
  302. // Set the position of the file pointer.
  303. // Absolute and relative positioning is supported via the absolutePos
  304. // parameter.
  305. //
  306. // If positioning absolutely, position MUST be positive - an IOError results if
  307. // position is negative.
  308. // Position can be negative if positioning relatively, however positioning
  309. // before the start of the file is an IOError.
  310. //
  311. // Returns the currentStatus of the file.
  312. //-----------------------------------------------------------------------------
  313. File::FileStatus File::setPosition(S32 position, bool absolutePos)
  314. {
  315. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  316. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::setPosition: invalid file handle");
  317. if (Ok != currentStatus && EOS != currentStatus)
  318. return currentStatus;
  319. U32 finalPos;
  320. if (absolutePos)
  321. {
  322. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  323. // position beyond EOS is OK
  324. finalPos = SetFilePointer((HANDLE)handle,
  325. position,
  326. NULL,
  327. FILE_BEGIN);
  328. }
  329. else
  330. {
  331. AssertFatal((getPosition() >= (U32)abs(position) && 0 > position) || 0 <= position, "File::setPosition: negative relative position");
  332. // position beyond EOS is OK
  333. finalPos = SetFilePointer((HANDLE)handle,
  334. position,
  335. NULL,
  336. FILE_CURRENT);
  337. }
  338. if (0xffffffff == finalPos)
  339. return setStatus(); // unsuccessful
  340. else if (finalPos >= getSize())
  341. return currentStatus = EOS; // success, at end of file
  342. else
  343. return currentStatus = Ok; // success!
  344. }
  345. //-----------------------------------------------------------------------------
  346. // Get the size of the file in bytes.
  347. // It is an error to query the file size for a Closed file, or for one with an
  348. // error status.
  349. //-----------------------------------------------------------------------------
  350. U32 File::getSize() const
  351. {
  352. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  353. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::getSize: invalid file handle");
  354. if (Ok == currentStatus || EOS == currentStatus)
  355. {
  356. DWORD high;
  357. return GetFileSize((HANDLE)handle, &high); // success!
  358. }
  359. else
  360. return 0; // unsuccessful
  361. }
  362. //-----------------------------------------------------------------------------
  363. // Flush the file.
  364. // It is an error to flush a read-only file.
  365. // Returns the currentStatus of the file.
  366. //-----------------------------------------------------------------------------
  367. File::FileStatus File::flush()
  368. {
  369. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  370. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::flush: invalid file handle");
  371. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  372. if (0 != FlushFileBuffers((HANDLE)handle))
  373. return setStatus(); // unsuccessful
  374. else
  375. return currentStatus = Ok; // success!
  376. }
  377. //-----------------------------------------------------------------------------
  378. // Close the File.
  379. //
  380. // Returns the currentStatus
  381. //-----------------------------------------------------------------------------
  382. File::FileStatus File::close()
  383. {
  384. // check if it's already closed...
  385. if (Closed == currentStatus)
  386. return currentStatus;
  387. // it's not, so close it...
  388. if (INVALID_HANDLE_VALUE != (HANDLE)handle)
  389. {
  390. if (0 == CloseHandle((HANDLE)handle))
  391. return setStatus(); // unsuccessful
  392. }
  393. handle = (void *)INVALID_HANDLE_VALUE;
  394. return currentStatus = Closed;
  395. }
  396. //-----------------------------------------------------------------------------
  397. // Self-explanatory.
  398. //-----------------------------------------------------------------------------
  399. File::FileStatus File::getStatus() const
  400. {
  401. return currentStatus;
  402. }
  403. //-----------------------------------------------------------------------------
  404. // Sets and returns the currentStatus when an error has been encountered.
  405. //-----------------------------------------------------------------------------
  406. File::FileStatus File::setStatus()
  407. {
  408. switch (GetLastError())
  409. {
  410. case ERROR_INVALID_HANDLE:
  411. case ERROR_INVALID_ACCESS:
  412. case ERROR_TOO_MANY_OPEN_FILES:
  413. case ERROR_FILE_NOT_FOUND:
  414. case ERROR_SHARING_VIOLATION:
  415. case ERROR_HANDLE_DISK_FULL:
  416. return currentStatus = IOError;
  417. default:
  418. return currentStatus = UnknownError;
  419. }
  420. }
  421. //-----------------------------------------------------------------------------
  422. // Sets and returns the currentStatus to status.
  423. //-----------------------------------------------------------------------------
  424. File::FileStatus File::setStatus(File::FileStatus status)
  425. {
  426. return currentStatus = status;
  427. }
  428. //-----------------------------------------------------------------------------
  429. // Read from a file.
  430. // The number of bytes to read is passed in size, the data is returned in src.
  431. // The number of bytes read is available in bytesRead if a non-Null pointer is
  432. // provided.
  433. //-----------------------------------------------------------------------------
  434. File::FileStatus File::read(U32 size, char *dst, U32 *bytesRead)
  435. {
  436. AssertFatal(Closed != currentStatus, "File::read: file closed");
  437. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::read: invalid file handle");
  438. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  439. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  440. AssertWarn(0 != size, "File::read: size of zero");
  441. if (Ok != currentStatus || 0 == size)
  442. return currentStatus;
  443. else
  444. {
  445. DWORD lastBytes;
  446. DWORD *bytes = (NULL == bytesRead) ? &lastBytes : (DWORD *)bytesRead;
  447. if (0 != ReadFile((HANDLE)handle, dst, size, bytes, NULL))
  448. {
  449. if(*((U32 *)bytes) != size)
  450. return currentStatus = EOS; // end of stream
  451. }
  452. else
  453. return setStatus(); // unsuccessful
  454. }
  455. return currentStatus = Ok; // successfully read size bytes
  456. }
  457. //-----------------------------------------------------------------------------
  458. // Write to a file.
  459. // The number of bytes to write is passed in size, the data is passed in src.
  460. // The number of bytes written is available in bytesWritten if a non-Null
  461. // pointer is provided.
  462. //-----------------------------------------------------------------------------
  463. File::FileStatus File::write(U32 size, const char *src, U32 *bytesWritten)
  464. {
  465. AssertFatal(Closed != currentStatus, "File::write: file closed");
  466. AssertFatal(INVALID_HANDLE_VALUE != (HANDLE)handle, "File::write: invalid file handle");
  467. AssertFatal(NULL != src, "File::write: NULL source pointer");
  468. AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability");
  469. AssertWarn(0 != size, "File::write: size of zero");
  470. if ((Ok != currentStatus && EOS != currentStatus) || 0 == size)
  471. return currentStatus;
  472. else
  473. {
  474. DWORD lastBytes;
  475. DWORD *bytes = (NULL == bytesWritten) ? &lastBytes : (DWORD *)bytesWritten;
  476. if (0 != WriteFile((HANDLE)handle, src, size, bytes, NULL))
  477. return currentStatus = Ok; // success!
  478. else
  479. return setStatus(); // unsuccessful
  480. }
  481. }
  482. //-----------------------------------------------------------------------------
  483. // Self-explanatory.
  484. //-----------------------------------------------------------------------------
  485. bool File::hasCapability(Capability cap) const
  486. {
  487. return (0 != (U32(cap) & capability));
  488. }
  489. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  490. {
  491. if(a.v2 > b.v2)
  492. return 1;
  493. if(a.v2 < b.v2)
  494. return -1;
  495. if(a.v1 > b.v1)
  496. return 1;
  497. if(a.v1 < b.v1)
  498. return -1;
  499. return 0;
  500. }
  501. static bool recurseDumpPath(const char *path, const char *pattern, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth )
  502. {
  503. WIN32_FIND_DATA findData;
  504. TempAlloc< char > fullPath( dStrlen( path ) * 3 + MAX_PATH * 3 + 1 );
  505. Platform::makeFullPathName( path, fullPath, fullPath.size );
  506. U32 lenFullPath = dStrlen( fullPath );
  507. TempAlloc< char > buf( lenFullPath + MAX_PATH * 3 + 2 );
  508. dSprintf( buf, buf.size, "%s/%s", fullPath.ptr, pattern );
  509. #ifdef UNICODE
  510. TempAlloc< WCHAR > searchBuf( buf.size );
  511. convertUTF8toUTF16( buf, searchBuf, searchBuf.size );
  512. WCHAR* search = searchBuf;
  513. #else
  514. char *search = buf;
  515. #endif
  516. backslash( search );
  517. HANDLE handle = FindFirstFile(search, &findData);
  518. if (handle == INVALID_HANDLE_VALUE)
  519. return false;
  520. do
  521. {
  522. #ifdef UNICODE
  523. convertUTF16toUTF8( findData.cFileName, buf, buf.size );
  524. char* fnbuf = buf;
  525. #else
  526. char *fnbuf = findData.cFileName;
  527. #endif
  528. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  529. {
  530. // make sure it is a directory
  531. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  532. continue;
  533. // skip . and .. directories
  534. if (dStrcmp( findData.cFileName, TEXT( "." ) ) == 0 || dStrcmp( findData.cFileName, TEXT( ".." ) ) == 0)
  535. continue;
  536. // Skip excluded directores
  537. if(Platform::isExcludedDirectory(fnbuf))
  538. continue;
  539. dSprintf( fullPath, fullPath.size, "%s/%s", path, fnbuf);
  540. char* child = fullPath;
  541. if( recurseDepth > 0 )
  542. recurseDumpPath(child, pattern, fileVector, recurseDepth - 1);
  543. else if (recurseDepth == -1)
  544. recurseDumpPath(child, pattern, fileVector, -1);
  545. }
  546. else
  547. {
  548. // make sure it is the kind of file we're looking for
  549. if (findData.dwFileAttributes &
  550. (FILE_ATTRIBUTE_DIRECTORY|
  551. FILE_ATTRIBUTE_OFFLINE|
  552. FILE_ATTRIBUTE_SYSTEM|
  553. FILE_ATTRIBUTE_TEMPORARY) )
  554. continue;
  555. // add it to the list
  556. fileVector.increment();
  557. Platform::FileInfo& rInfo = fileVector.last();
  558. forwardslash( fnbuf );
  559. rInfo.pFullPath = StringTable->insert(path);
  560. rInfo.pFileName = StringTable->insert(fnbuf);
  561. rInfo.fileSize = findData.nFileSizeLow;
  562. }
  563. }while(FindNextFile(handle, &findData));
  564. FindClose(handle);
  565. return true;
  566. }
  567. //--------------------------------------
  568. bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime)
  569. {
  570. WIN32_FIND_DATA findData;
  571. TempAlloc< TCHAR > fp( dStrlen( filePath ) + 1 );
  572. #ifdef UNICODE
  573. convertUTF8toUTF16( filePath, fp, fp.size );
  574. #else
  575. dStrcpy( fp, filePath );
  576. #endif
  577. backslash( fp );
  578. HANDLE h = FindFirstFile(fp, &findData);
  579. if(h == INVALID_HANDLE_VALUE)
  580. return false;
  581. if(createTime)
  582. {
  583. createTime->v1 = findData.ftCreationTime.dwLowDateTime;
  584. createTime->v2 = findData.ftCreationTime.dwHighDateTime;
  585. }
  586. if(modifyTime)
  587. {
  588. modifyTime->v1 = findData.ftLastWriteTime.dwLowDateTime;
  589. modifyTime->v2 = findData.ftLastWriteTime.dwHighDateTime;
  590. }
  591. FindClose(h);
  592. return true;
  593. }
  594. //--------------------------------------
  595. bool Platform::createPath(const char *file)
  596. {
  597. TempAlloc< TCHAR > pathbuf( dStrlen( file ) + 1 );
  598. #ifdef UNICODE
  599. TempAlloc< WCHAR > fileBuf( pathbuf.size );
  600. convertUTF8toUTF16( file, fileBuf, fileBuf.size );
  601. const WCHAR* fileName = fileBuf;
  602. const WCHAR* dir;
  603. #else
  604. const char* fileName = file;
  605. const char* dir;
  606. #endif
  607. pathbuf[ 0 ] = 0;
  608. U32 pathLen = 0;
  609. while((dir = dStrchr(fileName, '/')) != NULL)
  610. {
  611. TCHAR* pathptr = pathbuf;
  612. dMemcpy( pathptr + pathLen, fileName, ( dir - fileName ) * sizeof( TCHAR ) );
  613. pathbuf[pathLen + dir-fileName] = 0;
  614. // ignore return value because we are fine with already existing directory
  615. CreateDirectory(pathbuf, NULL);
  616. pathLen += dir - fileName;
  617. pathbuf[pathLen++] = '\\';
  618. fileName = dir + 1;
  619. }
  620. return true;
  621. }
  622. // [rene, 04/05/2008] Not used currently so did not bother updating.
  623. #if 0
  624. // [tom, 7/12/2005] Rather then converting this to unicode, just using the ANSI
  625. // versions of the Win32 API as its quicker for testing.
  626. bool Platform::cdFileExists(const char *filePath, const char *volumeName, S32 serialNum)
  627. {
  628. if (!filePath || !filePath[0])
  629. return true;
  630. //first find the CD device...
  631. char fileBuf[1024];
  632. char drivesBuf[256];
  633. S32 length = GetLogicalDriveStringsA(256, drivesBuf);
  634. char *drivePtr = drivesBuf;
  635. while (S32(drivePtr - drivesBuf) < length)
  636. {
  637. char driveVolume[256], driveFileSystem[256];
  638. U32 driveSerial, driveFNLength, driveFlags;
  639. if ((dStricmp(drivePtr, "A:\\") != 0 && dStricmp(drivePtr, "B:\\") != 0) &&
  640. GetVolumeInformationA((const char*)drivePtr, &driveVolume[0], (unsigned long)255,
  641. (unsigned long*)&driveSerial, (unsigned long*)&driveFNLength,
  642. (unsigned long*)&driveFlags, &driveFileSystem[0], (unsigned long)255))
  643. {
  644. #if defined (TORQUE_DEBUG) || !defined (TORQUE_SHIPPING)
  645. Con::printf("Found Drive: %s, vol: %s, serial: %d", drivePtr, driveVolume, driveSerial);
  646. #endif
  647. //see if the volume and serial number match
  648. if (!dStricmp(volumeName, driveVolume) && (!serialNum || (serialNum == driveSerial)))
  649. {
  650. //see if the file exists on this volume
  651. if(dStrlen(drivePtr) == 3 && drivePtr[2] == '\\' && filePath[0] == '\\')
  652. dSprintf(fileBuf, sizeof(fileBuf), "%s%s", drivePtr, filePath + 1);
  653. else
  654. dSprintf(fileBuf, sizeof(fileBuf), "%s%s", drivePtr, filePath);
  655. #if defined (TORQUE_DEBUG) || !defined (TORQUE_SHIPPING)
  656. Con::printf("Looking for file: %s on %s", fileBuf, driveVolume);
  657. #endif
  658. WIN32_FIND_DATAA findData;
  659. HANDLE h = FindFirstFileA(fileBuf, &findData);
  660. if(h != INVALID_HANDLE_VALUE)
  661. {
  662. FindClose(h);
  663. return true;
  664. }
  665. FindClose(h);
  666. }
  667. }
  668. //check the next drive
  669. drivePtr += dStrlen(drivePtr) + 1;
  670. }
  671. return false;
  672. }
  673. #endif
  674. //--------------------------------------
  675. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo> &fileVector, S32 recurseDepth)
  676. {
  677. return recurseDumpPath(path, "*", fileVector, recurseDepth );
  678. }
  679. //--------------------------------------
  680. //StringTableEntry Platform::getWorkingDirectory()
  681. //{
  682. // return getCurrentDirectory();
  683. //}
  684. StringTableEntry Platform::getCurrentDirectory()
  685. {
  686. TempAlloc< TCHAR > buf( 2048 );
  687. GetCurrentDirectory( buf.size, buf );
  688. forwardslash( buf );
  689. #ifdef UNICODE
  690. char* utf8 = convertUTF16toUTF8( buf );
  691. StringTableEntry result = StringTable->insert( utf8 );
  692. SAFE_DELETE_ARRAY( utf8 );
  693. return result;
  694. #else
  695. return StringTable->insert( buf );
  696. #endif
  697. }
  698. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  699. {
  700. if (Platform::getWebDeployment())
  701. return true;
  702. TempAlloc< TCHAR > buf( dStrlen( newDir ) + 2 );
  703. #ifdef UNICODE
  704. convertUTF8toUTF16( newDir, buf, buf.size - 1 );
  705. #else
  706. dStrcpy( buf, newDir );
  707. #endif
  708. backslash( buf );
  709. return SetCurrentDirectory( buf );
  710. }
  711. #ifdef UNICODE
  712. static void getExecutableInfo( StringTableEntry* path, StringTableEntry* exe )
  713. {
  714. static StringTableEntry pathEntry = NULL;
  715. static StringTableEntry exeEntry = NULL;
  716. if( !pathEntry )
  717. {
  718. if (!Platform::getWebDeployment())
  719. {
  720. WCHAR cen_buf[ 2048 ];
  721. GetModuleFileNameW( NULL, cen_buf, sizeof( cen_buf ) / sizeof( cen_buf[ 0 ] ) );
  722. forwardslash( cen_buf );
  723. WCHAR* delimiter = dStrrchr( cen_buf, '/' );
  724. if( delimiter )
  725. *delimiter = '\0';
  726. char* pathBuf = convertUTF16toUTF8( cen_buf );
  727. char* exeBuf = convertUTF16toUTF8( delimiter + 1 );
  728. pathEntry = StringTable->insert( pathBuf );
  729. exeEntry = StringTable->insert( exeBuf );
  730. SAFE_DELETE_ARRAY( pathBuf );
  731. SAFE_DELETE_ARRAY( exeBuf );
  732. }
  733. else
  734. {
  735. char cdir[4096];
  736. GetCurrentDirectoryA(4096, cdir);
  737. pathEntry = StringTable->insert(cdir);
  738. exeEntry = StringTable->insert("WebGameCtrl.exe");
  739. }
  740. }
  741. if( path )
  742. *path = pathEntry;
  743. if( exe )
  744. *exe = exeEntry;
  745. }
  746. #endif
  747. StringTableEntry Platform::getExecutableName()
  748. {
  749. #ifdef UNICODE
  750. StringTableEntry exe;
  751. getExecutableInfo( NULL, &exe );
  752. return exe;
  753. #else
  754. static StringTableEntry cen = NULL;
  755. if (!cen)
  756. {
  757. char cen_buf[2048];
  758. GetModuleFileNameA( NULL, cen_buf, 2047);
  759. forwardslash(cen_buf);
  760. char *delimiter = dStrrchr( cen_buf, '/' );
  761. if( delimiter != NULL )
  762. {
  763. *delimiter = 0x00;
  764. delimiter++;
  765. cen = StringTable->insert(delimiter);
  766. }
  767. else
  768. cen = StringTable->insert(cen_buf);
  769. }
  770. return cen;
  771. #endif
  772. }
  773. StringTableEntry Platform::getExecutablePath()
  774. {
  775. #ifdef UNICODE
  776. StringTableEntry path;
  777. getExecutableInfo( &path, NULL );
  778. return path;
  779. #else
  780. static StringTableEntry cen = NULL;
  781. if (!cen)
  782. {
  783. char cen_buf[2048];
  784. GetModuleFileNameA( NULL, cen_buf, 2047);
  785. forwardslash(cen_buf);
  786. char *delimiter = dStrrchr( cen_buf, '/' );
  787. if( delimiter != NULL )
  788. *delimiter = 0x00;
  789. cen = StringTable->insert(cen_buf);
  790. }
  791. return cen;
  792. #endif
  793. }
  794. //--------------------------------------
  795. bool Platform::isFile(const char *pFilePath)
  796. {
  797. if (!pFilePath || !*pFilePath)
  798. return false;
  799. TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 );
  800. #ifdef UNICODE
  801. convertUTF8toUTF16( pFilePath, buf, buf.size );
  802. #else
  803. dStrcpy( buf, pFilePath );
  804. #endif
  805. backslash( buf );
  806. // Get file info
  807. WIN32_FIND_DATA findData;
  808. HANDLE handle = FindFirstFile(buf, &findData);
  809. FindClose(handle);
  810. if(handle == INVALID_HANDLE_VALUE)
  811. {
  812. // Since file does not exist on disk see if it exists in a zip file loaded
  813. return Torque::FS::IsFile(pFilePath);
  814. }
  815. // if the file is a Directory, Offline, System or Temporary then FALSE
  816. if (findData.dwFileAttributes &
  817. (FILE_ATTRIBUTE_DIRECTORY|
  818. FILE_ATTRIBUTE_OFFLINE|
  819. FILE_ATTRIBUTE_SYSTEM|
  820. FILE_ATTRIBUTE_TEMPORARY) )
  821. return false;
  822. // must be a real file then
  823. return true;
  824. }
  825. //--------------------------------------
  826. S32 Platform::getFileSize(const char *pFilePath)
  827. {
  828. if (!pFilePath || !*pFilePath)
  829. return -1;
  830. TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 );
  831. #ifdef UNICODE
  832. convertUTF8toUTF16( pFilePath, buf, buf.size );
  833. #else
  834. dStrcpy( buf, pFilePath );
  835. #endif
  836. backslash( buf );
  837. // Get file info
  838. WIN32_FIND_DATA findData;
  839. HANDLE handle = FindFirstFile(buf, &findData);
  840. if(handle == INVALID_HANDLE_VALUE)
  841. return -1;
  842. FindClose(handle);
  843. // if the file is a Directory, Offline, System or Temporary then FALSE
  844. if (findData.dwFileAttributes &
  845. (FILE_ATTRIBUTE_DIRECTORY|
  846. FILE_ATTRIBUTE_OFFLINE|
  847. FILE_ATTRIBUTE_SYSTEM|
  848. FILE_ATTRIBUTE_TEMPORARY) )
  849. return -1;
  850. // must be a real file then
  851. return findData.nFileSizeLow;
  852. }
  853. //--------------------------------------
  854. bool Platform::isDirectory(const char *pDirPath)
  855. {
  856. if (!pDirPath || !*pDirPath)
  857. return false;
  858. TempAlloc< TCHAR > buf( dStrlen( pDirPath ) + 1 );
  859. #ifdef UNICODE
  860. convertUTF8toUTF16( pDirPath, buf, buf.size );
  861. #else
  862. dStrcpy( buf, pDirPath );
  863. #endif
  864. backslash( buf );
  865. // Get file info
  866. WIN32_FIND_DATA findData;
  867. HANDLE handle = FindFirstFile(buf, &findData);
  868. // [neo, 5/15/2007]
  869. // This check was AFTER FindClose for some reason - this is most probably the
  870. // original intent.
  871. if(handle == INVALID_HANDLE_VALUE)
  872. return false;
  873. FindClose(handle);
  874. // if the file is a Directory, Offline, System or Temporary then FALSE
  875. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  876. {
  877. // make sure it's a valid game directory
  878. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SYSTEM) )
  879. return false;
  880. // must be a directory
  881. return true;
  882. }
  883. return false;
  884. }
  885. //--------------------------------------
  886. bool Platform::isSubDirectory(const char *pParent, const char *pDir)
  887. {
  888. if (!pParent || !*pDir)
  889. return false;
  890. const char* fileName = avar("%s/*", pParent);
  891. TempAlloc< TCHAR > file( dStrlen( fileName ) + 1 );
  892. TempAlloc< TCHAR > dir( dStrlen( pDir ) + 1 );
  893. #ifdef UNICODE
  894. convertUTF8toUTF16( fileName, file, file.size );
  895. convertUTF8toUTF16( pDir, dir, dir.size );
  896. #else
  897. dStrcpy( file, fileName );
  898. dStrcpy( dir, pDir );
  899. #endif
  900. backslash( file );
  901. backslash( dir );
  902. // this is somewhat of a brute force method but we need to be 100% sure
  903. // that the user cannot enter things like ../dir or /dir etc,...
  904. WIN32_FIND_DATA findData;
  905. HANDLE handle = FindFirstFile(file, &findData);
  906. if (handle == INVALID_HANDLE_VALUE)
  907. return false;
  908. do
  909. {
  910. // if it is a directory...
  911. if (findData.dwFileAttributes &
  912. (FILE_ATTRIBUTE_DIRECTORY|
  913. FILE_ATTRIBUTE_OFFLINE|
  914. FILE_ATTRIBUTE_SYSTEM|
  915. FILE_ATTRIBUTE_TEMPORARY) )
  916. {
  917. //FIXME: this has to be dStrcasecmp but there's no implementation for Unicode
  918. // and the names match
  919. if (dStrcmp(dir, findData.cFileName ) == 0)
  920. {
  921. // then we have a real sub directory
  922. FindClose(handle);
  923. return true;
  924. }
  925. }
  926. }while(FindNextFile(handle, &findData));
  927. FindClose(handle);
  928. return false;
  929. }
  930. //------------------------------------------------------------------------------
  931. bool Platform::fileTimeToString(FileTime * time, char * string, U32 strLen)
  932. {
  933. if(!time || !string)
  934. return(false);
  935. dSprintf(string, strLen, "%d:%d", time->v2, time->v1);
  936. return(true);
  937. }
  938. bool Platform::stringToFileTime(const char * string, FileTime * time)
  939. {
  940. if(!time || !string)
  941. return(false);
  942. char buf[80];
  943. dSprintf(buf, sizeof(buf), (char *)string);
  944. char * sep = (char *)dStrstr((const char *)buf, (const char *)":");
  945. if(!sep)
  946. return(false);
  947. *sep = 0;
  948. sep++;
  949. time->v2 = dAtoi(buf);
  950. time->v1 = dAtoi(sep);
  951. return(true);
  952. }
  953. // Volume Functions
  954. void Platform::getVolumeNamesList( Vector<const char*>& out_rNameVector, bool bOnlyFixedDrives )
  955. {
  956. DWORD dwDrives = GetLogicalDrives();
  957. DWORD dwMask = 1;
  958. char driveLetter[12];
  959. out_rNameVector.clear();
  960. for(S32 i = 0; i < 32; i++ )
  961. {
  962. dMemset(driveLetter,0,12);
  963. if( dwDrives & dwMask )
  964. {
  965. dSprintf(driveLetter, 12, "%c:", (i + 'A'));
  966. if( bOnlyFixedDrives && GetDriveTypeA(driveLetter) == DRIVE_FIXED )
  967. out_rNameVector.push_back( StringTable->insert( driveLetter ) );
  968. else if ( !bOnlyFixedDrives )
  969. out_rNameVector.push_back( StringTable->insert( driveLetter ) );
  970. }
  971. dwMask <<= 1;
  972. }
  973. }
  974. void Platform::getVolumeInformationList( Vector<VolumeInformation>& out_rVolumeInfoVector, bool bOnlyFixedDrives )
  975. {
  976. Vector<const char*> drives;
  977. getVolumeNamesList( drives, bOnlyFixedDrives );
  978. if( ! drives.empty() )
  979. {
  980. Vector<StringTableEntry>::iterator i;
  981. for( i = drives.begin(); i != drives.end(); i++ )
  982. {
  983. VolumeInformation info;
  984. TCHAR lpszVolumeName[ 256 ];
  985. TCHAR lpszFileSystem[ 256 ];
  986. DWORD dwSerial = 0;
  987. DWORD dwMaxComponentLength = 0;
  988. DWORD dwFileSystemFlags = 0;
  989. dMemset( lpszVolumeName, 0, sizeof( lpszVolumeName ) );
  990. dMemset( lpszFileSystem, 0, sizeof( lpszFileSystem ) );
  991. dMemset( &info, 0, sizeof( VolumeInformation ) );
  992. // More volume information
  993. UINT uDriveType = GetDriveTypeA( (*i) );
  994. if( uDriveType == DRIVE_UNKNOWN )
  995. info.Type = DRIVETYPE_UNKNOWN;
  996. else if( uDriveType == DRIVE_REMOVABLE )
  997. info.Type = DRIVETYPE_REMOVABLE;
  998. else if( uDriveType == DRIVE_FIXED )
  999. info.Type = DRIVETYPE_FIXED;
  1000. else if( uDriveType == DRIVE_CDROM )
  1001. info.Type = DRIVETYPE_CDROM;
  1002. else if( uDriveType == DRIVE_RAMDISK )
  1003. info.Type = DRIVETYPE_RAMDISK;
  1004. else if( uDriveType == DRIVE_REMOTE )
  1005. info.Type = DRIVETYPE_REMOTE;
  1006. info.RootPath = StringTable->insert( (*i) );
  1007. // We don't retrieve drive volume info for removable drives, because it's loud :(
  1008. if( info.Type != DRIVETYPE_REMOVABLE )
  1009. {
  1010. #ifdef UNICODE
  1011. WCHAR ibuf[ 3 ];
  1012. ibuf[ 0 ] = ( *i )[ 0 ];
  1013. ibuf[ 1 ] = ':';
  1014. ibuf[ 2 ] = '\0';
  1015. #else
  1016. char* ibuf = *i;
  1017. #endif
  1018. // Standard volume information
  1019. GetVolumeInformation( ibuf, lpszVolumeName, sizeof( lpszVolumeName ) / sizeof( lpszVolumeName[ 0 ] ),
  1020. &dwSerial, &dwMaxComponentLength, &dwFileSystemFlags, lpszFileSystem,
  1021. sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) );
  1022. #ifdef UNICODE
  1023. char buf[ sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) * 3 + 1 ];
  1024. convertUTF16toUTF8( lpszFileSystem, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) );
  1025. info.FileSystem = StringTable->insert( buf );
  1026. convertUTF16toUTF8( lpszVolumeName, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) );
  1027. info.Name = StringTable->insert( buf );
  1028. #else
  1029. info.FileSystem = StringTable->insert( lpszFileSystem );
  1030. info.Name = StringTable->insert( lpszVolumeName );
  1031. #endif
  1032. info.SerialNumber = dwSerial;
  1033. // Won't compile on something prior to XP.
  1034. info.ReadOnly = dwFileSystemFlags & FILE_READ_ONLY_VOLUME;
  1035. }
  1036. out_rVolumeInfoVector.push_back( info );
  1037. // I opted not to get free disk space because of the overhead of the calculations required for it
  1038. }
  1039. }
  1040. }
  1041. bool Platform::hasSubDirectory(const char *pPath)
  1042. {
  1043. if( !pPath )
  1044. return false;
  1045. char searchBuf[1024];
  1046. // Compose our search string - Format : ([path]/[subpath]/*)
  1047. char trail = pPath[ dStrlen(pPath) - 1 ];
  1048. if( trail == '/' )
  1049. dStrcpy( searchBuf, pPath );
  1050. else
  1051. dSprintf(searchBuf, 1024, "%s/*", pPath );
  1052. #ifdef UNICODE
  1053. WCHAR buf[ 1024 ];
  1054. convertUTF8toUTF16( searchBuf, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) );
  1055. WCHAR* search = buf;
  1056. #else
  1057. char* search = searchBuf;
  1058. #endif
  1059. backslash( search );
  1060. // See if we get any hits
  1061. WIN32_FIND_DATA findData;
  1062. HANDLE handle = FindFirstFile(search, &findData);
  1063. if (handle == INVALID_HANDLE_VALUE)
  1064. return false;
  1065. bool result = false;
  1066. do
  1067. {
  1068. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1069. {
  1070. // skip . and .. directories
  1071. if (dStrcmp(findData.cFileName, TEXT( "." ) ) == 0 || dStrcmp(findData.cFileName, TEXT( ".." ) ) == 0)
  1072. continue;
  1073. #ifdef UNICODE
  1074. char fileName[ 1024 ];
  1075. convertUTF16toUTF8( findData.cFileName, fileName, sizeof( fileName ) / sizeof( fileName[ 0 ] ) );
  1076. #else
  1077. char* fileName = findData.cFileName;
  1078. #endif
  1079. if( Platform::isExcludedDirectory( fileName ) )
  1080. continue;
  1081. result = true;
  1082. break;
  1083. }
  1084. }
  1085. while(FindNextFile(handle, &findData));
  1086. FindClose(handle);
  1087. Platform::clearExcludedDirectories();
  1088. return result;
  1089. }
  1090. static bool recurseDumpDirectories(const char *basePath, const char *subPath, Vector<StringTableEntry> &directoryVector, S32 currentDepth, S32 recurseDepth, bool noBasePath)
  1091. {
  1092. TempAlloc< char > search( 1024 );
  1093. //-----------------------------------------------------------------------------
  1094. // Compose our search string - Format : ([path]/[subpath]/*)
  1095. //-----------------------------------------------------------------------------
  1096. char trail = basePath[ dStrlen(basePath) - 1 ];
  1097. char subTrail = subPath ? subPath[ dStrlen(subPath) - 1 ] : '\0';
  1098. if( trail == '/' )
  1099. {
  1100. // we have a sub path and it's not an empty string
  1101. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1102. {
  1103. if( subTrail == '/' )
  1104. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1105. else
  1106. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1107. }
  1108. else
  1109. dSprintf( search, search.size, "%s*", basePath );
  1110. }
  1111. else
  1112. {
  1113. if( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) )
  1114. if( subTrail == '/' )
  1115. dSprintf(search, search.size, "%s%s*", basePath,subPath );
  1116. else
  1117. dSprintf(search, search.size, "%s%s/*", basePath,subPath );
  1118. else
  1119. dSprintf(search, search.size, "%s/*", basePath );
  1120. }
  1121. #ifdef UNICODE
  1122. TempAlloc< WCHAR > searchStr( dStrlen( search ) + 1 );
  1123. convertUTF8toUTF16( search, searchStr, searchStr.size );
  1124. #else
  1125. char* searchStr = search;
  1126. #endif
  1127. backslash( searchStr );
  1128. //-----------------------------------------------------------------------------
  1129. // See if we get any hits
  1130. //-----------------------------------------------------------------------------
  1131. WIN32_FIND_DATA findData;
  1132. HANDLE handle = FindFirstFile(searchStr, &findData);
  1133. if (handle == INVALID_HANDLE_VALUE)
  1134. return false;
  1135. //-----------------------------------------------------------------------------
  1136. // add path to our return list ( provided it is valid )
  1137. //-----------------------------------------------------------------------------
  1138. if( !Platform::isExcludedDirectory( subPath ) )
  1139. {
  1140. if( noBasePath )
  1141. {
  1142. // We have a path and it's not an empty string or an excluded directory
  1143. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1144. directoryVector.push_back( StringTable->insert( subPath ) );
  1145. }
  1146. else
  1147. {
  1148. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ) )
  1149. {
  1150. char szPath [ 1024 ];
  1151. dMemset( szPath, 0, 1024 );
  1152. if( trail != '/' )
  1153. dSprintf( szPath, 1024, "%s%s", basePath, subPath );
  1154. else
  1155. dSprintf( szPath, 1024, "%s%s", basePath, &subPath[1] );
  1156. directoryVector.push_back( StringTable->insert( szPath ) );
  1157. }
  1158. else
  1159. directoryVector.push_back( StringTable->insert( basePath ) );
  1160. }
  1161. }
  1162. //-----------------------------------------------------------------------------
  1163. // Iterate through and grab valid directories
  1164. //-----------------------------------------------------------------------------
  1165. #ifdef UNICODE
  1166. TempAlloc< char > fileName( 1024 );
  1167. #endif
  1168. do
  1169. {
  1170. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1171. {
  1172. // skip . and .. directories
  1173. if (dStrcmp(findData.cFileName, TEXT( "." )) == 0 || dStrcmp(findData.cFileName, TEXT( ".." )) == 0)
  1174. continue;
  1175. #ifdef UNICODE
  1176. convertUTF16toUTF8( findData.cFileName, fileName, fileName.size );
  1177. #else
  1178. char* fileName = findData.cFileName;
  1179. #endif
  1180. // skip excluded directories
  1181. if( Platform::isExcludedDirectory( fileName ) )
  1182. continue;
  1183. if( ( subPath && ( dStrncmp( subPath, "", 1 ) != 0 ) ))
  1184. {
  1185. if( subTrail == '/' )
  1186. dSprintf(search, search.size, "%s%s", subPath, fileName);
  1187. else
  1188. dSprintf(search, search.size, "%s/%s", subPath, fileName);
  1189. char* child = search;
  1190. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1191. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1192. }
  1193. else
  1194. {
  1195. char* child;
  1196. if( trail == '/' )
  1197. child = fileName;
  1198. else
  1199. {
  1200. dSprintf(search, search.size, "/%s", fileName);
  1201. child = search;
  1202. }
  1203. if( currentDepth < recurseDepth || recurseDepth == -1 )
  1204. recurseDumpDirectories(basePath, child, directoryVector, currentDepth+1, recurseDepth, noBasePath );
  1205. }
  1206. }
  1207. }
  1208. while(FindNextFile(handle, &findData));
  1209. FindClose(handle);
  1210. return true;
  1211. }
  1212. bool Platform::dumpDirectories( const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath )
  1213. {
  1214. bool retVal = recurseDumpDirectories( path, "", directoryVector, -1, depth, noBasePath );
  1215. clearExcludedDirectories();
  1216. return retVal;
  1217. }
  1218. //-----------------------------------------------------------------------------
  1219. StringTableEntry osGetTemporaryDirectory()
  1220. {
  1221. TCHAR buf[ 1024 ];
  1222. const U32 bufSize = sizeof( buf ) / sizeof( buf[ 0 ] );
  1223. DWORD len = GetTempPath( sizeof( buf ) / sizeof( buf[ 0 ] ), buf );
  1224. TempAlloc< TCHAR > temp;
  1225. TCHAR* buffer = buf;
  1226. if( len > bufSize - 1 )
  1227. {
  1228. temp = TempAlloc< TCHAR >( len + 1 );
  1229. buffer = temp;
  1230. GetTempPath( len + 1, buffer );
  1231. }
  1232. // Remove the trailing slash
  1233. buffer[len-1] = 0;
  1234. #ifdef UNICODE
  1235. TempAlloc< char > dirBuffer( len * 3 + 1 );
  1236. char* dir = dirBuffer;
  1237. convertUTF16toUTF8( buffer, dir, dirBuffer.size );
  1238. #else
  1239. char* dir = buf;
  1240. #endif
  1241. forwardslash(dir);
  1242. return StringTable->insert(dir);
  1243. }