File.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Copyright (c) 2006-2009 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. namespace physfs
  31. {
  32. extern bool hack_setupWriteDirectory();
  33. File::File(std::string filename)
  34. : filename(filename), file(0), mode(filesystem::File::CLOSED)
  35. {
  36. }
  37. File::~File()
  38. {
  39. }
  40. bool File::open(Mode mode)
  41. {
  42. if(mode == CLOSED)
  43. return true;
  44. // File must exist if read mode.
  45. if((mode == READ))
  46. if(!PHYSFS_exists(filename.c_str()))
  47. throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
  48. // Check whether the write directory is set.
  49. if((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0))
  50. if(!hack_setupWriteDirectory())
  51. throw love::Exception("Could not set write directory.");
  52. // File already open?
  53. if(file != 0)
  54. return false;
  55. this->mode = mode;
  56. switch(mode)
  57. {
  58. case READ:
  59. file = PHYSFS_openRead(filename.c_str());
  60. break;
  61. case APPEND:
  62. file = PHYSFS_openAppend(filename.c_str());
  63. break;
  64. case WRITE:
  65. file = PHYSFS_openWrite(filename.c_str());
  66. break;
  67. }
  68. return (file != 0);
  69. }
  70. bool File::close()
  71. {
  72. if(!PHYSFS_close(file))
  73. return false;
  74. mode = CLOSED;
  75. file = 0;
  76. return true;
  77. }
  78. unsigned int File::getSize()
  79. {
  80. // If the file is closed, open it to
  81. // check the size.
  82. if(file == 0)
  83. {
  84. open(READ);
  85. unsigned int size = (unsigned int)PHYSFS_fileLength(file);
  86. close();
  87. return size;
  88. }
  89. return (unsigned int)PHYSFS_fileLength(file);
  90. }
  91. Data * File::read(int size)
  92. {
  93. bool isOpen = (file != 0);
  94. if(!isOpen)
  95. open(READ);
  96. int max = (int)PHYSFS_fileLength(file);
  97. size = (size == ALL) ? max : size;
  98. size = (size > max) ? max : size;
  99. FileData * fileData = new FileData(size, getFilename());
  100. read(fileData->getData(), size);
  101. if(!isOpen)
  102. close();
  103. return fileData;
  104. }
  105. int File::read(void * dst, int size)
  106. {
  107. bool isOpen = (file != 0);
  108. if(!isOpen)
  109. open(READ);
  110. int max = (int)PHYSFS_fileLength(file);
  111. size = (size == ALL) ? max : size;
  112. size = (size > max) ? max : size;
  113. int read = (int)PHYSFS_read(file, dst, 1, size);
  114. if(!isOpen)
  115. close();
  116. return read;
  117. }
  118. bool File::write(const void * data, int size)
  119. {
  120. if(file == 0)
  121. throw love::Exception("Could not write to file. File not open.");
  122. // Try to write.
  123. int written = static_cast<int>(PHYSFS_write(file, data, 1, size));
  124. // Check that correct amount of data was written.
  125. if(written != size)
  126. return false;
  127. return true;
  128. }
  129. bool File::write(const Data * data, int size)
  130. {
  131. return write(data->getData(), (size == ALL) ? data->getSize() : size);
  132. }
  133. bool File::eof()
  134. {
  135. if(file == 0 || PHYSFS_eof(file))
  136. return true;
  137. return false;
  138. }
  139. int File::tell()
  140. {
  141. if(file == 0)
  142. return -1;
  143. return (int)PHYSFS_tell(file);
  144. }
  145. bool File::seek(int pos)
  146. {
  147. if(file == 0)
  148. return false;
  149. if(!PHYSFS_seek(file, (PHYSFS_uint64)pos))
  150. return false;
  151. return true;
  152. }
  153. std::string File::getFilename() const
  154. {
  155. return filename;
  156. }
  157. std::string File::getExtension() const
  158. {
  159. std::string::size_type idx = filename.rfind('.');
  160. if(idx != std::string::npos)
  161. return filename.substr(idx+1);
  162. else
  163. return std::string();
  164. }
  165. filesystem::File::Mode File::getMode()
  166. {
  167. return mode;
  168. }
  169. } // physfs
  170. } // filesystem
  171. } // love