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-------------------------------------------------------------------------------
10License
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
35namespace Foam
36{
37
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
67namespace Foam
68{
69
70template<>
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
93namespace Foam
94{
95
96template<>
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
107template<>
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 (
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// ************************************************************************* //
streamFormat format() const noexcept
Get the current stream format.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
virtual Istream & read(token &)=0
Return next token from stream.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void transfer(List< T > &list)
Definition: List.C:447
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
void writeEntry(Ostream &os) const
Write the UList with its compound type.
Definition: UListIO.C:38
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:79
Istream & readList(Istream &is)
Read List contents from Istream.
Definition: UListIO.C:157
A templated class for holding compound tokens.
Definition: token.H:250
A token holds an item read from Istream.
Definition: token.H:69
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:90
@ SPACE
Space [isspace].
Definition: token.H:125
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:716
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Namespace for OpenFOAM.
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
To & dynamicCast(From &r)
Definition: typeInfo.H:88
static Ostream & writeChars(Ostream &os, const char *chars, std::streamsize count)
Definition: charUList.C:39