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