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 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  // Anull list
48  lst.clear();
49 
51 
52  token firstToken(is);
53 
54  is.fatalCheck("LList::readList : reading first token");
55 
56  if (firstToken.isLabel())
57  {
58  const label len = firstToken.labelToken();
59 
60  // Read beginning of contents
61  const char delimiter = is.readBeginList("LList");
62 
63  if (len)
64  {
65  if (delimiter == token::BEGIN_LIST)
66  {
67  for (label i=0; i<len; ++i)
68  {
69  T element;
70  is >> element;
71  lst.append(element);
72  }
73  }
74  else
75  {
76  T element;
77  is >> element;
78 
79  for (label i=0; i<len; ++i)
80  {
81  lst.append(element);
82  }
83  }
84  }
85 
86  // Read end of contents
87  is.readEndList("LList");
88  }
89  else if (firstToken.isPunctuation())
90  {
91  if (firstToken.pToken() != token::BEGIN_LIST)
92  {
94  << "incorrect first token, '(', found " << firstToken.info()
95  << exit(FatalIOError);
96  }
97 
98  token lastToken(is);
100 
101  while
102  (
103  !(
104  lastToken.isPunctuation()
105  && lastToken.pToken() == token::END_LIST
106  )
107  )
108  {
109  is.putBack(lastToken);
110 
111  T element;
112  is >> element;
113  lst.append(element);
114 
115  is >> lastToken;
117  }
118  }
119  else
120  {
122  << "incorrect first token, expected <int> or '(', found "
123  << firstToken.info()
124  << exit(FatalIOError);
125  }
126 
128  return is;
129 }
130 
131 
132 template<class LListBase, class T>
134 (
135  Ostream& os,
136  const label shortLen
137 ) const
138 {
139  const label len = this->size();
140 
141  if
142  (
143  (len <= 1 || !shortLen)
144  || (len <= shortLen)
145  )
146  {
147  // Size and start delimiter
148  os << len << token::BEGIN_LIST;
149 
150  // Contents
151  bool space = false;
152  for (const T& val : *this)
153  {
154  if (space) os << token::SPACE;
155  os << val;
156  space = true;
157  }
158 
159  // End delimiter
160  os << token::END_LIST;
161  }
162  else
163  {
164  // Size and start delimiter
165  os << nl << len << nl << token::BEGIN_LIST << nl;
166 
167  // Contents
168  for (const T& val : *this)
169  {
170  os << val << nl;
171  }
172 
173  // End delimiter
174  os << token::END_LIST;
175  }
176 
177  os.check(FUNCTION_NAME);
178  return os;
179 }
180 
181 
182 template<class LListBase, class T>
184 {
185  return lst.writeList(os, -1); // Always with line breaks
186 }
187 
188 
189 // ************************************************************************* //
Foam::LList::append
void append(const T &item)
Add copy at tail of list.
Definition: LList.H:237
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
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::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Istream.H
LList.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
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:385
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::LList::LList
LList()=default
Null 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:134