Ostream.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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-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 "word.H"
30 #include "Ostream.H"
31 #include "token.H"
32 #include "keyType.H"
33 #include "IOstreams.H"
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
38 {
39  if (!indentLevel_)
40  {
41  std::cerr
42  << "Ostream::decrIndent() : attempt to decrement 0 indent level\n";
43  }
44  else
45  {
46  --indentLevel_;
47  }
48 }
49 
50 
52 (
53  const std::string& str,
54  const bool quoted
55 )
56 {
57  return writeQuoted(str.data(), str.size(), quoted);
58 }
59 
60 
62 {
63  return writeQuoted(kw.data(), kw.size(), kw.isPattern());
64 }
65 
66 
68 {
69  indent();
70  writeQuoted(kw.data(), kw.size(), kw.isPattern());
71 
72  if (indentSize_ <= 1)
73  {
74  write(char(token::SPACE));
75  return *this;
76  }
77 
78  label padding = (entryIndentation_ - label(kw.size()));
79 
80  // Account for quotes surrounding pattern
81  if (kw.isPattern())
82  {
83  padding -= 2;
84  }
85 
86  // Write padding spaces (always at least one)
87  do
88  {
89  write(char(token::SPACE));
90  }
91  while (--padding > 0);
92 
93  return *this;
94 }
95 
96 
97 Foam::Ostream& Foam::Ostream::beginBlock(const keyType& kw)
98 {
99  indent(); writeQuoted(kw.data(), kw.size(), kw.isPattern()); write('\n');
100  beginBlock();
101 
102  return *this;
103 }
104 
105 
107 {
109  incrIndent();
110 
111  return *this;
112 }
113 
114 
116 {
118  indent(); write(char(token::END_BLOCK)); write('\n');
119 
120  return *this;
121 }
122 
123 
125 {
126  write(char(token::END_STATEMENT)); write('\n');
127 
128  return *this;
129 }
130 
131 
132 // ************************************************************************* //
Begin block [isseparator].
Definition: token.H:165
A class for handling keywords in dictionaries.
Definition: keyType.H:66
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:77
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
virtual Ostream & beginBlock()
Write begin block group without a name.
Definition: Ostream.C:99
End entry [isseparator].
Definition: token.H:160
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
void decrIndent()
Decrement the indent level.
Definition: Ostream.C:30
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
Space [isspace].
Definition: token.H:131
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:97
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition: Ostream.H:543
virtual Ostream & endEntry()
Write end entry (&#39;;&#39;) followed by newline.
Definition: Ostream.C:117
End block [isseparator].
Definition: token.H:166
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:502
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:60