int32IO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2014-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "int32.H"
27 #include "IOstreams.H"
28 
29 #include <inttypes.h>
30 #include <sstream>
31 #include <cerrno>
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 Foam::word Foam::name(const int32_t val)
36 {
37  std::ostringstream buf;
38  buf << val;
39  return buf.str();
40 }
41 
42 
43 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
44 
46 {
47  token t(is);
48 
49  if (!t.good())
50  {
51  is.setBad();
52  return is;
53  }
54 
55  if (t.isInteger32())
56  {
57  i = t.integer32Token();
58  }
59  else
60  {
61  is.setBad();
63  << "wrong token type - expected int32_t, found " << t.info()
64  << exit(FatalIOError);
65 
66  return is;
67  }
68 
69  // Check state of Istream
70  is.check("Istream& operator>>(Istream&, int32_t&)");
71 
72  return is;
73 }
74 
75 
77 {
78  int32_t val;
79  is >> val;
80 
81  return val;
82 }
83 
84 
85 bool Foam::read(const char* buf, int32_t& s)
86 {
87  char *endptr = nullptr;
88  errno = 0;
89  intmax_t l = strtoimax(buf, &endptr, 10);
90  s = int32_t(l);
91  return
92  (*endptr == 0) && (errno == 0)
93  && (l >= INT32_MIN) && (l <= INT32_MAX);
94 }
95 
96 
97 Foam::Ostream& Foam::operator<<(Ostream& os, const int32_t i)
98 {
99  os.write(i);
100  os.check("Ostream& operator<<(Ostream&, const int32_t)");
101  return os;
102 }
103 
104 
105 #if WM_ARCH_OPTION == 32
106 Foam::Istream& Foam::operator>>(Istream& is, long& i)
107 {
108  return operator>>(is, reinterpret_cast<int32_t&>(i));
109 }
110 
111 
112 Foam::Ostream& Foam::operator<<(Ostream& os, const long i)
113 {
114  os << int32_t(i);
115  return os;
116 }
117 #endif
118 
119 
120 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual Ostream & write(const char)=0
Write character.
A token holds items read from Istream.
Definition: token.H:73
int32_t integer32Token() const
Definition: tokenI.H:441
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:422
bool isInteger32() const
Definition: tokenI.H:429
bool good() const
Definition: tokenI.H:265
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
32bit integer
Istream & operator>>(Istream &is, volFieldValue::Result< Type > &result)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool read(const char *, int32_t &)
Definition: int32IO.C:85
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
int32_t readInt32(Istream &)
Definition: int32IO.C:76
Istream & operator>>(Istream &, pistonPointEdgeData &)
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)