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 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(IOstream::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 // ************************************************************************* //
token.H
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::token::Compound
A templated class for holding compound tokens.
Definition: token.H:246
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::charList
List< char > charList
A List of chars.
Definition: List.H:66
Foam::List::readList
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:49
charList.H
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
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::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Istream.H
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::transferCompoundToken
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:90
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::addCompoundToRunTimeSelectionTable
addCompoundToRunTimeSelectionTable(List< complex >, complexList)
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::defineCompoundTypeName
defineCompoundTypeName(List< complex >, complexList)
Foam::dynamicCast
To & dynamicCast(From &r)
Definition: typeInfo.H:88
Foam::List< char >
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::isCompound
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:716
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.