File.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <cstdio>
  29. #include "DebugNew.h"
  30. static const char* openMode[] =
  31. {
  32. "rb",
  33. "wb",
  34. "w+b"
  35. };
  36. OBJECTTYPESTATIC(File);
  37. File::File(Context* context) :
  38. Object(context),
  39. mode_(FILE_READ),
  40. handle_(0),
  41. offset_(0),
  42. checksum_(0)
  43. {
  44. }
  45. File::File(Context* context, const String& fileName, FileMode mode) :
  46. Object(context),
  47. mode_(FILE_READ),
  48. handle_(0),
  49. offset_(0),
  50. checksum_(0)
  51. {
  52. Open(fileName, mode);
  53. }
  54. File::File(Context* context, PackageFile* package, const String& fileName) :
  55. Object(context),
  56. mode_(FILE_READ),
  57. handle_(0),
  58. offset_(0),
  59. checksum_(0)
  60. {
  61. Open(package, fileName);
  62. }
  63. File::~File()
  64. {
  65. Close();
  66. }
  67. bool File::Open(const String& fileName, FileMode mode)
  68. {
  69. Close();
  70. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  71. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  72. {
  73. LOGERROR("Access denied to " + fileName);
  74. return false;
  75. }
  76. handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]);
  77. if (!handle_)
  78. {
  79. LOGERROR("Could not open file " + fileName);
  80. return false;
  81. }
  82. fileName_ = fileName;
  83. mode_ = mode;
  84. position_ = 0;
  85. fseek((FILE*)handle_, 0, SEEK_END);
  86. size_ = ftell((FILE*)handle_);
  87. fseek((FILE*)handle_, 0, SEEK_SET);
  88. offset_ = 0;
  89. checksum_ = 0;
  90. return true;
  91. }
  92. bool File::Open(PackageFile* package, const String& fileName)
  93. {
  94. Close();
  95. if (!package)
  96. return false;
  97. const PackageEntry* entry = package->GetEntry(fileName);
  98. if (!entry)
  99. return false;
  100. handle_ = fopen(GetNativePath(package->GetName()).CString(), "rb");
  101. if (!handle_)
  102. {
  103. LOGERROR("Could not open package file " + fileName);
  104. return false;
  105. }
  106. fileName_ = fileName;
  107. mode_ = FILE_READ;
  108. offset_ = entry->offset_;
  109. checksum_ = entry->checksum_;
  110. position_ = 0;
  111. size_ = entry->size_;
  112. fseek((FILE*)handle_, offset_, SEEK_SET);
  113. return true;
  114. }
  115. unsigned File::Read(void* dest, unsigned size)
  116. {
  117. if (mode_ == FILE_WRITE)
  118. {
  119. LOGERROR("File not opened for reading");
  120. return 0;
  121. }
  122. if (!handle_)
  123. {
  124. LOGERROR("File not open");
  125. return 0;
  126. }
  127. if (size + position_ > size_)
  128. size = size_ - position_;
  129. if (!size)
  130. return 0;
  131. size_t ret = fread(dest, size, 1, (FILE*)handle_);
  132. if (ret != 1)
  133. {
  134. // Return to the position where the read began
  135. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  136. LOGERROR("Error while reading from file");
  137. return 0;
  138. }
  139. position_ += size;
  140. return size;
  141. }
  142. unsigned File::Seek(unsigned position)
  143. {
  144. if (position > size_)
  145. position = size_;
  146. if (!handle_)
  147. {
  148. LOGERROR("File not open");
  149. return 0;
  150. }
  151. fseek((FILE*)handle_, position + offset_, SEEK_SET);
  152. position_ = position;
  153. return position_;
  154. }
  155. unsigned File::Write(const void* data, unsigned size)
  156. {
  157. if (mode_ == FILE_READ)
  158. {
  159. LOGERROR("File not opened for writing");
  160. return 0;
  161. }
  162. if (!handle_)
  163. {
  164. LOGERROR("File not open");
  165. return 0;
  166. }
  167. if (!size)
  168. return 0;
  169. if (fwrite(data, size, 1, (FILE*)handle_) != 1)
  170. {
  171. // Return to the position where the write began
  172. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  173. LOGERROR("Error while writing to file");
  174. return 0;
  175. }
  176. position_ += size;
  177. if (position_ > size_)
  178. size_ = position_;
  179. return size;
  180. }
  181. unsigned File::GetChecksum()
  182. {
  183. if (offset_ || checksum_)
  184. return checksum_;
  185. unsigned oldPos = position_;
  186. checksum_ = 0;
  187. Seek(0);
  188. while (!IsEof())
  189. checksum_ = SDBMHash(checksum_, ReadUByte());
  190. Seek(oldPos);
  191. return checksum_;
  192. }
  193. void File::Close()
  194. {
  195. if (handle_)
  196. {
  197. fclose((FILE*)handle_);
  198. handle_ = 0;
  199. position_ = 0;
  200. size_ = 0;
  201. offset_ = 0;
  202. checksum_ = 0;
  203. }
  204. }
  205. void File::SetName(const String& name)
  206. {
  207. fileName_ = name;
  208. }