Loading...
Searching...
No Matches
prefixOSstream.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) 2011-2014,2024 OpenFOAM Foundation
9 Copyright (C) 2020-2023 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "prefixOSstream.H"
30#include "Pstream.H"
31#include "token.H"
32
33// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
34
35inline void Foam::prefixOSstream::checkWritePrefix()
36{
37 if (printPrefix_ && !prefix_.empty())
38 {
39 OSstream::write(prefix_.c_str());
40 printPrefix_ = false;
41 }
42}
43
44
45// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46
48(
49 std::ostream& os,
50 const string& streamName,
51 IOstreamOption streamOpt
52)
53:
54 OSstream(os, streamName, streamOpt),
55 printPrefix_(true),
56 prefix_()
57{}
58
59
60// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
61
63{
64 os << "prefixOSstream ";
66}
67
70{
71 return OSstream::write(tok);
72}
73
74
76{
77 checkWritePrefix();
80 // Reset prefix state on newline
81 if (c == token::NL) printPrefix_ = true;
82 return *this;
83}
84
85
87(
88 const char* str,
89 std::streamsize len,
90 const bool quoted
91)
93 checkWritePrefix();
94 OSstream::writeQuoted(str, len, quoted);
95 return *this;
96}
97
98
100{
101 checkWritePrefix();
102 OSstream::write(str);
103
104 const size_t len = strlen(str);
105 if (len > 0 && str[len-1] == token::NL)
106 {
107 // Reset prefix state on newline
108 printPrefix_ = true;
109 }
110
111 return *this;
112}
113
114
117 // Unquoted, and no newlines expected.
118 checkWritePrefix();
119 return OSstream::write(val);
120}
121
123Foam::Ostream& Foam::prefixOSstream::write(const std::string& str)
124{
125 return writeQuoted(str.data(), str.size(), true);
126}
127
128
130{
131 checkWritePrefix();
132 return OSstream::write(val);
133}
134
135
137{
138 checkWritePrefix();
139 return OSstream::write(val);
140}
141
142
144{
145 checkWritePrefix();
146 return OSstream::write(val);
147}
148
149
151{
152 checkWritePrefix();
153 return OSstream::write(val);
154}
155
156
158{
159 checkWritePrefix();
160 return OSstream::write(val);
161}
162
163
165{
166 checkWritePrefix();
167 return OSstream::write(val);
168}
169
170
172(
173 const char* buf,
174 std::streamsize count
176{
177 checkWritePrefix();
178 return OSstream::write(buf, count);
179}
180
181
183{
184 checkWritePrefix();
186}
187
188
189// ************************************************************************* //
A simple container for options an IOstream can normally have.
OSstream(const OSstream &)=default
Copy construct.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OSstream.C:101
virtual void print(Ostream &os) const override
Print stream description to Ostream.
virtual void indent() override
Add indentation characters.
Definition OSstream.C:294
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition OSstream.C:29
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
virtual void print(Ostream &os) const override
Print stream description to Ostream.
virtual void indent() override
Add indentation characters.
prefixOSstream(std::ostream &os, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct and set stream status.
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
A token holds an item read from Istream.
Definition token.H:70
@ NL
Newline [isspace].
Definition token.H:143
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)