1 | /** @file
|
---|
2 | * IPRT / No-CRT - Minimal C++ fstream header.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2022 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_nocrt_fstream
|
---|
27 | #define IPRT_INCLUDED_nocrt_fstream
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/nocrt/ostream>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 | template<typename a_CharType, typename a_CharTraits /*= std::char_traits<a_CharType>*/ >
|
---|
39 | class basic_filebuf : public basic_streambuf<a_CharType, a_CharTraits>
|
---|
40 | {
|
---|
41 | protected:
|
---|
42 | PRTSTREAM m_pStrm;
|
---|
43 | bool m_fStdStream;
|
---|
44 | ios_base::openmode m_fMode;
|
---|
45 |
|
---|
46 | public:
|
---|
47 | basic_filebuf(PRTSTREAM a_pStrm = NULL, bool a_fStdStream = false)
|
---|
48 | : basic_streambuf()
|
---|
49 | , m_pStrm(a_pStrm)
|
---|
50 | , m_fStdStream(a_fStdStream)
|
---|
51 | , m_fMode(ios_base::openmode(0))
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 | virtual ~basic_filebuf()
|
---|
56 | {
|
---|
57 | if (m_pStrm)
|
---|
58 | {
|
---|
59 | if (m_fStdStream)
|
---|
60 | RTStrmClose(m_pStrm);
|
---|
61 | m_pStrm = NULL;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | bool is_open() const RT_NOEXCEPT
|
---|
66 | {
|
---|
67 | return m_pStrm != NULL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | basic_filebuf *open(const char *a_pszFilename, ios_base::openmode a_fMode)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Sanitize the a_fMode first.
|
---|
74 | */
|
---|
75 | AssertReturn(!is_open(), NULL);
|
---|
76 | AssertReturn(a_fMode & (ios_base::out | ios_base::in), NULL); /* Neither write nor read mode? */
|
---|
77 | AssertStmt((a_fMode & (ios_base::out | ios_base::app)) != ios_base::app, a_fMode &= ~ios_base::app);
|
---|
78 | AssertReturn((a_fMode & (ios_base::out | ios_base::trunc)) != ios_base::trunc, NULL);
|
---|
79 | AssertReturn(!(a_fMode & ios_base::trunc) || !(a_fMode & ios_base::app), NULL);
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Translate a_fMode into a stream mode string and try open the file.
|
---|
83 | */
|
---|
84 | char szMode[8];
|
---|
85 | szMode[0] = a_fMode & ios_base::trunc ? 'w' : a_fMode & ios_base::app ? 'a' : 'r';
|
---|
86 | size_t offMode = 1;
|
---|
87 | if ((a_fMode & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out))
|
---|
88 | szMode[offMode++] = '+';
|
---|
89 | if (a_fMode & ios_base::binary)
|
---|
90 | szMode[offMode++] = 'b';
|
---|
91 | szMode[offMode] = '\0';
|
---|
92 |
|
---|
93 | int rc = RTStrmOpen(a_pszFilename, szMode, &m_pStrm);
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | /** @todo if (a_fMode & ios_base::ate)? */
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Set up the buffer?
|
---|
100 | */
|
---|
101 | if (true)
|
---|
102 | {
|
---|
103 | return this;
|
---|
104 | }
|
---|
105 |
|
---|
106 | RTStrmClose(m_pStrm);
|
---|
107 | m_pStrm = NULL;
|
---|
108 | }
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected:
|
---|
113 | bool flushBuffered()
|
---|
114 | {
|
---|
115 | /** @todo buffering. */
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | //virtual int_type overflow(int_type a_iChar) RT_OVERRIDE
|
---|
120 | //{
|
---|
121 | // if (a_iChar != traits_type::eof())
|
---|
122 | // {
|
---|
123 | // if (flushBuffered())
|
---|
124 | // {
|
---|
125 | // char_type ch = traits_type::to_char_type(a_iChar);
|
---|
126 | // int rc = RTStrmWrite(m_pStrm, &ch, sizeof(ch));
|
---|
127 | // if (RT_SUCCESS(rc))
|
---|
128 | // return a_iChar;
|
---|
129 | // }
|
---|
130 | // }
|
---|
131 | // return traits_type::eof();
|
---|
132 | //}
|
---|
133 |
|
---|
134 | std::streamsize xsputn(char_type const *a_pchSrc, std::streamsize a_cchSrc) //RT_OVERRIDE
|
---|
135 | {
|
---|
136 | if (flushBuffered())
|
---|
137 | {
|
---|
138 | size_t cbWritten = 0;
|
---|
139 | int rc = RTStrmWriteEx(m_pStrm, &a_pchSrc, sizeof(a_pchSrc[0]) * a_cchSrc, &cbWritten);
|
---|
140 | if (RT_SUCCESS(rc))
|
---|
141 | return cbWritten / sizeof(a_pchSrc[0]);
|
---|
142 | }
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 | };
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Basic I/O stream.
|
---|
150 | */
|
---|
151 | template<typename a_CharType, typename a_CharTraits /*= std::char_traits<a_CharType>*/ >
|
---|
152 | class basic_ofstream : public basic_ostream<a_CharType, a_CharTraits>
|
---|
153 | {
|
---|
154 | protected:
|
---|
155 | basic_filebuf<a_CharType, a_CharTraits> m_FileBuf;
|
---|
156 |
|
---|
157 | public:
|
---|
158 | basic_ofstream()
|
---|
159 | : basic_ostream(&m_FileBuf) /** @todo m_FileBuf isn't initialized yet... */
|
---|
160 | , m_FileBuf()
|
---|
161 | {
|
---|
162 | }
|
---|
163 |
|
---|
164 | explicit basic_ofstream(const char *a_pszFilename, ios_base::openmode a_fMode = ios_base::out)
|
---|
165 | : basic_ostream(&m_FileBuf) /** @todo m_FileBuf isn't initialized yet... */
|
---|
166 | , m_FileBuf()
|
---|
167 | {
|
---|
168 | m_FileBuf.open(a_pszFilename, a_fMode);
|
---|
169 | }
|
---|
170 | private:
|
---|
171 | basic_ofstream(basic_ofstream const &a_rSrc); /* no copying */
|
---|
172 | basic_ofstream &operator=(basic_ofstream const &a_rSrc); /* no copying */
|
---|
173 |
|
---|
174 | public:
|
---|
175 | virtual ~basic_ofstream()
|
---|
176 | {
|
---|
177 | }
|
---|
178 |
|
---|
179 | public:
|
---|
180 |
|
---|
181 | bool is_open() const RT_NOEXCEPT
|
---|
182 | {
|
---|
183 | return m_FileBuf.is_open();
|
---|
184 | }
|
---|
185 |
|
---|
186 | basic_filebuf<a_CharType, a_CharTraits> *open(const char *a_pszFilename, ios_base::openmode a_fMode)
|
---|
187 | {
|
---|
188 | return m_FileBuf.open(a_pszFilename, a_fMode);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | };
|
---|
193 | }
|
---|
194 |
|
---|
195 | #endif /* !IPRT_INCLUDED_nocrt_fstream */
|
---|
196 |
|
---|