GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-strstrm.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_oct_strstrm_h)
27#define octave_oct_strstrm_h 1
28
29#include "octave-config.h"
30
31#include <string>
32#include <sstream>
33
34#include "oct-stream.h"
35
36OCTAVE_NAMESPACE_BEGIN
37
38class
39base_strstream : public base_stream
40{
41public:
42
43 base_strstream (std::ios::openmode m = std::ios::out,
46 const std::string& encoding = "utf-8")
47 : base_stream (m, ff, encoding) { }
48
49 // No copying!
50
51 base_strstream (const base_strstream&) = delete;
52
53 base_strstream& operator = (const base_strstream&) = delete;
54
55protected:
56
57 ~base_strstream (void) = default;
58
59public:
60
61 // Position a stream at OFFSET relative to ORIGIN.
62
63 int seek (off_t, int);
64
65 // Return current stream position.
66
67 virtual off_t tell (void);
68
69 // The name of the file.
70
71 std::string name (void) const { return ""; }
72
73 virtual std::streambuf * rdbuf (void) = 0;
74
75 virtual bool bad (void) const = 0;
76
77 virtual void clear (void) = 0;
78};
79
80class
82{
83public:
84
85 istrstream (const char *data,
86 std::ios::openmode arg_md = std::ios::out,
88 const std::string& encoding = "utf-8")
89 : base_strstream (arg_md, ff, encoding), m_istream (data) { }
90
91 istrstream (const std::string& data,
92 std::ios::openmode arg_md = std::ios::out,
94 const std::string& encoding = "utf-8")
95 : base_strstream (arg_md, ff, encoding), m_istream (data) { }
96
97 // No copying!
98
99 istrstream (const istrstream&) = delete;
100
101 istrstream& operator = (const istrstream&) = delete;
102
103protected:
104
105 ~istrstream (void) = default;
106
107public:
108
109
110 static stream
111 create (const char *data, std::ios::openmode arg_md = std::ios::out,
113 const std::string& encoding = "utf-8");
114
115 static stream
116 create (const std::string& data, std::ios::openmode arg_md = std::ios::out,
118 const std::string& encoding = "utf-8");
119
120 // Return nonzero if EOF has been reached on this stream.
121
122 bool eof (void) const { return m_istream.eof (); }
123
124 std::istream * input_stream (void) { return &m_istream; }
125
126 std::ostream * output_stream (void) { return nullptr; }
127
128 off_t tell (void) { return m_istream.tellg (); }
129
130 std::streambuf * rdbuf (void)
131 {
132 return m_istream ? m_istream.rdbuf () : nullptr;
133 }
134
135 bool bad (void) const { return m_istream.bad (); }
136
137 void clear (void) { m_istream.clear (); }
138
139private:
140
141 std::istringstream m_istream;
142};
143
144class
146{
147public:
148
149 ostrstream (std::ios::openmode arg_md = std::ios::out,
151 const std::string& encoding = "utf-8")
152 : base_strstream (arg_md, ff, encoding), m_ostream () { }
153
154 // No copying!
155
156 ostrstream (const ostrstream&) = delete;
157
158 ostrstream& operator = (const ostrstream&) = delete;
159
160protected:
161
162 ~ostrstream (void) = default;
163
164public:
165
166 static stream
167 create (std::ios::openmode arg_md = std::ios::out,
169 const std::string& encoding = "utf-8");
170
171 // Return nonzero if EOF has been reached on this stream.
172
173 bool eof (void) const { return m_ostream.eof (); }
174
175 std::istream * input_stream (void) { return nullptr; }
176
177 std::ostream * output_stream (void) { return &m_ostream; }
178
179 std::string str (void) { return m_ostream.str (); }
180
181 std::streambuf * rdbuf (void)
182 {
183 return m_ostream ? m_ostream.rdbuf () : nullptr;
184 }
185
186 bool bad (void) const { return m_ostream.bad (); }
187
188 void clear (void) { m_ostream.clear (); }
189
190private:
191
192 std::ostringstream m_ostream;
193};
194
195OCTAVE_NAMESPACE_END
196
197#if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
198
199OCTAVE_DEPRECATED (7, "use 'octave::base_strstream' instead")
200typedef octave::base_strstream octave_base_strstream;
201
202OCTAVE_DEPRECATED (7, "use 'octave::istrstream' instead")
203typedef octave::istrstream octave_istrstream;
204
205OCTAVE_DEPRECATED (7, "use 'octave::ostrstream' instead")
206typedef octave::ostrstream octave_ostrstream;
207
208#endif
209
210#endif
base_strstream(std::ios::openmode m=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
Definition: oct-strstrm.h:43
~base_strstream(void)=default
virtual void clear(void)=0
std::string name(void) const
Definition: oct-strstrm.h:71
virtual std::streambuf * rdbuf(void)=0
virtual bool bad(void) const =0
base_strstream(const base_strstream &)=delete
istrstream(const char *data, std::ios::openmode arg_md=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
Definition: oct-strstrm.h:85
std::streambuf * rdbuf(void)
Definition: oct-strstrm.h:130
std::istream * input_stream(void)
Definition: oct-strstrm.h:124
istrstream(const istrstream &)=delete
bool bad(void) const
Definition: oct-strstrm.h:135
void clear(void)
Definition: oct-strstrm.h:137
bool eof(void) const
Definition: oct-strstrm.h:122
off_t tell(void)
Definition: oct-strstrm.h:128
std::istringstream m_istream
Definition: oct-strstrm.h:141
istrstream(const std::string &data, std::ios::openmode arg_md=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
Definition: oct-strstrm.h:91
~istrstream(void)=default
std::ostream * output_stream(void)
Definition: oct-strstrm.h:126
~ostrstream(void)=default
bool bad(void) const
Definition: oct-strstrm.h:186
std::ostringstream m_ostream
Definition: oct-strstrm.h:192
void clear(void)
Definition: oct-strstrm.h:188
bool eof(void) const
Definition: oct-strstrm.h:173
ostrstream(std::ios::openmode arg_md=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
Definition: oct-strstrm.h:149
ostrstream(const ostrstream &)=delete
std::ostream * output_stream(void)
Definition: oct-strstrm.h:177
std::streambuf * rdbuf(void)
Definition: oct-strstrm.h:181
std::istream * input_stream(void)
Definition: oct-strstrm.h:175
std::string str(void)
Definition: oct-strstrm.h:179
float_format native_float_format(void)
Definition: mach-info.cc:65