LListIO.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) 2017-2022 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 "LList.H"
30#include "Istream.H"
31#include "Ostream.H"
32
33// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34
35template<class LListBase, class T>
37{
38 operator>>(is, *this);
39}
40
41
42// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
43
44template<class LListBase, class T>
46{
47 LList<LListBase, T>& list = *this;
48
49 // Anull list
50 list.clear();
51
53
54 token tok(is);
55
56 is.fatalCheck("LList::readList : reading first token");
57
58 if (tok.isLabel())
59 {
60 const label len = tok.labelToken();
61
62 // Begin of contents marker
63 const char delimiter = is.readBeginList("LList");
64
65 if (len)
66 {
67 if (delimiter == token::BEGIN_LIST)
68 {
69 for (label i=0; i<len; ++i)
70 {
71 T elem;
72 is >> elem;
73 list.append(std::move(elem));
74 }
75 }
76 else
77 {
78 // Uniform content (delimiter == token::BEGIN_BLOCK)
79
80 T elem;
81 is >> elem;
82
83 for (label i=0; i<len; ++i)
84 {
85 list.append(elem);
86 }
87 }
88 }
89
90 // End of contents marker
91 is.readEndList("LList");
92 }
93 else if (tok.isPunctuation(token::BEGIN_LIST))
94 {
95 is >> tok;
97
98 while (!tok.isPunctuation(token::END_LIST))
99 {
100 is.putBack(tok);
101
102 T elem;
103 is >> elem;
104 list.append(std::move(elem));
105
106 is >> tok;
108 }
109 }
110 else
111 {
113 << "incorrect first token, expected <int> or '(', found "
114 << tok.info()
115 << exit(FatalIOError);
116 }
117
119 return is;
120}
121
122
123template<class LListBase, class T>
125(
126 Ostream& os,
127 const label shortLen
128) const
129{
130 // NB: no binary, contiguous output
131
132 const label len = this->size();
133
134 if
135 (
136 (len <= 1 || !shortLen)
137 || (len <= shortLen)
138 )
139 {
140 // Size and start delimiter
141 os << len << token::BEGIN_LIST;
142
143 // Contents
144 bool space = false;
145 for (const T& val : *this)
146 {
147 if (space) os << token::SPACE;
148 os << val;
149 space = true;
150 }
151
152 // End delimiter
153 os << token::END_LIST;
154 }
155 else
156 {
157 // Size and start delimiter
158 os << nl << len << nl << token::BEGIN_LIST << nl;
159
160 // Contents
161 for (const T& val : *this)
162 {
163 os << val << nl;
164 }
165
166 // End delimiter
167 os << token::END_LIST;
168 }
169
170 os.check(FUNCTION_NAME);
171 return os;
172}
173
174
175template<class LListBase, class T>
177{
178 return list.readList(is);
179}
180
181
182template<class LListBase, class T>
183Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& list)
184{
185 return list.writeList(os, -1); // Always with line breaks
186}
187
188
189// ************************************************************************* //
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Template class for non-intrusive linked lists.
Definition: LList.H:79
LList()=default
Default construct.
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write LList with line-breaks when length exceeds shortLen.
Definition: LListIO.C:125
void clear()
Delete contents of list.
Definition: LList.C:78
Istream & readList(Istream &is)
Read list from Istream.
Definition: LListIO.C:45
void append(const T &elem)
Add copy at back of list.
Definition: LList.H:245
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A token holds an item read from Istream.
Definition: token.H:69
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53