1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Similar to std::ifstream/ofstream, but takes char16ptr_t on Windows.
8 // Until C++17, std functions can only take char* filenames. So Unicode
9 // filenames were lost on Windows. To address this limitations, this wrapper
10 // uses proprietary wchar_t* overloads on MSVC, and __gnu_cxx::stdio_filebuf
11 // extension on MinGW. Once we can use C++17 filesystem API everywhere,
12 // we will be able to avoid this wrapper.
14 #ifndef mozilla_FStream_h
15 #define mozilla_FStream_h
17 #include "mozilla/Char16.h"
21 #if defined(__MINGW32__) && defined(__GLIBCXX__)
22 #include "mozilla/UniquePtr.h"
24 #include <ext/stdio_filebuf.h>
29 #if defined(__MINGW32__) && defined(__GLIBCXX__)
30 // MinGW does not support wchar_t* overloads that are MSVC extension until
31 // C++17, so we have to implement widechar wrappers using a GNU extension.
32 class IFStream
: public std::istream
35 explicit IFStream(char16ptr_t filename
, openmode mode
= in
);
37 std::filebuf
* rdbuf() const { return mFileBuf
.get(); }
39 bool is_open() const { return mFileBuf
&& mFileBuf
->is_open(); }
40 void open(char16ptr_t filename
, openmode mode
= in
);
41 void close() { mFileBuf
&& mFileBuf
->close(); }
44 UniquePtr
<std::filebuf
> mFileBuf
;
48 IFStream::IFStream(char16ptr_t filename
, openmode mode
)
49 : std::istream(nullptr)
55 IFStream::open(char16ptr_t filename
, openmode mode
)
57 int fmode
= _O_RDONLY
;
63 int fd
= _wopen(filename
, fmode
);
64 mFileBuf
= MakeUnique
<__gnu_cxx::stdio_filebuf
<char>>(fd
, mode
);
65 std::istream::rdbuf(mFileBuf
.get());
68 class OFStream
: public std::ostream
71 explicit OFStream(char16ptr_t filename
, openmode mode
= out
);
73 std::filebuf
* rdbuf() const { return mFileBuf
.get(); }
75 bool is_open() const { return mFileBuf
&& mFileBuf
->is_open(); }
76 void open(char16ptr_t filename
, openmode mode
= out
);
77 void close() { mFileBuf
&& mFileBuf
->close(); }
80 UniquePtr
<std::filebuf
> mFileBuf
;
84 OFStream::OFStream(char16ptr_t filename
, openmode mode
)
85 : std::ostream(nullptr)
91 OFStream::open(char16ptr_t filename
, openmode mode
)
93 int fmode
= _O_WRONLY
;
100 fmode
|= _O_CREAT
| _O_TRUNC
;
102 int fd
= _wopen(filename
, fmode
);
103 mFileBuf
= MakeUnique
<__gnu_cxx::stdio_filebuf
<char>>(fd
, mode
);
104 std::ostream::rdbuf(mFileBuf
.get());
107 #elif defined(XP_WIN)
108 class IFStream
: public std::ifstream
111 explicit IFStream(char16ptr_t filename
, openmode mode
= in
)
112 : std::ifstream(filename
, mode
) {}
114 void open(char16ptr_t filename
, openmode mode
= in
)
116 std::ifstream::open(filename
, mode
);
120 class OFStream
: public std::ofstream
123 explicit OFStream(char16ptr_t filename
, openmode mode
= out
)
124 : std::ofstream(filename
, mode
) {}
126 void open(char16ptr_t filename
, openmode mode
= out
)
128 std::ofstream::open(filename
, mode
);
132 using IFStream
= std::ifstream
;
133 using OFStream
= std::ofstream
;
136 } // namespace mozilla
138 #endif /* mozilla_FStream_h */