File.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "File.h"
  25. #include "FileSystem.h"
  26. #include "Log.h"
  27. #include "PackageFile.h"
  28. #include "StringUtils.h"
  29. #include <cstdio>
  30. #include "DebugNew.h"
  31. static const char* openMode[] =
  32. {
  33. "rb",
  34. "wb",
  35. "w+b"
  36. };
  37. OBJECTTYPESTATIC(File);
  38. File::File(Context* context) :
  39. Object(context),
  40. mode_(FILE_READ),
  41. handle_(0),
  42. offset_(0),
  43. checksum_(0)
  44. {
  45. }
  46. File::File(Context* context, const std::string& fileName, FileMode mode) :
  47. Object(context),
  48. mode_(FILE_READ),
  49. handle_(0),
  50. offset_(0),
  51. checksum_(0)
  52. {
  53. Open(fileName, mode);
  54. }
  55. File::File(Context* context, PackageFile* package, const std::string& fileName) :
  56. Object(context),
  57. mode_(FILE_READ),
  58. handle_(0),
  59. offset_(0),
  60. checksum_(0)
  61. {
  62. Open(package, fileName);
  63. }
  64. File::~File()
  65. {
  66. Close();
  67. }
  68. bool File::Open(const std::string& fileName, FileMode mode)
  69. {
  70. Close();
  71. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  72. if ((fileSystem) && (!fileSystem->CheckAccess(GetPath(fileName))))
  73. {
  74. LOGERROR("Access denied to " + fileName);
  75. return false;
  76. }
  77. handle_ = fopen(GetNativePath(fileName).c_str(), openMode[mode]);
  78. if (!handle_)
  79. {
  80. LOGERROR("Could not open file " + fileName);
  81. return false;
  82. }
  83. fileName_ = fileName;
  84. mode_ = mode;
  85. position_ = 0;
  86. fseek((FILE*)handle_, 0, SEEK_END);
  87. size_ = ftell((FILE*)handle_);
  88. fseek((FILE*)handle_, 0, SEEK_SET);
  89. offset_ = 0;
  90. checksum_ = 0;
  91. return true;
  92. }
  93. bool File::Open(PackageFile* package, const std::string& fileName)
  94. {
  95. Close();
  96. if (!package)
  97. return false;
  98. const PackageEntry* entry = package->GetEntry(fileName);
  99. if (!entry)
  100. return false;
  101. handle_ = fopen(GetNativePath(package->GetName()).c_str(), "rb");
  102. if (!handle_)
  103. {
  104. LOGERROR("Could not open package file " + fileName);
  105. return false;
  106. }
  107. fileName_ = fileName;
  108. mode_ = FILE_READ;
  109. offset_ = entry->offset_;
  110. checksum_ = entry->checksum_;
  111. position_ = 0;
  112. size_ = entry->size_;
  113. fseek((FILE*)handle_, offset_, SEEK_SET);
  114. return true;
  115. }
  116. unsigned File::Read(void* dest, unsigned size)
  117. {
  118. if (mode_ == FILE_WRITE)
  119. {
  120. LOGERROR("File not opened for reading");
  121. return 0;
  122. }
  123. if (!handle_)
  124. {
  125. LOGERROR("File not open");
  126. return 0;
  127. }
  128. if (size + position_ > size_)
  129. size = size_ - position_;
  130. if (!size)
  131. return 0;
  132. size_t ret = fread(dest, size, 1, (FILE*)handle_);
  133. if (ret != 1)
  134. {
  135. // Return to the position where the read began
  136. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  137. LOGERROR("Error while reading from file");
  138. return 0;
  139. }
  140. position_ += size;
  141. return size;
  142. }
  143. unsigned File::Seek(unsigned position)
  144. {
  145. if (position > size_)
  146. position = size_;
  147. if (!handle_)
  148. {
  149. LOGERROR("File not open");
  150. return 0;
  151. }
  152. fseek((FILE*)handle_, position + offset_, SEEK_SET);
  153. position_ = position;
  154. return position_;
  155. }
  156. unsigned File::Write(const void* data, unsigned size)
  157. {
  158. if (mode_ == FILE_READ)
  159. {
  160. LOGERROR("File not opened for writing");
  161. return 0;
  162. }
  163. if (!handle_)
  164. {
  165. LOGERROR("File not open");
  166. return 0;
  167. }
  168. if (!size)
  169. return 0;
  170. if (fwrite(data, size, 1, (FILE*)handle_) != 1)
  171. {
  172. // Return to the position where the write began
  173. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  174. LOGERROR("Error while writing to file");
  175. return 0;
  176. }
  177. position_ += size;
  178. if (position_ > size_)
  179. size_ = position_;
  180. return size;
  181. }
  182. unsigned File::GetChecksum()
  183. {
  184. if ((offset_) || (checksum_))
  185. return checksum_;
  186. unsigned oldPos = position_;
  187. checksum_ = 0;
  188. Seek(0);
  189. while (!IsEof())
  190. checksum_ = SDBMHash(checksum_, ReadUByte());
  191. Seek(oldPos);
  192. return checksum_;
  193. }
  194. void File::Close()
  195. {
  196. if (handle_)
  197. {
  198. fclose((FILE*)handle_);
  199. handle_ = 0;
  200. position_ = 0;
  201. size_ = 0;
  202. offset_ = 0;
  203. checksum_ = 0;
  204. }
  205. }
  206. void File::SetName(const std::string& name)
  207. {
  208. fileName_ = name;
  209. }