Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / dynamicLibrary / dynamicCode / dynamicCodeContext.C
blob48691501eeca585848b66d0c10214a20599f8546
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011-2011 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "dynamicCodeContext.H"
27 #include "stringOps.H"
28 #include "OSHA1stream.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
35     dict_(dict),
36     code_(),
37     localCode_(),
38     include_(),
39     options_(),
40     libs_()
42     // expand dictionary entries
44     {
45         const entry& codeEntry = dict.lookupEntry("code", false, false);
46         code_ = stringOps::trim(codeEntry.stream());
47         stringOps::inplaceExpand(code_, dict);
48     }
50     // note: removes any leading/trailing whitespace
51     // - necessary for compilation options, convenient for includes
52     // and body.
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());
64         stringOps::inplaceExpand(include_, dict);
65     }
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());
77         stringOps::inplaceExpand(options_, dict);
78     }
80     // optional
81     const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
82     if (libsPtr)
83     {
84         libs_ = stringOps::trim(libsPtr->stream());
85         stringOps::inplaceExpand(libs_, dict);
86     }
88     // calculate SHA1 digest from include, options, localCode, code
89     OSHA1stream os;
90     os  << include_ << options_ << libs_ << localCode_ << code_;
91     sha1_ = os.digest();
95     // Add line number after calculating sha1 since includes processorDDD
96     // in path which differs between processors.
98     {
99         const entry& codeEntry = dict.lookupEntry("code", false, false);
100         addLineDirective(code_, codeEntry.startLineNumber(), dict.name());
101     }
102     if (includePtr)
103     {
104         addLineDirective(include_, includePtr->startLineNumber(), dict.name());
105     }
106     if (optionsPtr)
107     {
108         addLineDirective(options_, optionsPtr->startLineNumber(), dict.name());
109     }
110     if (libsPtr)
111     {
112         addLineDirective(libs_, libsPtr->startLineNumber(), dict.name());
113     }
117 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
119 void Foam::dynamicCodeContext::addLineDirective
121     string& code,
122     const label lineNum,
123     const fileName& name
126     code = "#line " + Foam::name(lineNum) + " \"" + name + "\"\n" + code;
130 // ************************************************************************* //