Bug 1499346 [wpt PR 13540] - Use a common blank reference for wpt/css., a=testonly
[gecko.git] / mfbt / FStream.h
blob8906ab3bdef5163a2e042a23ce35a051ccebac1f
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"
18 #include <istream>
19 #include <ostream>
20 #include <fstream>
21 #if defined(__MINGW32__) && defined(__GLIBCXX__)
22 #include "mozilla/UniquePtr.h"
23 #include <fcntl.h>
24 #include <ext/stdio_filebuf.h>
25 #endif
27 namespace mozilla {
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
34 public:
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(); }
43 private:
44 UniquePtr<std::filebuf> mFileBuf;
47 inline
48 IFStream::IFStream(char16ptr_t filename, openmode mode)
49 : std::istream(nullptr)
51 open(filename, mode);
54 inline void
55 IFStream::open(char16ptr_t filename, openmode mode)
57 int fmode = _O_RDONLY;
58 if (mode & binary) {
59 fmode |= _O_BINARY;
60 } else {
61 fmode |= _O_TEXT;
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
70 public:
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(); }
79 private:
80 UniquePtr<std::filebuf> mFileBuf;
83 inline
84 OFStream::OFStream(char16ptr_t filename, openmode mode)
85 : std::ostream(nullptr)
87 open(filename, mode);
90 inline void
91 OFStream::open(char16ptr_t filename, openmode mode)
93 int fmode = _O_WRONLY;
94 if (mode & binary) {
95 fmode |= _O_BINARY;
96 } else {
97 fmode |= _O_TEXT;
99 if (mode & trunc) {
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
110 public:
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
122 public:
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);
131 #else
132 using IFStream = std::ifstream;
133 using OFStream = std::ofstream;
134 #endif
136 } // namespace mozilla
138 #endif /* mozilla_FStream_h */