File.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  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/Filesystem.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/Assert.h>
  9. #include <cstring>
  10. #include <cstdarg>
  11. #if ANKI_OS_ANDROID
  12. # include <android_native_app_glue.h>
  13. # include <android/asset_manager.h>
  14. #endif
  15. namespace anki
  16. {
  17. #if ANKI_OS_ANDROID
  18. extern android_app* g_androidApp;
  19. # define ANKI_AFILE static_cast<AAsset*>(m_file)
  20. #endif
  21. #define ANKI_CFILE reinterpret_cast<FILE*>(m_file)
  22. File& File::operator=(File&& b)
  23. {
  24. close();
  25. if(b.m_file != nullptr)
  26. {
  27. m_file = b.m_file;
  28. m_flags = b.m_flags;
  29. m_size = b.m_size;
  30. }
  31. b.zero();
  32. return *this;
  33. }
  34. File::~File()
  35. {
  36. close();
  37. }
  38. Error File::open(const CString& filename, FileOpenFlag flags)
  39. {
  40. ANKI_ASSERT(m_file == nullptr && m_flags == FileOpenFlag::NONE);
  41. // Only these flags are accepted
  42. ANKI_ASSERT((flags
  43. & (FileOpenFlag::READ | FileOpenFlag::WRITE | FileOpenFlag::APPEND | FileOpenFlag::BINARY
  44. | FileOpenFlag::ENDIAN_LITTLE | FileOpenFlag::ENDIAN_BIG | FileOpenFlag::SPECIAL))
  45. != FileOpenFlag::NONE);
  46. // Cannot be both
  47. ANKI_ASSERT((flags & FileOpenFlag::READ) != (flags & FileOpenFlag::WRITE));
  48. // Open it
  49. #if ANKI_OS_ANDROID
  50. if(!!(flags & FileOpenFlag::SPECIAL))
  51. {
  52. ANKI_CHECK(openAndroidFile(filename, flags));
  53. }
  54. else
  55. #endif
  56. {
  57. ANKI_CHECK(openCFile(filename, flags));
  58. }
  59. // Set endianess
  60. if((flags & (FileOpenFlag::ENDIAN_BIG | FileOpenFlag::ENDIAN_LITTLE)) == FileOpenFlag::NONE)
  61. {
  62. // If the open() DIDN'T provided us the file endianess Set the machine's endianness
  63. m_flags = m_flags | getMachineEndianness();
  64. }
  65. else
  66. {
  67. // Else just make sure that only one of the flags is set
  68. ANKI_ASSERT((flags & FileOpenFlag::ENDIAN_BIG) != (flags & FileOpenFlag::ENDIAN_LITTLE));
  69. }
  70. return Error::NONE;
  71. }
  72. Error File::openCFile(const CString& filename, FileOpenFlag flags)
  73. {
  74. Error err = Error::NONE;
  75. const char* openMode;
  76. if((flags & FileOpenFlag::READ) != FileOpenFlag::NONE)
  77. {
  78. openMode = "rb";
  79. }
  80. else if((flags & FileOpenFlag::APPEND) == FileOpenFlag::APPEND)
  81. {
  82. openMode = "a+b";
  83. }
  84. else if((flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE)
  85. {
  86. openMode = "wb";
  87. }
  88. else
  89. {
  90. ANKI_ASSERT(0);
  91. openMode = nullptr;
  92. }
  93. // Open
  94. m_file = fopen(filename.cstr(), openMode);
  95. if(m_file == nullptr)
  96. {
  97. ANKI_UTIL_LOGE("Failed to open file \"%s\", open mode \"%s\"", &filename[0], openMode);
  98. err = Error::FILE_ACCESS;
  99. }
  100. else
  101. {
  102. m_flags = flags;
  103. }
  104. // Get file size
  105. if(((flags & FileOpenFlag::READ) != FileOpenFlag::NONE) && !err)
  106. {
  107. fseek(ANKI_CFILE, 0, SEEK_END);
  108. I64 size = ftell(ANKI_CFILE);
  109. if(size < 1)
  110. {
  111. ANKI_UTIL_LOGE("ftell() failed");
  112. err = Error::FUNCTION_FAILED;
  113. }
  114. else
  115. {
  116. m_size = U32(size);
  117. rewind(ANKI_CFILE);
  118. }
  119. }
  120. return err;
  121. }
  122. #if ANKI_OS_ANDROID
  123. Error File::openAndroidFile(const CString& filename, FileOpenFlag flags)
  124. {
  125. ANKI_ASSERT(!!(flags & FileOpenFlag::SPECIAL));
  126. if(!!(flags & FileOpenFlag::WRITE))
  127. {
  128. ANKI_UTIL_LOGE("Cannot write inside archives");
  129. return Error::FILE_ACCESS;
  130. }
  131. if(!(flags & FileOpenFlag::READ))
  132. {
  133. ANKI_UTIL_LOGE("Missing FileOpenFlag::READ flag");
  134. return Error::FILE_ACCESS;
  135. }
  136. // Open file
  137. ANKI_ASSERT(g_androidApp != nullptr && g_androidApp->activity && g_androidApp->activity->assetManager);
  138. m_file = AAssetManager_open(g_androidApp->activity->assetManager, filename.cstr(), AASSET_MODE_STREAMING);
  139. if(m_file == nullptr)
  140. {
  141. ANKI_UTIL_LOGE("AAssetManager_open() failed");
  142. return Error::FILE_ACCESS;
  143. }
  144. m_flags = flags;
  145. return Error::NONE;
  146. }
  147. #endif
  148. void File::close()
  149. {
  150. if(m_file)
  151. {
  152. #if ANKI_OS_ANDROID
  153. if(!!(m_flags & FileOpenFlag::SPECIAL))
  154. {
  155. AAsset_close(ANKI_AFILE);
  156. }
  157. else
  158. #endif
  159. {
  160. fclose(ANKI_CFILE);
  161. }
  162. }
  163. zero();
  164. }
  165. Error File::flush()
  166. {
  167. ANKI_ASSERT(m_file);
  168. Error err = Error::NONE;
  169. if((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE)
  170. {
  171. #if ANKI_OS_ANDROID
  172. if(!!(m_flags & FileOpenFlag::SPECIAL))
  173. {
  174. ANKI_ASSERT(0 && "Cannot have write these file types");
  175. }
  176. else
  177. #endif
  178. {
  179. const I ierr = fflush(ANKI_CFILE);
  180. if(ierr)
  181. {
  182. ANKI_UTIL_LOGE("fflush() failed");
  183. err = Error::FUNCTION_FAILED;
  184. }
  185. }
  186. }
  187. return err;
  188. }
  189. Error File::read(void* buff, PtrSize size)
  190. {
  191. ANKI_ASSERT(buff);
  192. ANKI_ASSERT(size > 0);
  193. ANKI_ASSERT(m_file);
  194. ANKI_ASSERT((m_flags & FileOpenFlag::READ) != FileOpenFlag::NONE);
  195. I64 readSize = 0;
  196. #if ANKI_OS_ANDROID
  197. if(!!(m_flags & FileOpenFlag::SPECIAL))
  198. {
  199. readSize = AAsset_read(ANKI_AFILE, buff, size);
  200. }
  201. else
  202. #endif
  203. {
  204. readSize = fread(buff, 1, size, ANKI_CFILE);
  205. }
  206. Error err = Error::NONE;
  207. if(static_cast<I64>(size) != readSize)
  208. {
  209. ANKI_UTIL_LOGE("File read failed");
  210. err = Error::FILE_ACCESS;
  211. }
  212. return err;
  213. }
  214. PtrSize File::getSize() const
  215. {
  216. ANKI_ASSERT(m_file);
  217. ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
  218. PtrSize out = 0;
  219. #if ANKI_OS_ANDROID
  220. if(!!(m_flags & FileOpenFlag::SPECIAL))
  221. {
  222. out = AAsset_getLength(ANKI_AFILE);
  223. }
  224. else
  225. #endif
  226. {
  227. ANKI_ASSERT(m_size != 0);
  228. out = m_size;
  229. }
  230. return out;
  231. }
  232. Error File::readU32(U32& out)
  233. {
  234. ANKI_ASSERT(m_file);
  235. ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
  236. ANKI_ASSERT((m_flags & FileOpenFlag::READ) != FileOpenFlag::NONE);
  237. ANKI_ASSERT((m_flags & FileOpenFlag::BINARY) != FileOpenFlag::NONE && "Should be binary file");
  238. ANKI_ASSERT((m_flags & FileOpenFlag::ENDIAN_BIG) != (m_flags & FileOpenFlag::ENDIAN_LITTLE)
  239. && "One of those 2 should be active");
  240. Error err = read(&out, sizeof(out));
  241. if(!err)
  242. {
  243. // Copy it
  244. FileOpenFlag machineEndianness = getMachineEndianness();
  245. FileOpenFlag fileEndianness = ((m_flags & FileOpenFlag::ENDIAN_BIG) != FileOpenFlag::NONE)
  246. ? FileOpenFlag::ENDIAN_BIG
  247. : FileOpenFlag::ENDIAN_LITTLE;
  248. if(machineEndianness == fileEndianness)
  249. {
  250. // Same endianness between the file and the machine. Do nothing
  251. }
  252. else if(machineEndianness == FileOpenFlag::ENDIAN_BIG && fileEndianness == FileOpenFlag::ENDIAN_LITTLE)
  253. {
  254. U8* c = reinterpret_cast<U8*>(&out);
  255. out = (c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
  256. }
  257. else
  258. {
  259. U8* c = reinterpret_cast<U8*>(&out);
  260. out = ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]);
  261. }
  262. }
  263. return err;
  264. }
  265. Error File::readF32(F32& out)
  266. {
  267. U32 integer = MAX_U32;
  268. Error err = readU32(integer);
  269. if(!err)
  270. {
  271. memcpy(&out, &integer, sizeof(F32));
  272. }
  273. return err;
  274. }
  275. Error File::write(const void* buff, PtrSize size)
  276. {
  277. ANKI_ASSERT(buff);
  278. ANKI_ASSERT(size > 0);
  279. ANKI_ASSERT(m_file);
  280. ANKI_ASSERT((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE);
  281. Error err = Error::NONE;
  282. #if ANKI_OS_ANDROID
  283. if(!!(m_flags & FileOpenFlag::SPECIAL))
  284. {
  285. ANKI_UTIL_LOGE("Writting to special files is not supported");
  286. err = Error::FILE_ACCESS;
  287. }
  288. else
  289. #endif
  290. {
  291. PtrSize writeSize = 0;
  292. writeSize = std::fwrite(buff, 1, size, ANKI_CFILE);
  293. if(writeSize != size)
  294. {
  295. ANKI_UTIL_LOGE("std::fwrite() failed");
  296. err = Error::FILE_ACCESS;
  297. }
  298. }
  299. return err;
  300. }
  301. Error File::writeText(CString format, ...)
  302. {
  303. ANKI_ASSERT(m_file);
  304. ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
  305. ANKI_ASSERT((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE);
  306. ANKI_ASSERT((m_flags & FileOpenFlag::BINARY) == FileOpenFlag::NONE);
  307. Error err = Error::NONE;
  308. va_list args;
  309. va_start(args, format);
  310. #if ANKI_OS_ANDROID
  311. if(!!(m_flags & FileOpenFlag::SPECIAL))
  312. {
  313. ANKI_UTIL_LOGE("Writting to special files is not supported");
  314. err = Error::FILE_ACCESS;
  315. }
  316. else
  317. #endif
  318. {
  319. std::vfprintf(ANKI_CFILE, &format[0], args);
  320. }
  321. va_end(args);
  322. return err;
  323. }
  324. Error File::seek(PtrSize offset, FileSeekOrigin origin)
  325. {
  326. ANKI_ASSERT(m_file);
  327. ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
  328. Error err = Error::NONE;
  329. #if ANKI_OS_ANDROID
  330. if(!!(m_flags & FileOpenFlag::SPECIAL))
  331. {
  332. if(AAsset_seek(ANKI_AFILE, offset, I32(origin)) == off_t(-1))
  333. {
  334. ANKI_UTIL_LOGE("AAsset_seek() failed");
  335. err = Error::FUNCTION_FAILED;
  336. }
  337. }
  338. else
  339. #endif
  340. {
  341. if(fseek(ANKI_CFILE, offset, I32(origin)) != 0)
  342. {
  343. ANKI_UTIL_LOGE("fseek() failed");
  344. err = Error::FUNCTION_FAILED;
  345. }
  346. }
  347. return err;
  348. }
  349. PtrSize File::tell()
  350. {
  351. ANKI_ASSERT(m_file);
  352. ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
  353. #if ANKI_OS_ANDROID
  354. if(!!(m_flags & FileOpenFlag::SPECIAL))
  355. {
  356. ANKI_ASSERT(0);
  357. }
  358. else
  359. #endif
  360. {
  361. return ftell(ANKI_CFILE);
  362. }
  363. return 0;
  364. }
  365. FileOpenFlag File::getMachineEndianness()
  366. {
  367. I32 num = 1;
  368. if(*(char*)&num == 1)
  369. {
  370. return FileOpenFlag::ENDIAN_LITTLE;
  371. }
  372. else
  373. {
  374. return FileOpenFlag::ENDIAN_BIG;
  375. }
  376. }
  377. Error File::readAllText(GenericMemoryPoolAllocator<U8> alloc, String& out)
  378. {
  379. Error err = Error::NONE;
  380. PtrSize size = getSize();
  381. if(size != 0)
  382. {
  383. out.create(alloc, '?', size);
  384. err = read(&out[0], size);
  385. }
  386. else
  387. {
  388. err = Error::FUNCTION_FAILED;
  389. }
  390. return err;
  391. }
  392. Error File::readAllText(StringAuto& out)
  393. {
  394. Error err = Error::NONE;
  395. PtrSize size = getSize();
  396. if(size != 0)
  397. {
  398. out.create('?', size);
  399. err = read(&out[0], size);
  400. }
  401. else
  402. {
  403. err = Error::FUNCTION_FAILED;
  404. }
  405. return err;
  406. }
  407. } // end namespace anki