From 95996effaf04c87b7091c904e6545bc1e5e25aee Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 22 Sep 2019 12:23:41 -0700 Subject: [PATCH] Move the ifstream wrapper to common --- CMakeLists.txt | 2 + alc/alconfig.cpp | 10 ++- alc/ambdec.cpp | 2 +- alc/compat.h | 84 +++------------------ alc/helpers.cpp | 137 ---------------------------------- alc/hrtf.cpp | 3 +- common/alfstream.cpp | 147 +++++++++++++++++++++++++++++++++++++ alc/compat.h => common/alfstream.h | 11 +-- 8 files changed, 170 insertions(+), 226 deletions(-) rewrite alc/compat.h (91%) create mode 100644 common/alfstream.cpp copy alc/compat.h => common/alfstream.h (91%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9087e95..c20fa021 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -527,6 +527,8 @@ SET(COMMON_OBJS common/alcomplex.h common/alexcpt.cpp common/alexcpt.h + common/alfstream.cpp + common/alfstream.h common/almalloc.cpp common/almalloc.h common/alnumeric.h diff --git a/alc/alconfig.cpp b/alc/alconfig.cpp index b266fe5b..ede39156 100644 --- a/alc/alconfig.cpp +++ b/alc/alconfig.cpp @@ -41,15 +41,17 @@ #include #endif -#include -#include #include +#include +#include +#include -#include "alcmain.h" +#include "alfstream.h" #include "alstring.h" +#include "compat.h" #include "logging.h" #include "strutils.h" -#include "compat.h" +#include "vector.h" namespace { diff --git a/alc/ambdec.cpp b/alc/ambdec.cpp index fa29d268..adf116fe 100644 --- a/alc/ambdec.cpp +++ b/alc/ambdec.cpp @@ -10,7 +10,7 @@ #include #include -#include "compat.h" +#include "alfstream.h" #include "logging.h" diff --git a/alc/compat.h b/alc/compat.h dissimilarity index 91% index f2e10513..960b4b94 100644 --- a/alc/compat.h +++ b/alc/compat.h @@ -1,75 +1,9 @@ -#ifndef AL_COMPAT_H -#define AL_COMPAT_H - -#ifdef _WIN32 - -#define WIN32_LEAN_AND_MEAN -#include - -#include -#include -#include - - -namespace al { - -// Windows' std::ifstream fails with non-ANSI paths since the standard only -// specifies names using const char* (or std::string). MSVC has a non-standard -// extension using const wchar_t* (or std::wstring?) to handle Unicode paths, -// but not all Windows compilers support it. So we have to make our own istream -// that accepts UTF-8 paths and forwards to Unicode-aware I/O functions. -class filebuf final : public std::streambuf { - std::array mBuffer; - HANDLE mFile{INVALID_HANDLE_VALUE}; - - int_type underflow() override; - pos_type seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) override; - pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override; - -public: - filebuf() = default; - ~filebuf() override; - - bool open(const wchar_t *filename, std::ios_base::openmode mode); - bool open(const char *filename, std::ios_base::openmode mode); - - bool is_open() const noexcept { return mFile != INVALID_HANDLE_VALUE; } -}; - -// Inherit from std::istream to use our custom streambuf -class ifstream final : public std::istream { - filebuf mStreamBuf; - -public: - ifstream(const wchar_t *filename, std::ios_base::openmode mode = std::ios_base::in); - ifstream(const std::wstring &filename, std::ios_base::openmode mode = std::ios_base::in) - : ifstream(filename.c_str(), mode) { } - ifstream(const char *filename, std::ios_base::openmode mode = std::ios_base::in); - ifstream(const std::string &filename, std::ios_base::openmode mode = std::ios_base::in) - : ifstream(filename.c_str(), mode) { } - ~ifstream() override; - - bool is_open() const noexcept { return mStreamBuf.is_open(); } -}; - -} // namespace al - -#else /* _WIN32 */ - -#include - -namespace al { - -using filebuf = std::filebuf; -using ifstream = std::ifstream; - -} // namespace al - -#endif /* _WIN32 */ - -#include - -struct PathNamePair { std::string path, fname; }; -const PathNamePair &GetProcBinary(void); - -#endif /* AL_COMPAT_H */ +#ifndef AL_COMPAT_H +#define AL_COMPAT_H + +#include + +struct PathNamePair { std::string path, fname; }; +const PathNamePair &GetProcBinary(void); + +#endif /* AL_COMPAT_H */ diff --git a/alc/helpers.cpp b/alc/helpers.cpp index 1d19d004..1d534619 100644 --- a/alc/helpers.cpp +++ b/alc/helpers.cpp @@ -248,143 +248,6 @@ void FPUCtl::leave() #ifdef _WIN32 -namespace al { - -auto filebuf::underflow() -> int_type -{ - if(mFile != INVALID_HANDLE_VALUE && gptr() == egptr()) - { - // Read in the next chunk of data, and set the pointers on success - DWORD got{}; - if(ReadFile(mFile, mBuffer.data(), static_cast(mBuffer.size()), &got, nullptr)) - setg(mBuffer.data(), mBuffer.data(), mBuffer.data()+got); - } - if(gptr() == egptr()) - return traits_type::eof(); - return traits_type::to_int_type(*gptr()); -} - -auto filebuf::seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) -> pos_type -{ - if(mFile == INVALID_HANDLE_VALUE || (mode&std::ios_base::out) || !(mode&std::ios_base::in)) - return traits_type::eof(); - - LARGE_INTEGER fpos{}; - switch(whence) - { - case std::ios_base::beg: - fpos.QuadPart = offset; - if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_BEGIN)) - return traits_type::eof(); - break; - - case std::ios_base::cur: - // If the offset remains in the current buffer range, just - // update the pointer. - if((offset >= 0 && offset < off_type(egptr()-gptr())) || - (offset < 0 && -offset <= off_type(gptr()-eback()))) - { - // Get the current file offset to report the correct read - // offset. - fpos.QuadPart = 0; - if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_CURRENT)) - return traits_type::eof(); - setg(eback(), gptr()+offset, egptr()); - return fpos.QuadPart - off_type(egptr()-gptr()); - } - // Need to offset for the file offset being at egptr() while - // the requested offset is relative to gptr(). - offset -= off_type(egptr()-gptr()); - fpos.QuadPart = offset; - if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_CURRENT)) - return traits_type::eof(); - break; - - case std::ios_base::end: - fpos.QuadPart = offset; - if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_END)) - return traits_type::eof(); - break; - - default: - return traits_type::eof(); - } - setg(nullptr, nullptr, nullptr); - return fpos.QuadPart; -} - -auto filebuf::seekpos(pos_type pos, std::ios_base::openmode mode) -> pos_type -{ - // Simplified version of seekoff - if(mFile == INVALID_HANDLE_VALUE || (mode&std::ios_base::out) || !(mode&std::ios_base::in)) - return traits_type::eof(); - - LARGE_INTEGER fpos{}; - fpos.QuadPart = pos; - if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_BEGIN)) - return traits_type::eof(); - - setg(nullptr, nullptr, nullptr); - return fpos.QuadPart; -} - -filebuf::~filebuf() -{ - if(mFile != INVALID_HANDLE_VALUE) - CloseHandle(mFile); - mFile = INVALID_HANDLE_VALUE; -} - -bool filebuf::open(const wchar_t *filename, std::ios_base::openmode mode) -{ - if((mode&std::ios_base::out) || !(mode&std::ios_base::in)) - return false; - HANDLE f{CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, nullptr)}; - if(f == INVALID_HANDLE_VALUE) return false; - - if(mFile != INVALID_HANDLE_VALUE) - CloseHandle(mFile); - mFile = f; - - setg(nullptr, nullptr, nullptr); - return true; -} -bool filebuf::open(const char *filename, std::ios_base::openmode mode) -{ - std::wstring wname{utf8_to_wstr(filename)}; - return open(wname.c_str(), mode); -} - - -ifstream::ifstream(const wchar_t *filename, std::ios_base::openmode mode) - : std::istream{nullptr} -{ - init(&mStreamBuf); - - // Set the failbit if the file failed to open. - if((mode&std::ios_base::out) || !mStreamBuf.open(filename, mode|std::ios_base::in)) - clear(failbit); -} - -ifstream::ifstream(const char *filename, std::ios_base::openmode mode) - : std::istream{nullptr} -{ - init(&mStreamBuf); - - // Set the failbit if the file failed to open. - if((mode&std::ios_base::out) || !mStreamBuf.open(filename, mode|std::ios_base::in)) - clear(failbit); -} - -/* This is only here to ensure the compiler doesn't define an implicit - * destructor, which it tries to automatically inline and subsequently complain - * it can't inline without excessive code growth. - */ -ifstream::~ifstream() { } - -} // namespace al - const PathNamePair &GetProcBinary() { static PathNamePair ret; diff --git a/alc/hrtf.cpp b/alc/hrtf.cpp index ebdee46a..12d310d4 100644 --- a/alc/hrtf.cpp +++ b/alc/hrtf.cpp @@ -36,17 +36,18 @@ #include #include #include +#include #include #include "AL/al.h" #include "alcmain.h" #include "alconfig.h" +#include "alfstream.h" #include "almalloc.h" #include "alnumeric.h" #include "aloptional.h" #include "alspan.h" -#include "compat.h" #include "filters/splitter.h" #include "logging.h" #include "math_defs.h" diff --git a/common/alfstream.cpp b/common/alfstream.cpp new file mode 100644 index 00000000..7378c678 --- /dev/null +++ b/common/alfstream.cpp @@ -0,0 +1,147 @@ + +#include "config.h" + +#include "alfstream.h" + +#include "strutils.h" + +#ifdef _WIN32 + +namespace al { + +auto filebuf::underflow() -> int_type +{ + if(mFile != INVALID_HANDLE_VALUE && gptr() == egptr()) + { + // Read in the next chunk of data, and set the pointers on success + DWORD got{}; + if(ReadFile(mFile, mBuffer.data(), static_cast(mBuffer.size()), &got, nullptr)) + setg(mBuffer.data(), mBuffer.data(), mBuffer.data()+got); + } + if(gptr() == egptr()) + return traits_type::eof(); + return traits_type::to_int_type(*gptr()); +} + +auto filebuf::seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) -> pos_type +{ + if(mFile == INVALID_HANDLE_VALUE || (mode&std::ios_base::out) || !(mode&std::ios_base::in)) + return traits_type::eof(); + + LARGE_INTEGER fpos{}; + switch(whence) + { + case std::ios_base::beg: + fpos.QuadPart = offset; + if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_BEGIN)) + return traits_type::eof(); + break; + + case std::ios_base::cur: + // If the offset remains in the current buffer range, just + // update the pointer. + if((offset >= 0 && offset < off_type(egptr()-gptr())) || + (offset < 0 && -offset <= off_type(gptr()-eback()))) + { + // Get the current file offset to report the correct read + // offset. + fpos.QuadPart = 0; + if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_CURRENT)) + return traits_type::eof(); + setg(eback(), gptr()+offset, egptr()); + return fpos.QuadPart - off_type(egptr()-gptr()); + } + // Need to offset for the file offset being at egptr() while + // the requested offset is relative to gptr(). + offset -= off_type(egptr()-gptr()); + fpos.QuadPart = offset; + if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_CURRENT)) + return traits_type::eof(); + break; + + case std::ios_base::end: + fpos.QuadPart = offset; + if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_END)) + return traits_type::eof(); + break; + + default: + return traits_type::eof(); + } + setg(nullptr, nullptr, nullptr); + return fpos.QuadPart; +} + +auto filebuf::seekpos(pos_type pos, std::ios_base::openmode mode) -> pos_type +{ + // Simplified version of seekoff + if(mFile == INVALID_HANDLE_VALUE || (mode&std::ios_base::out) || !(mode&std::ios_base::in)) + return traits_type::eof(); + + LARGE_INTEGER fpos{}; + fpos.QuadPart = pos; + if(!SetFilePointerEx(mFile, fpos, &fpos, FILE_BEGIN)) + return traits_type::eof(); + + setg(nullptr, nullptr, nullptr); + return fpos.QuadPart; +} + +filebuf::~filebuf() +{ + if(mFile != INVALID_HANDLE_VALUE) + CloseHandle(mFile); + mFile = INVALID_HANDLE_VALUE; +} + +bool filebuf::open(const wchar_t *filename, std::ios_base::openmode mode) +{ + if((mode&std::ios_base::out) || !(mode&std::ios_base::in)) + return false; + HANDLE f{CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, nullptr)}; + if(f == INVALID_HANDLE_VALUE) return false; + + if(mFile != INVALID_HANDLE_VALUE) + CloseHandle(mFile); + mFile = f; + + setg(nullptr, nullptr, nullptr); + return true; +} +bool filebuf::open(const char *filename, std::ios_base::openmode mode) +{ + std::wstring wname{utf8_to_wstr(filename)}; + return open(wname.c_str(), mode); +} + + +ifstream::ifstream(const wchar_t *filename, std::ios_base::openmode mode) + : std::istream{nullptr} +{ + init(&mStreamBuf); + + // Set the failbit if the file failed to open. + if((mode&std::ios_base::out) || !mStreamBuf.open(filename, mode|std::ios_base::in)) + clear(failbit); +} + +ifstream::ifstream(const char *filename, std::ios_base::openmode mode) + : std::istream{nullptr} +{ + init(&mStreamBuf); + + // Set the failbit if the file failed to open. + if((mode&std::ios_base::out) || !mStreamBuf.open(filename, mode|std::ios_base::in)) + clear(failbit); +} + +/* This is only here to ensure the compiler doesn't define an implicit + * destructor, which it tries to automatically inline and subsequently complain + * it can't inline without excessive code growth. + */ +ifstream::~ifstream() { } + +} // namespace al + +#endif diff --git a/alc/compat.h b/common/alfstream.h similarity index 91% copy from alc/compat.h copy to common/alfstream.h index f2e10513..046a6e2a 100644 --- a/alc/compat.h +++ b/common/alfstream.h @@ -1,5 +1,5 @@ -#ifndef AL_COMPAT_H -#define AL_COMPAT_H +#ifndef AL_FSTREAM_H +#define AL_FSTREAM_H #ifdef _WIN32 @@ -67,9 +67,4 @@ using ifstream = std::ifstream; #endif /* _WIN32 */ -#include - -struct PathNamePair { std::string path, fname; }; -const PathNamePair &GetProcBinary(void); - -#endif /* AL_COMPAT_H */ +#endif /* AL_FSTREAM_H */ -- 2.11.4.GIT