FileStream.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <minizip/ioapi.h>
  15. #else
  16. #include "../minizip/unzip.h"
  17. #include "../minizip/ioapi.h"
  18. #endif
  19. #include <cstdio>
  20. #define GETFILE static_cast<std::FILE*>(filePtr)
  21. #ifdef VCMI_WINDOWS
  22. #ifndef _CRT_SECURE_NO_WARNINGS
  23. #define _CRT_SECURE_NO_WARNINGS
  24. #endif
  25. #include <cwchar>
  26. #define CHAR_LITERAL(s) L##s
  27. using CharType = wchar_t;
  28. #else
  29. #define CHAR_LITERAL(s) s
  30. using CharType = char;
  31. #endif
  32. namespace
  33. {
  34. inline FILE* do_open(const CharType* name, const CharType* mode)
  35. {
  36. #ifdef VCMI_WINDOWS
  37. return _wfopen(name, mode);
  38. #else
  39. return std::fopen(name, mode);
  40. #endif
  41. }
  42. voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
  43. {
  44. const CharType* mode_fopen = [mode]() -> const CharType*
  45. {
  46. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  47. return CHAR_LITERAL("rb");
  48. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  49. return CHAR_LITERAL("r+b");
  50. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  51. return CHAR_LITERAL("wb");
  52. return nullptr;
  53. }();
  54. if (filename != nullptr && mode_fopen != nullptr)
  55. return do_open(static_cast<const CharType*>(filename), mode_fopen);
  56. else
  57. return nullptr;
  58. }
  59. } // namespace
  60. template struct boost::iostreams::stream<VCMI_LIB_WRAP_NAMESPACE(FileBuf)>;
  61. VCMI_LIB_NAMESPACE_BEGIN
  62. zlib_filefunc64_def* FileStream::GetMinizipFilefunc()
  63. {
  64. static zlib_filefunc64_def MinizipFilefunc;
  65. static bool initialized = false;
  66. if (!initialized)
  67. {
  68. fill_fopen64_filefunc(&MinizipFilefunc);
  69. MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
  70. initialized = true;
  71. }
  72. return &MinizipFilefunc;
  73. }
  74. /*static*/
  75. bool FileStream::createFile(const boost::filesystem::path& filename)
  76. {
  77. FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
  78. bool result = (f != nullptr);
  79. if(result)
  80. fclose(f);
  81. return result;
  82. }
  83. FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
  84. {
  85. auto openmode = [mode]() -> std::basic_string<CharType>
  86. {
  87. using namespace std;
  88. switch (mode & (~ios_base::ate & ~ios_base::binary))
  89. {
  90. case (ios_base::in):
  91. return CHAR_LITERAL("r");
  92. case (ios_base::out):
  93. case (ios_base::out | ios_base::trunc):
  94. return CHAR_LITERAL("w");
  95. case (ios_base::app):
  96. case (ios_base::out | ios_base::app):
  97. return CHAR_LITERAL("a");
  98. case (ios_base::out | ios_base::in):
  99. return CHAR_LITERAL("r+");
  100. case (ios_base::out | ios_base::in | ios_base::trunc):
  101. return CHAR_LITERAL("w+");
  102. case (ios_base::out | ios_base::in | ios_base::app):
  103. case (ios_base::in | ios_base::app):
  104. return CHAR_LITERAL("a+");
  105. default:
  106. throw std::ios_base::failure("invalid open mode");
  107. }
  108. }();
  109. if (mode & std::ios_base::binary)
  110. openmode += CHAR_LITERAL('b');
  111. filePtr = do_open(filename.c_str(), openmode.c_str());
  112. if (filePtr == nullptr)
  113. throw std::ios_base::failure("could not open file");
  114. if (mode & std::ios_base::ate) {
  115. if (std::fseek(GETFILE, 0, SEEK_END)) {
  116. fclose(GETFILE);
  117. throw std::ios_base::failure("could not open file");
  118. }
  119. }
  120. }
  121. void FileBuf::close()
  122. {
  123. std::fclose(GETFILE);
  124. }
  125. std::streamsize FileBuf::read(char* s, std::streamsize n)
  126. {
  127. return static_cast<std::streamsize>(std::fread(s, 1, n, GETFILE));
  128. }
  129. std::streamsize FileBuf::write(const char* s, std::streamsize n)
  130. {
  131. return static_cast<std::streamsize>(std::fwrite(s, 1, n, GETFILE));
  132. }
  133. std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
  134. {
  135. const auto src = [way]() -> int
  136. {
  137. switch(way)
  138. {
  139. case std::ios_base::beg:
  140. return SEEK_SET;
  141. case std::ios_base::cur:
  142. return SEEK_CUR;
  143. case std::ios_base::end:
  144. return SEEK_END;
  145. default:
  146. throw std::ios_base::failure("bad seek direction");
  147. }
  148. }();
  149. if(std::fseek(GETFILE, static_cast<long>(off), src))
  150. throw std::ios_base::failure("bad seek offset");
  151. return static_cast<std::streamsize>(std::ftell(GETFILE));
  152. }
  153. VCMI_LIB_NAMESPACE_END