VirtualBox

source: vbox/trunk/include/iprt/nocrt/fstream@ 106557

Last change on this file since 106557 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette