charList.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) 2021-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "charList.H"
29 #include "Istream.H"
30 #include "Ostream.H"
31 #include "token.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineCompoundTypeName(List<char>, charList);
40 }
41 
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 template<>
50 {
51  List<char>& list = *this;
52 
53  // Anull list
54  list.clear();
55 
57 
58  token tok(is);
59 
60  is.fatalCheck("List<char>::readList(Istream&) : reading first token");
61 
62  if (tok.isCompound())
63  {
64  // Compound: simply transfer contents
65 
66  list.transfer
67  (
69  (
70  tok.transferCompoundToken(is)
71  )
72  );
73  }
74  else if (tok.isLabel())
75  {
76  // Label: could be int(..) or just a plain '0'
77 
78  const label len = tok.labelToken();
79 
80  // Resize to actual length read
81  list.resize(len);
82 
83  // Binary, always contiguous
84 
85  if (len)
86  {
87  const auto oldFmt = is.format(IOstreamOption::BINARY);
88 
89  // read(...) includes surrounding start/end delimiters
90  is.read(list.data(), std::streamsize(len));
91 
92  is.format(oldFmt);
93 
94  is.fatalCheck
95  (
96  "List<char>::readList(Istream&) : "
97  "reading binary block"
98  );
99  }
100  }
101  else
102  {
104  << "incorrect first token, expected <int>, found "
105  << tok.info() << nl
106  << exit(FatalIOError);
107  }
108 
109  return is;
110 }
111 
112 
113 } // End namespace Foam
114 
115 
116 // ************************************************************************* //
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:86
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
addCompoundToRunTimeSelectionTable(List< complex >, complexList)
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:132
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:452
defineCompoundTypeName(List< complex >, complexList)
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:709
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
A token holds an item read from Istream.
Definition: token.H:64
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:229
Macros for easy insertion into run-time selection tables.
A templated class for holding compound tokens.
Definition: token.H:281
virtual Istream & read(token &)=0
Return next token from stream.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:42
#define FUNCTION_NAME
Type & dynamicCast(U &obj)
A dynamic_cast (for references) that generates FatalError on failed casts.
Definition: typeInfo.H:111
List< char > charList
A List of chars.
Definition: List.H:61
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
label labelToken() const
Return label value.
Definition: tokenI.H:506
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:490
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:789
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...