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 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // Write tokens without keyword, suppress/ignore bad tokens.
38 // Mostly like primitiveEntry::write(os, false);
39 
40 static void writeTokens(Ostream& os, const tokenList& 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  {
52  os << token::SPACE;
53  }
54  else
55  {
56  started = true;
57  }
58 
59  // Output token with direct handling in Ostream(s),
60  // or use 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; // already have newline as separator
77  }
78  }
79  }
80 
81  // Always finish up with a newline?
82  // eg,
83  //
84  // if (started)
85  // {
86  // os << nl;
87  // }
88 }
89 
90 } // End namespace Foam
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 (
97  const keyType& key,
98  const char* s,
99  std::streamsize len
100 )
101 :
102  primitiveEntry(key, token::undefinedToken) // Construct with one token
103 {
104  if (s)
105  {
107  token(token::tokenType::CHAR_DATA, std::string(s, len));
108  }
109 }
110 
111 
113 (
114  const keyType& key,
115  const std::string& content
116 )
117 :
118  primitiveEntry
119  (
121  token(token::tokenType::CHAR_DATA, std::move(content))
122  )
123 {}
124 
125 
127 (
128  const keyType& key,
129  std::string&& content
130 )
131 :
133  (
134  key,
135  token(token::tokenType::CHAR_DATA, std::move(content))
136  )
137 {}
138 
139 
140 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
141 
143 {
144  writeTokens(os, *this);
145 }
146 
147 
148 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
virtual Ostream & write(const char c) override
Write character.
Definition: OBJstream.C:69
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
A token holds an item read from Istream.
Definition: token.H:65
T & front()
Access first element of the list, position [0].
Definition: UListI.H:237
formattingEntry(const keyType &key, const char *s, std::streamsize len)
Construct with character data, using the provided keyword.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
Space [isspace].
Definition: token.H:131
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 write(Ostream &os) const
Write content without the keyword.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
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))
static void writeTokens(Ostream &os, const tokenList &toks)
Namespace for OpenFOAM.