File.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "File.h"
  21. // STD
  22. #include <cstring>
  23. // LOVE
  24. #include "Filesystem.h"
  25. #include "filesystem/FileData.h"
  26. namespace love
  27. {
  28. namespace filesystem
  29. {
  30. extern bool hack_setupWriteDirectory();
  31. namespace physfs
  32. {
  33. File::File(const std::string &filename)
  34. : filename(filename)
  35. , file(nullptr)
  36. , mode(MODE_CLOSED)
  37. , bufferMode(BUFFER_NONE)
  38. , bufferSize(0)
  39. {
  40. }
  41. File::~File()
  42. {
  43. if (mode != MODE_CLOSED)
  44. close();
  45. }
  46. bool File::open(Mode mode)
  47. {
  48. if (mode == MODE_CLOSED)
  49. return true;
  50. if (!PHYSFS_isInit())
  51. throw love::Exception("PhysFS is not initialized.");
  52. // File must exist if read mode.
  53. if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
  54. throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
  55. // Check whether the write directory is set.
  56. if ((mode == MODE_APPEND || mode == MODE_WRITE) && (PHYSFS_getWriteDir() == nullptr) && !hack_setupWriteDirectory())
  57. throw love::Exception("Could not set write directory.");
  58. // File already open?
  59. if (file != nullptr)
  60. return false;
  61. PHYSFS_getLastError(); // Clear the error buffer.
  62. PHYSFS_File *handle = nullptr;
  63. switch (mode)
  64. {
  65. case MODE_READ:
  66. handle = PHYSFS_openRead(filename.c_str());
  67. break;
  68. case MODE_APPEND:
  69. handle = PHYSFS_openAppend(filename.c_str());
  70. break;
  71. case MODE_WRITE:
  72. handle = PHYSFS_openWrite(filename.c_str());
  73. break;
  74. default:
  75. break;
  76. }
  77. if (handle == nullptr)
  78. {
  79. const char *err = PHYSFS_getLastError();
  80. if (err == nullptr)
  81. err = "unknown error";
  82. throw love::Exception("Could not open file %s (%s)", filename.c_str(), err);
  83. }
  84. file = handle;
  85. this->mode = mode;
  86. if (file != nullptr && !setBuffer(bufferMode, bufferSize))
  87. {
  88. // Revert to buffer defaults if we don't successfully set the buffer.
  89. bufferMode = BUFFER_NONE;
  90. bufferSize = 0;
  91. }
  92. return (file != nullptr);
  93. }
  94. bool File::close()
  95. {
  96. if (file == nullptr || !PHYSFS_close(file))
  97. return false;
  98. mode = MODE_CLOSED;
  99. file = nullptr;
  100. return true;
  101. }
  102. bool File::isOpen() const
  103. {
  104. return mode != MODE_CLOSED && file != nullptr;
  105. }
  106. int64 File::getSize()
  107. {
  108. // If the file is closed, open it to
  109. // check the size.
  110. if (file == nullptr)
  111. {
  112. open(MODE_READ);
  113. int64 size = (int64) PHYSFS_fileLength(file);
  114. close();
  115. return size;
  116. }
  117. return (int64) PHYSFS_fileLength(file);
  118. }
  119. int64 File::read(void *dst, int64 size)
  120. {
  121. if (!file || mode != MODE_READ)
  122. throw love::Exception("File is not opened for reading.");
  123. int64 max = (int64)PHYSFS_fileLength(file);
  124. size = (size == ALL) ? max : size;
  125. size = (size > max) ? max : size;
  126. if (size < 0)
  127. throw love::Exception("Invalid read size.");
  128. #ifdef LOVE_USE_PHYSFS_2_1
  129. int64 read = PHYSFS_readBytes(file, dst, (PHYSFS_uint64) size);
  130. #else
  131. // Sadly, we'll have to clamp to 32 bits here
  132. size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
  133. int64 read = (int64)PHYSFS_read(file, dst, 1, (PHYSFS_uint32) size);
  134. #endif
  135. return read;
  136. }
  137. bool File::write(const void *data, int64 size)
  138. {
  139. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  140. throw love::Exception("File is not opened for writing.");
  141. if (size < 0)
  142. throw love::Exception("Invalid write size.");
  143. // Try to write.
  144. #ifdef LOVE_USE_PHYSFS_2_1
  145. int64 written = PHYSFS_writeBytes(file, data, (PHYSFS_uint64) size);
  146. #else
  147. // Another clamp, for the time being.
  148. size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
  149. int64 written = (int64) PHYSFS_write(file, data, 1, (PHYSFS_uint32) size);
  150. #endif
  151. // Check that correct amount of data was written.
  152. if (written != size)
  153. return false;
  154. // Manually flush the buffer in BUFFER_LINE mode if we find a newline.
  155. if (bufferMode == BUFFER_LINE && bufferSize > size)
  156. {
  157. if (memchr(data, '\n', (size_t) size) != NULL)
  158. flush();
  159. }
  160. return true;
  161. }
  162. bool File::flush()
  163. {
  164. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  165. throw love::Exception("File is not opened for writing.");
  166. return PHYSFS_flush(file) != 0;
  167. }
  168. #ifdef LOVE_WINDOWS
  169. // MSVC doesn't like the 'this' keyword
  170. // well, we'll use 'that'.
  171. // It zigs, we zag.
  172. inline bool test_eof(File *that, PHYSFS_File *)
  173. {
  174. int64 pos = that->tell();
  175. int64 size = that->getSize();
  176. return pos == -1 || size == -1 || pos >= size;
  177. }
  178. #else
  179. inline bool test_eof(File *, PHYSFS_File *file)
  180. {
  181. return PHYSFS_eof(file);
  182. }
  183. #endif
  184. bool File::isEOF()
  185. {
  186. return file == nullptr || test_eof(this, file);
  187. }
  188. int64 File::tell()
  189. {
  190. if (file == nullptr)
  191. return -1;
  192. return (int64) PHYSFS_tell(file);
  193. }
  194. bool File::seek(uint64 pos)
  195. {
  196. return file != nullptr && PHYSFS_seek(file, (PHYSFS_uint64) pos) != 0;
  197. }
  198. bool File::setBuffer(BufferMode bufmode, int64 size)
  199. {
  200. if (size < 0)
  201. return false;
  202. // If the file isn't open, we'll make sure the buffer values are set in
  203. // File::open.
  204. if (!isOpen())
  205. {
  206. bufferMode = bufmode;
  207. bufferSize = size;
  208. return true;
  209. }
  210. int ret = 1;
  211. switch (bufmode)
  212. {
  213. case BUFFER_NONE:
  214. default:
  215. ret = PHYSFS_setBuffer(file, 0);
  216. size = 0;
  217. break;
  218. case BUFFER_LINE:
  219. case BUFFER_FULL:
  220. ret = PHYSFS_setBuffer(file, size);
  221. break;
  222. }
  223. if (ret == 0)
  224. return false;
  225. bufferMode = bufmode;
  226. bufferSize = size;
  227. return true;
  228. }
  229. File::BufferMode File::getBuffer(int64 &size) const
  230. {
  231. size = bufferSize;
  232. return bufferMode;
  233. }
  234. const std::string &File::getFilename() const
  235. {
  236. return filename;
  237. }
  238. filesystem::File::Mode File::getMode() const
  239. {
  240. return mode;
  241. }
  242. } // physfs
  243. } // filesystem
  244. } // love