File.cpp 9.0 KB

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