Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

203 wiersze
3.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if __CELLOS_LV2__
  14. # include <sys/paths.h>
  15. # include <cell/cell_fs.h>
  16. #elif __ANDROID__
  17. # include <android/asset_manager_jni.h>
  18. #endif
  19. #include "core.h"
  20. namespace lol
  21. {
  22. #if __ANDROID__
  23. extern AAssetManager *g_assets;
  24. #endif
  25. class FileData
  26. {
  27. friend class File;
  28. void Open(String const &file, FileAccess mode)
  29. {
  30. #if __CELLOS_LV2__
  31. String realfile = String(SYS_APP_HOME) + '/' + file;
  32. CellFsErrno err = cellFsOpen(realfile.C(), CELL_FS_O_RDONLY,
  33. &m_fd, NULL, 0);
  34. if (err != CELL_FS_SUCCEEDED)
  35. m_fd = -1;
  36. #elif __ANDROID__
  37. m_asset = AAssetManager_open(g_assets, file.C(), AASSET_MODE_UNKNOWN);
  38. #elif HAVE_STDIO_H
  39. /* FIXME: no modes, no error checking, no nothing */
  40. m_fd = fopen(file.C(), "r");
  41. #endif
  42. }
  43. inline bool IsValid() const
  44. {
  45. #if __CELLOS_LV2__
  46. return m_fd > -1;
  47. #elif __ANDROID__
  48. return !!m_asset;
  49. #elif HAVE_STDIO_H
  50. return !!m_fd;
  51. #else
  52. return false;
  53. #endif
  54. }
  55. int Read(uint8_t *buf, int count)
  56. {
  57. #if __CELLOS_LV2__
  58. uint64_t done;
  59. CellFsErrno err = cellFsRead(m_fd, buf, count, &done);
  60. if (err != CELL_FS_SUCCEEDED)
  61. return -1;
  62. return (int)done;
  63. #elif __ANDROID__
  64. return AAsset_read(m_asset, buf, count);
  65. #elif HAVE_STDIO_H
  66. size_t done = fread(buf, 1, count, m_fd);
  67. if (done <= 0)
  68. return -1;
  69. return (int)done;
  70. #else
  71. return 0;
  72. #endif
  73. }
  74. String ReadString()
  75. {
  76. Array<uint8_t> buf;
  77. buf.Resize(BUFSIZ);
  78. String ret;
  79. while (IsValid())
  80. {
  81. int done = Read(&buf[0], buf.Count());
  82. if (done <= 0)
  83. break;
  84. int oldsize = ret.Count();
  85. ret.Resize(oldsize + done);
  86. memcpy(&ret[oldsize], &buf[0], done);
  87. buf.Resize(buf.Count() * 3 / 2);
  88. }
  89. return ret;
  90. }
  91. void Close()
  92. {
  93. #if __CELLOS_LV2__
  94. if (m_fd >= 0)
  95. cellFsClose(m_fd);
  96. m_fd = -1;
  97. #elif __ANDROID__
  98. if (m_asset)
  99. AAsset_close(m_asset);
  100. m_asset = nullptr;
  101. #elif HAVE_STDIO_H
  102. if (m_fd)
  103. fclose(m_fd);
  104. m_fd = nullptr;
  105. #endif
  106. }
  107. #if __CELLOS_LV2__
  108. int m_fd;
  109. #elif __ANDROID__
  110. AAsset *m_asset;
  111. #elif HAVE_STDIO_H
  112. FILE *m_fd;
  113. #endif
  114. Atomic<int> m_refcount;
  115. };
  116. File::File()
  117. : m_data(new FileData)
  118. {
  119. ++m_data->m_refcount;
  120. }
  121. File::File(File const &that)
  122. : m_data(that.m_data)
  123. {
  124. ++m_data->m_refcount;
  125. }
  126. File &File::operator =(File const &that)
  127. {
  128. if (this == &that)
  129. return *this;
  130. /* FIXME: this needs auditing */
  131. int refcount = --m_data->m_refcount;
  132. if (refcount == 0)
  133. {
  134. m_data->Close();
  135. delete m_data;
  136. }
  137. m_data = that.m_data;
  138. ++m_data->m_refcount;
  139. return *this;
  140. }
  141. File::~File()
  142. {
  143. int refcount = --m_data->m_refcount;
  144. if (refcount == 0)
  145. {
  146. m_data->Close();
  147. delete m_data;
  148. }
  149. }
  150. void File::Open(String const &file, FileAccess mode)
  151. {
  152. return m_data->Open(file, mode);
  153. }
  154. bool File::IsValid() const
  155. {
  156. return m_data->IsValid();
  157. }
  158. int File::Read(uint8_t *buf, int count)
  159. {
  160. return m_data->Read(buf, count);
  161. }
  162. String File::ReadString()
  163. {
  164. return m_data->ReadString();
  165. }
  166. void File::Close()
  167. {
  168. m_data->Close();
  169. }
  170. } /* namespace lol */