primitiveEntry.C 5.42 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    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
27

\*---------------------------------------------------------------------------*/

#include "primitiveEntry.H"
#include "dictionary.H"
28
#include "OSspecific.H"
29
#include "stringOps.H"
30
31
32
33
34
35
36
37
38
39
40
41
42
43

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::primitiveEntry::append(const UList<token>& varTokens)
{
    forAll(varTokens, i)
    {
        newElmt(tokenIndex()++) = varTokens[i];
    }
}


bool Foam::primitiveEntry::expandVariable
(
44
    const string& w,
45
46
47
    const dictionary& dict
)
{
48
    if (w.size() > 2 && w[0] == '$' && w[1] == token::BEGIN_BLOCK)
49
    {
50
        // Recursive substitution mode. Replace between {} with expansion.
51
        string s(w(2, w.size()-3));
52
        // Substitute dictionary and environment variables. Do not allow
53
        // empty substitutions.
54
        stringOps::inplaceExpand(s, dict, true, false);
55
56
57
58
        string newW(w);
        newW.std::string::replace(1, newW.size()-1, s);

        return expandVariable(newW, dict);
59
60
61
    }
    else
    {
62
63
64
65
66
67
68
69
70
71
72
73
74
        string varName = w(1, w.size()-1);

        // lookup the variable name in the given dictionary....
        // Note: allow wildcards to match? For now disabled since following
        // would expand internalField to wildcard match and not expected
        // internalField:
        //      internalField XXX;
        //      boundaryField { ".*" {YYY;} movingWall {value $internalField;}
        const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false);

        // ...if defined append its tokens into this
        if (ePtr)
        {
75
76
77
78
79
80
81
82
            if (ePtr->isDict())
            {
                append(ePtr->dict().tokens());
            }
            else
            {
                append(ePtr->stream());
            }
83
84
        }
        else
85
        {
86
87
88
89
90
            // not in the dictionary - try an environment variable
            string envStr = getEnv(varName);

            if (envStr.empty())
            {
91
92
93
94
95
96
97
98
99
                FatalIOErrorIn
                (
                    "primitiveEntry::expandVariable"
                    "(const string&, const dictionary&",
                    dict
                )   << "Illegal dictionary entry or environment variable name "
                    << varName << endl << "Valid dictionary entries are "
                    << dict.toc() << exit(FatalIOError);

100
101
102
                return false;
            }
            append(tokenList(IStringStream('(' + envStr + ')')()));
103
104
105
106
107
        }
    }
    return true;
}

108
109
110

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

111
Foam::primitiveEntry::primitiveEntry(const keyType& key, const ITstream& is)
112
113
:
    entry(key),
114
    ITstream(is)
115
{
116
    name() += '.' + keyword();
117
118
119
}


120
Foam::primitiveEntry::primitiveEntry(const keyType& key, const token& t)
121
:
122
123
    entry(key),
    ITstream(key, tokenList(1, t))
124
125
126
127
128
{}


Foam::primitiveEntry::primitiveEntry
(
129
130
    const keyType& key,
    const UList<token>& tokens
131
132
)
:
133
134
135
136
137
138
139
140
    entry(key),
    ITstream(key, tokens)
{}


Foam::primitiveEntry::primitiveEntry
(
    const keyType& key,
Andrew Heather's avatar
Andrew Heather committed
141
    const Xfer<List<token> >& tokens
142
143
144
145
)
:
    entry(key),
    ITstream(key, tokens)
146
147
148
149
150
151
152
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

Foam::label Foam::primitiveEntry::startLineNumber() const
{
153
154
155
    const tokenList& tokens = *this;

    if (tokens.empty())
156
    {
157
        return -1;
158
159
160
    }
    else
    {
161
        return tokens.first().lineNumber();
162
163
164
    }
}

165

166
167
Foam::label Foam::primitiveEntry::endLineNumber() const
{
168
169
170
    const tokenList& tokens = *this;

    if (tokens.empty())
171
    {
172
        return -1;
173
174
175
    }
    else
    {
176
        return tokens.last().lineNumber();
177
178
179
180
181
182
    }
}


Foam::ITstream& Foam::primitiveEntry::stream() const
{
183
184
185
    ITstream& is = const_cast<primitiveEntry&>(*this);
    is.rewind();
    return is;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
}


const Foam::dictionary& Foam::primitiveEntry::dict() const
{
    FatalErrorIn("const dictionary& primitiveEntry::dict() const")
        << "Attempt to return primitive entry " << info()
        << " as a sub-dictionary"
        << abort(FatalError);

    return dictionary::null;
}


Foam::dictionary& Foam::primitiveEntry::dict()
{
    FatalErrorIn("const dictionary& primitiveEntry::dict()")
        << "Attempt to return primitive entry " << info()
        << " as a sub-dictionary"
        << abort(FatalError);

    return const_cast<dictionary&>(dictionary::null);
}


// ************************************************************************* //