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-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 
26 InClass
27  Foam::StringStream
28 
29 Description
30  Input/output from string buffers.
31 
32 SourceFiles
33  StringStream.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_StringStream_H
38 #define Foam_StringStream_H
39 
40 #include "ISstream.H"
41 #include "OSstream.H"
42 #include <sstream>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class IStringStream Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 //- Input from string buffer, using a ISstream. Always UNCOMPRESSED.
54 class IStringStream
55 :
56  public Foam::Detail::StreamAllocator<std::istringstream>,
57  public Foam::ISstream
58 {
59  typedef
62 
63 public:
64 
65  // Constructors
66 
67  //- Default construct or with specified stream option
68  explicit IStringStream
69  (
70  IOstreamOption streamOpt = IOstreamOption()
71  )
72  :
74  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
75  {}
76 
77  //- Construct from std::string
78  explicit IStringStream
79  (
80  const std::string& s,
82  )
83  :
85  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
86  {
87  stream_.str(s);
88  }
89 
90  //- Construct from char*
91  explicit IStringStream
92  (
93  const char* s,
94  IOstreamOption streamOpt = IOstreamOption()
95  )
96  :
98  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
99  {
100  stream_.str(s);
101  }
102 
103  //- Copy construct, copies content and format
105  :
106  allocator_type(),
107  ISstream(stream_, str.name(), static_cast<IOstreamOption>(str))
108  {
109  stream_.str(str.str());
110  }
111 
112 
113  // Member Functions
114 
115  //- Get the string.
116  //- As Foam::string instead of std::string (may change in future)
117  Foam::string str() const { return Foam::string(stream_.str()); }
118 
119  //- Set the string
120  void str(const std::string& s) { stream_.str(s); }
121 
122 
123  //- Reset the input buffer and rewind the stream
124  virtual void reset(const std::string& s)
125  {
126  this->str(s);
127  this->rewind();
128  }
129 
130  //- Print stream description to Ostream
131  virtual void print(Ostream& os) const override;
132 
133 
134  // Member Operators
135 
136  //- Return a non-const reference to const Istream
137  // Needed for read-constructors where the stream argument is temporary.
138  Istream& operator()() const
139  {
140  // Could also rewind
141  return const_cast<IStringStream&>(*this);
142  }
143 
144 
145  // Additional constructors and methods (as per v2012 and earlier)
146  #ifdef Foam_IOstream_extras
147 
148  //- Construct empty with given format
150  :
152  {}
153 
154  //- Construct from std::string with given format
156  (
157  const std::string& s,
159  )
160  :
162  {}
163 
164  //- Construct from char* with given format
166  (
167  const char* s,
169  )
170  :
172  {}
173 
174  #endif /* Foam_IOstream_extras */
175 };
176 
177 
178 /*---------------------------------------------------------------------------*\
179  Class OStringStream Declaration
180 \*---------------------------------------------------------------------------*/
181 
182 //- Output to string buffer, using a OSstream. Always UNCOMPRESSED.
183 class OStringStream
184 :
185  public Foam::Detail::StreamAllocator<std::ostringstream>,
186  public Foam::OSstream
187 {
188  typedef
190  allocator_type;
191 
192 public:
193 
194  // Constructors
195 
196  //- Default construct or with specified stream option
197  explicit OStringStream
198  (
199  IOstreamOption streamOpt = IOstreamOption()
200  )
201  :
202  allocator_type(),
203  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
204  {}
205 
206  //- Copy construct, copies content and format
208  :
209  allocator_type(),
210  OSstream(stream_, str.name(), static_cast<IOstreamOption>(str))
211  {
212  stream_.str(str.str());
213  }
214 
215 
216  // Member Functions
217 
218  //- The number of bytes outputted
219  std::streamsize count() { return stream_.tellp(); }
220 
221  //- Get the string.
222  //- As Foam::string instead of std::string (may change in future)
223  Foam::string str() const { return Foam::string(stream_.str()); }
224 
225  //- Set the string
226  void str(const std::string& s) { stream_.str(s); }
227 
228 
229  //- Reset the output buffer and rewind the stream
230  void reset()
231  {
232  this->str(""); // No other way to reset the end
233  this->rewind();
234  }
235 
236  //- Rewind the output stream
237  virtual void rewind()
238  {
239  stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
240  }
241 
242  //- Print stream description to Ostream
243  virtual void print(Ostream& os) const override;
244 
245 
246  // Older style, without stream option (including 2012 release)
247  #ifdef Foam_IOstream_extras
248 
249  //- Construct empty with given format
251  :
253  {}
254 
255  #endif /* Foam_IOstream_extras */
256 };
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 } // End namespace Foam
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 #endif
266 
267 // ************************************************************************* //
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:267
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:25
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: StringStream.H:155
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A simple container for options an IOstream can normally have.
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
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:276
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future)
Definition: StringStream.H:256
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
virtual void reset(const std::string &s)
Reset the input buffer and rewind the stream.
Definition: StringStream.H:136
std::istringstream stream_
The std::stream.
Definition: IOstream.H:609
OStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:225
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
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future)
Definition: StringStream.H:125
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: StringStream.C:36
std::streamsize count()
The number of bytes outputted.
Definition: StringStream.H:250
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:52
versionNumber version() const noexcept
Get the stream version.
streamFormat
Data format (ascii | binary)
IStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:69
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: StringStream.C:25
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))
A class for handling character strings derived from std::string.
Definition: string.H:72
OSstream(const OSstream &)=default
Copy construct.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:208
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.