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  // The char '0' == 0
40  static constexpr int offsetZero = int('0');
41 
42  // The char 'A' (or 'a') == 10
43  static constexpr int offsetAlpha = int('A') - 10;
44 
45  char c = 0;
46 
47  // Get next non-whitespace character
48  while (is.get(c) && isspace(c))
49  {}
50 
51  T result = 0;
52  do
53  {
54  if (isspace(c) || c == 0) break;
55 
56  if (!isxdigit(c))
57  {
59  << "Illegal hex digit: '" << c << "'"
60  << exit(FatalIOError);
61  }
62 
63  result <<= 4;
64 
65  if (isdigit(c))
66  {
67  result += int(c) - offsetZero;
68  }
69  else
70  {
71  result += toupper(c) - offsetAlpha;
72  }
73  } while (is.get(c));
74 
75  return result;
76 }
77 
78 
79 // ************************************************************************* //
static constexpr int offsetZero
Definition: SHA1Digest.C:33
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Read a hex integer from an input stream.
const volScalarField & T
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
static constexpr int offsetAlpha
Definition: SHA1Digest.C:36
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
const dimensionedScalar c
Speed of light in a vacuum.
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:69
T ReadHex(ISstream &is)
Read a hex integer from an input stream.
Definition: ReadHex.C:31
ISstream & get(char &c)
Raw, low-level get character function.
Definition: ISstreamI.H:49
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...