GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-strstrm.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
37
39{
40public:
41
42 base_strstream (std::ios::openmode m = std::ios::out,
43 mach_info::float_format ff
44 = mach_info::native_float_format (),
45 const std::string& encoding = "utf-8")
46 : base_stream (m, ff, encoding) { }
47
48 OCTAVE_DISABLE_COPY_MOVE (base_strstream)
49
50protected:
51
52 ~base_strstream () = default;
53
54public:
55
56 // Position a stream at OFFSET relative to ORIGIN.
57
58 int seek (off_t, int);
59
60 // Return current stream position.
61
62 virtual off_t tell ();
63
64 // The name of the file.
65
66 std::string name () const { return ""; }
67
68 virtual std::streambuf * rdbuf () = 0;
69
70 virtual bool bad () const = 0;
71
72 virtual void clear () = 0;
73};
74
76{
77public:
78
79 istrstream (const char *data,
80 std::ios::openmode arg_md = std::ios::out,
81 mach_info::float_format ff = mach_info::native_float_format (),
82 const std::string& encoding = "utf-8")
83 : base_strstream (arg_md, ff, encoding), m_istream (data) { }
84
85 istrstream (const std::string& data,
86 std::ios::openmode arg_md = std::ios::out,
87 mach_info::float_format ff = mach_info::native_float_format (),
88 const std::string& encoding = "utf-8")
89 : base_strstream (arg_md, ff, encoding), m_istream (data) { }
90
91 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (istrstream)
92
93protected:
94
95 ~istrstream () = default;
96
97public:
98
99
100 static stream
101 create (const char *data, std::ios::openmode arg_md = std::ios::out,
102 mach_info::float_format ff = mach_info::native_float_format (),
103 const std::string& encoding = "utf-8");
104
105 static stream
106 create (const std::string& data, std::ios::openmode arg_md = std::ios::out,
107 mach_info::float_format ff = mach_info::native_float_format (),
108 const std::string& encoding = "utf-8");
109
110 // Return nonzero if EOF has been reached on this stream.
111
112 bool eof () const { return m_istream.eof (); }
113
114 std::istream * input_stream () { return &m_istream; }
115
116 std::ostream * output_stream () { return nullptr; }
117
118 off_t tell () { return m_istream.tellg (); }
119
120 std::streambuf * rdbuf ()
121 {
122 return m_istream ? m_istream.rdbuf () : nullptr;
123 }
124
125 bool bad () const { return m_istream.bad (); }
126
127 void clear () { m_istream.clear (); }
128
129private:
130
131 std::istringstream m_istream;
132};
133
135{
136public:
137
138 ostrstream (std::ios::openmode arg_md = std::ios::out,
139 mach_info::float_format ff = mach_info::native_float_format (),
140 const std::string& encoding = "utf-8")
141 : base_strstream (arg_md, ff, encoding), m_ostream () { }
142
143 OCTAVE_DISABLE_COPY_MOVE (ostrstream)
144
145protected:
146
147 ~ostrstream () = default;
148
149public:
150
151 static stream
152 create (std::ios::openmode arg_md = std::ios::out,
153 mach_info::float_format ff = mach_info::native_float_format (),
154 const std::string& encoding = "utf-8");
155
156 // Return nonzero if EOF has been reached on this stream.
157
158 bool eof () const { return m_ostream.eof (); }
159
160 std::istream * input_stream () { return nullptr; }
161
162 std::ostream * output_stream () { return &m_ostream; }
163
164 std::string str () { return m_ostream.str (); }
165
166 std::streambuf * rdbuf ()
167 {
168 return m_ostream ? m_ostream.rdbuf () : nullptr;
169 }
170
171 bool bad () const { return m_ostream.bad (); }
172
173 void clear () { m_ostream.clear (); }
174
175private:
176
177 std::ostringstream m_ostream;
178};
179
180OCTAVE_END_NAMESPACE(octave)
181
182#endif
std::string encoding() const
Definition oct-stream.h:184
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:42
std::string name() const
Definition oct-strstrm.h:66
virtual void clear()=0
virtual bool bad() const =0
virtual std::streambuf * rdbuf()=0
~base_strstream()=default
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:79
static stream create(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")
bool eof() const
bool bad() const
std::ostream * output_stream()
~istrstream()=default
void clear()
std::istream * input_stream()
std::streambuf * rdbuf()
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:85
off_t tell()
std::string str()
std::streambuf * rdbuf()
bool eof() const
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")
static stream create(std::ios::openmode arg_md=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
bool bad() const
std::istream * input_stream()
void clear()
~ostrstream()=default
std::ostream * output_stream()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn