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-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"
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(IOstreamOption::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(IOstreamOption::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 // ************************************************************************* //
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:71
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:118
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:439
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
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:230
Istream & readList(Istream &is)
Read List contents from Istream.
Definition: UListIO.C:150
A templated class for holding compound tokens.
Definition: token.H:281
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:72
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual Istream & read(token &)=0
Return next token from stream.
static Ostream & writeChars(Ostream &os, const char *chars, std::streamsize count)
Definition: charUList.C:32
A class for handling words, derived from Foam::string.
Definition: word.H:63
Space [isspace].
Definition: token.H:128
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:99
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Type & dynamicCast(U &obj)
A dynamic_cast (for references) that generates FatalError on failed casts.
Definition: typeInfo.H:108
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
void writeEntry(Ostream &os) const
Write the UList with its compound type.
Definition: UListIO.C:31
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 ...