dynamicCodeContext.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 | Copyright (C) 2011-2012 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 "dynamicCodeContext.H"
27 #include "stringOps.H"
28 #include "OSHA1stream.H"
29 
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 :
35  dict_(dict),
36  code_(),
37  localCode_(),
38  include_(),
39  options_(),
40  libs_()
41 {
42  // expand dictionary entries
43 
44  {
45  const entry& codeEntry = dict.lookupEntry("code", false, false);
46  code_ = stringOps::trim(codeEntry.stream());
48  }
49 
50  // note: removes any leading/trailing whitespace
51  // - necessary for compilation options, convenient for includes
52  // and body.
53 
54  // optional
55  const entry* includePtr = dict.lookupEntryPtr
56  (
57  "codeInclude",
58  false,
59  false
60  );
61  if (includePtr)
62  {
63  include_ = stringOps::trim(includePtr->stream());
65  }
66 
67  // optional
68  const entry* optionsPtr = dict.lookupEntryPtr
69  (
70  "codeOptions",
71  false,
72  false
73  );
74  if (optionsPtr)
75  {
76  options_ = stringOps::trim(optionsPtr->stream());
78  }
79 
80  // optional
81  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
82  if (libsPtr)
83  {
84  libs_ = stringOps::trim(libsPtr->stream());
86  }
87 
88  // optional
89  const entry* localPtr = dict.lookupEntryPtr("localCode", false, false);
90  if (localPtr)
91  {
92  localCode_ = stringOps::trim(localPtr->stream());
94  }
95 
96  // calculate SHA1 digest from include, options, localCode, code
97  OSHA1stream os;
98  os << include_ << options_ << libs_ << localCode_ << code_;
99  sha1_ = os.digest();
100 
101 
102 
103  // Add line number after calculating sha1 since includes processorDDD
104  // in path which differs between processors.
105 
106  {
107  const entry& codeEntry = dict.lookupEntry("code", false, false);
109  }
110  if (includePtr)
111  {
113  }
114 
115  // Do not add line directive to options_ (Make/options) and libs since
116  // they are preprocessed as a single line at this point. Can be fixed.
117 
118  if (localPtr)
119  {
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
126 
128 (
129  string& code,
130  const label lineNum,
131  const fileName& name
132 )
133 {
134  code = "#line " + Foam::name(lineNum + 1) + " \"" + name + "\"\n" + code;
135 }
136 
137 
138 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::dynamicCodeContext::localCode_
string localCode_
Optional "localCode" entry.
Definition: dynamicCodeContext.H:60
Foam::dictionary::lookupEntry
const entry & lookupEntry(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream if present otherwise error.
Definition: dictionary.C:426
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
Foam::dynamicCodeContext::dict
const dictionary & dict() const
Return the parent dictionary context.
Definition: dynamicCodeContext.H:84
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::dynamicCodeContext::libs_
string libs_
Optional "codeLib" entry.
Definition: dynamicCodeContext.H:69
Foam::dynamicCodeContext::sha1_
SHA1Digest sha1_
Calculated SHA1Digest.
Definition: dynamicCodeContext.H:72
Foam::dynamicCodeContext::addLineDirective
static void addLineDirective(string &, const label lineNum, const fileName &name)
Helper: add #line directive.
Definition: dynamicCodeContext.C:128
dynamicCodeContext.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::OSHA1stream
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:133
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::entry::startLineNumber
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::dictionary::lookupEntryPtr
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:343
Foam::dynamicCodeContext::include_
string include_
Optional "codeInclude" entry.
Definition: dynamicCodeContext.H:63
Foam::dynamicCodeContext::dynamicCodeContext
dynamicCodeContext(const dictionary &)
Construct from a dictionary.
Definition: dynamicCodeContext.C:33
OSHA1stream.H
Foam::dynamicCodeContext::options_
string options_
Optional "codeOptions" entry.
Definition: dynamicCodeContext.H:66
Foam::OSHA1stream::digest
Foam::SHA1Digest digest()
Return SHA1::Digest for the data processed until now.
Definition: OSHA1stream.H:185
stringOps.H
Foam::stringOps::inplaceExpand
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:86
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dynamicCodeContext::code_
string code_
Mandatory "code" entry.
Definition: dynamicCodeContext.H:57
Foam::stringOps::trim
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:922