File.cpp 6.3 KB

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