~dominik-burgdoerfer/webplodder/0.4

« back to all changes in this revision

Viewing changes to src/wesl/elements/script.cpp

  • Committer: Dominik Burgdörfer
  • Date: 2010-07-07 14:35:20 UTC
  • Revision ID: dominik@domachine-20100707143520-wpywl29fsg9quz54
restructured sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// =======================================================================
2
 
// 
3
 
//       Filename:  scriptelement.cpp
4
 
// 
5
 
//    Description:  
6
 
// 
7
 
//        Version:  1.0
8
 
//        Created:  20.04.2010 00:56:49
9
 
//       Revision:  none
10
 
//       Compiler:  g++
11
 
// 
12
 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
13
 
//        Company:  -
14
 
// 
15
 
// =======================================================================
16
 
 
17
 
#include "script.hpp"
18
 
#include "../elementstack.hpp"
19
 
#include "../exceptions/processingerror.hpp"
20
 
#include "../types/string.hpp"
21
 
#include "../process/process.hpp"
22
 
#include <sstream>
23
 
#include <boost/foreach.hpp>
24
 
#define foreach BOOST_FOREACH
25
 
 
26
 
using namespace std;
27
 
using namespace boost;
28
 
 
29
 
namespace wesl {
30
 
    namespace elements {
31
 
        shared_ptr<ElementStack::VariableMap>
32
 
            buildCompleteVariableMap(Element& object)
33
 
        {
34
 
 
35
 
            list<ElementStack*> path;
36
 
 
37
 
            ElementStack* current = &object.parent();
38
 
 
39
 
            while(1) {
40
 
                path.push_front(current);
41
 
 
42
 
                if(current->hasParent())
43
 
                    current = &current->parent();
44
 
                else
45
 
                    break;
46
 
            }
47
 
 
48
 
            shared_ptr<ElementStack::VariableMap> cMapPtr(
49
 
                    new ElementStack::VariableMap());
50
 
 
51
 
            ElementStack::VariableMap::iterator staticVarMapIter;
52
 
            for(staticVarMapIter = 
53
 
                    ElementStack::STATIC_VARIABLE_MAP.begin();
54
 
                    staticVarMapIter != 
55
 
                    ElementStack::STATIC_VARIABLE_MAP.end();
56
 
                    ++staticVarMapIter)
57
 
            {
58
 
                (*cMapPtr)[staticVarMapIter->first] =
59
 
                    staticVarMapIter->second;
60
 
            }
61
 
 
62
 
            foreach(ElementStack* es, path) {
63
 
                ElementStack::VariableMap& m =
64
 
                    es->dynamicVariableMap();
65
 
 
66
 
                ElementStack::VariableMap::iterator iter;
67
 
 
68
 
                for(iter = m.begin(); iter != m.end(); ++iter)
69
 
                {
70
 
                    (*cMapPtr)[iter->first] = iter->second;
71
 
                }
72
 
            }
73
 
 
74
 
            return cMapPtr;
75
 
        }
76
 
 
77
 
        Script::Script(ElementStack& parent)
78
 
            : Text(parent),
79
 
            Resolvable(this),
80
 
            Callable(this)
81
 
        {
82
 
 
83
 
        }
84
 
 
85
 
        Script::Script(ElementStack& parent,
86
 
                       const string& spec)
87
 
            : Text(parent), Resolvable(this),
88
 
            Callable(this),
89
 
            mSpec(spec)
90
 
        {
91
 
 
92
 
        }
93
 
 
94
 
        Script::~Script() {
95
 
        }
96
 
 
97
 
        string Script::specification() const
98
 
        {
99
 
            return mSpec;
100
 
        }
101
 
 
102
 
        void Script::setSpecification(const string& spec)
103
 
        {
104
 
            mSpec = spec;
105
 
        }
106
 
 
107
 
        void Script::print(ostream& stream) {
108
 
            if(RULES[specification()].empty()) {
109
 
                throw exceptions::ProcessingError(
110
 
                        "no rule defined for: " +
111
 
                            specification(),
112
 
                            parent().sourcePath(),
113
 
                            line());
114
 
            }
115
 
            else {
116
 
                shared_ptr<ElementStack::VariableMap> cMap =
117
 
                    buildCompleteVariableMap(*this);
118
 
                ElementStack::VariableMap::iterator pageIter;
119
 
 
120
 
                for(pageIter = cMap->begin(); 
121
 
                        pageIter != cMap->end();
122
 
                        ++pageIter)
123
 
                {
124
 
                    string envVar("WP_");
125
 
                    envVar.append(pageIter->first);
126
 
 
127
 
                    setenv(envVar.c_str(), 
128
 
                            pageIter->second->toString().c_str(), 1);
129
 
                }
130
 
 
131
 
                string scriptSpec = RULES[
132
 
                    specification()];
133
 
 
134
 
                process::Process process(scriptSpec);
135
 
                pid_t ret = process.launch();
136
 
 
137
 
                if (ret >= 0)
138
 
                {
139
 
                    char ch;
140
 
 
141
 
                    while(*this >> ch) {
142
 
                        process << ch;
143
 
                    }
144
 
 
145
 
                    process.closeWriteChannel();
146
 
 
147
 
                    while(process >> ch) {
148
 
                        stream << ch;
149
 
                    }
150
 
 
151
 
                    process.closeReadChannel();
152
 
                }
153
 
 
154
 
                for(pageIter = cMap->begin(); 
155
 
                        pageIter != cMap->end();
156
 
                        ++pageIter)
157
 
                {
158
 
                    string envVar("WP_");
159
 
                    envVar.append(pageIter->first);
160
 
 
161
 
                    unsetenv(envVar.c_str());
162
 
                }
163
 
            }
164
 
        }
165
 
 
166
 
        types::ObjectPtr Script::resolve() {
167
 
            ostringstream os;
168
 
 
169
 
            print(os);
170
 
 
171
 
            types::StringPtr stringPtr(
172
 
                    new types::String(os.str()));
173
 
 
174
 
            return stringPtr;
175
 
        }
176
 
 
177
 
        void Script::call() {
178
 
            if(RULES[specification()].empty()) {
179
 
                throw exceptions::ProcessingError(
180
 
                        "no rule defined for: " +
181
 
                            specification(),
182
 
                            parent().sourcePath(),
183
 
                            line());
184
 
            }
185
 
            else {
186
 
                shared_ptr<ElementStack::VariableMap> cMap =
187
 
                    buildCompleteVariableMap(*this);
188
 
                ElementStack::VariableMap::iterator pageIter;
189
 
 
190
 
                for(pageIter = cMap->begin(); 
191
 
                        pageIter != cMap->end();
192
 
                        ++pageIter)
193
 
                {
194
 
                    string envVar("WP_");
195
 
                    envVar.append(pageIter->first);
196
 
 
197
 
                    setenv(envVar.c_str(), 
198
 
                            pageIter->second->toString().c_str(), 1);
199
 
                }
200
 
 
201
 
                string scriptSpec = RULES[
202
 
                    specification()];
203
 
 
204
 
                process::Process process(scriptSpec);
205
 
                pid_t ret = process.launch();
206
 
 
207
 
                if (ret >= 0)
208
 
                {
209
 
                    char ch;
210
 
 
211
 
                    while(*this >> ch) {
212
 
                        process << ch;
213
 
                    }
214
 
 
215
 
                    process.closeWriteChannel();
216
 
 
217
 
                    while(process >> ch) {
218
 
                        // do nothing
219
 
                    }
220
 
 
221
 
                    process.closeReadChannel();
222
 
                }
223
 
 
224
 
                for(pageIter = cMap->begin(); 
225
 
                        pageIter != cMap->end();
226
 
                        ++pageIter)
227
 
                {
228
 
                    string envVar("WP_");
229
 
                    envVar.append(pageIter->first);
230
 
 
231
 
                    unsetenv(envVar.c_str());
232
 
                }
233
 
            }
234
 
        }
235
 
 
236
 
        map<string,string> Script::RULES =
237
 
                                map<string,string>();
238
 
    }
239
 
}