1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

replaced std::string with boost::filesystem::path in several places

This commit is contained in:
Zyx-2000 2016-01-16 16:36:16 +01:00
parent 933b7c1f5e
commit cf61837ced
18 changed files with 110 additions and 108 deletions

View File

@ -1362,9 +1362,9 @@ void SelectionTab::select( int position )
if(txt)
{
std::string filename = *CResourceHandler::get("local")->getResourceName(
auto filename = *CResourceHandler::get("local")->getResourceName(
ResourceID(curItems[py]->fileURI, EResType::CLIENT_SAVEGAME));
txt->setText(CFileInfo(filename).getBaseName());
txt->setText(filename.stem());
}
onSelect(curItems[py]);
@ -1487,8 +1487,8 @@ void SelectionTab::printMaps(SDL_Surface *to)
}
else
{
name = CFileInfo(*CResourceHandler::get("local")->getResourceName(
ResourceID(currentItem->fileURI, EResType::CLIENT_SAVEGAME))).getBaseName();
name = CResourceHandler::get("local")->getResourceName(
ResourceID(currentItem->fileURI, EResType::CLIENT_SAVEGAME))->stem().string();
}
//print name

View File

@ -267,8 +267,8 @@ void CClient::loadGame(const std::string & fname, const bool server, const std::
std::unique_ptr<CLoadFile> loader;
try
{
std::string clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME));
std::string controlServerSaveName;
boost::filesystem::path clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME));
boost::filesystem::path controlServerSaveName;
if (CResourceHandler::get("local")->existsResource(ResourceID(fname, EResType::SERVER_SAVEGAME)))
{
@ -276,7 +276,7 @@ void CClient::loadGame(const std::string & fname, const bool server, const std::
}
else// create entry for server savegame. Triggered if save was made after launch and not yet present in res handler
{
controlServerSaveName = clientSaveName.substr(0, clientSaveName.find_last_of(".")) + ".vsgm1";
controlServerSaveName = boost::filesystem::path(clientSaveName).replace_extension(".vsgm1");
CResourceHandler::get("local")->createResource(controlServerSaveName, true);
}

View File

@ -24,6 +24,6 @@ inline boost::filesystem::path qstringToPath(const QString & path)
#ifdef VCMI_WINDOWS
return boost::filesystem::path(path.toStdWString());
#else
return boost::filesystem::path(filename.toUtf8().data());
return boost::filesystem::path(path.toUtf8().data());
#endif
}

View File

@ -1,5 +1,6 @@
#pragma once
#include "StdInc.h"
#include "../../lib/CConfigHandler.h"
namespace Ui {

View File

@ -69,8 +69,8 @@ void CModManager::loadMods()
ResourceID resID(CModInfo::getModFile(modname));
if (CResourceHandler::get()->existsResource(resID))
{
std::string name = *CResourceHandler::get()->getResourceName(resID);
auto mod = JsonUtils::JsonFromFile(QString::fromUtf8(name.c_str()));
boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
auto mod = JsonUtils::JsonFromFile(qstringToPath(name));
localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
}
}
@ -262,7 +262,7 @@ bool CModManager::doUninstallMod(QString modname)
{
ResourceID resID(std::string("Mods/") + modname.toUtf8().data(), EResType::DIRECTORY);
// Get location of the mod, in case-insensitive way
QString modDir = QString::fromUtf8((*CResourceHandler::get()->getResourceName(resID)).c_str());
QString modDir = qstringFromPath(*CResourceHandler::get()->getResourceName(resID));
if (!QDir(modDir).exists())
return addError(modname, "Data with this mod was not found");

View File

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

View File

@ -2,6 +2,7 @@
#include "CModHandler.h"
#include "mapObjects/CObjectClassesHandler.h"
#include "JsonNode.h"
#include "filesystem/FileStream.h"
#include "filesystem/Filesystem.h"
#include "filesystem/AdapterLoaders.h"
#include "filesystem/CFilesystemLoader.h"
@ -880,6 +881,6 @@ void CModHandler::afterLoad()
}
modSettings["core"] = coreMod.saveLocalData();
std::ofstream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::trunc);
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
file << modSettings;
}

View File

@ -4,6 +4,7 @@
#include "registerTypes/RegisterTypes.h"
#include "mapping/CMap.h"
#include "CGameState.h"
#include "filesystem/FileStream.h"
#include <boost/asio.hpp>
@ -282,7 +283,7 @@ void CConnection::enableSmartVectorMemberSerializatoin()
CSerializer::smartVectorMembersSerialization = true;
}
CSaveFile::CSaveFile( const std::string &fname ): serializer(this)
CSaveFile::CSaveFile( const boost::filesystem::path &fname ): serializer(this)
{
registerTypes(serializer);
openNextFile(fname);
@ -298,12 +299,12 @@ int CSaveFile::write( const void * data, unsigned size )
return size;
}
void CSaveFile::openNextFile(const std::string &fname)
void CSaveFile::openNextFile(const boost::filesystem::path &fname)
{
fName = fname;
try
{
sfile = make_unique<std::ofstream>(fname.c_str(), std::ios::binary);
sfile = make_unique<FileStream>(fname, std::ios::out | std::ios::binary);
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
if(!(*sfile))
@ -364,7 +365,7 @@ void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalV
try
{
fName = fname.string();
sfile = make_unique<boost::filesystem::ifstream>(fname, std::ios::binary);
sfile = make_unique<FileStream>(fname, std::ios::in | std::ios::binary);
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
if(!(*sfile))
@ -569,7 +570,7 @@ void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib)
smartVectorMembersSerialization = true;
}
CLoadIntegrityValidator::CLoadIntegrityValidator( const std::string &primaryFileName, const std::string &controlFileName, int minimalVersion /*= version*/ )
CLoadIntegrityValidator::CLoadIntegrityValidator( const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion /*= version*/ )
: serializer(this), foundDesync(false)
{
registerTypes(serializer);

View File

@ -39,6 +39,7 @@ class CGameState;
class CCreature;
class LibClasses;
class CHero;
class FileStream;
struct CPack;
extern DLL_LINKAGE LibClasses * VLC;
namespace mpl = boost::mpl;
@ -1550,14 +1551,14 @@ public:
COSer serializer;
std::string fName;
std::unique_ptr<std::ofstream> sfile;
boost::filesystem::path fName;
std::unique_ptr<FileStream> sfile;
CSaveFile(const std::string &fname); //throws!
CSaveFile(const boost::filesystem::path &fname); //throws!
~CSaveFile();
int write(const void * data, unsigned size) override;
void openNextFile(const std::string &fname); //throws!
void openNextFile(const boost::filesystem::path &fname); //throws!
void clear();
void reportState(CLogger * out) override;
@ -1577,8 +1578,8 @@ class DLL_LINKAGE CLoadFile
public:
CISer serializer;
std::string fName;
std::unique_ptr<boost::filesystem::ifstream> sfile;
boost::filesystem::path fName;
std::unique_ptr<FileStream> sfile;
CLoadFile(const boost::filesystem::path & fname, int minimalVersion = version); //throws!
~CLoadFile();
@ -1606,7 +1607,7 @@ public:
std::unique_ptr<CLoadFile> primaryFile, controlFile;
bool foundDesync;
CLoadIntegrityValidator(const std::string &primaryFileName, const std::string &controlFileName, int minimalVersion = version); //throws!
CLoadIntegrityValidator(const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion = version); //throws!
int read( void * data, unsigned size) override; //throws!
void checkMagicBytes(const std::string &text);

View File

@ -27,7 +27,7 @@ std::string CMappedFileLoader::getMountPoint() const
return ""; // does not have any meaning with this type of data source
}
boost::optional<std::string> CMappedFileLoader::getResourceName(const ResourceID & resourceName) const
boost::optional<boost::filesystem::path> CMappedFileLoader::getResourceName(const ResourceID & resourceName) const
{
return CResourceHandler::get()->getResourceName(fileList.at(resourceName));
}
@ -80,11 +80,11 @@ std::string CFilesystemList::getMountPoint() const
return "";
}
boost::optional<std::string> CFilesystemList::getResourceName(const ResourceID & resourceName) const
boost::optional<boost::filesystem::path> CFilesystemList::getResourceName(const ResourceID & resourceName) const
{
if (existsResource(resourceName))
return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
return boost::optional<std::string>();
return boost::optional<boost::filesystem::path>();
}
std::unordered_set<ResourceID> CFilesystemList::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const

View File

@ -41,7 +41,7 @@ public:
std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
bool existsResource(const ResourceID & resourceName) const override;
std::string getMountPoint() const override;
boost::optional<std::string> getResourceName(const ResourceID & resourceName) const override;
boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const override;
std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
private:
@ -77,7 +77,7 @@ public:
std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
bool existsResource(const ResourceID & resourceName) const override;
std::string getMountPoint() const override;
boost::optional<std::string> getResourceName(const ResourceID & resourceName) const override;
boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const override;
std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
bool createResource(std::string filename, bool update = false) override;
std::vector<const ISimpleResourceLoader *> getResourcesWithName(const ResourceID & resourceName) const override;

View File

@ -3,6 +3,7 @@
#include "CFileInfo.h"
#include "CFileInputStream.h"
#include "FileStream.h"
namespace bfs = boost::filesystem;
@ -32,11 +33,11 @@ std::string CFilesystemLoader::getMountPoint() const
return mountPoint;
}
boost::optional<std::string> CFilesystemLoader::getResourceName(const ResourceID & resourceName) const
boost::optional<boost::filesystem::path> CFilesystemLoader::getResourceName(const ResourceID & resourceName) const
{
assert(existsResource(resourceName));
return (baseDirectory / fileList.at(resourceName)).string();
return baseDirectory / fileList.at(resourceName);
}
std::unordered_set<ResourceID> CFilesystemLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
@ -68,8 +69,7 @@ bool CFilesystemLoader::createResource(std::string filename, bool update)
if (!update)
{
bfs::ofstream newfile(baseDirectory / filename);
if (!newfile.good())
if (!FileStream::CreateFile(baseDirectory / filename))
return false;
}
fileList[resID] = filename;

View File

@ -38,7 +38,7 @@ public:
bool existsResource(const ResourceID & resourceName) const override;
std::string getMountPoint() const override;
bool createResource(std::string filename, bool update = false) override;
boost::optional<std::string> getResourceName(const ResourceID & resourceName) const override;
boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const override;
std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
private:

View File

@ -15,10 +15,10 @@
*
*/
CZipStream::CZipStream(const std::string & archive, unz_file_pos filepos)
CZipStream::CZipStream(const boost::filesystem::path & archive, unz64_file_pos filepos)
{
file = unzOpen(archive.c_str());
unzGoToFilePos(file, &filepos);
file = unzOpen2_64(archive.c_str(), FileStream::GetMinizipFilefunc());
unzGoToFilePos64(file, &filepos);
unzOpenCurrentFile(file);
}
@ -35,19 +35,19 @@ si64 CZipStream::readMore(ui8 * data, si64 size)
si64 CZipStream::getSize()
{
unz_file_info info;
unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
unz_file_info64 info;
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
return info.uncompressed_size;
}
ui32 CZipStream::calculateCRC32()
{
unz_file_info info;
unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
unz_file_info64 info;
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
return info.crc;
}
CZipLoader::CZipLoader(const std::string & mountPoint, const std::string & archive):
CZipLoader::CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive):
archiveName(archive),
mountPoint(mountPoint),
files(listFiles(mountPoint, archive))
@ -55,27 +55,27 @@ CZipLoader::CZipLoader(const std::string & mountPoint, const std::string & archi
logGlobal->traceStream() << "Zip archive loaded, " << files.size() << " files found";
}
std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const std::string & archive)
std::unordered_map<ResourceID, unz64_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const boost::filesystem::path & archive)
{
std::unordered_map<ResourceID, unz_file_pos> ret;
std::unordered_map<ResourceID, unz64_file_pos> ret;
unzFile file = unzOpen(archive.c_str());
unzFile file = unzOpen2_64(archive.c_str(), FileStream::GetMinizipFilefunc());
if (unzGoToFirstFile(file) == UNZ_OK)
{
do
{
unz_file_info info;
unz_file_info64 info;
std::vector<char> filename;
// Fill unz_file_info structure with current file info
unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
filename.resize(info.size_filename);
// Get name of current file. Contrary to docs "info" parameter can't be null
unzGetCurrentFileInfo (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
unzGetCurrentFileInfo64 (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
std::string filenameString(filename.data(), filename.size());
unzGetFilePos(file, &ret[ResourceID(mountPoint + filenameString)]);
unzGetFilePos64(file, &ret[ResourceID(mountPoint + filenameString)]);
}
while (unzGoToNextFile(file) == UNZ_OK);
}

View File

@ -32,7 +32,7 @@ public:
* @param archive path to archive to open
* @param filepos position of file to open
*/
CZipStream(const std::string & archive, unz_file_pos filepos);
CZipStream(const boost::filesystem::path & archive, unz64_file_pos filepos);
~CZipStream();
si64 getSize() override;
@ -44,14 +44,14 @@ protected:
class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
{
std::string archiveName;
boost::filesystem::path archiveName;
std::string mountPoint;
std::unordered_map<ResourceID, unz_file_pos> files;
std::unordered_map<ResourceID, unz64_file_pos> files;
std::unordered_map<ResourceID, unz_file_pos> listFiles(const std::string & mountPoint, const std::string &archive);
std::unordered_map<ResourceID, unz64_file_pos> listFiles(const std::string & mountPoint, const boost::filesystem::path &archive);
public:
CZipLoader(const std::string & mountPoint, const std::string & archive);
CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive);
/// Interface implementation
/// @see ISimpleResourceLoader

View File

@ -1,5 +1,4 @@
#include "StdInc.h"
#define INC_FROM_FILESTREAM_CPP
#include "FileStream.h"
#include "../minizip/unzip.h"

View File

@ -4,7 +4,7 @@
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/stream.hpp>
class FileBuf
class DLL_LINKAGE FileBuf
{
public:
typedef char char_type;
@ -29,8 +29,6 @@ typedef zlib_filefunc64_def_s zlib_filefunc64_def;
#ifdef VCMI_DLL
extern template class DLL_LINKAGE boost::iostreams::stream<FileBuf>;
#else
template class DLL_LINKAGE boost::iostreams::stream<FileBuf>;
#endif
class DLL_LINKAGE FileStream : public boost::iostreams::stream<FileBuf>

View File

@ -48,9 +48,9 @@ public:
*
* @return path or empty optional if file can't be accessed independently (e.g. file in archive)
*/
virtual boost::optional<std::string> getResourceName(const ResourceID & resourceName) const
virtual boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const
{
return boost::optional<std::string>();
return boost::optional<boost::filesystem::path>();
}
/**