charUList.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"
32 
33 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 static Ostream& writeChars
39 (
40  Ostream& os,
41  const char* chars,
42  std::streamsize count
43 )
44 {
45  // Contents are binary and contiguous
46  os << nl << label(count) << nl;
47 
48  if (count)
49  {
50  const auto oldFmt = os.format(IOstream::BINARY);
51 
52  // write(...) includes surrounding start/end delimiters
53  os.write(chars, count);
54 
55  os.format(oldFmt);
56  }
57 
59  return os;
60 }
61 
62 } // End namespace Foam
63 
64 
65 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
66 
67 namespace Foam
68 {
69 
70 template<>
72 {
73  const std::streamsize count(this->size());
74 
75  os << word("List<char>");
76 
77  if (count)
78  {
79  writeChars(os, this->cdata(), count);
80  }
81  else
82  {
83  // Zero-sized binary - Write size only
84  os << token::SPACE << label(0);
85  }
86 }
87 
88 } // End namespace Foam
89 
90 
91 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
92 
93 namespace Foam
94 {
95 
96 template<>
98 (
99  Ostream& os,
100  const label /* shortLen (unused) */
101 ) const
102 {
103  return writeChars(os, this->cdata(), std::streamsize(this->size()));
104 }
105 
106 
107 template<>
109 {
110  UList<char>& list = *this;
111 
112  // The target list length - must match with sizes read
113  const label len = list.size();
114 
116 
117  token tok(is);
118 
119  is.fatalCheck("UList<char>::readList(Istream&) : reading first token");
120 
121  if (tok.isCompound())
122  {
123  // Compound: simply transfer contents
124 
125  List<char> elems;
126  elems.transfer
127  (
129  (
130  tok.transferCompoundToken(is)
131  )
132  );
133 
134  const label inputLen = elems.size();
135 
136  // List lengths must match
137  if (inputLen != len)
138  {
140  << "incorrect length for UList. Read "
141  << inputLen << " expected " << len
142  << exit(FatalIOError);
143  }
144 
145  this->deepCopy(elems);
146  }
147  if (tok.isLabel())
148  {
149  // Label: could be int(..) or just a plain '0'
150 
151  const label inputLen = tok.labelToken();
152 
153  // List lengths must match
154  if (inputLen != len)
155  {
157  << "incorrect length for UList. Read "
158  << inputLen << " expected " << len
159  << exit(FatalIOError);
160  }
161 
162  // Binary, always contiguous
163 
164  if (len)
165  {
166  const auto oldFmt = is.format(IOstream::BINARY);
167 
168  // read(...) includes surrounding start/end delimiters
169  is.read(list.data(), std::streamsize(len));
170 
171  is.format(oldFmt);
172 
173  is.fatalCheck
174  (
175  "UList<char>::readList(Istream&) : "
176  "reading binary block"
177  );
178  }
179  }
180  else
181  {
183  << "incorrect first token, expected <int>, found "
184  << tok.info() << nl
185  << exit(FatalIOError);
186  }
187 
188  return is;
189 }
190 
191 } // End namespace Foam
192 
193 // ************************************************************************* //
Foam::UList::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:79
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
Foam::writeChars
static Ostream & writeChars(Ostream &os, const char *chars, std::streamsize count)
Definition: charUList.C:39
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::token::Compound
A templated class for holding compound tokens.
Definition: token.H:246
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::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
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
Foam::UList::readList
Istream & readList(Istream &is)
Read List contents from Istream.
Definition: UListIO.C:157
Istream.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::UList::data
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
os
OBJstream os(runTime.globalPath()/outputName)
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::UList::writeEntry
void writeEntry(Ostream &os) const
Write the UList with its compound type.
Definition: UListIO.C:38
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::dynamicCast
To & dynamicCast(From &r)
Definition: typeInfo.H:88
Foam::List< char >
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
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::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.