AndroidFileio.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platform.h"
  23. #include "platformAndroid/platformAndroid.h"
  24. #include "platform/platformFileIO.h"
  25. #include "collection/vector.h"
  26. #include "string/stringTable.h"
  27. #include "string/stringUnit.h"
  28. #include "console/console.h"
  29. #include "debug/profiler.h"
  30. #include "io/resource/resourceManager.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <utime.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include <sys/time.h>
  40. #define MAX_MAC_PATH_LONG 2048
  41. //-----------------------------------------------------------------------------
  42. bool Platform::fileDelete(const char * name)
  43. {
  44. Con::warnf("Platform::FileDelete() - Not supported on android.");
  45. return false;
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool dFileTouch(const char *path)
  49. {
  50. Con::warnf("Platform::dFileTouch() - Not supported on android.");
  51. return true;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Constructors & Destructor
  55. //-----------------------------------------------------------------------------
  56. //-----------------------------------------------------------------------------
  57. // After construction, the currentStatus will be Closed and the capabilities
  58. // will be 0.
  59. //-----------------------------------------------------------------------------
  60. File::File()
  61. : currentStatus(Closed), capability(0)
  62. {
  63. buffer = NULL;
  64. size = 0;
  65. filePointer = 0;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // insert a copy constructor here... (currently disabled)
  69. //-----------------------------------------------------------------------------
  70. //-----------------------------------------------------------------------------
  71. // Destructor
  72. //-----------------------------------------------------------------------------
  73. File::~File()
  74. {
  75. if (buffer != NULL)
  76. close();
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Open a file in the mode specified by openMode (Read, Write, or ReadWrite).
  80. // Truncate the file if the mode is either Write or ReadWrite and truncate is
  81. // true.
  82. //
  83. // Sets capability appropriate to the openMode.
  84. // Returns the currentStatus of the file.
  85. //-----------------------------------------------------------------------------
  86. File::Status File::open(const char *filename, const AccessMode openMode)
  87. {
  88. if (dStrlen(filename) > MAX_MAC_PATH_LONG)
  89. Con::warnf("File::open: Filename length is pretty long...");
  90. // Close the file if it was already open...
  91. if (currentStatus != Closed || buffer != NULL)
  92. close();
  93. // create the appropriate type of file...
  94. switch (openMode)
  95. {
  96. case Read:
  97. filePointer = 0;
  98. buffer = (U8*)_AndroidLoadFile(filename, &size);
  99. if (buffer == NULL) {
  100. currentStatus = UnknownError;
  101. }
  102. break;
  103. case Write:
  104. AssertFatal(false, "File::open: Write not supported on Android");
  105. return currentStatus;
  106. case ReadWrite:
  107. AssertFatal(false, "File::open: ReadWrite not supported on Android");
  108. return currentStatus;
  109. case WriteAppend:
  110. AssertFatal(false, "File::open: WriteAppend not supported on Android");
  111. return currentStatus;
  112. default:
  113. AssertFatal(false, "File::open: bad access mode");
  114. }
  115. capability = FileRead;
  116. // must set the file status before setting the position.
  117. currentStatus = Ok;
  118. // success!
  119. return currentStatus;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Get the current position of the file pointer.
  123. //-----------------------------------------------------------------------------
  124. U32 File::getPosition() const
  125. {
  126. AssertFatal(currentStatus != Closed , "File::getPosition: file closed");
  127. AssertFatal(buffer != NULL, "File::getPosition: invalid file buffer");
  128. return filePointer;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Set the position of the file pointer.
  132. // Absolute and relative positioning is supported via the absolutePos
  133. // parameter.
  134. //
  135. // If positioning absolutely, position MUST be positive - an IOError results if
  136. // position is negative.
  137. // Position can be negative if positioning relatively, however positioning
  138. // before the start of the file is an IOError.
  139. //
  140. // Returns the currentStatus of the file.
  141. //-----------------------------------------------------------------------------
  142. File::Status File::setPosition(S32 position, bool absolutePos)
  143. {
  144. AssertFatal(Closed != currentStatus, "File::setPosition: file closed");
  145. AssertFatal(buffer != NULL, "File::setPosition: invalid file buffer");
  146. if (currentStatus != Ok && currentStatus != EOS )
  147. return currentStatus;
  148. U32 finalPos;
  149. if(absolutePos)
  150. {
  151. // absolute position
  152. AssertFatal(0 <= position, "File::setPosition: negative absolute position");
  153. // position beyond EOS is OK
  154. filePointer = position;
  155. finalPos = filePointer;
  156. }
  157. else
  158. {
  159. // relative position
  160. AssertFatal((getPosition() + position) >= 0, "File::setPosition: negative relative position");
  161. // position beyond EOS is OK
  162. filePointer += position;
  163. finalPos = filePointer;
  164. }
  165. // ftell returns -1 on error. set error status
  166. if (0xffffffff == finalPos)
  167. return setStatus();
  168. // success, at end of file
  169. else if (finalPos >= getSize())
  170. return currentStatus = EOS;
  171. // success!
  172. else
  173. return currentStatus = Ok;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Get the size of the file in bytes.
  177. // It is an error to query the file size for a Closed file, or for one with an
  178. // error status.
  179. //-----------------------------------------------------------------------------
  180. U32 File::getSize() const
  181. {
  182. AssertWarn(Closed != currentStatus, "File::getSize: file closed");
  183. AssertFatal(buffer != NULL, "File::getSize: invalid file buffer");
  184. if (Ok == currentStatus || EOS == currentStatus)
  185. {
  186. return size;
  187. }
  188. return 0;
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Flush the file.
  192. // It is an error to flush a read-only file.
  193. // Returns the currentStatus of the file.
  194. //-----------------------------------------------------------------------------
  195. File::Status File::flush()
  196. {
  197. AssertFatal(Closed != currentStatus, "File::flush: file closed");
  198. AssertFatal(buffer != NULL, "File::flush: invalid file buffer");
  199. AssertFatal(true == hasCapability(FileWrite), "File::flush: cannot flush a read-only file");
  200. return setStatus();
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Close the File.
  204. //
  205. // Returns the currentStatus
  206. //-----------------------------------------------------------------------------
  207. File::Status File::close()
  208. {
  209. // check if it's already closed...
  210. if (Closed == currentStatus)
  211. return currentStatus;
  212. // it's not, so close it...
  213. if (buffer != NULL)
  214. {
  215. delete[] buffer;
  216. buffer = NULL;
  217. size = 0;
  218. filePointer = 0;
  219. }
  220. return currentStatus = Closed;
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Self-explanatory.
  224. //-----------------------------------------------------------------------------
  225. File::Status File::getStatus() const
  226. {
  227. return currentStatus;
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Sets and returns the currentStatus when an error has been encountered.
  231. //-----------------------------------------------------------------------------
  232. File::Status File::setStatus()
  233. {
  234. currentStatus = UnknownError;
  235. return currentStatus;
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Sets and returns the currentStatus to status.
  239. //-----------------------------------------------------------------------------
  240. File::Status File::setStatus(File::Status status)
  241. {
  242. return currentStatus = status;
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Read from a file.
  246. // The number of bytes to read is passed in size, the data is returned in src.
  247. // The number of bytes read is available in bytesRead if a non-Null pointer is
  248. // provided.
  249. //-----------------------------------------------------------------------------
  250. File::Status File::read(U32 _size, char *dst, U32 *bytesRead)
  251. {
  252. AssertFatal(Closed != currentStatus, "File::read: file closed");
  253. AssertFatal(buffer != NULL, "File::read: invalid file buffer");
  254. AssertFatal(NULL != dst, "File::read: NULL destination pointer");
  255. AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability");
  256. AssertWarn(0 != size, "File::read: size of zero");
  257. if (Ok != currentStatus || 0 == size)
  258. return currentStatus;
  259. // read from stream
  260. U32 nBytes = 0;
  261. if ((size-filePointer) > (_size * *bytesRead))
  262. {
  263. memcpy(dst, buffer+filePointer, _size * *bytesRead);
  264. nBytes = *bytesRead;
  265. }
  266. else if (size-filePointer <= 0)
  267. {
  268. nBytes = 0;
  269. }
  270. else
  271. {
  272. memcpy(dst, buffer+filePointer, size-filePointer);
  273. nBytes = *bytesRead;
  274. }
  275. // did we hit the end of the stream?
  276. if( nBytes != size)
  277. currentStatus = EOS;
  278. // if bytesRead is a valid pointer, send number of bytes read there.
  279. if(bytesRead)
  280. *bytesRead = nBytes;
  281. // successfully read size bytes
  282. return currentStatus;
  283. }
  284. //-----------------------------------------------------------------------------
  285. // Write to a file.
  286. // The number of bytes to write is passed in size, the data is passed in src.
  287. // The number of bytes written is available in bytesWritten if a non-Null
  288. // pointer is provided.
  289. //-----------------------------------------------------------------------------
  290. File::Status File::write(U32 size, const char *src, U32 *bytesWritten)
  291. {
  292. AssertFatal(0, "File::write: Not supported on Android.");
  293. return setStatus();
  294. }
  295. //-----------------------------------------------------------------------------
  296. // Self-explanatory.
  297. //-----------------------------------------------------------------------------
  298. bool File::hasCapability(Capability cap) const
  299. {
  300. return (0 != (U32(cap) & capability));
  301. }
  302. //-----------------------------------------------------------------------------
  303. S32 Platform::compareFileTimes(const FileTime &a, const FileTime &b)
  304. {
  305. if(a > b)
  306. return 1;
  307. if(a < b)
  308. return -1;
  309. return 0;
  310. }
  311. //-----------------------------------------------------------------------------
  312. // either time param COULD be null.
  313. //-----------------------------------------------------------------------------
  314. bool Platform::getFileTimes(const char *path, FileTime *createTime, FileTime *modifyTime)
  315. {
  316. // MacOSX is NOT guaranteed to be running off a HFS volume,
  317. // and UNIX does not keep a record of a file's creation time anywhere.
  318. // So instead of creation time we return changed time,
  319. // just like the Linux platform impl does.
  320. if (!path || !*path)
  321. return false;
  322. Con::warnf("Platform::getFileTimes - Not supported on android.");
  323. return true;
  324. }
  325. //-----------------------------------------------------------------------------
  326. bool Platform::createPath(const char *file)
  327. {
  328. Con::warnf("Platform::createPath() - Not supported on android.");
  329. return false;
  330. }
  331. #pragma mark ---- Directories ----
  332. //-----------------------------------------------------------------------------
  333. StringTableEntry Platform::getCurrentDirectory()
  334. {
  335. // get the current directory, the one that would be opened if we did a fopen(".")
  336. char* cwd = getcwd(NULL, 0);
  337. StringTableEntry ret = StringTable->insert(cwd);
  338. free(cwd);
  339. return ret;
  340. }
  341. //-----------------------------------------------------------------------------
  342. bool Platform::setCurrentDirectory(StringTableEntry newDir)
  343. {
  344. return (chdir(newDir) == 0);
  345. }
  346. //-----------------------------------------------------------------------------
  347. void Platform::openFolder(const char* path )
  348. {
  349. // TODO: users can still run applications by calling openfolder on an app bundle.
  350. // this may be a bad thing.
  351. if(!Platform::isDirectory(path))
  352. {
  353. Con::errorf(avar("Error: not a directory: %s",path));
  354. return;
  355. }
  356. const char* arg = avar("open '%s'", path);
  357. U32 ret = system(arg);
  358. if(ret != 0)
  359. Con::printf(strerror(errno));
  360. }
  361. static bool isMainDotCsPresent(char *dir)
  362. {
  363. char maincsbuf[MAX_MAC_PATH_LONG];
  364. const char *maincsname = "/main.cs";
  365. const U32 len = dStrlen(dir) + dStrlen(maincsname)+1;
  366. AssertISV(len < MAX_MAC_PATH_LONG, "Sorry, path is too long, I can't run from this folder.");
  367. dSprintf(maincsbuf,MAX_MAC_PATH_LONG,"%s%s", dir, maincsname);
  368. return Platform::isFile(maincsbuf);
  369. }
  370. //-----------------------------------------------------------------------------
  371. /// Finds and sets the current working directory.
  372. /// Torque tries to automatically detect whether you have placed the game files
  373. /// inside or outside the application's bundle. It checks for the presence of
  374. /// the file 'main.cs'. If it finds it, Torque will assume that the other game
  375. /// files are there too. If Torque does not see 'main.cs' inside its bundle, it
  376. /// will assume the files are outside the bundle.
  377. /// Since you probably don't want to copy the game files into the app every time
  378. /// you build, you will want to leave them outside the bundle for development.
  379. ///
  380. /// Android reads all assets out of compressed bundle so we dont realy have an executable path
  381. StringTableEntry Platform::getExecutablePath()
  382. {
  383. if(platState.mainDotCsDir)
  384. return platState.mainDotCsDir;
  385. char* ret = NULL;
  386. if(StringTable)
  387. platState.mainDotCsDir = StringTable->insert(".");
  388. else
  389. ret = dStrdup(".");
  390. return ret ? ret : platState.mainDotCsDir;
  391. }
  392. //-----------------------------------------------------------------------------
  393. StringTableEntry Platform::getExecutableName()
  394. {
  395. //TODO: does this need anything further?
  396. return StringTable->insert("Torque2D");
  397. }
  398. //-----------------------------------------------------------------------------
  399. bool Platform::isFile(const char *path)
  400. {
  401. if (!path || !*path)
  402. return false;
  403. return android_IsFile(path);
  404. }
  405. //-----------------------------------------------------------------------------
  406. bool Platform::isDirectory(const char *path)
  407. {
  408. if (!path || !*path)
  409. return false;
  410. return android_IsDir(path);
  411. }
  412. S32 Platform::getFileSize(const char* pFilePath)
  413. {
  414. if (!pFilePath || !*pFilePath)
  415. return 0;
  416. return android_GetFileSize(pFilePath);
  417. }
  418. //-----------------------------------------------------------------------------
  419. bool Platform::isSubDirectory(const char *pathParent, const char *pathSub)
  420. {
  421. char fullpath[MAX_MAC_PATH_LONG];
  422. dStrcpyl(fullpath, MAX_MAC_PATH_LONG, pathParent, "/", pathSub, NULL);
  423. return isDirectory((const char *)fullpath);
  424. }
  425. void getDirectoryName(const char* path, char* name)
  426. {
  427. int cnt = StringUnit::getUnitCount(path, "/");
  428. strcpy(name,StringUnit::getUnit(path, cnt-1, "/"));
  429. }
  430. //-----------------------------------------------------------------------------
  431. // utility for platform::hasSubDirectory() and platform::dumpDirectories()
  432. // ensures that the entry is a directory, and isnt on the ignore lists.
  433. inline bool isGoodDirectory(const char* path)
  434. {
  435. char name[80];
  436. getDirectoryName(path, name);
  437. return (Platform::isDirectory(path) // is a dir
  438. && dStrcmp(name,".") != 0 // not here
  439. && dStrcmp(name,"..") != 0 // not parent
  440. && !Platform::isExcludedDirectory(name)); // not excluded
  441. }
  442. //-----------------------------------------------------------------------------
  443. bool Platform::hasSubDirectory(const char *path)
  444. {
  445. android_InitDirList(path);
  446. char dir[80];
  447. char pdir[255];
  448. strcpy(dir,"");
  449. android_GetNextDir(path, dir);
  450. while(strcmp(dir,"") != 0)
  451. {
  452. sprintf(pdir, "%s/%s", path, dir);
  453. if (isGoodDirectory(pdir))
  454. return true;
  455. android_GetNextDir(path, dir);
  456. }
  457. return false;
  458. }
  459. //-----------------------------------------------------------------------------
  460. bool recurseDumpDirectories(const char *basePath, const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath)
  461. {
  462. const U32 len = dStrlen(basePath) + dStrlen(path) + 2;
  463. char pathbuf[len];
  464. // construct the file path
  465. dSprintf(pathbuf, len, "%s/%s", basePath, path);
  466. // be sure it opens.
  467. android_InitDirList(pathbuf);
  468. // look inside the current directory
  469. char dir[80];
  470. char pdir[255];
  471. strcpy(dir,"");
  472. android_GetNextDir(pathbuf, dir);
  473. while(strcmp(dir,"") != 0)
  474. {
  475. sprintf(pdir, "%s/%s", pathbuf, dir);
  476. if (!isGoodDirectory(pdir))
  477. return false;
  478. // construct the new path string, we'll need this below.
  479. const U32 newpathlen = dStrlen(path) + dStrlen(dir) + 2;
  480. char newpath[newpathlen];
  481. if(dStrlen(path) > 0)
  482. {
  483. dSprintf(newpath, newpathlen, "%s/%s", path, dir);
  484. }
  485. else
  486. {
  487. dSprintf(newpath, newpathlen, "%s", dir);
  488. }
  489. // we have a directory, add it to the list.
  490. if( noBasePath )
  491. {
  492. directoryVector.push_back(StringTable->insert(newpath));
  493. }
  494. else
  495. {
  496. const U32 fullpathlen = dStrlen(basePath) + dStrlen(newpath) + 2;
  497. char fullpath[fullpathlen];
  498. dSprintf(fullpath, fullpathlen, "%s/%s",basePath,newpath);
  499. directoryVector.push_back(StringTable->insert(fullpath));
  500. }
  501. // and recurse into it, unless we've run out of depth
  502. if( depth != 0) // passing a val of -1 as the recurse depth means go forever
  503. recurseDumpDirectories(basePath, newpath, directoryVector, depth-1, noBasePath);
  504. android_GetNextDir(pathbuf, dir);
  505. }
  506. return true;
  507. }
  508. //-----------------------------------------------------------------------------
  509. bool Platform::dumpDirectories(const char *path, Vector<StringTableEntry> &directoryVector, S32 depth, bool noBasePath)
  510. {
  511. PROFILE_START(dumpDirectories);
  512. ResourceManager->initExcludedDirectories();
  513. const S32 len = dStrlen(path)+1;
  514. char newpath[len];
  515. dSprintf(newpath, len, "%s", path);
  516. if(newpath[len - 1] == '/')
  517. newpath[len - 1] = '\0'; // cut off the trailing slash, if there is one
  518. // Insert base path to follow what Windows does.
  519. if ( !noBasePath )
  520. directoryVector.push_back(StringTable->insert(newpath));
  521. bool ret = recurseDumpDirectories(newpath, "", directoryVector, depth, noBasePath);
  522. PROFILE_END();
  523. return ret;
  524. }
  525. //-----------------------------------------------------------------------------
  526. static bool recurseDumpPath(const char* curPath, Vector<Platform::FileInfo>& fileVector, U32 depth)
  527. {
  528. android_InitDirList(curPath);
  529. // look inside the current directory
  530. char dir[80];
  531. char file[80];
  532. strcpy(dir,"");
  533. strcpy(file,"");
  534. android_GetNextDir(curPath, dir);
  535. android_GetNextFile(curPath, file);
  536. while(strcmp(file,"") != 0)
  537. {
  538. // construct the full file path. we need this to get the file size and to recurse
  539. const U32 len = dStrlen(curPath) + dStrlen(file) + 2;
  540. char pathbuf[len];
  541. dSprintf( pathbuf, len, "%s/%s", curPath, file);
  542. //add the file entry to the list
  543. // unlike recurseDumpDirectories(), we need to return more complex info here.
  544. //<Mat> commented this out in case we ever want a dir file printout again
  545. //printf( "File Name: %s ", entry->d_name );
  546. const U32 fileSize = Platform::getFileSize(pathbuf);
  547. fileVector.increment();
  548. Platform::FileInfo& rInfo = fileVector.last();
  549. rInfo.pFullPath = StringTable->insert(curPath);
  550. rInfo.pFileName = StringTable->insert(file);
  551. rInfo.fileSize = fileSize;
  552. android_GetNextFile(curPath, file);
  553. }
  554. while(strcmp(dir,"") != 0)
  555. {
  556. // construct the full file path. we need this to get the file size and to recurse
  557. const U32 len = dStrlen(curPath) + dStrlen(dir) + 2;
  558. char pathbuf[len];
  559. if (strcmp(curPath,"") == 0)
  560. dSprintf( pathbuf, len, "%s", dir);
  561. else
  562. dSprintf( pathbuf, len, "%s/%s", curPath, dir);
  563. if( depth == 0)
  564. {
  565. android_GetNextDir(curPath, dir);
  566. continue;
  567. }
  568. // filter out dirs we dont want.
  569. if( !isGoodDirectory(pathbuf) )
  570. {
  571. android_GetNextDir(curPath, dir);
  572. continue;
  573. }
  574. // recurse into the dir
  575. recurseDumpPath( pathbuf, fileVector, depth-1);
  576. android_GetNextDir(curPath, dir);
  577. }
  578. return true;
  579. }
  580. //-----------------------------------------------------------------------------
  581. bool Platform::dumpPath(const char *path, Vector<Platform::FileInfo>& fileVector, S32 depth)
  582. {
  583. PROFILE_START(dumpPath);
  584. const S32 len = dStrlen(path) + 1;
  585. char newpath[len];
  586. dSprintf(newpath, len, "%s", path);
  587. if(newpath[len - 2] == '/')
  588. newpath[len - 2] = '\0'; // cut off the trailing slash, if there is one
  589. bool ret = recurseDumpPath( newpath, fileVector, depth);
  590. PROFILE_END();
  591. return ret;
  592. }
  593. //-----------------------------------------------------------------------------
  594. #if defined(TORQUE_DEBUG)
  595. ConsoleFunction(testHasSubdir,void,2,2,"tests platform::hasSubDirectory") {
  596. Con::printf("testing %s",argv[1]);
  597. Platform::addExcludedDirectory(".svn");
  598. if(Platform::hasSubDirectory(argv[1]))
  599. Con::printf(" has subdir");
  600. else
  601. Con::printf(" does not have subdir");
  602. }
  603. ConsoleFunction(testDumpDirectories,void,4,4,"testDumpDirectories('path', int depth, bool noBasePath)") {
  604. Vector<StringTableEntry> paths;
  605. S32 depth = dAtoi(argv[2]);
  606. Platform::addExcludedDirectory(".svn");
  607. Platform::dumpDirectories(argv[1],paths,dAtoi(argv[2]),dAtob(argv[3]));
  608. Con::printf("Dumping directories starting from %s with depth %i", argv[1],depth);
  609. for(Vector<StringTableEntry>::iterator itr = paths.begin(); itr != paths.end(); itr++) {
  610. Con::printf(*itr);
  611. }
  612. }
  613. ConsoleFunction(testDumpPaths, void, 3, 3, "testDumpPaths('path', int depth)")
  614. {
  615. Vector<Platform::FileInfo> files;
  616. S32 depth = dAtoi(argv[2]);
  617. Platform::addExcludedDirectory(".svn");
  618. Platform::dumpPath(argv[1], files, depth);
  619. for(Vector<Platform::FileInfo>::iterator itr = files.begin(); itr != files.end(); itr++) {
  620. Con::printf("%s/%s",itr->pFullPath, itr->pFileName);
  621. }
  622. }
  623. //-----------------------------------------------------------------------------
  624. ConsoleFunction(testFileTouch, bool , 2,2, "testFileTouch('path')")
  625. {
  626. return dFileTouch(argv[1]);
  627. }
  628. ConsoleFunction(testGetFileTimes, bool, 2,2, "testGetFileTimes('path')")
  629. {
  630. FileTime create, modify;
  631. bool ok;
  632. ok = Platform::getFileTimes(argv[1],&create, &modify);
  633. Con::printf("%s Platform::getFileTimes %i, %i", ok ? "+OK" : "-FAIL", create, modify);
  634. return ok;
  635. }
  636. #endif