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-2023 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 \*---------------------------------------------------------------------------*/
27 
28 #include "error.H"
29 #include "OTstream.H"
30 #include <cctype>
31 
32 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
33 
34 bool 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
51  tokens().push_back(token(token::punctuationToken(c)));
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() = 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 
84 Foam::Ostream& Foam::OTstream::write(const char* str)
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 
103 Foam::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 
112 Foam::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 
121 Foam::Ostream& Foam::OTstream::write(const int32_t val)
122 {
123  tokens().push_back(token(label(val))); // tokenType::LABEL
124 
125  return *this;
126 }
127 
128 
129 Foam::Ostream& Foam::OTstream::write(const int64_t val)
130 {
131  tokens().push_back(token(label(val))); // tokenType::LABEL
132 
133  return *this;
134 }
135 
136 
137 Foam::Ostream& Foam::OTstream::write(const float val)
138 {
139  tokens().push_back(token(val)); // tokenType::FLOAT
140 
141  return *this;
142 }
143 
144 
145 Foam::Ostream& Foam::OTstream::write(const double val)
146 {
147  tokens().push_back(token(val)); // tokenType::DOUBLE
148 
149  return *this;
150 }
151 
152 
153 Foam::Ostream& Foam::OTstream::write(const char* data, std::streamsize count)
154 {
155  // if (format() != IOstreamOption::BINARY)
156  // {
157  // FatalErrorInFunction
158  // << "stream format not binary"
159  // << Foam::abort(FatalError);
160  // }
163  return *this;
164 }
165 
166 
168 (
169  const char* data,
170  std::streamsize count
171 )
172 {
173  // No check for IOstreamOption::BINARY since this is either done in the
174  // beginRawWrite() method, or the caller knows what they are doing.
175 
177  return *this;
178 }
179 
180 
181 bool Foam::OTstream::beginRawWrite(std::streamsize count)
182 {
183  // if (format() != IOstreamOption::BINARY)
184  // {
185  // FatalErrorInFunction
186  // << "stream format not binary"
187  // << Foam::abort(FatalError);
188  // }
189 
191  return true;
192 }
193 
194 
195 void Foam::OTstream::print(Ostream& os) const
196 {
197  os << "OTstream : " << name().c_str() << ", " << size() << " tokens, ";
199 }
200 
201 
202 // ************************************************************************* //
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:507
A token holds an item read from Istream.
Definition: token.H:65
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition: OTstream.C:174
const DynamicList< token > & tokens() const noexcept
The tokens.
Definition: OTstream.H:109
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: OTstream.C:27
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition: OTstream.C:161
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:126
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:67
void push_back(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:555
const dimensionedScalar c
Speed of light in a vacuum.
void print(Ostream &os) const override
Print stream description to Ostream.
Definition: OTstream.C:188
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:69
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
A class for handling character strings derived from std::string.
Definition: string.H:72