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-2019 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 // * * * * * * * * * * * * * Private 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 variable var(is);
66 
67  // Strip the leading '$' from the variable name
68  const string varName = var(1, var.size() - 1);
69 
70  // Lookup the variable name in the parent dictionary....
71  const entry* ePtr = parentDict.lookupScopedEntryPtr(varName, true, false);
72 
73  if (ePtr && ePtr->isStream())
74  {
75  const token variable(ePtr->stream());
76 
77  // Convert to a string
78  OStringStream os(is.format());
79  os << variable;
80  const string str(os.str());
81 
82  // Negate
83  if (str[0] == '-')
84  {
85  return str(1, str.size() - 1);
86  }
87  else
88  {
89  return '-' + str;
90  }
91  }
92  else
93  {
95  (
96  parentDict
97  ) << "Illegal dictionary variable name " << varName << endl
98  << "Valid dictionary entries are " << parentDict.toc()
99  << exit(FatalIOError);
100 
101  return string::null;
102  }
103 }
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
109 (
110  dictionary& parentDict,
111  Istream& is
112 )
113 {
114  return insert(parentDict, negateVariable(parentDict, is));
115 }
116 
117 
119 (
120  const dictionary& parentDict,
121  primitiveEntry& thisEntry,
122  Istream& is
123 )
124 {
125  return insert(parentDict, thisEntry, negateVariable(parentDict, is));
126 }
127 
128 
129 // ************************************************************************* //
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:156
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: negEntry.C:109
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:251
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
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:88
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
Macros for easy insertion into member function selection tables.
A class for handling character strings derived from std::string.
Definition: string.H:76
defineTypeNameAndDebug(calcEntry, 0)
Namespace for OpenFOAM.
IOerror FatalIOError