int32IO.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 | Copyright (C) 2014-2015 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 "error.H"
27 
28 #include "int32.H"
29 #include "IOstreams.H"
30 
31 #include <inttypes.h>
32 #include <sstream>
33 #include <cerrno>
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 Foam::word Foam::name(const int32_t val)
38 {
39  std::ostringstream buf;
40  buf << val;
41  return buf.str();
42 }
43 
44 
45 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
46 
48 {
49  token t(is);
50 
51  if (!t.good())
52  {
53  is.setBad();
54  return is;
55  }
56 
57  if (t.isLabel())
58  {
59  i = int32_t(t.labelToken());
60  }
61  else
62  {
63  is.setBad();
64  FatalIOErrorIn("operator>>(Istream&, int32_t&)", is)
65  << "wrong token type - expected int32_t, found " << t.info()
66  << exit(FatalIOError);
67 
68  return is;
69  }
70 
71  // Check state of Istream
72  is.check("Istream& operator>>(Istream&, int32_t&)");
73 
74  return is;
75 }
76 
77 
79 {
80  int32_t val;
81  is >> val;
82 
83  return val;
84 }
85 
86 
87 bool Foam::read(const char* buf, int32_t& s)
88 {
89  char *endptr = NULL;
90  errno = 0;
91  intmax_t l = strtoimax(buf, &endptr, 10);
92  s = int32_t(l);
93  return
94  (*endptr == 0) && (errno == 0)
95  && (l >= INT32_MIN) && (l <= INT32_MAX);
96 }
97 
98 
99 Foam::Ostream& Foam::operator<<(Ostream& os, const int32_t i)
100 {
101  os.write(label(i));
102  os.check("Ostream& operator<<(Ostream&, const int32_t)");
103  return os;
104 }
105 
106 
107 #if WM_ARCH_OPTION == 32
109 {
110  return operator>>(is, reinterpret_cast<int32_t&>(i));
111 }
112 
113 Foam::Ostream& Foam::operator<<(Ostream& os, const long i)
114 {
115  os << int32_t(i);
116  return os;
117 }
118 #endif
119 
120 
121 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:262
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE ))
int32_t readInt32(Istream &)
Definition: int32IO.C:78
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
32bit integer
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
IOerror FatalIOError
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
virtual Ostream & write(const token &)=0
Write next token to stream.
A token holds items read from Istream.
Definition: token.H:67
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
label labelToken() const
Definition: tokenI.H:267
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool read(const char *, int32_t &)
Definition: int32IO.C:87
bool good() const
Definition: tokenI.H:188
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325