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