Просмотр исходного кода

Removed boost::iostreams in favor of std::stream / boost::filesystem

Ivan Savenko 2 лет назад
Родитель
Сommit
21a39f0b01

+ 0 - 2
cmake_modules/VCMI_lib.cmake

@@ -60,7 +60,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
 		${MAIN_LIB_DIR}/filesystem/CZipLoader.cpp
 		${MAIN_LIB_DIR}/filesystem/CZipSaver.cpp
 		${MAIN_LIB_DIR}/filesystem/FileInfo.cpp
-		${MAIN_LIB_DIR}/filesystem/FileStream.cpp
 		${MAIN_LIB_DIR}/filesystem/Filesystem.cpp
 		${MAIN_LIB_DIR}/filesystem/MinizipExtensions.cpp
 		${MAIN_LIB_DIR}/filesystem/ResourceID.cpp
@@ -384,7 +383,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
 		${MAIN_LIB_DIR}/filesystem/CZipLoader.h
 		${MAIN_LIB_DIR}/filesystem/CZipSaver.h
 		${MAIN_LIB_DIR}/filesystem/FileInfo.h
-		${MAIN_LIB_DIR}/filesystem/FileStream.h
 		${MAIN_LIB_DIR}/filesystem/Filesystem.h
 		${MAIN_LIB_DIR}/filesystem/ISimpleResourceLoader.h
 		${MAIN_LIB_DIR}/filesystem/MinizipExtensions.h

+ 1 - 2
launcher/jsonutils.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "jsonutils.h"
-#include "../lib/filesystem/FileStream.h"
 
 static QVariantMap JsonToMap(const JsonMap & json)
 {
@@ -114,7 +113,7 @@ JsonNode toJson(QVariant object)
 
 void JsonToFile(QString filename, QVariant object)
 {
-	FileStream file(qstringToPath(filename), std::ios::out | std::ios_base::binary);
+	boost::filesystem::fstream file(qstringToPath(filename), std::ios::out | std::ios_base::binary);
 	file << toJson(object).toJson();
 }
 

+ 1 - 2
lib/CConfigHandler.cpp

@@ -11,7 +11,6 @@
 #include "CConfigHandler.h"
 
 #include "../lib/filesystem/Filesystem.h"
-#include "../lib/filesystem/FileStream.h"
 #include "../lib/GameConstants.h"
 #include "../lib/VCMIDirs.h"
 
@@ -77,7 +76,7 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
 	savedConf.Struct().erase("session");
 	JsonUtils::minimize(savedConf, "vcmi:settings");
 
-	FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
+	boost::filesystem::fstream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
 	file << savedConf.toJson();
 }
 

+ 1 - 2
lib/CModHandler.cpp

@@ -10,7 +10,6 @@
 #include "StdInc.h"
 #include "CModHandler.h"
 #include "rmg/CRmgTemplateStorage.h"
-#include "filesystem/FileStream.h"
 #include "filesystem/AdapterLoaders.h"
 #include "filesystem/CFilesystemLoader.h"
 #include "filesystem/Filesystem.h"
@@ -1158,7 +1157,7 @@ void CModHandler::afterLoad(bool onlyEssential)
 
 	if(!onlyEssential)
 	{
-		FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
+		boost::filesystem::fstream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
 		file << modSettings.toJson();
 	}
 

+ 1 - 2
lib/filesystem/CFileInputStream.h

@@ -10,7 +10,6 @@
 #pragma once
 
 #include "CInputStream.h"
-#include "FileStream.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -75,7 +74,7 @@ private:
 	si64 dataSize;
 
 	/** Native c++ input file stream object. */
-	FileStream fileStream;
+	boost::filesystem::fstream fileStream;
 };
 
 VCMI_LIB_NAMESPACE_END

+ 4 - 2
lib/filesystem/CFilesystemLoader.cpp

@@ -11,7 +11,6 @@
 #include "CFilesystemLoader.h"
 
 #include "CFileInputStream.h"
-#include "FileStream.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -88,7 +87,10 @@ bool CFilesystemLoader::createResource(std::string filename, bool update)
 
 	if (!update)
 	{
-		if (!FileStream::createFile(baseDirectory / filename))
+		// create file, if not exists
+		boost::filesystem::fstream file(baseDirectory / filename);
+
+		if (!file.is_open())
 			return false;
 	}
 	fileList[resID] = filename;

+ 9 - 4
lib/filesystem/CZipLoader.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "CZipLoader.h"
-#include "FileStream.h"
 
 #include "../ScopeGuard.h"
 
@@ -155,7 +154,10 @@ std::vector<std::string> ZipArchive::listFiles(const boost::filesystem::path & f
 {
 	std::vector<std::string> ret;
 
-	unzFile file = unzOpen2_64(filename.c_str(), FileStream::GetMinizipFilefunc());
+	CDefaultIOApi zipAPI;
+	auto zipStructure = zipAPI.getApiStructure();
+
+	unzFile file = unzOpen2_64(filename.c_str(), &zipStructure);
 
 	if (unzGoToFirstFile(file) == UNZ_OK)
 	{
@@ -188,7 +190,10 @@ bool ZipArchive::extract(const boost::filesystem::path & from, const boost::file
 
 bool ZipArchive::extract(const boost::filesystem::path & from, const boost::filesystem::path & where, const std::vector<std::string> & what)
 {
-	unzFile archive = unzOpen2_64(from.c_str(), FileStream::GetMinizipFilefunc());
+	CDefaultIOApi zipAPI;
+	auto zipStructure = zipAPI.getApiStructure();
+
+	unzFile archive = unzOpen2_64(from.c_str(), &zipStructure);
 
 	auto onExit = vstd::makeScopeGuard([&]()
 	{
@@ -209,7 +214,7 @@ bool ZipArchive::extract(const boost::filesystem::path & from, const boost::file
 		if (boost::algorithm::ends_with(file, "/"))
 			continue;
 
-		FileStream destFile(fullName, std::ios::out | std::ios::binary);
+		boost::filesystem::fstream destFile(fullName, std::ios::out | std::ios::binary);
 		if (!destFile.good())
 			return false;
 

+ 0 - 175
lib/filesystem/FileStream.cpp

@@ -1,175 +0,0 @@
-/*
- * FileStream.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
-#include "StdInc.h"
-#include "FileStream.h"
-
-#ifdef USE_SYSTEM_MINIZIP
-#include <minizip/unzip.h>
-#include <minizip/ioapi.h>
-#else
-#include "../minizip/unzip.h"
-#include "../minizip/ioapi.h"
-#endif
-
-#include <cstdio>
-
-#define GETFILE static_cast<std::FILE*>(filePtr)
-
-#ifdef VCMI_WINDOWS
-	#ifndef _CRT_SECURE_NO_WARNINGS
-		#define _CRT_SECURE_NO_WARNINGS
-	#endif
-	#include <cwchar>
-	#define CHAR_LITERAL(s) L##s
-	using CharType = wchar_t;
-#else
-	#define CHAR_LITERAL(s) s
-	using CharType = char;
-#endif
-
-namespace
-{
-inline FILE* do_open(const CharType* name, const CharType* mode)
-{
-	#ifdef VCMI_WINDOWS
-		return _wfopen(name, mode);
-	#else
-		return std::fopen(name, mode);
-	#endif
-}
-
-voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
-{
-	const CharType* mode_fopen = [mode]() -> const CharType*
-	{
-		if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
-			return CHAR_LITERAL("rb");
-		else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
-			return CHAR_LITERAL("r+b");
-		else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
-			return CHAR_LITERAL("wb");
-		return nullptr;
-	}();
-
-	if (filename != nullptr && mode_fopen != nullptr)
-		return do_open(static_cast<const CharType*>(filename), mode_fopen);
-	else
-		return nullptr;
-}
-} // namespace
-
-template struct boost::iostreams::stream<VCMI_LIB_WRAP_NAMESPACE(FileBuf)>;
-
-VCMI_LIB_NAMESPACE_BEGIN
-
-zlib_filefunc64_def* FileStream::GetMinizipFilefunc()
-{
-	static zlib_filefunc64_def MinizipFilefunc;
-	static bool initialized = false;
-	if (!initialized)
-	{
-		fill_fopen64_filefunc(&MinizipFilefunc);
-		MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
-		initialized = true;
-	}
-	return &MinizipFilefunc;
-}
-
-/*static*/
-bool FileStream::createFile(const boost::filesystem::path& filename)
-{
-	FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
-	bool result = (f != nullptr);
-	if(result)
-		fclose(f);
-	return result;
-}
-
-FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
-{
-	auto openmode = [mode]() -> std::basic_string<CharType>
-	{
-		using namespace std;
-		switch (mode & (~ios_base::ate & ~ios_base::binary))
-		{
-		case (ios_base::in):
-			return CHAR_LITERAL("r");
-		case (ios_base::out):
-		case (ios_base::out | ios_base::trunc):
-			return CHAR_LITERAL("w");
-		case (ios_base::app):
-		case (ios_base::out | ios_base::app):
-			return CHAR_LITERAL("a");
-		case (ios_base::out | ios_base::in):
-			return CHAR_LITERAL("r+");
-		case (ios_base::out | ios_base::in | ios_base::trunc):
-			return CHAR_LITERAL("w+");
-		case (ios_base::out | ios_base::in | ios_base::app):
-		case (ios_base::in | ios_base::app):
-			return CHAR_LITERAL("a+");
-		default:
-			throw std::ios_base::failure("invalid open mode");
-		}
-	}();
-
-	if (mode & std::ios_base::binary)
-		openmode += CHAR_LITERAL('b');
-
-	filePtr = do_open(filename.c_str(), openmode.c_str());
-
-	if (filePtr == nullptr)
-		throw std::ios_base::failure("could not open file");
-
-	if (mode & std::ios_base::ate) {
-		if (std::fseek(GETFILE, 0, SEEK_END)) {
-			fclose(GETFILE);
-			throw std::ios_base::failure("could not open file");
-		}
-	}
-}
-
-void FileBuf::close()
-{
-    std::fclose(GETFILE);
-}
-
-std::streamsize FileBuf::read(char* s, std::streamsize n)
-{
-	return static_cast<std::streamsize>(std::fread(s, 1, n, GETFILE));
-}
-
-std::streamsize FileBuf::write(const char* s, std::streamsize n)
-{
-	return static_cast<std::streamsize>(std::fwrite(s, 1, n, GETFILE));
-}
-
-std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
-{
-	const auto src = [way]() -> int
-	{
-		switch(way)
-		{
-		case std::ios_base::beg:
-			return SEEK_SET;
-		case std::ios_base::cur:
-			return SEEK_CUR;
-		case std::ios_base::end:
-			return SEEK_END;
-		default:
-			throw std::ios_base::failure("bad seek direction");
-		}
-	}();
-	if(std::fseek(GETFILE, static_cast<long>(off), src))
-		throw std::ios_base::failure("bad seek offset");
-
-	return static_cast<std::streamsize>(std::ftell(GETFILE));
-}
-
-VCMI_LIB_NAMESPACE_END

+ 0 - 67
lib/filesystem/FileStream.h

@@ -1,67 +0,0 @@
-/*
- * FileStream.h, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
-#pragma once
-
-#include <boost/iostreams/categories.hpp>
-#include <boost/iostreams/stream.hpp>
-
-VCMI_LIB_NAMESPACE_BEGIN
-
-class DLL_LINKAGE FileBuf
-{
-public:
-	using char_type = char;
-	using category = struct category_ :
-		boost::iostreams::seekable_device_tag,
-		boost::iostreams::closable_tag
-		{};
-
-	FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode);
-
-	std::streamsize read(char* s, std::streamsize n);
-	std::streamsize write(const char* s, std::streamsize n);
-	std::streamoff  seek(std::streamoff off, std::ios_base::seekdir way);
-
-	void close();
-private:
-	void* filePtr;
-};
-
-VCMI_LIB_NAMESPACE_END
-
-struct zlib_filefunc64_def_s;
-using zlib_filefunc64_def = zlib_filefunc64_def_s;
-
-#ifdef VCMI_DLL
-#ifdef _MSC_VER
-#pragma warning (push)
-#pragma warning (disable : 4910)
-#endif
-extern template struct DLL_LINKAGE boost::iostreams::stream<VCMI_LIB_WRAP_NAMESPACE(FileBuf)>;
-#ifdef _MSC_VER
-#pragma warning (pop)
-#endif
-#endif
-
-VCMI_LIB_NAMESPACE_BEGIN
-
-class DLL_LINKAGE FileStream : public boost::iostreams::stream<FileBuf>
-{
-public:
-	FileStream() = default;
-	explicit FileStream(const boost::filesystem::path& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
-		: boost::iostreams::stream<FileBuf>(p, mode) {}
-
-	static bool createFile(const boost::filesystem::path& filename);
-
-	static zlib_filefunc64_def* GetMinizipFilefunc();
-};
-
-VCMI_LIB_NAMESPACE_END

+ 50 - 2
lib/filesystem/MinizipExtensions.cpp

@@ -11,7 +11,6 @@
 #include "MinizipExtensions.h"
 
 #include "CMemoryBuffer.h"
-#include "FileStream.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -86,10 +85,59 @@ inline int streamProxyClose(voidpf opaque, voidpf stream)
 }
 
 ///CDefaultIOApi
+#define GETFILE static_cast<std::FILE*>(filePtr)
+
+#ifdef VCMI_WINDOWS
+	#ifndef _CRT_SECURE_NO_WARNINGS
+		#define _CRT_SECURE_NO_WARNINGS
+	#endif
+	#include <cwchar>
+	#define CHAR_LITERAL(s) L##s
+	using CharType = wchar_t;
+#else
+	#define CHAR_LITERAL(s) s
+	using CharType = char;
+#endif
+
+static inline FILE* do_open(const CharType* name, const CharType* mode)
+{
+	#ifdef VCMI_WINDOWS
+		return _wfopen(name, mode);
+	#else
+		return std::fopen(name, mode);
+	#endif
+}
+
+static voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
+{
+	const CharType* mode_fopen = [mode]() -> const CharType*
+	{
+		if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
+			return CHAR_LITERAL("rb");
+		else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
+			return CHAR_LITERAL("r+b");
+		else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
+			return CHAR_LITERAL("wb");
+		return nullptr;
+	}();
+
+	if (filename != nullptr && mode_fopen != nullptr)
+		return do_open(static_cast<const CharType*>(filename), mode_fopen);
+	else
+		return nullptr;
+}
 
 zlib_filefunc64_def CDefaultIOApi::getApiStructure()
 {
-	return * FileStream::GetMinizipFilefunc();
+	static zlib_filefunc64_def MinizipFilefunc;
+	static bool initialized = false;
+	if (!initialized)
+	{
+		fill_fopen64_filefunc(&MinizipFilefunc);
+		MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
+		initialized = true;
+	}
+	return MinizipFilefunc;
 }
 
 ///CProxyIOApi

+ 0 - 1
lib/logging/CLogger.h

@@ -10,7 +10,6 @@
 #pragma once
 
 #include "../CConsoleHandler.h"
-#include "../filesystem/FileStream.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 

+ 1 - 2
lib/serializer/BinaryDeserializer.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "BinaryDeserializer.h"
-#include "../filesystem/FileStream.h"
 
 #include "../registerTypes/RegisterTypes.h"
 
@@ -41,7 +40,7 @@ void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalV
 	try
 	{
 		fName = fname.string();
-		sfile = std::make_unique<FileStream>(fname, std::ios::in | std::ios::binary);
+		sfile = std::make_unique<boost::filesystem::fstream>(fname, std::ios::in | std::ios::binary);
 		sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
 
 		if(!(*sfile))

+ 1 - 2
lib/serializer/BinaryDeserializer.h

@@ -19,7 +19,6 @@
 VCMI_LIB_NAMESPACE_BEGIN
 
 class CStackInstance;
-class FileStream;
 
 class DLL_LINKAGE CLoaderBase
 {
@@ -581,7 +580,7 @@ public:
 	BinaryDeserializer serializer;
 
 	std::string fName;
-	std::unique_ptr<FileStream> sfile;
+	std::unique_ptr<boost::filesystem::fstream> sfile;
 
 	CLoadFile(const boost::filesystem::path & fname, int minimalVersion = SERIALIZATION_VERSION); //throws!
 	virtual ~CLoadFile();

+ 1 - 2
lib/serializer/BinarySerializer.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "BinarySerializer.h"
-#include "../filesystem/FileStream.h"
 
 #include "../registerTypes/RegisterTypes.h"
 
@@ -38,7 +37,7 @@ void CSaveFile::openNextFile(const boost::filesystem::path &fname)
 	fName = fname;
 	try
 	{
-		sfile = std::make_unique<FileStream>(fname, std::ios::out | std::ios::binary);
+		sfile = std::make_unique<boost::filesystem::fstream>(fname, std::ios::out | std::ios::binary);
 		sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
 
 		if(!(*sfile))

+ 1 - 3
lib/serializer/BinarySerializer.h

@@ -14,8 +14,6 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-class FileStream;
-
 class DLL_LINKAGE CSaverBase
 {
 protected:
@@ -392,7 +390,7 @@ public:
 	BinarySerializer serializer;
 
 	boost::filesystem::path fName;
-	std::unique_ptr<FileStream> sfile;
+	std::unique_ptr<boost::filesystem::fstream> sfile;
 
 	CSaveFile(const boost::filesystem::path &fname); //throws!
 	~CSaveFile();

+ 0 - 1
lib/serializer/CLoadIntegrityValidator.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "CLoadIntegrityValidator.h"
-#include "../filesystem/FileStream.h"
 
 #include "../registerTypes/RegisterTypes.h"
 

+ 1 - 2
mapeditor/jsonutils.cpp

@@ -9,7 +9,6 @@
  */
 #include "StdInc.h"
 #include "jsonutils.h"
-#include "../lib/filesystem/FileStream.h"
 
 static QVariantMap JsonToMap(const JsonMap & json)
 {
@@ -120,7 +119,7 @@ JsonNode toJson(QVariant object)
 
 void JsonToFile(QString filename, QVariant object)
 {
-	FileStream file(qstringToPath(filename), std::ios::out | std::ios_base::binary);
+	boost::filesystem::fstream file(qstringToPath(filename), std::ios::out | std::ios_base::binary);
 	file << toJson(object).toJson();
 }