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

ISstream.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "ISstream.H"
00028 #include "int.H"
00029 #include "token.H"
00030 #include <cctype>
00031 
00032 
00033 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00034 
00035 char Foam::ISstream::nextValid()
00036 {
00037     char c = 0;
00038 
00039     while (true)
00040     {
00041         // Get next non-whitespace character
00042         while (get(c) && isspace(c))
00043         {}
00044 
00045         // Return if stream is bad
00046         if (bad() || isspace(c))
00047         {
00048             return 0;
00049         }
00050 
00051         // Is this the start of a C/C++ comment?
00052         if (c == '/')
00053         {
00054             // If cannot get another character, return this one
00055             if (!get(c))
00056             {
00057                 return '/';
00058             }
00059 
00060             if (c == '/')
00061             {
00062                 // This is the start of a C++ style one-line comment
00063                 while (get(c) && c != '\n')
00064                 {}
00065             }
00066             else if (c == '*')
00067             {
00068                 // This is the start of a C style comment
00069                 while (true)
00070                 {
00071                     if (get(c) && c == '*')
00072                     {
00073                         if (get(c) && c == '/')
00074                         {
00075                             break;
00076                         }
00077                         else
00078                         {
00079                             putback(c);
00080                         }
00081                     }
00082 
00083                     if (!good())
00084                     {
00085                         return 0;
00086                     }
00087                 }
00088             }
00089             else  // A lone '/' so return it.
00090             {
00091                 putback(c);
00092                 return '/';
00093             }
00094         }
00095         else  // c is a valid character so return it
00096         {
00097             return c;
00098         }
00099     }
00100 }
00101 
00102 
00103 Foam::Istream& Foam::ISstream::read(token& t)
00104 {
00105     static char numberBuffer[100];
00106 
00107     // Return the put back token if it exists
00108     if (Istream::getBack(t))
00109     {
00110         return *this;
00111     }
00112 
00113     // Assume that the streams supplied are in working order.
00114     // Lines are counted by '\n'
00115 
00116     // Get next 'valid character': i.e. proceed through any white space
00117     // and/or comments until a semantically valid character is hit upon.
00118 
00119     char c = nextValid();
00120 
00121     // Set the line number of this token to the current stream line number
00122     t.lineNumber() = lineNumber();
00123 
00124     // return on error
00125     if (!c)
00126     {
00127         t.setBad();
00128         return *this;
00129     }
00130 
00131     // Analyse input starting with this character.
00132     switch (c)
00133     {
00134         // First check for punctuation characters.
00135 
00136         case token::END_STATEMENT :
00137         case token::BEGIN_LIST :
00138         case token::END_LIST :
00139         case token::BEGIN_SQR :
00140         case token::END_SQR :
00141         case token::BEGIN_BLOCK :
00142         case token::END_BLOCK :
00143         case token::COLON :
00144         case token::COMMA :
00145         case token::ASSIGN :
00146         case token::ADD :
00147      // case token::SUBTRACT : // Handled later as the posible start of a number
00148         case token::MULTIPLY :
00149         case token::DIVIDE :
00150         {
00151             t = token::punctuationToken(c);
00152             return *this;
00153         }
00154 
00155         // Strings: enclosed by double quotes.
00156         case token::BEGIN_STRING :
00157         {
00158             putback(c);
00159             string* sPtr = new string;
00160 
00161             if (!read(*sPtr).bad())
00162             {
00163                 t = sPtr;
00164             }
00165             else
00166             {
00167                 delete sPtr;
00168                 t.setBad();
00169             }
00170             return *this;
00171         }
00172 
00173         // Numbers: do not distinguish at this point between Types.
00174         case '-' :
00175         case '.' :
00176         case '0' : case '1' : case '2' : case '3' : case '4' :
00177         case '5' : case '6' : case '7' : case '8' : case '9' :
00178         {
00179             bool isScalar = false;
00180 
00181             if (c == '.')
00182             {
00183                 isScalar = true;
00184             }
00185 
00186             int i=0;
00187             numberBuffer[i++] = c;
00188 
00189             while
00190             (
00191                 is_.get(c)
00192              && (
00193                     isdigit(c)
00194                  || c == '.'
00195                  || c == 'e'
00196                  || c == 'E'
00197                  || c == '+'
00198                  || c == '-'
00199                 )
00200             )
00201             {
00202                 numberBuffer[i++] = c;
00203 
00204                 if (!isdigit(c))
00205                 {
00206                     isScalar = true;
00207                 }
00208             }
00209             numberBuffer[i] = '\0';
00210 
00211             setState(is_.rdstate());
00212 
00213             if (!is_.bad())
00214             {
00215                 is_.putback(c);
00216 
00217                 if (i == 1 && numberBuffer[0] == '-')
00218                 {
00219                     t = token::punctuationToken(token::SUBTRACT);
00220                 }
00221                 else if (isScalar)
00222                 {
00223                     t = scalar(atof(numberBuffer));
00224                 }
00225                 else
00226                 {
00227                     long lt = atol(numberBuffer);
00228                     t = label(lt);
00229 
00230                     // If the integer is too large to be represented as a label
00231                     // return it as a scalar
00232                     if (t.labelToken() != lt)
00233                     {
00234                         isScalar = true;
00235                         t = scalar(atof(numberBuffer));
00236                     }
00237                 }
00238             }
00239             else
00240             {
00241                 t.setBad();
00242             }
00243 
00244             return *this;
00245         }
00246 
00247         // Should be a word (which can be a single character)
00248         default:
00249         {
00250             putback(c);
00251             word* wPtr = new word;
00252 
00253             if (!read(*wPtr).bad())
00254             {
00255                 if (token::compound::isCompound(*wPtr))
00256                 {
00257                     t = token::compound::New(*wPtr, *this).ptr();
00258                     delete wPtr;
00259                 }
00260                 else
00261                 {
00262                     t = wPtr;
00263                 }
00264             }
00265             else
00266             {
00267                 delete wPtr;
00268                 t.setBad();
00269             }
00270             return *this;
00271         }
00272     }
00273 }
00274 
00275 
00276 Foam::Istream& Foam::ISstream::read(char& c)
00277 {
00278     c = nextValid();
00279     return *this;
00280 }
00281 
00282 
00283 Foam::Istream& Foam::ISstream::read(word& str)
00284 {
00285     static const int maxLen = 1024;
00286     static const int errLen = 80; // truncate error message for readability
00287     static char buf[maxLen];
00288 
00289     register int i = 0;
00290     register int bc = 0;
00291     char c;
00292 
00293     while (get(c) && word::valid(c))
00294     {
00295         if (fail())
00296         {
00297             if (i < maxLen-1)
00298             {
00299                 buf[i] = '\0';
00300             }
00301             else
00302             {
00303                 buf[maxLen-1] = '\0';
00304             }
00305             buf[errLen] = '\0';
00306 
00307             FatalIOErrorIn("ISstream::read(word&)", *this)
00308                 << "problem while reading word '" << buf << "'\n"
00309                 << exit(FatalIOError);
00310 
00311             return *this;
00312         }
00313 
00314         if (i >= maxLen)
00315         {
00316             buf[maxLen-1] = '\0';
00317             buf[errLen] = '\0';
00318 
00319             FatalIOErrorIn("ISstream::read(word&)", *this)
00320                 << "word '" << buf << "' ...\n"
00321                 << "    is too long (max. " << maxLen << " characters)"
00322                 << exit(FatalIOError);
00323 
00324             return *this;
00325         }
00326 
00327         if (c == token::BEGIN_LIST)
00328         {
00329             bc++;
00330         }
00331         else if (c == token::END_LIST)
00332         {
00333             bc--;
00334 
00335             if (bc == -1)
00336             {
00337                 break;
00338             }
00339         }
00340 
00341         buf[i++] = c;
00342     }
00343 
00344     if (i == 0)
00345     {
00346         FatalIOErrorIn("ISstream::read(word&)", *this)
00347             << "invalid first character found : " << c
00348             << exit(FatalIOError);
00349     }
00350 
00351     buf[i] = '\0';        // Terminator
00352     str = buf;
00353     putback(c);
00354 
00355     return *this;
00356 }
00357 
00358 
00359 Foam::Istream& Foam::ISstream::read(string& str)
00360 {
00361     static const int maxLen = 1024;
00362     static const int errLen = 80; // truncate error message for readability
00363     static char buf[maxLen];
00364 
00365     char c;
00366 
00367     if (!get(c))
00368     {
00369         buf[0] = '\0';
00370 
00371         FatalIOErrorIn("ISstream::read(string&)", *this)
00372             << "cannot read start of string"
00373             << exit(FatalIOError);
00374 
00375         return *this;
00376     }
00377 
00378     char endTok = token::END_STRING;
00379 
00380     // Note, we could also handle single-quoted strings here (if desired)
00381     if (c != token::BEGIN_STRING)
00382     {
00383         buf[0] = '\0';
00384 
00385         FatalIOErrorIn("ISstream::read(string&)", *this)
00386             << "Incorrect start of string character"
00387             << exit(FatalIOError);
00388 
00389         return *this;
00390     }
00391 
00392     register int i = 0;
00393     bool escaped = false;
00394 
00395     while (get(c))
00396     {
00397         if (c == endTok)
00398         {
00399             if (escaped)
00400             {
00401                 escaped = false;
00402                 i--;    // overwrite backslash
00403             }
00404             else
00405             {
00406                 // done reading string
00407                 buf[i] = '\0';
00408                 str = buf;
00409                 return *this;
00410             }
00411         }
00412         else if (c == token::NL)
00413         {
00414             if (escaped)
00415             {
00416                 escaped = false;
00417                 i--;    // overwrite backslash
00418             }
00419             else
00420             {
00421                 buf[i] = '\0';
00422                 buf[errLen] = '\0';
00423 
00424                 FatalIOErrorIn("ISstream::read(string&)", *this)
00425                     << "found '\\n' while reading string \""
00426                     << buf << "...\""
00427                     << exit(FatalIOError);
00428 
00429                 return *this;
00430             }
00431         }
00432         else if (c == '\\')
00433         {
00434             escaped = !escaped;    // toggle state (retains backslashes)
00435         }
00436         else
00437         {
00438             escaped = false;
00439         }
00440 
00441         buf[i] = c;
00442         if (i++ == maxLen)
00443         {
00444             buf[maxLen-1] = '\0';
00445             buf[errLen] = '\0';
00446 
00447             FatalIOErrorIn("ISstream::read(string&)", *this)
00448                 << "string \"" << buf << "...\"\n"
00449                 << "    is too long (max. " << maxLen << " characters)"
00450                 << exit(FatalIOError);
00451 
00452             return *this;
00453         }
00454     }
00455 
00456 
00457     // don't worry about a dangling backslash if string terminated prematurely
00458     buf[i] = '\0';
00459     buf[errLen] = '\0';
00460 
00461     FatalIOErrorIn("ISstream::read(string&)", *this)
00462         << "problem while reading string \"" << buf << "...\""
00463         << exit(FatalIOError);
00464 
00465     return *this;
00466 }
00467 
00468 
00469 Foam::Istream& Foam::ISstream::read(label& val)
00470 {
00471     is_ >> val;
00472     setState(is_.rdstate());
00473     return *this;
00474 }
00475 
00476 
00477 Foam::Istream& Foam::ISstream::read(floatScalar& val)
00478 {
00479     is_ >> val;
00480     setState(is_.rdstate());
00481     return *this;
00482 }
00483 
00484 
00485 Foam::Istream& Foam::ISstream::read(doubleScalar& val)
00486 {
00487     is_ >> val;
00488     setState(is_.rdstate());
00489     return *this;
00490 }
00491 
00492 
00493 // read binary block
00494 Foam::Istream& Foam::ISstream::read(char* buf, std::streamsize count)
00495 {
00496     if (format() != BINARY)
00497     {
00498         FatalIOErrorIn("ISstream::read(char*, std::streamsize)", *this)
00499             << "stream format not binary"
00500             << exit(FatalIOError);
00501     }
00502 
00503     readBegin("binaryBlock");
00504     is_.read(buf, count);
00505     readEnd("binaryBlock");
00506 
00507     setState(is_.rdstate());
00508 
00509     return *this;
00510 }
00511 
00512 
00513 Foam::Istream& Foam::ISstream::rewind()
00514 {
00515     stream().rdbuf()->pubseekpos(0);
00516 
00517     return *this;
00518 }
00519 
00520 
00521 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00522 
00523 
00524 std::ios_base::fmtflags Foam::ISstream::flags() const
00525 {
00526     return is_.flags();
00527 }
00528 
00529 
00530 std::ios_base::fmtflags Foam::ISstream::flags(const ios_base::fmtflags f)
00531 {
00532     return is_.flags(f);
00533 }
00534 
00535 
00536 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd