GNU Octave  9.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-2024 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 
38 class
40 {
41 public:
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  OCTAVE_DISABLE_COPY_MOVE (base_strstream)
50 
51 protected:
52 
53  ~base_strstream () = default;
54 
55 public:
56 
57  // Position a stream at OFFSET relative to ORIGIN.
58 
59  int seek (off_t, int);
60 
61  // Return current stream position.
62 
63  virtual off_t tell ();
64 
65  // The name of the file.
66 
67  std::string name () const { return ""; }
68 
69  virtual std::streambuf * rdbuf () = 0;
70 
71  virtual bool bad () const = 0;
72 
73  virtual void clear () = 0;
74 };
75 
76 class
78 {
79 public:
80 
81  istrstream (const char *data,
82  std::ios::openmode arg_md = std::ios::out,
84  const std::string& encoding = "utf-8")
85  : base_strstream (arg_md, ff, encoding), m_istream (data) { }
86 
87  istrstream (const std::string& data,
88  std::ios::openmode arg_md = std::ios::out,
90  const std::string& encoding = "utf-8")
91  : base_strstream (arg_md, ff, encoding), m_istream (data) { }
92 
93  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (istrstream)
94 
95 protected:
96 
97  ~istrstream () = default;
98 
99 public:
100 
101 
102  static stream
103  create (const char *data, std::ios::openmode arg_md = std::ios::out,
105  const std::string& encoding = "utf-8");
106 
107  static stream
108  create (const std::string& data, std::ios::openmode arg_md = std::ios::out,
110  const std::string& encoding = "utf-8");
111 
112  // Return nonzero if EOF has been reached on this stream.
113 
114  bool eof () const { return m_istream.eof (); }
115 
116  std::istream * input_stream () { return &m_istream; }
117 
118  std::ostream * output_stream () { return nullptr; }
119 
120  off_t tell () { return m_istream.tellg (); }
121 
122  std::streambuf * rdbuf ()
123  {
124  return m_istream ? m_istream.rdbuf () : nullptr;
125  }
126 
127  bool bad () const { return m_istream.bad (); }
128 
129  void clear () { m_istream.clear (); }
130 
131 private:
132 
133  std::istringstream m_istream;
134 };
135 
136 class
137 ostrstream : public base_strstream
138 {
139 public:
140 
141  ostrstream (std::ios::openmode arg_md = std::ios::out,
143  const std::string& encoding = "utf-8")
144  : base_strstream (arg_md, ff, encoding), m_ostream () { }
145 
146  OCTAVE_DISABLE_COPY_MOVE (ostrstream)
147 
148 protected:
149 
150  ~ostrstream () = default;
151 
152 public:
153 
154  static stream
155  create (std::ios::openmode arg_md = std::ios::out,
157  const std::string& encoding = "utf-8");
158 
159  // Return nonzero if EOF has been reached on this stream.
160 
161  bool eof () const { return m_ostream.eof (); }
162 
163  std::istream * input_stream () { return nullptr; }
164 
165  std::ostream * output_stream () { return &m_ostream; }
166 
167  std::string str () { return m_ostream.str (); }
168 
169  std::streambuf * rdbuf ()
170  {
171  return m_ostream ? m_ostream.rdbuf () : nullptr;
172  }
173 
174  bool bad () const { return m_ostream.bad (); }
175 
176  void clear () { m_ostream.clear (); }
177 
178 private:
179 
180  std::ostringstream m_ostream;
181 };
182 
183 OCTAVE_END_NAMESPACE(octave)
184 
185 #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
std::string name() const
Definition: oct-strstrm.h:67
virtual std::streambuf * rdbuf()=0
virtual void clear()=0
virtual bool bad() const =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:81
std::ostream * output_stream()
Definition: oct-strstrm.h:118
bool eof() const
Definition: oct-strstrm.h:114
bool bad() const
Definition: oct-strstrm.h:127
~istrstream()=default
void clear()
Definition: oct-strstrm.h:129
std::streambuf * rdbuf()
Definition: oct-strstrm.h:122
std::istream * input_stream()
Definition: oct-strstrm.h:116
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:87
off_t tell()
Definition: oct-strstrm.h:120
std::string str()
Definition: oct-strstrm.h:167
bool eof() const
Definition: oct-strstrm.h:161
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:141
bool bad() const
Definition: oct-strstrm.h:174
std::istream * input_stream()
Definition: oct-strstrm.h:163
void clear()
Definition: oct-strstrm.h:176
~ostrstream()=default
std::streambuf * rdbuf()
Definition: oct-strstrm.h:169
std::ostream * output_stream()
Definition: oct-strstrm.h:165
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
float_format native_float_format()
Definition: mach-info.cc:67
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781