negEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "negEntry.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 namespace functionEntries
34 {
35  defineTypeNameAndDebug(negEntry, 0);
36 
38  (
39  functionEntry,
40  negEntry,
41  execute,
42  dictionaryIstream
43  );
44 
46  (
47  functionEntry,
48  negEntry,
49  execute,
50  primitiveEntryIstream
51  );
52 }
53 }
54 
55 
56 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
57 
58 Foam::string Foam::functionEntries::negEntry::negateVariable
59 (
60  const dictionary& parentDict,
61  Istream& is
62 )
63 {
64  // Read variable name as a word including the '$'
65  const word varWord(is);
66 
67  if (varWord[0] != '$')
68  {
70  (
71  parentDict
72  ) << "Expected variable name beginning with a '$' but found '"
73  << varWord << "'" << exit(FatalIOError);
74 
75  return string::null;
76  }
77 
78  // Strip the leading '$' from the variable name
79  const string varName = varWord(1, varWord.size()-1);
80 
81  // Lookup the variable name in the parent dictionary....
82  const entry* ePtr = parentDict.lookupScopedEntryPtr(varName, true, false);
83 
84  if (ePtr && ePtr->isStream())
85  {
86  const token variable(ePtr->stream());
87 
88  // Convert to a string
89  OStringStream os(is.format());
90  os << variable;
91  const string str(os.str());
92 
93  // Negate
94  if (str[0] == '-')
95  {
96  return str(1, str.size() - 1);
97  }
98  else
99  {
100  return '-' + str;
101  }
102  }
103  else
104  {
106  (
107  parentDict
108  ) << "Illegal dictionary variable name " << varName << endl
109  << "Valid dictionary entries are " << parentDict.toc()
110  << exit(FatalIOError);
111 
112  return string::null;
113  }
114 }
115 
116 
118 (
119  const dictionary& parentDict,
120  primitiveEntry& thisEntry,
121  Istream& is
122 )
123 {
124  // Reinsert negated variable into entry
125  IStringStream resultStream(negateVariable(parentDict, is));
126  thisEntry.read(parentDict, resultStream);
127 
128  return true;
129 }
130 
131 
133 (
134  dictionary& parentDict,
135  Istream& is
136 )
137 {
138  // Reinsert negated variable into dictionary
139  IStringStream resultStream(negateVariable(parentDict, is));
140  parentDict.read(resultStream);
141 
142  return true;
143 }
144 
145 
146 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:125
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: negEntry.C:133
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
static const string null
An empty string.
Definition: string.H:86
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Input from memory buffer stream.
Definition: IStringStream.H:49
Macros for easy insertion into member function selection tables.
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
A class for handling character strings derived from std::string.
Definition: string.H:74
defineTypeNameAndDebug(calcEntry, 0)
Namespace for OpenFOAM.
IOerror FatalIOError