OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

longLongIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Reads a long long from an input stream, for a given version
00027     number and File format. If an ascii File is being read, then the line
00028     numbers are counted and an erroneous read ised.
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include "error.H"
00033 
00034 #include "longLong.H"
00035 #include "IOstreams.H"
00036 
00037 #include <sstream>
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 Foam::word Foam::name(long long val)
00042 {
00043     std::ostringstream buf;
00044     buf << val;
00045     return buf.str();
00046 }
00047 
00048 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00049 
00050 Foam::Istream& Foam::operator>>(Istream& is, long long& l)
00051 {
00052     l = readLongLong(is);
00053 
00054     // Check state of Istream
00055     is.check("Istream& operator>>(Istream&, long long&)");
00056 
00057     return is;
00058 }
00059 
00060 
00061 long long Foam::readLongLong(Istream& is)
00062 {
00063     register long long result = 0;
00064 
00065     char c = 0;
00066 
00067     static const label zeroOffset = int('0');
00068 
00069     // Get next non-whitespace character
00070     while (is.read(c) && isspace(c))
00071     {}
00072 
00073     do
00074     {
00075         if (isspace(c) || c == 0) break;
00076 
00077         if (!isdigit(c))
00078         {
00079             FatalIOErrorIn("readLongLong(ISstream& is)", is)
00080                 << "Illegal digit: \"" << c << "\""
00081                 << exit(FatalIOError);
00082         }
00083 
00084         result *= 10 + int(c) - zeroOffset;
00085     } while (is.read(c));
00086 
00087     return result;
00088 }
00089 
00090 
00091 Foam::Ostream& Foam::operator<<(Ostream& os, const long long l)
00092 {
00093     long long val = l;
00094 
00095     long long mask = 1000000000000000000LL;
00096 
00097     bool printZeroes = false;
00098 
00099     while (mask > 0)
00100     {
00101         int d = int(val/mask);
00102 
00103         if (d == 0)
00104         {
00105             if (printZeroes)
00106             {
00107                 os.write('0');
00108             }
00109         }
00110         else
00111         {
00112             printZeroes = true;
00113             os.write(char(d+'0'));
00114         }
00115 
00116         val = val % mask;
00117         mask /= 10;
00118     }
00119     os.check("Ostream& operator<<(Ostream&, const long long)");
00120     return os;
00121 }
00122 
00123 
00124 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd