DroppedFile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Copyright (c) 2006-2020 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. // LOVE
  21. #include "DroppedFile.h"
  22. #include "common/utf8.h"
  23. // Assume POSIX or Visual Studio.
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #ifdef LOVE_WINDOWS
  27. #include <wchar.h>
  28. #else
  29. #include <unistd.h> // POSIX.
  30. #endif
  31. namespace love
  32. {
  33. namespace filesystem
  34. {
  35. love::Type DroppedFile::type("DroppedFile", &File::type);
  36. DroppedFile::DroppedFile(const std::string &filename)
  37. : filename(filename)
  38. , file(nullptr)
  39. , mode(MODE_CLOSED)
  40. , bufferMode(BUFFER_NONE)
  41. , bufferSize(0)
  42. {
  43. }
  44. DroppedFile::~DroppedFile()
  45. {
  46. if (mode != MODE_CLOSED)
  47. close();
  48. }
  49. bool DroppedFile::open(Mode newmode)
  50. {
  51. if (newmode == MODE_CLOSED)
  52. return true;
  53. // File already open?
  54. if (file != nullptr)
  55. return false;
  56. #ifdef LOVE_WINDOWS
  57. // make sure non-ASCII filenames work.
  58. std::wstring modestr = to_widestr(getModeString(newmode));
  59. std::wstring wfilename = to_widestr(filename);
  60. file = _wfopen(wfilename.c_str(), modestr.c_str());
  61. #else
  62. file = fopen(filename.c_str(), getModeString(newmode));
  63. #endif
  64. if (newmode == MODE_READ && file == nullptr)
  65. throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
  66. mode = newmode;
  67. if (file != nullptr && !setBuffer(bufferMode, bufferSize))
  68. {
  69. // Revert to buffer defaults if we don't successfully set the buffer.
  70. bufferMode = BUFFER_NONE;
  71. bufferSize = 0;
  72. }
  73. return file != nullptr;
  74. }
  75. bool DroppedFile::close()
  76. {
  77. if (file == nullptr || fclose(file) != 0)
  78. return false;
  79. mode = MODE_CLOSED;
  80. file = nullptr;
  81. return true;
  82. }
  83. bool DroppedFile::isOpen() const
  84. {
  85. return mode != MODE_CLOSED && file != nullptr;
  86. }
  87. int64 DroppedFile::getSize()
  88. {
  89. int fd = file ? fileno(file) : -1;
  90. #ifdef LOVE_WINDOWS
  91. struct _stat64 buf;
  92. if (fd != -1)
  93. {
  94. if (_fstat64(fd, &buf) != 0)
  95. return -1;
  96. }
  97. else
  98. {
  99. // make sure non-ASCII filenames work.
  100. std::wstring wfilename = to_widestr(filename);
  101. if (_wstat64(wfilename.c_str(), &buf) != 0)
  102. return -1;
  103. }
  104. return (int64) buf.st_size;
  105. #else
  106. // Assume POSIX support...
  107. struct stat buf;
  108. if (fd != -1)
  109. {
  110. if (fstat(fd, &buf) != 0)
  111. return -1;
  112. }
  113. else if (stat(filename.c_str(), &buf) != 0)
  114. return -1;
  115. return (int64) buf.st_size;
  116. #endif
  117. }
  118. int64 DroppedFile::read(void *dst, int64 size)
  119. {
  120. if (!file || mode != MODE_READ)
  121. throw love::Exception("File is not opened for reading.");
  122. if (size < 0)
  123. throw love::Exception("Invalid read size.");
  124. size_t read = fread(dst, 1, (size_t) size, file);
  125. return (int64) read;
  126. }
  127. bool DroppedFile::write(const void *data, int64 size)
  128. {
  129. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  130. throw love::Exception("File is not opened for writing.");
  131. if (size < 0)
  132. throw love::Exception("Invalid write size.");
  133. int64 written = (int64) fwrite(data, 1, (size_t) size, file);
  134. return written == size;
  135. }
  136. bool DroppedFile::flush()
  137. {
  138. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  139. throw love::Exception("File is not opened for writing.");
  140. return fflush(file) == 0;
  141. }
  142. bool DroppedFile::isEOF()
  143. {
  144. return file == nullptr || tell() >= getSize();
  145. }
  146. int64 DroppedFile::tell()
  147. {
  148. if (file == nullptr)
  149. return -1;
  150. #ifdef LOVE_WINDOWS
  151. return (int64) _ftelli64(file);
  152. #else
  153. return (int64) ftello(file);
  154. #endif
  155. }
  156. bool DroppedFile::seek(uint64 pos)
  157. {
  158. return file != nullptr &&
  159. #ifdef LOVE_WINDOWS
  160. _fseeki64(file, (int64) pos, SEEK_SET) == 0;
  161. #else
  162. fseeko(file, (off_t) pos, SEEK_SET) == 0;
  163. #endif
  164. }
  165. bool DroppedFile::setBuffer(BufferMode bufmode, int64 size)
  166. {
  167. if (size < 0)
  168. return false;
  169. if (bufmode == BUFFER_NONE)
  170. size = 0;
  171. // If the file isn't open, we'll make sure the buffer values are set in
  172. // DroppedFile::open.
  173. if (!isOpen())
  174. {
  175. bufferMode = bufmode;
  176. bufferSize = size;
  177. return true;
  178. }
  179. int vbufmode;
  180. switch (bufmode)
  181. {
  182. case File::BUFFER_NONE:
  183. default:
  184. vbufmode = _IONBF;
  185. break;
  186. case File::BUFFER_LINE:
  187. vbufmode = _IOLBF;
  188. break;
  189. case File::BUFFER_FULL:
  190. vbufmode = _IOFBF;
  191. break;
  192. }
  193. if (setvbuf(file, nullptr, vbufmode, (size_t) size) != 0)
  194. return false;
  195. bufferMode = bufmode;
  196. bufferSize = size;
  197. return true;
  198. }
  199. File::BufferMode DroppedFile::getBuffer(int64 &size) const
  200. {
  201. size = bufferSize;
  202. return bufferMode;
  203. }
  204. const std::string &DroppedFile::getFilename() const
  205. {
  206. return filename;
  207. }
  208. File::Mode DroppedFile::getMode() const
  209. {
  210. return mode;
  211. }
  212. const char *DroppedFile::getModeString(Mode mode)
  213. {
  214. switch (mode)
  215. {
  216. case File::MODE_CLOSED:
  217. default:
  218. return "c";
  219. case File::MODE_READ:
  220. return "rb";
  221. case File::MODE_WRITE:
  222. return "wb";
  223. case File::MODE_APPEND:
  224. return "ab";
  225. }
  226. }
  227. } // filesystem
  228. } // love