wordIO.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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "word.H"
30 #include "token.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
36 {
37  is >> *this;
38 }
39 
40 
41 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
42 
44 {
45  token tok(is);
46 
47  if (tok.isWord())
48  {
49  val = tok.wordToken();
50  }
51  else if (tok.isQuotedString() || tok.isVariable())
52  {
53  // Try a bit harder, convert some string types to word
54  // - accept "quoted" or $tag, but not verbatim/expression
55 
56  const auto& str = tok.stringToken();
57  val = word::validate(str);
58 
59  // Flag empty strings and bad chars as an error
60  if (val.empty() || val.length() != str.length())
61  {
63  << "Empty word or non-word characters "
64  << tok.info() << exit(FatalIOError);
65  is.setBad();
66  return is;
67  }
68  }
69  else
70  {
72  if (tok.good())
73  {
75  << "Wrong token type - expected word, found "
76  << tok.info();
77  }
78  else
79  {
81  << "Bad token - could not get word";
82  }
84  is.setBad();
85  return is;
86  }
87 
88  is.check(FUNCTION_NAME);
89  return is;
90 }
91 
92 
94 {
95  os.write(val);
97  return os;
98 }
99 
100 
101 // ************************************************************************* //
token.H
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::isQuotedString
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:647
Foam::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
Foam::token::isVariable
bool isVariable() const noexcept
Token is VARIABLE (string variant)
Definition: tokenI.H:671
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
os
OBJstream os(runTime.globalPath()/outputName)
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::token::good
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:405
Foam::word::word
word()=default
Default construct.
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::IOstream::setBad
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H