1 /***************************************************************************
2 *
3 * Copyright 2012 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19 #include "LMControl.h"
20 #include "Expression.h"
21 #include "ExpressionInterpreter.h"
22 #include "SceneStore.h"
23 #include <cstdio>
24 #include <cmath>
25 #include <iostream>
26 #include <sstream>
27 #include <fstream>
28 #include <iomanip>
29 #include <vector>
30 #include <map>
31 #include <set>
32 #include <algorithm>
33 #include <iterator>
34 #include <cstring>
35 #include <pthread.h>
36 #include <stdarg.h>
37
38 using namespace std;
39
40
41 namespace {
captureSceneDataHelper(t_ilm_surface surfaceId,t_scene_data * pSceneData,IlmSurface * pSurface)42 void captureSceneDataHelper(t_ilm_surface surfaceId, t_scene_data* pSceneData, IlmSurface* pSurface)
43 {
44 ilmSurfaceProperties& props = pSceneData->surfaceProperties[surfaceId];
45
46 pSurface->set("id", surfaceId);
47 pSurface->set("destHeight", props.destHeight);
48 pSurface->set("destWidth", props.destWidth);
49 pSurface->set("destX", props.destX);
50 pSurface->set("destY", props.destY);
51 pSurface->set("frameCounter", props.frameCounter);
52 pSurface->set("opacity", props.opacity);
53 pSurface->set("origSourceHeight", props.origSourceHeight);
54 pSurface->set("origSourceWidth", props.origSourceWidth);
55 pSurface->set("sourceHeight", props.sourceHeight);
56 pSurface->set("sourceWidth", props.sourceWidth);
57 pSurface->set("sourceX", props.sourceX);
58 pSurface->set("sourceY", props.sourceY);
59 pSurface->set("visibility", props.visibility);
60 }
61
captureSceneDataHelper(t_ilm_layer layerId,t_scene_data * pSceneData,IlmLayer * pLayer)62 void captureSceneDataHelper(t_ilm_layer layerId, t_scene_data* pSceneData, IlmLayer* pLayer)
63 {
64 ilmLayerProperties& props = pSceneData->layerProperties[layerId];
65 pLayer->set("id", layerId);
66 pLayer->set("destHeight", props.destHeight);
67 pLayer->set("destWidth", props.destWidth);
68 pLayer->set("destX", props.destX);
69 pLayer->set("destY", props.destY);
70 pLayer->set("opacity", props.opacity);
71 pLayer->set("sourceHeight", props.sourceHeight);
72 pLayer->set("sourceWidth", props.sourceWidth);
73 pLayer->set("sourceX", props.sourceX);
74 pLayer->set("sourceY", props.sourceY);
75 pLayer->set("visibility", props.visibility);
76
77 if (pSceneData->layerSurfaces.find(layerId) != pSceneData->layerSurfaces.end())
78 {
79 for (vector<t_ilm_surface>::iterator it = pSceneData->layerSurfaces[layerId].begin();
80 it != pSceneData->layerSurfaces[layerId].end(); ++it)
81 {
82 IlmSurface* pIlmsurface = new IlmSurface;
83 captureSceneDataHelper(*it, pSceneData, pIlmsurface);
84 pLayer->add(pIlmsurface);
85 }
86 }
87 }
88
captureSceneData(IlmScene * scene)89 void captureSceneData(IlmScene* scene)
90 {
91 t_scene_data sceneStruct;
92 captureSceneData(&sceneStruct);
93
94 for (vector<t_ilm_display>::iterator it = sceneStruct.screens.begin();
95 it != sceneStruct.screens.end(); ++it)
96 {
97 t_ilm_display displayId = *it;
98 IlmDisplay* pIlmdisplay = new IlmDisplay;
99 pIlmdisplay->set("id", displayId);
100 pIlmdisplay->set("width", sceneStruct.screenWidth);
101 pIlmdisplay->set("height", sceneStruct.screenHeight);
102
103 if (sceneStruct.screenLayers.find(displayId) != sceneStruct.screenLayers.end())
104 {
105 for (vector<t_ilm_layer>::iterator it = sceneStruct.screenLayers[displayId].begin();
106 it != sceneStruct.screenLayers[displayId].end(); ++it)
107 {
108 IlmLayer* pIlmlayer = new IlmLayer;
109 captureSceneDataHelper(*it, &sceneStruct, pIlmlayer);
110
111 pIlmdisplay->add(pIlmlayer);
112 }
113 }
114
115 scene->add(pIlmdisplay);
116 }
117
118 for (vector<t_ilm_layer>::iterator it = sceneStruct.layers.begin();
119 it != sceneStruct.layers.end(); ++it)
120 {
121 if (sceneStruct.layerScreen.find(*it) == sceneStruct.layerScreen.end())
122 {
123 IlmLayer* pIlmlayer = new IlmLayer;
124 captureSceneDataHelper(*it, &sceneStruct, pIlmlayer);
125
126 scene->add(pIlmlayer);
127 }
128 }
129
130 for (vector<t_ilm_surface>::iterator it = sceneStruct.surfaces.begin();
131 it != sceneStruct.surfaces.end(); ++it)
132 {
133 if (sceneStruct.surfaceLayer.find(*it) == sceneStruct.surfaceLayer.end())
134 {
135 IlmSurface* pIlmsurface = new IlmSurface;
136 captureSceneDataHelper(*it, &sceneStruct, pIlmsurface);
137
138 scene->add(pIlmsurface);
139 }
140 }
141 }
142
encodeEscapesequences(string s)143 string encodeEscapesequences(string s)
144 {
145 map<string, string> code;
146
147 code["\\"] = "\\[\\]";
148 code["\n"] = "\\[n]";
149 code["\t"] = "\\[t]";
150 code["\v"] = "\\[v]";
151 code["\b"] = "\\[b]";
152 code["\f"] = "\\[f]";
153 code["\r"] = "\\[r]";
154
155 return replaceAll(s, code);
156 }
157
exportSceneToTXTHelper(ostream & stream,StringMapTree * tree,string prefix="")158 void exportSceneToTXTHelper(ostream& stream, StringMapTree* tree, string prefix = "")
159 {
160 stream << prefix << encodeEscapesequences(tree->mNodeLabel) + ":{\n";
161 for (map<string, pair<string, string> >::iterator it = tree->mNodeValues.begin();
162 it != tree->mNodeValues.end(); ++it)
163 {
164 stream << prefix + "\t[" + encodeEscapesequences(it->first) + ":"
165 + encodeEscapesequences(it->second.first) + "]=["
166 + encodeEscapesequences(it->second.second) + "]\n";
167 }
168
169 for (list<StringMapTree*>::iterator it = tree->mChildren.begin(); it != tree->mChildren.end(); ++it)
170 {
171 exportSceneToTXTHelper(stream, *it, prefix + "\t");
172 stream << "\n";
173 }
174
175 stream << prefix + "}";
176 }
177
decodeEscapesequences(string s)178 string decodeEscapesequences(string s)
179 {
180 map<string, string> code;
181 code["\\[\\]"] = "\\";
182 code["\\[n]"] = "\n";
183 code["\\[t]"] = "\t";
184 code["\\[v]"] = "\v";
185 code["\\[b]"] = "\b";
186 code["\\[f]"] = "\f";
187 code["\\[r]"] = "\r";
188 return replaceAll(s, code);
189 }
190
importSceneFromTXTHelper(istream & stream,StringMapTree * node)191 void importSceneFromTXTHelper(istream& stream, StringMapTree* node)
192 {
193 string in;
194 //Type
195 getline(stream, in);
196 int typeSize = in.find(":") - in.find_first_not_of('\t');
197 int typeStart = in.find_first_not_of('\t');
198 node->mNodeLabel = in.substr(typeStart, typeSize);
199 while (true)
200 {
201 long streamPosition = stream.tellg();
202 getline(stream, in);
203 in = rtrim(in);
204
205 //end of object
206 if (in.substr(0, 1) == "}")
207 return;
208
209 //start of object property
210 if (in.substr(0, 1) == "[")
211 {
212 int startIndex = in.find('[') + 1;
213 int endIndex = in.find(":");
214 string propertyName = in.substr(startIndex, endIndex - startIndex);
215 propertyName = decodeEscapesequences(propertyName);
216
217 startIndex = endIndex + 1;
218 endIndex = in.find("]");
219 string propertyType = in.substr(startIndex, endIndex - startIndex);
220 propertyType = decodeEscapesequences(propertyType);
221
222 startIndex = in.find('[', endIndex) + 1;
223 endIndex = in.find_last_of(']');
224 string propertyValue = in.substr(startIndex, endIndex - startIndex);
225 propertyValue = decodeEscapesequences(propertyValue);
226
227 node->mNodeValues[propertyName] = make_pair(propertyType, propertyValue);
228 }
229 else
230 {
231 stream.seekg(streamPosition);
232 StringMapTree* child = new StringMapTree;
233 node->mChildren.push_back(child);
234 importSceneFromTXTHelper(stream, child);
235 }
236 }
237 }
238
makeValidXMLCharacters(string s)239 string makeValidXMLCharacters(string s)
240 {
241 map<string, string> code;
242 code["<"] = "<";
243 code[">"] = ">";
244 code["&"] = "&";
245 code["\'"] = "'";
246 code["\""] = """;
247 return replaceAll(s, code);
248 }
249
exportSceneToXMLHelper(ostream & stream,StringMapTree * tree,string prefix="")250 void exportSceneToXMLHelper(ostream& stream, StringMapTree* tree, string prefix = "")
251 {
252 stream << prefix << "<" << tree->mNodeLabel << ">\n";
253
254 for (map<string, pair<string, string> >::iterator it = tree->mNodeValues.begin();
255 it != tree->mNodeValues.end(); ++it)
256 {
257 stream << prefix + "\t<Property name=\"" << it->first << "\" type=\"" << it->second.first << "\">\n";
258 stream << prefix << "\t\t" << makeValidXMLCharacters(it->second.second) + "\n";
259 stream << prefix << "\t</Property>\n";
260 }
261
262 for (list<StringMapTree*>::iterator it = tree->mChildren.begin();
263 it != tree->mChildren.end(); ++it)
264 {
265 exportSceneToXMLHelper(stream, *it, prefix + "\t");
266 stream << "\n";
267 }
268
269 stream << prefix << "</" << tree->mNodeLabel << ">\n";
270 }
271 } //end of anonymous namespace
272
exportSceneToFile(string filename)273 void exportSceneToFile(string filename)
274 {
275 IlmScene ilmscene;
276 IlmScene* pScene = &ilmscene;
277 captureSceneData(&ilmscene);
278 stringstream buffer;
279 StringMapTree sceneTree;
280 pScene->toStringMapTree(&sceneTree);
281
282 //check extension
283 if (filename.find_last_of(".") != string::npos)
284 {
285 string extension = filename.substr(filename.find_last_of("."));
286 cout << extension << endl;
287
288 if (extension == ".xml")
289 {
290 buffer << "<\?xml version=\"1.0\"\?>\n";
291 exportSceneToXMLHelper(buffer, &sceneTree);
292 cout << "DONE WRITING XML" << endl;
293 }
294 else if (extension == ".txt")
295 {
296 exportSceneToTXTHelper(buffer, &sceneTree);
297 cout << "DONE WRITING TXT" << endl;
298 }
299 }
300 else
301 {
302 //defult:
303 exportSceneToTXTHelper(buffer, &sceneTree);
304 cout << "DONE WRITING TXT" << endl;
305 }
306
307 fstream stream(filename.c_str(), ios::out);
308
309 cout << buffer.str() << endl;
310 stream << buffer.str();
311 stream.flush();
312 stream.close();
313 }
314
exportXtext(string fileName,string grammar,string url)315 void exportXtext(string fileName, string grammar, string url)
316 {
317 string name = grammar.substr(grammar.find_last_of('.') + 1);
318 //make sure first character is lower case
319 std::transform(name.begin(), ++(name.begin()), name.begin(), ::tolower);
320
321 IlmScene scene;
322 StringMapTree grammarTree;
323 scene.toGrammarMapTree(&grammarTree);
324
325 //writing to file
326 stringstream buffer;
327 buffer << "grammar " << grammar << " with org.eclipse.xtext.common.Terminals" << endl;
328 buffer << "generate " << name << " \"" << url << "\"" << endl;
329
330 list<string> doneTypes;
331 list<StringMapTree*> waitingNodes;
332 waitingNodes.push_back(&grammarTree);
333 while (!waitingNodes.empty())
334 {
335 //pop first element of the waiting types
336 StringMapTree* typeNode = *(waitingNodes.begin());
337 waitingNodes.pop_front();
338 string type = typeNode->mNodeLabel;
339
340 //if the type was not printed before
341 if (find(doneTypes.begin(), doneTypes.end(), type) == doneTypes.end())
342 {
343 doneTypes.push_back(type);
344
345 buffer << type << ":" << endl;
346 buffer << "\t\'" << type << ":\'" << endl;
347
348 for (map<string, pair<string, string> >::iterator it = typeNode->mNodeValues.begin();
349 it != typeNode->mNodeValues.end(); ++it)
350 {
351 buffer << "\t\t\'" << it->first << ":\' " << it->first << "=" <<
352 (it->second.second.size() > 0 ? it->second.second : it->second.first) << endl;
353 }
354
355 for (list<StringMapTree*>::iterator it = typeNode->mChildren.begin();
356 it != typeNode->mChildren.end(); ++it)
357 {
358 waitingNodes.push_back(*it);
359 string childName = (*it)->mNodeLabel;
360 //make lower case
361 std::transform(childName.begin(), childName.end(), childName.begin(), ::tolower);
362 childName += "s";
363
364 buffer << "\t\t" << childName << "+=" << (*it)->mNodeLabel << "*" << endl;
365 }
366
367 buffer << ";" << endl;
368 }
369 }
370
371 cout << "Xtext:[" << buffer.str() << "]" << endl;
372
373 fstream fileout(fileName.c_str(), ios::out);
374 fileout << buffer.str();
375 fileout.flush();
376 fileout.close();
377 }
378