FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Description
00025     Reads a long long from an input stream, for a given version
00026     number and File format. If an ascii File is being read, then the line
00027     numbers are counted and an erroneous read ised.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include <OpenFOAM/error.H>
00032 
00033 #include "longLong.H"
00034 #include <OpenFOAM/IOstreams.H>
00035 
00036 #include <sstream>
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 
00040 Foam::word Foam::name(long long val)
00041 {
00042     std::ostringstream buf;
00043     buf << val;
00044     return buf.str();
00045 }
00046 
00047 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00048 
00049 Foam::Istream& Foam::operator>>(Istream& is, long long& l)
00050 {
00051     l = readLongLong(is);
00052 
00053     // Check state of Istream
00054     is.check("Istream& operator>>(Istream&, long long&)");
00055 
00056     return is;
00057 }
00058 
00059 
00060 long long Foam::readLongLong(Istream& is)
00061 {
00062     register long long result = 0;
00063 
00064     char c = 0;
00065 
00066     static const label zeroOffset = int('0');
00067 
00068     // Get next non-whitespace character
00069     while (is.read(c) && isspace(c))
00070     {}
00071 
00072     do
00073     {
00074         if (isspace(c) || c == 0) break;
00075 
00076         if (!isdigit(c))
00077         {
00078             FatalIOErrorIn("readLongLong(ISstream& is)", is)
00079                 << "Illegal digit: \"" << c << "\""
00080                 << exit(FatalIOError);
00081         }
00082 
00083         result *= 10 + int(c) - zeroOffset;
00084     } while (is.read(c));
00085 
00086     return result;
00087 }
00088 
00089 
00090 Foam::Ostream& Foam::operator<<(Ostream& os, const long long l)
00091 {
00092     long long val = l;
00093 
00094     long long mask = 1000000000000000000LL;
00095 
00096     bool printZeroes = false;
00097 
00098     while (mask > 0)
00099     {
00100         int d = int(val/mask);
00101 
00102         if (d == 0)
00103         {
00104             if (printZeroes)
00105             {
00106                 os.write('0');
00107             }
00108         }
00109         else
00110         {
00111             printZeroes = true;
00112             os.write(char(d+'0'));
00113         }
00114 
00115         val = val % mask;
00116         mask /= 10;
00117     }
00118     os.check("Ostream& operator<<(Ostream&, const long long)");
00119     return os;
00120 }
00121 
00122 
00123 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines