ReadHex.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) 2013-2018 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 Description
25  Read a non-delimited hex label
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "ReadHex.H"
30 #include <cctype>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  // Takes into account that 'a' (or 'A') is 10
38  static const int alphaOffset = toupper('A') - 10;
39  // Takes into account that '0' is 0
40  static const int zeroOffset = int('0');
41 
42  char c = 0;
43 
44  // Get next non-whitespace character
45  while (is.get(c) && isspace(c))
46  {}
47 
48  T result = 0;
49  do
50  {
51  if (isspace(c) || c == 0) break;
52 
53  if (!isxdigit(c))
54  {
56  << "Illegal hex digit: '" << c << "'"
57  << exit(FatalIOError);
58  }
59 
60  result <<= 4;
61 
62  if (isdigit(c))
63  {
64  result += int(c) - zeroOffset;
65  }
66  else
67  {
68  result += toupper(c) - alphaOffset;
69  }
70  } while (is.get(c));
71 
72  return result;
73 }
74 
75 
76 // ************************************************************************* //
Read a hex integer from an input stream.
Generic input stream.
Definition: ISstream.H:55
ISstream & get(char &)
Low-level get character function.
Definition: ISstreamI.H:58
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
const dimensionedScalar c
Speed of light in a vacuum.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
T ReadHex(ISstream &)
Read a hex label from an input stream.
Definition: ReadHex.C:35
IOerror FatalIOError
bool isspace(char c)
Definition: char.H:53
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)