File.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Copyright (c) 2006-2015 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. // File must exist if read mode.
  51. if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
  52. throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
  53. // Check whether the write directory is set.
  54. if ((mode == MODE_APPEND || mode == MODE_WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory())
  55. throw love::Exception("Could not set write directory.");
  56. // File already open?
  57. if (file != nullptr)
  58. return false;
  59. PHYSFS_getLastError(); // Clear the error buffer.
  60. PHYSFS_File *handle = nullptr;
  61. switch (mode)
  62. {
  63. case MODE_READ:
  64. handle = PHYSFS_openRead(filename.c_str());
  65. break;
  66. case MODE_APPEND:
  67. handle = PHYSFS_openAppend(filename.c_str());
  68. break;
  69. case MODE_WRITE:
  70. handle = PHYSFS_openWrite(filename.c_str());
  71. break;
  72. default:
  73. break;
  74. }
  75. if (handle == nullptr)
  76. {
  77. const char *err = PHYSFS_getLastError();
  78. if (err == nullptr)
  79. err = "unknown error";
  80. throw love::Exception("Could not open file %s (%s)", filename.c_str(), err);
  81. }
  82. file = handle;
  83. this->mode = mode;
  84. if (file != nullptr && !setBuffer(bufferMode, bufferSize))
  85. {
  86. // Revert to buffer defaults if we don't successfully set the buffer.
  87. bufferMode = BUFFER_NONE;
  88. bufferSize = 0;
  89. }
  90. return (file != nullptr);
  91. }
  92. bool File::close()
  93. {
  94. if (file == nullptr || !PHYSFS_close(file))
  95. return false;
  96. mode = MODE_CLOSED;
  97. file = nullptr;
  98. return true;
  99. }
  100. bool File::isOpen() const
  101. {
  102. return mode != MODE_CLOSED && file != nullptr;
  103. }
  104. int64 File::getSize()
  105. {
  106. // If the file is closed, open it to
  107. // check the size.
  108. if (file == nullptr)
  109. {
  110. open(MODE_READ);
  111. int64 size = (int64) PHYSFS_fileLength(file);
  112. close();
  113. return size;
  114. }
  115. return (int64) PHYSFS_fileLength(file);
  116. }
  117. int64 File::read(void *dst, int64 size)
  118. {
  119. if (!file || mode != MODE_READ)
  120. throw love::Exception("File is not opened for reading.");
  121. int64 max = (int64)PHYSFS_fileLength(file);
  122. size = (size == ALL) ? max : size;
  123. size = (size > max) ? max : size;
  124. // Sadly, we'll have to clamp to 32 bits here
  125. size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_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. int64 read = (int64)PHYSFS_read(file, dst, 1, (PHYSFS_uint32) size);
  132. #endif
  133. return read;
  134. }
  135. bool File::write(const void *data, int64 size)
  136. {
  137. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  138. throw love::Exception("File is not opened for writing.");
  139. // Another clamp, for the time being.
  140. size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
  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. int64 written = (int64) PHYSFS_write(file, data, 1, (PHYSFS_uint32) size);
  148. #endif
  149. // Check that correct amount of data was written.
  150. if (written != size)
  151. return false;
  152. // Manually flush the buffer in BUFFER_LINE mode if we find a newline.
  153. if (bufferMode == BUFFER_LINE && bufferSize > size)
  154. {
  155. if (memchr(data, '\n', (size_t) size) != NULL)
  156. flush();
  157. }
  158. return true;
  159. }
  160. bool File::flush()
  161. {
  162. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  163. throw love::Exception("File is not opened for writing.");
  164. return PHYSFS_flush(file) != 0;
  165. }
  166. #ifdef LOVE_WINDOWS
  167. // MSVC doesn't like the 'this' keyword
  168. // well, we'll use 'that'.
  169. // It zigs, we zag.
  170. inline bool test_eof(File *that, PHYSFS_File *)
  171. {
  172. int64 pos = that->tell();
  173. int64 size = that->getSize();
  174. return pos == -1 || size == -1 || pos >= size;
  175. }
  176. #else
  177. inline bool test_eof(File *, PHYSFS_File *file)
  178. {
  179. return PHYSFS_eof(file);
  180. }
  181. #endif
  182. bool File::isEOF()
  183. {
  184. return file == nullptr || test_eof(this, file);
  185. }
  186. int64 File::tell()
  187. {
  188. if (file == nullptr)
  189. return -1;
  190. return (int64) PHYSFS_tell(file);
  191. }
  192. bool File::seek(uint64 pos)
  193. {
  194. return file != nullptr && PHYSFS_seek(file, (PHYSFS_uint64) pos) != 0;
  195. }
  196. bool File::setBuffer(BufferMode bufmode, int64 size)
  197. {
  198. if (size < 0)
  199. return false;
  200. // If the file isn't open, we'll make sure the buffer values are set in
  201. // File::open.
  202. if (!isOpen())
  203. {
  204. bufferMode = bufmode;
  205. bufferSize = size;
  206. return true;
  207. }
  208. int ret = 1;
  209. switch (bufmode)
  210. {
  211. case BUFFER_NONE:
  212. default:
  213. ret = PHYSFS_setBuffer(file, 0);
  214. size = 0;
  215. break;
  216. case BUFFER_LINE:
  217. case BUFFER_FULL:
  218. ret = PHYSFS_setBuffer(file, size);
  219. break;
  220. }
  221. if (ret == 0)
  222. return false;
  223. bufferMode = bufmode;
  224. bufferSize = size;
  225. return true;
  226. }
  227. File::BufferMode File::getBuffer(int64 &size) const
  228. {
  229. size = bufferSize;
  230. return bufferMode;
  231. }
  232. const std::string &File::getFilename() const
  233. {
  234. return filename;
  235. }
  236. filesystem::File::Mode File::getMode() const
  237. {
  238. return mode;
  239. }
  240. } // physfs
  241. } // filesystem
  242. } // love