DroppedFile.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright (c) 2006-2016 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(&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. #ifdef LOVE_WINDOWS
  90. // make sure non-ASCII filenames work.
  91. std::wstring wfilename = to_widestr(filename);
  92. struct _stat buf;
  93. if (_wstat(wfilename.c_str(), &buf) != 0)
  94. return -1;
  95. return (int64) buf.st_size;
  96. #else
  97. // Assume POSIX support...
  98. struct stat buf;
  99. if (stat(filename.c_str(), &buf) != 0)
  100. return -1;
  101. return (int64) buf.st_size;
  102. #endif
  103. }
  104. int64 DroppedFile::read(void *dst, int64 size)
  105. {
  106. if (!file || mode != MODE_READ)
  107. throw love::Exception("File is not opened for reading.");
  108. if (size < 0)
  109. throw love::Exception("Invalid read size.");
  110. size_t read = fread(dst, 1, (size_t) size, file);
  111. return (int64) read;
  112. }
  113. bool DroppedFile::write(const void *data, int64 size)
  114. {
  115. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  116. throw love::Exception("File is not opened for writing.");
  117. if (size < 0)
  118. throw love::Exception("Invalid write size.");
  119. int64 written = (int64) fwrite(data, 1, (size_t) size, file);
  120. return written == size;
  121. }
  122. bool DroppedFile::flush()
  123. {
  124. if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
  125. throw love::Exception("File is not opened for writing.");
  126. return fflush(file) == 0;
  127. }
  128. bool DroppedFile::isEOF()
  129. {
  130. return file == nullptr || feof(file) != 0;
  131. }
  132. int64 DroppedFile::tell()
  133. {
  134. if (file == nullptr)
  135. return -1;
  136. return (int64) ftell(file);
  137. }
  138. bool DroppedFile::seek(uint64 pos)
  139. {
  140. return file != nullptr && fseek(file, (long) pos, SEEK_SET) == 0;
  141. }
  142. bool DroppedFile::setBuffer(BufferMode bufmode, int64 size)
  143. {
  144. if (size < 0)
  145. return false;
  146. if (bufmode == BUFFER_NONE)
  147. size = 0;
  148. // If the file isn't open, we'll make sure the buffer values are set in
  149. // DroppedFile::open.
  150. if (!isOpen())
  151. {
  152. bufferMode = bufmode;
  153. bufferSize = size;
  154. return true;
  155. }
  156. int vbufmode;
  157. switch (bufmode)
  158. {
  159. case File::BUFFER_NONE:
  160. default:
  161. vbufmode = _IONBF;
  162. break;
  163. case File::BUFFER_LINE:
  164. vbufmode = _IOLBF;
  165. break;
  166. case File::BUFFER_FULL:
  167. vbufmode = _IOFBF;
  168. break;
  169. }
  170. if (setvbuf(file, nullptr, vbufmode, (size_t) size) != 0)
  171. return false;
  172. bufferMode = bufmode;
  173. bufferSize = size;
  174. return true;
  175. }
  176. File::BufferMode DroppedFile::getBuffer(int64 &size) const
  177. {
  178. size = bufferSize;
  179. return bufferMode;
  180. }
  181. const std::string &DroppedFile::getFilename() const
  182. {
  183. return filename;
  184. }
  185. File::Mode DroppedFile::getMode() const
  186. {
  187. return mode;
  188. }
  189. const char *DroppedFile::getModeString(Mode mode)
  190. {
  191. switch (mode)
  192. {
  193. case File::MODE_CLOSED:
  194. default:
  195. return "c";
  196. case File::MODE_READ:
  197. return "rb";
  198. case File::MODE_WRITE:
  199. return "wb";
  200. case File::MODE_APPEND:
  201. return "ab";
  202. }
  203. }
  204. } // filesystem
  205. } // love