1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
32 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
34 char Foam::ISstream::nextValid()
40 // Get next non-whitespace character
41 while (get(c) && isspace(c))
44 // Return if stream is bad
45 if (bad() || isspace(c))
50 // Is this the start of a C/C++ comment?
53 // If cannot get another character, return this one
61 // This is the start of a C++ style one-line comment
62 while (get(c) && c != '\n')
67 // This is the start of a C style comment
70 if (get(c) && c == '*')
72 if (get(c) && c == '/')
88 else // A lone '/' so return it.
94 else // c is a valid character so return it
102 Foam::Istream& Foam::ISstream::read(token& t)
104 static char numberBuffer[100];
106 // Return the put back token if it exists
107 if (Istream::getBack(t))
112 // Assume that the streams supplied are in working order.
113 // Lines are counted by '\n'
115 // Get next 'valid character': i.e. proceed through any white space
116 // and/or comments until a semantically valid character is hit upon.
118 char c = nextValid();
120 // Set the line number of this token to the current stream line number
121 t.lineNumber() = lineNumber();
130 // Analyse input starting with this character.
133 // First check for punctuation characters.
135 case token::END_STATEMENT :
136 case token::BEGIN_LIST :
137 case token::END_LIST :
138 case token::BEGIN_SQR :
139 case token::END_SQR :
140 case token::BEGIN_BLOCK :
141 case token::END_BLOCK :
146 // case token::SUBTRACT : // Handled later as the posible start of a number
147 case token::MULTIPLY :
150 t = token::punctuationToken(c);
154 // Strings: enclosed by double quotes.
155 case token::BEGIN_STRING :
158 string* sPtr = new string;
160 if (!read(*sPtr).bad())
172 // Numbers: do not distinguish at this point between Types.
175 case '0' : case '1' : case '2' : case '3' : case '4' :
176 case '5' : case '6' : case '7' : case '8' : case '9' :
178 bool isScalar = false;
186 numberBuffer[i++] = c;
201 numberBuffer[i++] = c;
208 numberBuffer[i] = '\0';
210 setState(is_.rdstate());
216 if (i == 1 && numberBuffer[0] == '-')
218 t = token::punctuationToken(token::SUBTRACT);
222 t = scalar(atof(numberBuffer));
226 long lt = atol(numberBuffer);
229 // If the integer is too large to be represented as a label
230 // return it as a scalar
231 if (t.labelToken() != lt)
234 t = scalar(atof(numberBuffer));
246 // Should be a word (which can be a single character)
250 word* wPtr = new word;
252 if (!read(*wPtr).bad())
254 if (token::compound::isCompound(*wPtr))
256 t = token::compound::New(*wPtr, *this).ptr();
275 Foam::Istream& Foam::ISstream::read(char& c)
282 Foam::Istream& Foam::ISstream::read(word& str)
284 static const int maxLen = 1024;
285 static const int errLen = 80; // truncate error message for readability
286 static char buf[maxLen];
292 while (get(c) && word::valid(c))
302 buf[maxLen-1] = '\0';
306 FatalIOErrorIn("ISstream::read(word&)", *this)
307 << "problem while reading word '" << buf << "'\n"
308 << exit(FatalIOError);
315 buf[maxLen-1] = '\0';
318 FatalIOErrorIn("ISstream::read(word&)", *this)
319 << "word '" << buf << "' ...\n"
320 << " is too long (max. " << maxLen << " characters)"
321 << exit(FatalIOError);
326 if (c == token::BEGIN_LIST)
330 else if (c == token::END_LIST)
345 FatalIOErrorIn("ISstream::read(word&)", *this)
346 << "invalid first character found : " << c
347 << exit(FatalIOError);
350 buf[i] = '\0'; // Terminator
358 Foam::Istream& Foam::ISstream::read(string& str)
360 static const int maxLen = 1024;
361 static const int errLen = 80; // truncate error message for readability
362 static char buf[maxLen];
370 FatalIOErrorIn("ISstream::read(string&)", *this)
371 << "cannot read start of string"
372 << exit(FatalIOError);
377 char endTok = token::END_STRING;
379 // Note, we could also handle single-quoted strings here (if desired)
380 if (c != token::BEGIN_STRING)
384 FatalIOErrorIn("ISstream::read(string&)", *this)
385 << "Incorrect start of string character"
386 << exit(FatalIOError);
392 bool escaped = false;
401 i--; // overwrite backslash
405 // done reading string
411 else if (c == token::NL)
416 i--; // overwrite backslash
423 FatalIOErrorIn("ISstream::read(string&)", *this)
424 << "found '\\n' while reading string \""
426 << exit(FatalIOError);
433 escaped = !escaped; // toggle state (retains backslashes)
443 buf[maxLen-1] = '\0';
446 FatalIOErrorIn("ISstream::read(string&)", *this)
447 << "string \"" << buf << "...\"\n"
448 << " is too long (max. " << maxLen << " characters)"
449 << exit(FatalIOError);
456 // don't worry about a dangling backslash if string terminated prematurely
460 FatalIOErrorIn("ISstream::read(string&)", *this)
461 << "problem while reading string \"" << buf << "...\""
462 << exit(FatalIOError);
468 Foam::Istream& Foam::ISstream::read(label& val)
471 setState(is_.rdstate());
476 Foam::Istream& Foam::ISstream::read(floatScalar& val)
479 setState(is_.rdstate());
484 Foam::Istream& Foam::ISstream::read(doubleScalar& val)
487 setState(is_.rdstate());
492 Foam::Istream& Foam::ISstream::read(longDoubleScalar& val)
495 setState(is_.rdstate());
501 Foam::Istream& Foam::ISstream::read(char* buf, std::streamsize count)
503 if (format() != BINARY)
505 FatalIOErrorIn("ISstream::read(char*, std::streamsize)", *this)
506 << "stream format not binary"
507 << exit(FatalIOError);
510 readBegin("binaryBlock");
511 is_.read(buf, count);
512 readEnd("binaryBlock");
514 setState(is_.rdstate());
520 Foam::Istream& Foam::ISstream::rewind()
522 stream().rdbuf()->pubseekpos(0);
528 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
531 std::ios_base::fmtflags Foam::ISstream::flags() const
537 std::ios_base::fmtflags Foam::ISstream::flags(const ios_base::fmtflags f)
543 // ************************************************************************* //