Loading...
Searching...
No Matches
formattingEntry.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) 2023 Sergey Lesnik
9 Copyright (C) 2023-2025 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 "formattingEntry.H"
30#include "IOstreams.H"
31
32// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34namespace Foam
35{
36
37// Write tokens without keyword, suppress/ignore bad tokens.
38// Mostly like primitiveEntry::write(os, true);
39
40static void writeTokens(Ostream& os, const UList<token>& toks)
41{
42 bool started = false; // Separate from previous token?
43
44 for (const token& tok : toks)
45 {
46 if (!tok.good()) // silently ignore bad tokens
47 {
48 continue;
49 }
50 if (started)
51 {
53 }
54 else
55 {
56 started = true;
57 }
58
59 // Token output via direct handling in Ostream(s),
60 // or normal '<<' output operator
61 if (!os.write(tok))
62 {
63 os << tok;
64 }
65
66 if (tok.isCharData())
67 {
68 // If content appears to be a C++ comment
69 // - better make certain that it gets a newline
70 // or later parsing of it will be a problem
71
72 const auto& s = tok.stringToken();
73 if (s.starts_with("//") && !s.ends_with('\n'))
74 {
75 os << '\n';
76 started = false; // Does not need further space separator
77 }
78 }
79 }
81
82} // End namespace Foam
83
84
85// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86
88(
89 const keyType& key,
90 const char* s,
91 std::streamsize len
92)
93:
94 primitiveEntry(key, token::undefinedToken) // Construct with one token
95{
96 if (s)
97 {
99 token(token::tokenType::CHAR_DATA, std::string(s, len));
100 }
101}
102
103
105(
106 const keyType& key,
107 const std::string& content
108)
109:
110 primitiveEntry
112 key,
113 token(token::tokenType::CHAR_DATA, content)
114 )
115{}
116
117
119(
120 const keyType& key,
121 std::string&& content
122)
123:
125 (
126 key,
127 token(token::tokenType::CHAR_DATA, std::move(content))
128 )
129{}
130
131
132// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133
135{
136 if (active_)
137 {
138 writeTokens(os, *this);
139 }
140}
141
142
143// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
T & front()
Access first element of the list, position [0].
Definition UListI.H:239
formattingEntry(const keyType &key, const char *s, std::streamsize len)
Construct with character data, using the provided keyword.
primitiveEntry(const keyType &key)
Inherit all constructors from primitiveEntry.
virtual void write(Ostream &os) const
Write content without the keyword.
A class for handling keywords in dictionaries.
Definition keyType.H:69
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
A token holds an item read from Istream.
Definition token.H:70
@ CHAR_DATA
String-variant: plain character content.
Definition token.H:110
@ SPACE
Space [isspace].
Definition token.H:144
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.
static void writeTokens(Ostream &os, const UList< token > &toks)