Define the CoreAudio default name only when needed
[openal-soft.git] / common / alfstream.h
blob353fd2de699b92c7d9fa1c70c4eedc5509246fea
1 #ifndef AL_FSTREAM_H
2 #define AL_FSTREAM_H
4 #ifdef _WIN32
6 #define WIN32_LEAN_AND_MEAN
7 #include <windows.h>
9 #include <array>
10 #include <string>
11 #include <fstream>
14 namespace al {
16 // Windows' std::ifstream fails with non-ANSI paths since the standard only
17 // specifies names using const char* (or std::string). MSVC has a non-standard
18 // extension using const wchar_t* (or std::wstring?) to handle Unicode paths,
19 // but not all Windows compilers support it. So we have to make our own istream
20 // that accepts UTF-8 paths and forwards to Unicode-aware I/O functions.
21 class filebuf final : public std::streambuf {
22 std::array<char_type,4096> mBuffer;
23 HANDLE mFile{INVALID_HANDLE_VALUE};
25 int_type underflow() override;
26 pos_type seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) override;
27 pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override;
29 public:
30 filebuf() = default;
31 ~filebuf() override;
33 bool open(const wchar_t *filename, std::ios_base::openmode mode);
34 bool open(const char *filename, std::ios_base::openmode mode);
36 bool is_open() const noexcept { return mFile != INVALID_HANDLE_VALUE; }
38 void close();
41 // Inherit from std::istream to use our custom streambuf
42 class ifstream final : public std::istream {
43 filebuf mStreamBuf;
45 public:
46 ifstream(const wchar_t *filename, std::ios_base::openmode mode = std::ios_base::in);
47 ifstream(const std::wstring &filename, std::ios_base::openmode mode = std::ios_base::in)
48 : ifstream(filename.c_str(), mode) { }
49 ifstream(const char *filename, std::ios_base::openmode mode = std::ios_base::in);
50 ifstream(const std::string &filename, std::ios_base::openmode mode = std::ios_base::in)
51 : ifstream(filename.c_str(), mode) { }
52 ~ifstream() override;
54 bool is_open() const noexcept { return mStreamBuf.is_open(); }
56 void close() { mStreamBuf.close(); }
59 } // namespace al
61 #else /* _WIN32 */
63 #include <fstream>
65 namespace al {
67 using filebuf = std::filebuf;
68 using ifstream = std::ifstream;
70 } // namespace al
72 #endif /* _WIN32 */
74 #endif /* AL_FSTREAM_H */