mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-23 22:37:55 +02:00
Small improvements to FileStream
This commit is contained in:
@@ -11,14 +11,21 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
#define CHAR_LITERAL(s) L##s
|
#define CHAR_LITERAL(s) L##s
|
||||||
#define DO_OPEN(name, mode) _wfopen(name, mode)
|
|
||||||
using CharType = wchar_t;
|
using CharType = wchar_t;
|
||||||
#else
|
#else
|
||||||
#define CHAR_LITERAL(s) s
|
#define CHAR_LITERAL(s) s
|
||||||
#define DO_OPEN(name, mode) fopen(name, mode)
|
|
||||||
using CharType = char;
|
using CharType = char;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
inline FILE* do_open(const CharType* name, const CharType* mode)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return _wfopen(name, mode);
|
||||||
|
#else
|
||||||
|
return std::fopen(name, mode);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#define GETFILE static_cast<std::FILE*>(filePtr)
|
#define GETFILE static_cast<std::FILE*>(filePtr)
|
||||||
|
|
||||||
voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
|
voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
|
||||||
@@ -35,7 +42,7 @@ voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
|
|||||||
}();
|
}();
|
||||||
|
|
||||||
if (filename != nullptr && mode_fopen != nullptr)
|
if (filename != nullptr && mode_fopen != nullptr)
|
||||||
return DO_OPEN(static_cast<const CharType*>(filename), mode_fopen);
|
return do_open(static_cast<const CharType*>(filename), mode_fopen);
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -57,7 +64,7 @@ template class boost::iostreams::stream<FileBuf>;
|
|||||||
/*static*/
|
/*static*/
|
||||||
bool FileStream::CreateFile(const boost::filesystem::path& filename)
|
bool FileStream::CreateFile(const boost::filesystem::path& filename)
|
||||||
{
|
{
|
||||||
FILE* f = DO_OPEN(filename.c_str(), CHAR_LITERAL("wb"));
|
FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
|
||||||
bool result = (f != nullptr);
|
bool result = (f != nullptr);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return result;
|
return result;
|
||||||
@@ -65,39 +72,35 @@ bool FileStream::CreateFile(const boost::filesystem::path& filename)
|
|||||||
|
|
||||||
FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
|
FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
|
||||||
{
|
{
|
||||||
std::string openmode = [mode]() -> std::string
|
auto openmode = [mode]() -> std::basic_string<CharType>
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
switch (mode & (~ios_base::ate & ~ios_base::binary))
|
switch (mode & (~ios_base::ate & ~ios_base::binary))
|
||||||
{
|
{
|
||||||
case (ios_base::in):
|
case (ios_base::in):
|
||||||
return "r";
|
return CHAR_LITERAL("r");
|
||||||
case (ios_base::out):
|
case (ios_base::out):
|
||||||
case (ios_base::out | ios_base::trunc):
|
case (ios_base::out | ios_base::trunc):
|
||||||
return "w";
|
return CHAR_LITERAL("w");
|
||||||
case (ios_base::app):
|
case (ios_base::app):
|
||||||
case (ios_base::out | ios_base::app):
|
case (ios_base::out | ios_base::app):
|
||||||
return "a";
|
return CHAR_LITERAL("a");
|
||||||
case (ios_base::out | ios_base::in):
|
case (ios_base::out | ios_base::in):
|
||||||
return "r+";
|
return CHAR_LITERAL("r+");
|
||||||
case (ios_base::out | ios_base::in | ios_base::trunc):
|
case (ios_base::out | ios_base::in | ios_base::trunc):
|
||||||
return "w+";
|
return CHAR_LITERAL("w+");
|
||||||
case (ios_base::out | ios_base::in | ios_base::app):
|
case (ios_base::out | ios_base::in | ios_base::app):
|
||||||
case (ios_base::in | ios_base::app):
|
case (ios_base::in | ios_base::app):
|
||||||
return "a+";
|
return CHAR_LITERAL("a+");
|
||||||
default:
|
default:
|
||||||
throw std::ios_base::failure("invalid open mode");
|
throw std::ios_base::failure("invalid open mode");
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (mode & std::ios_base::binary)
|
if (mode & std::ios_base::binary)
|
||||||
openmode += 'b';
|
openmode += CHAR_LITERAL('b');
|
||||||
|
|
||||||
#if defined(_WIN32)
|
filePtr = do_open(filename.c_str(), openmode.c_str());
|
||||||
filePtr = _wfopen(filename.c_str(), std::wstring(openmode.begin(), openmode.end()).c_str());
|
|
||||||
#else
|
|
||||||
filePtr = std::open(filename.c_str(), openmode);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (filePtr == nullptr)
|
if (filePtr == nullptr)
|
||||||
throw std::ios_base::failure("could not open file");
|
throw std::ios_base::failure("could not open file");
|
||||||
@@ -127,7 +130,7 @@ std::streamsize FileBuf::write(const char* s, std::streamsize n)
|
|||||||
|
|
||||||
std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
|
std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
|
||||||
{
|
{
|
||||||
auto src = [way]() -> int
|
const auto src = [way]() -> int
|
||||||
{
|
{
|
||||||
switch(way)
|
switch(way)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <iosfwd>
|
|
||||||
#include <boost/iostreams/categories.hpp>
|
#include <boost/iostreams/categories.hpp>
|
||||||
#include <boost/iostreams/stream.hpp>
|
#include <boost/iostreams/stream.hpp>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user