File.cpp 3.9 KB

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