Loading...
Searching...
No Matches
StringStream.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2017-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26InClass
27 Foam::StringStream
28
29Description
30 Input/output from string buffers.
31
32\*---------------------------------------------------------------------------*/
33
34#ifndef Foam_StringStream_H
35#define Foam_StringStream_H
36
37#include "ISstream.H"
38#include "OSstream.H"
39#include <sstream>
40
41// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42
43namespace Foam
44{
45
46/*---------------------------------------------------------------------------*\
47 Class IStringStream Declaration
48\*---------------------------------------------------------------------------*/
49
50//- Input from string buffer, using a ISstream. Always UNCOMPRESSED.
51class IStringStream
52:
53 public Foam::Detail::StreamAllocator<std::istringstream>,
54 public Foam::ISstream
55{
56 typedef
58 allocator_type;
59
60public:
61
62 // Constructors
64 //- Default construct or with specified stream option
65 explicit IStringStream
66 (
67 IOstreamOption streamOpt = IOstreamOption()
68 )
69 :
70 allocator_type(),
71 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
72 {}
73
74 //- Construct from std::string
76 (
77 const std::string& s,
78 IOstreamOption streamOpt = IOstreamOption()
79 )
80 :
81 allocator_type(),
82 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
83 {
84 stream_.str(s);
85 }
86
87 //- Construct from char*
88 explicit IStringStream
89 (
90 const char* s,
91 IOstreamOption streamOpt = IOstreamOption()
92 )
93 :
94 allocator_type(),
95 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
96 {
97 stream_.str(s);
98 }
99
100 //- Copy construct, copies content and format
102 :
103 allocator_type(),
104 ISstream(stream_, str.name(), static_cast<IOstreamOption>(str))
106 stream_.str(str.str());
107 }
108
109
110 // Member Functions
111
112 //- Get the string.
113 //- As Foam::string instead of std::string (may change in future)
114 Foam::string str() const { return Foam::string(stream_.str()); }
115
116 //- Set the string
117 void str(const std::string& s) { stream_.str(s); }
118
119
120 //- Reset the input buffer and rewind the stream
121 virtual void reset(const std::string& s)
122 {
123 this->str(s);
124 this->rewind();
126
127 //- Print stream description
128 virtual void print(Ostream& os) const override
129 {
130 os << "istringstream : buffer =\n" << this->str() << '\n';
132 IOstream::print(os, stream_.rdstate());
133 }
134
135
136 // Member Operators
137
138 //- Return a non-const reference to const Istream
139 // Needed for read-constructors where the stream argument is temporary.
141 {
142 // Could also rewind
143 return const_cast<IStringStream&>(*this);
144 }
145
146
147 // Additional constructors and methods (as per v2012 and earlier)
148 #ifdef Foam_IOstream_extras
149
150 //- Construct empty with given format
152 :
154 {}
156 //- Construct from std::string with given format
158 (
159 const std::string& s,
161 )
162 :
164 {}
165
166 //- Construct from char* with given format
168 (
169 const char* s,
171 )
172 :
174 {}
175
176 #endif /* Foam_IOstream_extras */
177};
178
179
180/*---------------------------------------------------------------------------*\
181 Class OStringStream Declaration
182\*---------------------------------------------------------------------------*/
183
184//- Output to string buffer, using a OSstream. Always UNCOMPRESSED.
185class OStringStream
186:
187 public Foam::Detail::StreamAllocator<std::ostringstream>,
188 public Foam::OSstream
189{
190 typedef
191 Foam::Detail::StreamAllocator<std::ostringstream>
192 allocator_type;
193
194public:
195
196 // Constructors
197
198 //- Default construct or with specified stream option
199 explicit OStringStream
200 (
201 IOstreamOption streamOpt = IOstreamOption()
202 )
203 :
204 allocator_type(),
205 OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
206 {}
207
208 //- Copy construct, copies content and format
210 :
211 allocator_type(),
212 OSstream(stream_, str.name(), static_cast<IOstreamOption>(str))
213 {
214 stream_.str(str.str());
215 }
216
217
218 // Member Functions
219
220 //- Get the string.
221 //- As Foam::string instead of std::string (may change in future)
222 Foam::string str() const { return Foam::string(stream_.str()); }
223
224 //- Set the string
225 void str(const std::string& s) { stream_.str(s); }
226
227
228 //- Reset the output buffer and rewind the stream
229 void reset()
230 {
231 this->str(""); // No other way to reset the end
232 this->rewind();
233 }
234
235 //- Rewind the output stream
236 virtual void rewind()
237 {
238 stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
239 }
240
241 //- Print stream description
242 virtual void print(Ostream& os) const override
243 {
244 os << "ostringstream : buffer =\n" << this->str() << '\n';
245 IOstream::print(os);
246 IOstream::print(os, stream_.rdstate());
247 }
248
249
250 // Older style, without stream option (including 2012 release)
251 #ifdef Foam_IOstream_extras
252
253 //- Construct empty with given format
255 :
257 {}
258
259 #endif /* Foam_IOstream_extras */
260};
261
263// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264
265} // End namespace Foam
266
267// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268
269#endif
270
271// ************************************************************************* //
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition IOstream.H:620
A simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression.
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary | coherent).
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition IOstream.C:87
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name).
Definition ISstream.H:147
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition ISstream.C:1170
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition ISstreamI.H:25
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Istream & operator()() const
Return a non-const reference to const Istream.
IStringStream(const std::string &s, IOstreamOption streamOpt=IOstreamOption())
Construct from std::string.
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future).
virtual void reset(const std::string &s)
Reset the input buffer and rewind the stream.
void str(const std::string &s)
Set the string.
IStringStream(const char *s, IOstreamOption streamOpt=IOstreamOption())
Construct from char*.
IStringStream(const IStringStream &str)
Copy construct, copies content and format.
virtual void print(Ostream &os) const override
Print stream description.
IStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name).
Definition OSstream.H:134
OSstream(const OSstream &)=default
Copy construct.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future).
void str(const std::string &s)
Set the string.
OStringStream(const OStringStream &str)
Copy construct, copies content and format.
OStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
virtual void print(Ostream &os) const override
Print stream description.
void reset()
Reset the output buffer and rewind the stream.
virtual void rewind()
Rewind the output stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling character strings derived from std::string.
Definition string.H:76
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127