Loading...
Searching...
No Matches
OTstream.C
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) 2019-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
26\*---------------------------------------------------------------------------*/
28#include "error.H"
29#include "OTstream.H"
30#include <cctype>
31
32// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
33
34bool Foam::OTstream::write(const token& tok)
35{
36 if (tok.good())
37 {
38 tokens().push_back(tok);
39 return true;
40 }
41
42 return false;
43}
44
45
47{
48 if (!std::isspace(c) && std::isprint(c))
49 {
50 // Should generally work, but need to verify corner cases
52 }
53
54 return *this;
55}
56
57
59(
60 const char* str,
61 std::streamsize len,
62 const bool quoted
63)
64{
65 if (quoted)
66 {
67 // tokenType::STRING
68 tokens().emplace_back() = Foam::string(str, len);
69 }
70 else if (len > 0)
71 {
72 // Create from std::string with specified type never strips
73 tokens().emplace_back
74 (
75 token::tokenType::WORD, // or perhaps tokenType::CHAR_DATA ?
76 std::string(str, len)
77 );
78 }
79
80 return *this;
81}
82
83
85{
86 word nonWhiteChars(string::validate<word>(str));
87
88 if (nonWhiteChars.size() == 1)
89 {
90 // Like punctuation
91 write(nonWhiteChars[0]);
92 }
93 else if (nonWhiteChars.size())
94 {
95 // As a word
96 tokens().emplace_back() = std::move(nonWhiteChars); // Move assign
97 }
98
99 return *this;
100}
101
102
103Foam::Ostream& Foam::OTstream::write(const word& str)
104{
105 // tokenType::WORD
106 tokens().emplace_back() = str; // Copy assign
107
108 return *this;
109}
110
111
112Foam::Ostream& Foam::OTstream::write(const std::string& str)
113{
114 // tokenType::STRING
115 tokens().emplace_back() = Foam::string(str); // Move assign
116
117 return *this;
118}
119
120
121Foam::Ostream& Foam::OTstream::write(const int32_t val)
123 tokens().emplace_back().int32Token(val);
124
125 return *this;
126}
127
128
130{
131 tokens().emplace_back().int64Token(val);
132 return *this;
133}
134
135
136Foam::Ostream& Foam::OTstream::write(const uint32_t val)
138 tokens().emplace_back().uint32Token(val);
139
140 return *this;
141}
142
143
145{
146 tokens().emplace_back().uint64Token(val);
147 return *this;
148}
149
150
152{
153 tokens().emplace_back().floatToken(val);
154 return *this;
155}
156
157
159{
160 tokens().emplace_back().doubleToken(val);
161 return *this;
162}
163
164
165Foam::Ostream& Foam::OTstream::write(const char* data, std::streamsize count)
166{
167 // if (format() != IOstreamOption::BINARY)
168 // {
169 // FatalErrorInFunction
170 // << "stream format not binary"
171 // << Foam::abort(FatalError);
172 // }
173
175 return *this;
176}
177
178
180(
181 const char* data,
182 std::streamsize count
183)
184{
185 // No check for IOstreamOption::BINARY since this is either done in the
186 // beginRawWrite() method, or the caller knows what they are doing.
187
189 return *this;
190}
191
192
193bool Foam::OTstream::beginRawWrite(std::streamsize count)
194{
195 // if (format() != IOstreamOption::BINARY)
196 // {
197 // FatalErrorInFunction
198 // << "stream format not binary"
199 // << Foam::abort(FatalError);
200 // }
201
203 return true;
204}
205
206
208{
209 os << "OTstream : " << name().c_str() << ", " << size() << " tokens, ";
211}
212
213
214// ************************************************************************* //
void push_back(const T &val)
Copy append an element to the end of this list.
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition IOstream.C:87
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OTstream.C:52
void print(Ostream &os) const override
Print stream description to Ostream.
Definition OTstream.C:200
const DynamicList< token > & tokens() const noexcept
The tokens.
Definition OTstream.H:109
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition OTstream.C:173
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition OTstream.C:186
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition OTstream.C:27
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Ostream(const Ostream &)=default
Copy construct.
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:274
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A class for handling character strings derived from std::string.
Definition string.H:76
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition stringI.H:193
A token holds an item read from Istream.
Definition token.H:70
@ WORD
Foam::word.
Definition token.H:97
punctuationToken
Standard punctuation tokens (a character).
Definition token.H:140
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition tokenI.H:584
A class for handling words, derived from Foam::string.
Definition word.H:66
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition error.H:688
OBJstream os(runTime.globalPath()/outputName)
auto & name
runTime write()