aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/compat.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-09 21:56:05 -0800
committerChris Robinson <[email protected]>2018-11-09 21:56:05 -0800
commitadd776ddbb859548e78d390bf54baebe169105d1 (patch)
tree8373338e9e26c55e18a4734aadf4dd7635464d77 /Alc/compat.h
parentdb56fd5e3df68808b44880012397dd18a644cba5 (diff)
Handle the open mode in al::ifstream
Diffstat (limited to 'Alc/compat.h')
-rw-r--r--Alc/compat.h40
1 files changed, 28 insertions, 12 deletions
diff --git a/Alc/compat.h b/Alc/compat.h
index 1c348c34..25bb0c45 100644
--- a/Alc/compat.h
+++ b/Alc/compat.h
@@ -147,17 +147,25 @@ class filebuf final : public std::streambuf {
}
public:
- bool open(const wchar_t *filename)
+ bool open(const wchar_t *filename, std::ios_base::openmode mode)
{
- mFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, nullptr,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
- if(mFile == INVALID_HANDLE_VALUE) return false;
+ 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 open(const char *filename)
+ bool open(const char *filename, std::ios_base::openmode mode)
{
std::wstring wname{utf8_to_wstr(filename)};
- return open(wname.c_str());
+ return open(wname.c_str(), mode);
}
bool is_open() const noexcept { return mFile != INVALID_HANDLE_VALUE; }
@@ -176,22 +184,30 @@ class ifstream final : public std::istream {
filebuf mStreamBuf;
public:
- ifstream(const std::wstring &filename) : ifstream{filename.c_str()} { }
- ifstream(const wchar_t *filename) : std::istream{nullptr}
+ ifstream(const std::wstring &filename, std::ios_base::openmode mode = std::ios_base::in)
+ : ifstream(filename.c_str(), mode) { }
+ ifstream(const wchar_t *filename, std::ios_base::openmode mode = std::ios_base::in)
+ : std::istream{nullptr}
{
init(&mStreamBuf);
// Set the failbit if the file failed to open.
- if(!mStreamBuf.open(filename)) clear(failbit);
+ if((mode&std::ios_base::out) ||
+ !mStreamBuf.open(filename, mode|std::ios_base::in))
+ clear(failbit);
}
- ifstream(const std::string &filename) : ifstream{filename.c_str()} { }
- ifstream(const char *filename) : std::istream{nullptr}
+ ifstream(const std::string &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)
+ : std::istream{nullptr}
{
init(&mStreamBuf);
// Set the failbit if the file failed to open.
- if(!mStreamBuf.open(filename)) clear(failbit);
+ if((mode&std::ios_base::out) ||
+ !mStreamBuf.open(filename, mode|std::ios_base::in))
+ clear(failbit);
}
bool is_open() const noexcept { return mStreamBuf.is_open(); }