File.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright (C) 2014, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "anki/util/File.h"
  6. #include "anki/util/Exception.h"
  7. #include "anki/util/Assert.h"
  8. #include <fstream>
  9. #include <cstring>
  10. #include <cstdarg>
  11. #include <contrib/minizip/unzip.h>
  12. #if ANKI_OS == ANKI_OS_ANDROID
  13. # include <android_native_app_glue.h>
  14. # include <android/asset_manager.h>
  15. #endif
  16. #if ANKI_POSIX
  17. # include <sys/stat.h>
  18. # include <sys/types.h>
  19. # include <dirent.h>
  20. # include <cerrno>
  21. #endif
  22. namespace anki {
  23. //==============================================================================
  24. // File =
  25. //==============================================================================
  26. #if ANKI_OS == ANKI_OS_ANDROID
  27. extern android_app* gAndroidApp;
  28. #endif
  29. //==============================================================================
  30. File::~File()
  31. {
  32. close();
  33. }
  34. //==============================================================================
  35. void File::open(const char* filename, OpenFlag flags)
  36. {
  37. ANKI_ASSERT(filename);
  38. ANKI_ASSERT(strlen(filename) > 0);
  39. ANKI_ASSERT(m_file == nullptr && m_flags == OpenFlag::NONE
  40. && m_type == Type::NONE);
  41. // Only these flags are accepted
  42. ANKI_ASSERT((flags & (OpenFlag::READ | OpenFlag::WRITE | OpenFlag::APPEND
  43. | OpenFlag::BINARY | OpenFlag::LITTLE_ENDIAN
  44. | OpenFlag::BIG_ENDIAN)) != OpenFlag::NONE);
  45. // Cannot be both
  46. ANKI_ASSERT((flags & OpenFlag::READ) != (flags & OpenFlag::WRITE));
  47. //
  48. // Determine the file type and open it
  49. //
  50. String archive, filenameInArchive;
  51. Type ft = identifyFile(filename, archive, filenameInArchive);
  52. switch(ft)
  53. {
  54. #if ANKI_OS == ANKI_OS_ANDROID
  55. case Type::SPECIAL:
  56. openAndroidFile(filename, flags);
  57. break;
  58. #endif
  59. case Type::C:
  60. openCFile(filename, flags);
  61. break;
  62. case Type::ZIP:
  63. openZipFile(archive.c_str(), filenameInArchive.c_str(), flags);
  64. break;
  65. default:
  66. ANKI_ASSERT(0);
  67. }
  68. //
  69. // Set endianess
  70. //
  71. // If the open() DIDN'T provided us the file endianess
  72. if((flags & (OpenFlag::BIG_ENDIAN | OpenFlag::LITTLE_ENDIAN))
  73. == OpenFlag::NONE)
  74. {
  75. // Set the machine's endianness
  76. m_flags = m_flags | getMachineEndianness();
  77. }
  78. else
  79. {
  80. // Else just make sure that only one of the flags is set
  81. ANKI_ASSERT((flags & OpenFlag::BIG_ENDIAN)
  82. != (flags & OpenFlag::LITTLE_ENDIAN));
  83. }
  84. }
  85. //==============================================================================
  86. void File::openCFile(const char* filename, OpenFlag flags)
  87. {
  88. const char* openMode;
  89. if((flags & OpenFlag::READ) != OpenFlag::NONE)
  90. {
  91. openMode = "r";
  92. }
  93. else if((flags & OpenFlag::APPEND) == OpenFlag::APPEND)
  94. {
  95. openMode = "a+";
  96. }
  97. else if((flags & OpenFlag::WRITE) != OpenFlag::NONE)
  98. {
  99. openMode = "w";
  100. }
  101. else
  102. {
  103. ANKI_ASSERT(0);
  104. openMode = nullptr;
  105. }
  106. // Open
  107. m_file = (FILE*)fopen(filename, openMode);
  108. if(m_file == nullptr)
  109. {
  110. throw ANKI_EXCEPTION("Failed to open file: %s", filename);
  111. }
  112. m_flags = flags;
  113. m_type = Type::C;
  114. }
  115. //==============================================================================
  116. void File::openZipFile(
  117. const char* archive, const char* archived, OpenFlag flags)
  118. {
  119. if((flags & OpenFlag::WRITE) != OpenFlag::NONE)
  120. {
  121. throw ANKI_EXCEPTION("Cannot write inside archives");
  122. }
  123. if((flags & OpenFlag::READ) == OpenFlag::NONE)
  124. {
  125. throw ANKI_EXCEPTION("Missing OpenFlag::READ flag");
  126. }
  127. // Open archive
  128. unzFile zfile = unzOpen(archive);
  129. if(zfile == nullptr)
  130. {
  131. throw ANKI_EXCEPTION("Failed to open archive: %s", archive);
  132. }
  133. // Locate archived
  134. const int caseSensitive = 1;
  135. if(unzLocateFile(zfile, archived, caseSensitive) != UNZ_OK)
  136. {
  137. unzClose(zfile);
  138. throw ANKI_EXCEPTION("Failed to locate file in archive: %s", archived);
  139. }
  140. // Open file
  141. if(unzOpenCurrentFile(zfile) != UNZ_OK)
  142. {
  143. unzClose(zfile);
  144. throw ANKI_EXCEPTION("unzOpenCurrentFile failed: %s", archived);
  145. }
  146. static_assert(sizeof(m_file) == sizeof(zfile), "See file");
  147. m_file = (void*)zfile;
  148. m_flags = flags;
  149. m_type = Type::ZIP;
  150. // Get size just in case
  151. unz_file_info zinfo;
  152. zinfo.uncompressed_size = 0;
  153. unzGetCurrentFileInfo(zfile, &zinfo, nullptr, 0, nullptr, 0, nullptr, 0);
  154. m_size = zinfo.uncompressed_size;
  155. ANKI_ASSERT(m_size != 0);
  156. }
  157. //==============================================================================
  158. #if ANKI_OS == ANKI_OS_ANDROID
  159. void File::openAndroidFile(const char* filename, OpenFlag flags)
  160. {
  161. if((flags & OpenFlag::WRITE) != OpenFlag::NONE)
  162. {
  163. throw ANKI_EXCEPTION("Cannot write inside archives");
  164. }
  165. if((flags & OpenFlag::READ) != OpenFlag::NONE)
  166. {
  167. throw ANKI_EXCEPTION("Missing OpenFlag::READ flag");
  168. }
  169. // Open file
  170. ANKI_ASSERT(gAndroidApp != nullptr && gAndroidApp->activity
  171. && gAndroidApp->activity->assetManager);
  172. m_file = AAssetManager_open(
  173. gAndroidApp->activity->assetManager,
  174. filename + 1,
  175. AASSET_MODE_STREAMING);
  176. if(m_file == nullptr)
  177. {
  178. throw ANKI_EXCEPTION("Failed to open file: %s", filename);
  179. }
  180. m_flags = flags;
  181. m_type = Type::SPECIAL;
  182. }
  183. #endif
  184. //==============================================================================
  185. void File::close()
  186. {
  187. if(m_file)
  188. {
  189. if(m_type == Type::C)
  190. {
  191. fclose((FILE*)m_file);
  192. }
  193. else if(m_type == Type::ZIP)
  194. {
  195. unzClose(m_file);
  196. }
  197. #if ANKI_OS == ANKI_OS_ANDROID
  198. else if(m_type == Type::SPECIAL)
  199. {
  200. AAsset_close((AAsset*)m_file);
  201. }
  202. #endif
  203. else
  204. {
  205. ANKI_ASSERT(0);
  206. }
  207. }
  208. m_file = nullptr;
  209. m_flags = OpenFlag::NONE;
  210. m_type = Type::NONE;
  211. }
  212. //==============================================================================
  213. void File::flush()
  214. {
  215. ANKI_ASSERT(m_file);
  216. if((m_flags & OpenFlag::WRITE) != OpenFlag::NONE)
  217. {
  218. if(m_type == Type::C)
  219. {
  220. I err = fflush((FILE*)m_file);
  221. if(err)
  222. {
  223. throw ANKI_EXCEPTION("fflush() failed");
  224. }
  225. }
  226. else if(m_type == Type::ZIP
  227. #if ANKI_OS == ANKI_OS_ANDROID
  228. || m_type == Type::SPECIAL
  229. #endif
  230. )
  231. {
  232. ANKI_ASSERT(0 && "Cannot have write these file types");
  233. }
  234. else
  235. {
  236. ANKI_ASSERT(0);
  237. }
  238. }
  239. }
  240. //==============================================================================
  241. void File::read(void* buff, PtrSize size)
  242. {
  243. ANKI_ASSERT(buff);
  244. ANKI_ASSERT(size > 0);
  245. ANKI_ASSERT(m_file);
  246. ANKI_ASSERT((m_flags & OpenFlag::READ) != OpenFlag::NONE);
  247. I64 readSize = 0;
  248. if(m_type == Type::C)
  249. {
  250. readSize = fread(buff, 1, size, (FILE*)m_file);
  251. }
  252. else if(m_type == Type::ZIP)
  253. {
  254. readSize = unzReadCurrentFile(m_file, buff, size);
  255. }
  256. #if ANKI_OS == ANKI_OS_ANDROID
  257. else if(m_type == Type::SPECIAL)
  258. {
  259. readSize = AAsset_read((AAsset*)m_file, buff, size);
  260. }
  261. #endif
  262. else
  263. {
  264. ANKI_ASSERT(0);
  265. }
  266. if((I64)size != readSize)
  267. {
  268. throw ANKI_EXCEPTION("File read failed");
  269. }
  270. }
  271. //==============================================================================
  272. PtrSize File::getSize()
  273. {
  274. ANKI_ASSERT(m_file);
  275. ANKI_ASSERT(m_flags != OpenFlag::NONE);
  276. PtrSize out;
  277. if(m_type == Type::C)
  278. {
  279. // Get file size
  280. fseek((FILE*)m_file, 0, SEEK_END);
  281. I64 size = ftell((FILE*)m_file);
  282. if(size < 1)
  283. {
  284. throw ANKI_EXCEPTION("ftell() failed");
  285. }
  286. rewind((FILE*)m_file);
  287. out = size;
  288. }
  289. else if(m_type == Type::ZIP)
  290. {
  291. ANKI_ASSERT(m_size != 0);
  292. out = m_size;
  293. }
  294. #if ANKI_OS == ANKI_OS_ANDROID
  295. else if(m_type == Type::SPECIAL)
  296. {
  297. out = AAsset_getLength((AAsset*)m_file);
  298. }
  299. #endif
  300. else
  301. {
  302. ANKI_ASSERT(0);
  303. }
  304. ANKI_ASSERT(out != 0);
  305. return out;
  306. }
  307. //==============================================================================
  308. U32 File::readU32()
  309. {
  310. ANKI_ASSERT(m_file);
  311. ANKI_ASSERT(m_flags != OpenFlag::NONE);
  312. ANKI_ASSERT((m_flags & OpenFlag::READ) != OpenFlag::NONE);
  313. ANKI_ASSERT((m_flags & OpenFlag::BINARY) != OpenFlag::NONE
  314. && "Should be binary file");
  315. ANKI_ASSERT(
  316. (m_flags & OpenFlag::BIG_ENDIAN) != (m_flags & OpenFlag::LITTLE_ENDIAN)
  317. && "One of those 2 should be active");
  318. U32 out;
  319. read(&out, sizeof(out));
  320. // Copy it
  321. OpenFlag machineEndianness = getMachineEndianness();
  322. OpenFlag fileEndianness =
  323. ((m_flags & OpenFlag::BIG_ENDIAN) != OpenFlag::NONE)
  324. ? OpenFlag::BIG_ENDIAN
  325. : OpenFlag::LITTLE_ENDIAN;
  326. if(machineEndianness == fileEndianness)
  327. {
  328. // Same endianness between the file and the machine. Do nothing
  329. }
  330. else if(machineEndianness == OpenFlag::BIG_ENDIAN
  331. && fileEndianness == OpenFlag::LITTLE_ENDIAN)
  332. {
  333. U16* c = (U16*)&out;
  334. out = (c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
  335. }
  336. else
  337. {
  338. U16* c = (U16*)&out;
  339. out = ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]);
  340. }
  341. return out;
  342. }
  343. //==============================================================================
  344. F32 File::readF32()
  345. {
  346. F32 out;
  347. U32 integer = readU32();
  348. memcpy(&out, &integer, sizeof(F32));
  349. return out;
  350. }
  351. //==============================================================================
  352. void File::write(void* buff, PtrSize size)
  353. {
  354. ANKI_ASSERT(buff);
  355. ANKI_ASSERT(size > 0);
  356. ANKI_ASSERT(m_file);
  357. ANKI_ASSERT((m_flags & OpenFlag::WRITE) != OpenFlag::NONE);
  358. if(m_type == Type::C)
  359. {
  360. PtrSize writeSize = 0;
  361. writeSize = fwrite(buff, 1, size, (FILE*)m_file);
  362. if(writeSize != size)
  363. {
  364. throw ANKI_EXCEPTION("Failed to write on file");
  365. }
  366. }
  367. else if(m_type == Type::ZIP
  368. #if ANKI_OS == ANKI_OS_ANDROID
  369. || m_type == Type::SPECIAL
  370. #endif
  371. )
  372. {
  373. throw ANKI_EXCEPTION("Writting to archives is not supported");
  374. }
  375. else
  376. {
  377. ANKI_ASSERT(0);
  378. }
  379. }
  380. //==============================================================================
  381. void File::writeText(const char* format, ...)
  382. {
  383. ANKI_ASSERT(format);
  384. ANKI_ASSERT(m_file);
  385. ANKI_ASSERT(m_flags != OpenFlag::NONE);
  386. ANKI_ASSERT((m_flags & OpenFlag::WRITE) != OpenFlag::NONE);
  387. ANKI_ASSERT((m_flags & OpenFlag::BINARY) == OpenFlag::NONE);
  388. va_list args;
  389. va_start(args, format);
  390. if(m_type == Type::C)
  391. {
  392. vfprintf((FILE*)m_file, format, args);
  393. }
  394. else if(m_type == Type::ZIP
  395. #if ANKI_OS == ANKI_OS_ANDROID
  396. || m_type == Type::SPECIAL
  397. #endif
  398. )
  399. {
  400. throw ANKI_EXCEPTION("Writting to archives is not supported");
  401. }
  402. else
  403. {
  404. ANKI_ASSERT(0);
  405. }
  406. va_end(args);
  407. }
  408. //==============================================================================
  409. void File::seek(PtrSize offset, SeekOrigin origin)
  410. {
  411. ANKI_ASSERT(m_file);
  412. ANKI_ASSERT(m_flags != OpenFlag::NONE);
  413. if(m_type == Type::C)
  414. {
  415. if(fseek((FILE*)m_file, offset, (I)origin) != 0)
  416. {
  417. throw ANKI_EXCEPTION("fseek() failed");
  418. }
  419. }
  420. else if(m_type == Type::ZIP)
  421. {
  422. // Rewind if needed
  423. if(origin == SeekOrigin::BEGINNING)
  424. {
  425. if(unzCloseCurrentFile(m_file)
  426. || unzOpenCurrentFile(m_file))
  427. {
  428. throw ANKI_EXCEPTION("Rewind failed");
  429. }
  430. }
  431. // Move forward by reading dummy data
  432. char buff[256];
  433. while(offset != 0)
  434. {
  435. PtrSize toRead = std::min(offset, sizeof(buff));
  436. read(buff, toRead);
  437. offset -= toRead;
  438. }
  439. }
  440. #if ANKI_OS == ANKI_OS_ANDROID
  441. else if(m_type == Type::SPECIAL)
  442. {
  443. if(AAsset_seek((AAsset*)file, offset, origin) == (off_t)-1)
  444. {
  445. throw ANKI_EXCEPTION("AAsset_seek() failed");
  446. }
  447. }
  448. #endif
  449. else
  450. {
  451. ANKI_ASSERT(0);
  452. }
  453. }
  454. //==============================================================================
  455. File::Type File::identifyFile(const char* filename,
  456. String& archive, String& filenameInArchive)
  457. {
  458. ANKI_ASSERT(filename);
  459. ANKI_ASSERT(strlen(filename) > 1);
  460. #if ANKI_OS == ANKI_OS_ANDROID
  461. if(filename[0] == '$')
  462. {
  463. return Type::SPECIAL;
  464. }
  465. else
  466. #endif
  467. {
  468. static const char aext[] = {".ankizip"};
  469. const char* ptrToArchiveExt = strstr(filename, aext);
  470. if(ptrToArchiveExt == nullptr)
  471. {
  472. // It's a C file
  473. return Type::C;
  474. }
  475. else
  476. {
  477. // Maybe it's a file in a zipped archive
  478. PtrSize fnameLen = strlen(filename);
  479. const PtrSize extLen = sizeof(aext) - 1;
  480. PtrSize archLen = (PtrSize)(ptrToArchiveExt - filename) + extLen;
  481. if(archLen + 1 >= fnameLen)
  482. {
  483. throw ANKI_EXCEPTION("Too sort archived filename");
  484. }
  485. archive = String(filename, archLen);
  486. if(directoryExists(archive.c_str()))
  487. {
  488. // It's a directory so failback to C file
  489. return Type::C;
  490. }
  491. else
  492. {
  493. // It's a ziped file
  494. filenameInArchive = String(
  495. filename + archLen + 1, fnameLen - archLen);
  496. return Type::ZIP;
  497. }
  498. }
  499. }
  500. ANKI_ASSERT(0);
  501. return Type::C;
  502. }
  503. //==============================================================================
  504. File::OpenFlag File::getMachineEndianness()
  505. {
  506. I32 num = 1;
  507. if(*(char*)&num == 1)
  508. {
  509. return OpenFlag::LITTLE_ENDIAN;
  510. }
  511. else
  512. {
  513. return OpenFlag::BIG_ENDIAN;
  514. }
  515. }
  516. //==============================================================================
  517. const char* File::getFileExtension(const char* filename)
  518. {
  519. ANKI_ASSERT(filename);
  520. const char* pc = strrchr(filename, '.');
  521. if(pc == nullptr)
  522. {
  523. return nullptr;
  524. }
  525. ++pc;
  526. return (*pc == '\0') ? nullptr : pc;
  527. }
  528. } // end namespace anki