FileStream.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * FileStream.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "FileStream.h"
  12. #ifdef USE_SYSTEM_MINIZIP
  13. #include <minizip/unzip.h>
  14. #else
  15. #include "../minizip/unzip.h"
  16. #endif
  17. #include <cstdio>
  18. ///copied from ioapi.c due to linker issues on MSVS
  19. #include "../minizip/ioapi.h"
  20. #if defined(__APPLE__) || defined(IOAPI_NO_64)
  21. // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
  22. #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
  23. #define FTELLO_FUNC(stream) ftello(stream)
  24. #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
  25. #else
  26. #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
  27. #define FTELLO_FUNC(stream) ftello64(stream)
  28. #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
  29. #endif
  30. voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
  31. {
  32. if (pfilefunc->zfile_func64.zopen64_file != NULL)
  33. return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
  34. else
  35. {
  36. return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
  37. }
  38. }
  39. long call_zseek64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, ZPOS64_T offset, int origin)
  40. {
  41. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  42. return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque, filestream, offset, origin);
  43. else
  44. {
  45. uLong offsetTruncated = (uLong)offset;
  46. if (offsetTruncated != offset)
  47. return -1;
  48. else
  49. return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque, filestream, offsetTruncated, origin);
  50. }
  51. }
  52. ZPOS64_T call_ztell64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream)
  53. {
  54. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  55. return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque, filestream);
  56. else
  57. {
  58. uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque, filestream);
  59. if ((tell_uLong) == MAXU32)
  60. return (ZPOS64_T)-1;
  61. else
  62. return tell_uLong;
  63. }
  64. }
  65. static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  66. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  67. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  68. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  69. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  70. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  71. static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode)
  72. {
  73. FILE* file = NULL;
  74. const char* mode_fopen = NULL;
  75. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  76. mode_fopen = "rb";
  77. else
  78. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  79. mode_fopen = "r+b";
  80. else
  81. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  82. mode_fopen = "wb";
  83. if ((filename != NULL) && (mode_fopen != NULL))
  84. file = FOPEN_FUNC((const char*)filename, mode_fopen);
  85. return file;
  86. }
  87. static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size)
  88. {
  89. uLong ret;
  90. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  91. return ret;
  92. }
  93. static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size)
  94. {
  95. uLong ret;
  96. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  97. return ret;
  98. }
  99. static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream)
  100. {
  101. ZPOS64_T ret;
  102. ret = FTELLO_FUNC((FILE *)stream);
  103. return ret;
  104. }
  105. static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  106. {
  107. int fseek_origin = 0;
  108. long ret;
  109. switch (origin)
  110. {
  111. case ZLIB_FILEFUNC_SEEK_CUR:
  112. fseek_origin = SEEK_CUR;
  113. break;
  114. case ZLIB_FILEFUNC_SEEK_END:
  115. fseek_origin = SEEK_END;
  116. break;
  117. case ZLIB_FILEFUNC_SEEK_SET:
  118. fseek_origin = SEEK_SET;
  119. break;
  120. default: return -1;
  121. }
  122. ret = 0;
  123. if (FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
  124. ret = -1;
  125. return ret;
  126. }
  127. static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream)
  128. {
  129. int ret;
  130. ret = fclose((FILE *)stream);
  131. return ret;
  132. }
  133. static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream)
  134. {
  135. int ret;
  136. ret = ferror((FILE *)stream);
  137. return ret;
  138. }
  139. ///end of ioapi.c
  140. //extern MINIZIP_EXPORT void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def);
  141. #ifdef VCMI_WINDOWS
  142. #ifndef _CRT_SECURE_NO_WARNINGS
  143. #define _CRT_SECURE_NO_WARNINGS
  144. #endif
  145. #include <cwchar>
  146. #define CHAR_LITERAL(s) L##s
  147. using CharType = wchar_t;
  148. #else
  149. #define CHAR_LITERAL(s) s
  150. using CharType = char;
  151. #endif
  152. inline FILE* do_open(const CharType* name, const CharType* mode)
  153. {
  154. #ifdef VCMI_WINDOWS
  155. return _wfopen(name, mode);
  156. #else
  157. return std::fopen(name, mode);
  158. #endif
  159. }
  160. #define GETFILE static_cast<std::FILE*>(filePtr)
  161. voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
  162. {
  163. const CharType* mode_fopen = [mode]() -> const CharType*
  164. {
  165. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  166. return CHAR_LITERAL("rb");
  167. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  168. return CHAR_LITERAL("r+b");
  169. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  170. return CHAR_LITERAL("wb");
  171. return nullptr;
  172. }();
  173. if (filename != nullptr && mode_fopen != nullptr)
  174. return do_open(static_cast<const CharType*>(filename), mode_fopen);
  175. else
  176. return nullptr;
  177. }
  178. void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def)
  179. {
  180. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  181. pzlib_filefunc_def->zread_file = fread_file_func;
  182. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  183. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  184. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  185. pzlib_filefunc_def->zclose_file = fclose_file_func;
  186. pzlib_filefunc_def->zerror_file = ferror_file_func;
  187. pzlib_filefunc_def->opaque = NULL;
  188. }
  189. template struct boost::iostreams::stream<VCMI_LIB_WRAP_NAMESPACE(FileBuf)>;
  190. VCMI_LIB_NAMESPACE_BEGIN
  191. zlib_filefunc64_def* FileStream::GetMinizipFilefunc()
  192. {
  193. static zlib_filefunc64_def MinizipFilefunc;
  194. static bool initialized = false;
  195. if (!initialized)
  196. {
  197. fill_fopen64_filefunc(&MinizipFilefunc);
  198. MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
  199. initialized = true;
  200. }
  201. return &MinizipFilefunc;
  202. }
  203. /*static*/
  204. bool FileStream::CreateFile(const boost::filesystem::path& filename)
  205. {
  206. FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
  207. bool result = (f != nullptr);
  208. if(result)
  209. fclose(f);
  210. return result;
  211. }
  212. FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
  213. {
  214. auto openmode = [mode]() -> std::basic_string<CharType>
  215. {
  216. using namespace std;
  217. switch (mode & (~ios_base::ate & ~ios_base::binary))
  218. {
  219. case (ios_base::in):
  220. return CHAR_LITERAL("r");
  221. case (ios_base::out):
  222. case (ios_base::out | ios_base::trunc):
  223. return CHAR_LITERAL("w");
  224. case (ios_base::app):
  225. case (ios_base::out | ios_base::app):
  226. return CHAR_LITERAL("a");
  227. case (ios_base::out | ios_base::in):
  228. return CHAR_LITERAL("r+");
  229. case (ios_base::out | ios_base::in | ios_base::trunc):
  230. return CHAR_LITERAL("w+");
  231. case (ios_base::out | ios_base::in | ios_base::app):
  232. case (ios_base::in | ios_base::app):
  233. return CHAR_LITERAL("a+");
  234. default:
  235. throw std::ios_base::failure("invalid open mode");
  236. }
  237. }();
  238. if (mode & std::ios_base::binary)
  239. openmode += CHAR_LITERAL('b');
  240. filePtr = do_open(filename.c_str(), openmode.c_str());
  241. if (filePtr == nullptr)
  242. throw std::ios_base::failure("could not open file");
  243. if (mode & std::ios_base::ate) {
  244. if (std::fseek(GETFILE, 0, SEEK_END)) {
  245. fclose(GETFILE);
  246. throw std::ios_base::failure("could not open file");
  247. }
  248. }
  249. }
  250. void FileBuf::close()
  251. {
  252. std::fclose(GETFILE);
  253. }
  254. std::streamsize FileBuf::read(char* s, std::streamsize n)
  255. {
  256. return static_cast<std::streamsize>(std::fread(s, 1, n, GETFILE));
  257. }
  258. std::streamsize FileBuf::write(const char* s, std::streamsize n)
  259. {
  260. return static_cast<std::streamsize>(std::fwrite(s, 1, n, GETFILE));
  261. }
  262. std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
  263. {
  264. const auto src = [way]() -> int
  265. {
  266. switch(way)
  267. {
  268. case std::ios_base::beg:
  269. return SEEK_SET;
  270. case std::ios_base::cur:
  271. return SEEK_CUR;
  272. case std::ios_base::end:
  273. return SEEK_END;
  274. default:
  275. throw std::ios_base::failure("bad seek direction");
  276. }
  277. }();
  278. if(std::fseek(GETFILE, (long)off, src))
  279. throw std::ios_base::failure("bad seek offset");
  280. return static_cast<std::streamsize>(std::ftell(GETFILE));
  281. }
  282. VCMI_LIB_NAMESPACE_END