Copyright update for 2011
[bcusdk.git] / bcugen / lib / xmlwriteappinfo.cpp
blobc10c244fbd60f2d0b47238fadb065df17af760dc
1 /*
2 BCU SDK bcu development enviroment
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "xmlwriteappinfo.h"
21 #include <libxml/tree.h>
23 static char buf[4096];
25 static void
26 a (xmlNodePtr p, const char *name, const char *val)
28 xmlNewProp (p, (const xmlChar *) name, (const xmlChar *) val);
31 xmlNodePtr
32 nn (const char *name)
34 return xmlNewNode (0, (const xmlChar *) name);
37 xmlNodePtr
38 nc (xmlNodePtr parent, const char *name, const char *val)
40 return xmlNewTextChild (parent, 0, (const xmlChar *) name,
41 (const xmlChar *) val);
44 static xmlNodePtr
45 GenDeviceDesc (const Device & d)
47 xmlNodePtr n = nn ("Description");
48 sprintf (buf, "%04X", d.MaskVersion);
49 nc (n, "MaskVersion", buf);
50 nc (n, "Title", d.Title ());
51 if (d.AddInfo ())
52 nc (n, "AddInfo", d.AddInfo ());
53 if (d.OrderNo ())
54 nc (n, "OrderNo", d.OrderNo ());
55 if (d.Manufacturer ())
56 nc (n, "Manufacturer", d.Manufacturer ());
57 if (d.Category ())
58 nc (n, "Category", d.Category ());
59 if (d.Author ())
60 nc (n, "Author", d.Author ());
61 if (d.Copyright ())
62 nc (n, "Copyright", d.Copyright ());
63 return n;
66 static xmlNodePtr
67 GenInterface (const Interface & d)
69 if (!d.ID_lineno)
70 return 0;
71 xmlNodePtr p;
72 int count = 0;
73 xmlNodePtr n = nn ("Interface");
74 a (n, "id", d.ID ());
75 sprintf (buf, "%0.3f", d.DPType);
76 nc (n, "DPType", buf);
77 if (d.Abbreviation ())
78 nc (n, "Abbreviation", d.Abbreviation ());
79 if (d.Title ())
80 nc (n, "Title", d.Title ());
81 if (d.AddInfo ())
82 nc (n, "AddInfo", d.AddInfo ());
83 if (d.GroupTitle ())
84 nc (n, "GroupTitle", d.GroupTitle ());
85 if (d.InvisibleIf_lineno)
86 nc (n, "InvisibleIf", printExpression (d.InvisibleIf) ());
88 for (int i = 0; i < d.References (); i++)
90 p = nn ("Reference");
91 a (p, "idref", d.References[i] ());
92 xmlAddChild (n, p);
93 count++;
95 if (!count)
97 xmlFreeNode (n);
98 n = 0;
100 return n;
103 static xmlNodePtr
104 GenFunctionalBlock (const FunctionalBlock & d)
106 if (!d.ID_lineno)
107 return 0;
108 int count = 0;
109 xmlNodePtr p;
110 xmlNodePtr n = nn ("FunctionalBlock");
111 a (n, "id", d.ID ());
112 sprintf (buf, "%d", d.ProfileID);
113 nc (n, "ProfileID", buf);
114 nc (n, "Title", d.Title ());
115 if (d.AddInfo ())
116 nc (n, "AddInfo", d.AddInfo ());
117 for (int i = 0; i < d.Interfaces (); i++)
119 p = GenInterface (d.Interfaces[i]);
120 if (p)
121 xmlAddChild (n, p);
122 count++;
124 if (!count)
126 xmlFreeNode (n);
127 n = 0;
129 return n;
132 static xmlNodePtr
133 GenGroupObject (const GroupObject & d)
135 if (!d.ID_lineno)
136 return 0;
137 xmlNodePtr n = nn ("GroupObject");
138 a (n, "id", d.ID ());
139 nc (n, "Title", d.Title ());
140 if (d.AddInfo ())
141 nc (n, "AddInfo", d.AddInfo ());
142 sprintf (buf, "%d", d.Type);
143 nc (n, "GroupType", buf);
144 nc (n, "Sending", d.Sending ? "true" : "false");
145 nc (n, "Receiving", d.Receiving ? "true" : "false");
146 nc (n, "Reading", d.Reading ? "true" : "false");
147 nc (n, "StateBased", d.StateBased ? "true" : "false");
148 return n;
151 static xmlNodePtr
152 GenProperty (const Property & d, itype objindex)
154 if (!d.ID_lineno)
155 return 0;
156 xmlNodePtr n = nn ("Property");
157 a (n, "id", d.ID ());
158 nc (n, "Title", d.Title ());
159 if (d.AddInfo ())
160 nc (n, "AddInfo", d.AddInfo ());
161 sprintf (buf, "%d", d.Type);
162 nc (n, "PropertyType", buf);
164 sprintf (buf, "%d", objindex);
165 nc (n, "ObjectIndex", buf);
166 sprintf (buf, "%d", d.PropertyID);
167 nc (n, "PropertyId", buf);
169 nc (n, "Writeable", d.Writeable ? "true" : "false");
170 sprintf (buf, "%d", d.MaxArrayLength);
171 nc (n, "MaxArrayLength", buf);
173 return n;
176 static xmlNodePtr
177 GenPollingMaster (const PollingMaster & d)
179 if (!d.ID_lineno)
180 return 0;
181 xmlNodePtr n = nn ("PollingMaster");
182 a (n, "id", d.ID ());
183 nc (n, "Title", d.Title ());
184 if (d.AddInfo ())
185 nc (n, "AddInfo", d.AddInfo ());
187 return n;
190 static xmlNodePtr
191 GenPollingSlave (const PollingSlave & d)
193 if (!d.ID_lineno)
194 return 0;
195 xmlNodePtr n = nn ("PollingSlave");
196 a (n, "id", d.ID ());
197 nc (n, "Title", d.Title ());
198 if (d.AddInfo ())
199 nc (n, "AddInfo", d.AddInfo ());
201 return n;
204 static xmlNodePtr
205 GenListParameter (const ListParameter & d)
207 xmlNodePtr p;
208 if (!d.ID ())
209 return 0;
210 xmlNodePtr n = nn ("ListParameter");
211 a (n, "id", d.ID ());
212 nc (n, "Title", d.Title ());
213 if (d.AddInfo ())
214 nc (n, "AddInfo", d.AddInfo ());
216 p = nn ("ListDefault");
217 a (p, "idref", d.ListDefault ());
218 xmlAddChild (n, p);
219 for (int i = 0; i < d.ListElements (); i++)
221 p = nc (n, "ListElement", d.ListElements[i].Value ());
222 a (p, "id", d.ListElements[i].Name ());
224 if (d.Unit ())
225 nc (n, "Unit", d.Unit ());
226 return n;
229 static xmlNodePtr
230 GenIntParameter (const IntParameter & d)
232 if (!d.ID_lineno)
233 return 0;
234 xmlNodePtr n = nn ("IntParameter");
235 a (n, "id", d.ID ());
236 nc (n, "Title", d.Title ());
237 if (d.AddInfo ())
238 nc (n, "AddInfo", d.AddInfo ());
239 sprintf (buf, "%d", d.MinValue);
240 nc (n, "MinValue", buf);
241 sprintf (buf, "%d", d.MaxValue);
242 nc (n, "MaxValue", buf);
243 sprintf (buf, "%d", d.Default);
244 nc (n, "Default", buf);
245 if (d.Unit ())
246 nc (n, "Unit", d.Unit ());
247 if (d.Precision_lineno)
249 sprintf (buf, "%d", d.Precision);
250 nc (n, "Precision", buf);
252 if (d.Increment_lineno)
254 sprintf (buf, "%d", d.Increment);
255 nc (n, "Increment", buf);
257 return n;
260 static xmlNodePtr
261 GenFloatParameter (const FloatParameter & d)
263 if (!d.ID_lineno)
264 return 0;
265 xmlNodePtr n = nn ("FloatParameter");
266 a (n, "id", d.ID ());
267 nc (n, "Title", d.Title ());
268 if (d.AddInfo ())
269 nc (n, "AddInfo", d.AddInfo ());
270 sprintf (buf, "%f", d.MinValue);
271 nc (n, "MinValue", buf);
272 sprintf (buf, "%f", d.MaxValue);
273 nc (n, "MaxValue", buf);
274 sprintf (buf, "%f", d.Default);
275 nc (n, "Default", buf);
276 if (d.Unit ())
277 nc (n, "Unit", d.Unit ());
278 if (d.Precision_lineno)
280 sprintf (buf, "%f", d.Precision);
281 nc (n, "Precision", buf);
283 if (d.Increment_lineno)
285 sprintf (buf, "%f", d.Increment);
286 nc (n, "Increment", buf);
289 return n;
292 static xmlNodePtr
293 GenStringParameter (const StringParameter & d)
295 if (!d.ID_lineno)
296 return 0;
297 xmlNodePtr n = nn ("StringParameter");
298 a (n, "id", d.ID ());
299 nc (n, "Title", d.Title ());
300 if (d.AddInfo ())
301 nc (n, "AddInfo", d.AddInfo ());
303 nc (n, "Default", d.Default ());
304 if (d.RegExp ())
305 nc (n, "RegExp", d.RegExp ());
306 sprintf (buf, "%d", d.MaxLength);
307 nc (n, "MaxLength", buf);
308 if (d.Unit ())
309 nc (n, "Unit", d.Unit ());
311 return n;
314 static xmlNodePtr
315 GenParameters (const Device & d)
317 int i;
318 int cnt = 0;
319 xmlNodePtr p;
320 xmlNodePtr n = nn ("Parameter");
321 for (i = 0; i < d.ListParameters (); i++)
323 p = GenListParameter (d.ListParameters[i]);
324 if (p)
325 xmlAddChild (n, p), cnt++;
327 for (i = 0; i < d.IntParameters (); i++)
329 p = GenIntParameter (d.IntParameters[i]);
330 if (p)
331 xmlAddChild (n, p), cnt++;
333 for (i = 0; i < d.FloatParameters (); i++)
335 p = GenFloatParameter (d.FloatParameters[i]);
336 if (p)
337 xmlAddChild (n, p), cnt++;
339 for (i = 0; i < d.StringParameters (); i++)
341 p = GenStringParameter (d.StringParameters[i]);
342 if (p)
343 xmlAddChild (n, p), cnt++;
345 if (!cnt)
347 xmlFreeNode (n);
348 n = 0;
350 return n;
353 void
354 GenChild (xmlNodePtr n, const Device & d)
356 xmlNodePtr p;
357 int i, j;
359 for (i = 0, j = 0; i < d.FunctionalBlocks (); i++)
361 p = GenFunctionalBlock (d.FunctionalBlocks[i]);
362 if (p)
363 xmlAddChild (n, p), j++;
365 if (!j)
366 die (_("no functional block with enabled interfaces"));
367 for (i = 0; i < d.GroupObjects (); i++)
369 p = GenGroupObject (d.GroupObjects[i]);
370 if (p)
371 xmlAddChild (n, p);
373 for (i = 0; i < d.Objects (); i++)
375 if (!d.Objects[i].ObjectIndex)
376 continue;
377 for (j = 0; j < d.Objects[i].Propertys (); j++)
380 GenProperty (d.Objects[i].Propertys[j], d.Objects[i].ObjectIndex);
381 if (p)
382 xmlAddChild (n, p);
385 for (i = 0; i < d.PollingMasters (); i++)
387 p = GenPollingMaster (d.PollingMasters[i]);
388 if (p)
389 xmlAddChild (n, p);
391 for (i = 0; i < d.PollingSlaves (); i++)
393 p = GenPollingSlave (d.PollingSlaves[i]);
394 if (p)
395 xmlAddChild (n, p);
398 p = GenParameters (d);
399 if (p)
400 xmlAddChild (n, p);
403 void
404 WriteAppInfo (const Device * d, const char *file)
406 xmlDocPtr doc = 0;
407 xmlNodePtr root = 0;
409 if (!d)
410 die (_("no device defined"));
411 LIBXML_TEST_VERSION;
413 doc = xmlNewDoc ((const xmlChar *) "1.0");
414 xmlCreateIntSubset (doc, (const xmlChar *) "DeviceDesc", NULL,
415 (const xmlChar *)
416 "http://www.auto.tuwien.ac.at/~mkoegler/eib/xml/appinfo.dtd");
417 root = xmlNewNode (NULL, (const xmlChar *) "DeviceDesc");
418 xmlDocSetRootElement (doc, root);
420 a (root, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
421 a (root, "xsi:schemaLocation",
422 "http://www.auto.tuwien.ac.at/~mkoegler/eib/appinfo.xsd");
423 a (root, "xmlns",
424 "http://www.auto.tuwien.ac.at/~mkoegler/eib/xml/appinfo.xsd");
425 a (root, "version", "0.0.0");
427 nc (root, "ProgramID", d->ProgramID ());
428 xmlAddChild (root, GenDeviceDesc (*d));
431 GenChild (root, *d);
433 if (xmlSaveFormatFileEnc (file, doc, "UTF-8", 1) < 0)
434 die (_("write to %s failed"), file);
436 xmlFreeDoc (doc);