winFileio.cpp 44 KB

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