aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-09-12 22:41:17 -0700
committerChris Robinson <[email protected]>2022-09-12 22:50:43 -0700
commit090cf39b17c633586339efa7b8691788563c4fe2 (patch)
tree8673683fd97ad74dd02c67ac64f27be508abe5a5
parent2d49920e7a2069ea6f0b4529f59286f8955d145f (diff)
Use ifstream's wchar_t constructors on Windows
MinGW seems to have added them a while ago, so that greatly simplifies things.
-rw-r--r--common/alfstream.cpp138
-rw-r--r--common/alfstream.h53
-rw-r--r--core/ambdec.cpp2
3 files changed, 20 insertions, 173 deletions
diff --git a/common/alfstream.cpp b/common/alfstream.cpp
index 3beda833..8991ce03 100644
--- a/common/alfstream.cpp
+++ b/common/alfstream.cpp
@@ -9,141 +9,17 @@
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<DWORD>(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()
-{ close(); }
-
-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);
-}
-
-void filebuf::close()
-{
- if(mFile != INVALID_HANDLE_VALUE)
- CloseHandle(mFile);
- mFile = INVALID_HANDLE_VALUE;
-}
-
-
-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);
+ : std::ifstream{utf8_to_wstr(filename).c_str(), mode}
+{ }
- // 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);
+void ifstream::open(const char *filename, std::ios_base::openmode mode)
+{
+ std::wstring wstr{utf8_to_wstr(filename)};
+ std::ifstream::open(wstr.c_str(), mode);
}
-/* 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() { }
+ifstream::~ifstream() = default;
} // namespace al
diff --git a/common/alfstream.h b/common/alfstream.h
index 353fd2de..62b2e12c 100644
--- a/common/alfstream.h
+++ b/common/alfstream.h
@@ -3,57 +3,29 @@
#ifdef _WIN32
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-#include <array>
#include <string>
#include <fstream>
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<char_type,4096> 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;
-
+// Inherit from std::ifstream to accept UTF-8 filenames
+class ifstream final : public std::ifstream {
public:
- filebuf() = default;
- ~filebuf() override;
+ explicit ifstream(const char *filename, std::ios_base::openmode mode=std::ios_base::in);
+ explicit ifstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
+ : ifstream{filename.c_str(), mode} { }
- bool open(const wchar_t *filename, std::ios_base::openmode mode);
- bool open(const char *filename, std::ios_base::openmode mode);
+ explicit ifstream(const wchar_t *filename, std::ios_base::openmode mode=std::ios_base::in)
+ : std::ifstream{filename, mode} { }
+ explicit ifstream(const std::wstring &filename, std::ios_base::openmode mode=std::ios_base::in)
+ : ifstream{filename.c_str(), mode} { }
- bool is_open() const noexcept { return mFile != INVALID_HANDLE_VALUE; }
-
- void close();
-};
+ void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in);
+ void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
+ { open(filename.c_str(), mode); }
-// 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(); }
-
- void close() { mStreamBuf.close(); }
};
} // namespace al
@@ -64,7 +36,6 @@ public:
namespace al {
-using filebuf = std::filebuf;
using ifstream = std::ifstream;
} // namespace al
diff --git a/core/ambdec.cpp b/core/ambdec.cpp
index 4e4c7e1e..db8f45ab 100644
--- a/core/ambdec.cpp
+++ b/core/ambdec.cpp
@@ -15,7 +15,7 @@
#include "albit.h"
#include "alfstream.h"
#include "alspan.h"
-#include "core/logging.h"
+#include "opthelpers.h"
namespace {