thermferm/rdconfig.c

changeset 75
4b976601737d
parent 72
f7cb53c50ee1
child 76
d2c7b32f27d6
equal deleted inserted replaced
74:879bd09e2b96 75:4b976601737d
21 *****************************************************************************/ 21 *****************************************************************************/
22 22
23 #include "thermferm.h" 23 #include "thermferm.h"
24 24
25 25
26 bool debug = FALSE; 26 int debug = FALSE;
27 static char *mypath; 27 static char *mypath;
28 static char *k, *v; 28 static char *k, *v;
29 static int linecnt = 0; 29 static int linecnt = 0;
30 sys_config Config; /* System configuration */ 30 sys_config Config; /* System configuration */
31 31
32 #define MY_ENCODING "utf-8"
32 33
33 34
34 //static int getstr(char **); 35 //static int getstr(char **);
35 static int getw1(char **); 36 static int getw1(char **);
36 #ifdef HAVE_WIRINGPI_H 37 #ifdef HAVE_WIRINGPI_H
98 defaultControlConstants(); 99 defaultControlConstants();
99 } 100 }
100 101
101 102
102 103
103 int wrconfig(char *config) 104 int wrconfig(char *config, char *confxml)
104 { 105 {
105 int rc = 0; 106 int rc = 0;
106 FILE *fp; 107 FILE *fp;
107 w1_therm *tmp1; 108 xmlTextWriterPtr writer;
108 units_list *tmp3; 109 xmlBufferPtr buf;
110 w1_therm *tmp1;
111 units_list *tmp3;
109 112
110 if (getenv((char *)"USER") == NULL) { 113 if (getenv((char *)"USER") == NULL) {
111 mypath = xstrcpy((char *)"/root"); 114 mypath = xstrcpy((char *)"/root");
112 } else { 115 } else {
113 mypath = xstrcpy(getenv((char *)"HOME")); 116 mypath = xstrcpy(getenv((char *)"HOME"));
190 fclose(fp); 193 fclose(fp);
191 syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc); 194 syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc);
192 free(mypath); 195 free(mypath);
193 mypath = NULL; 196 mypath = NULL;
194 197
198
199 /*
200 * Create a new XML buffer, to which the XML document will be written
201 */
202 if ((buf = xmlBufferCreate()) == NULL) {
203 syslog(LOG_NOTICE, "wrconfig: error creating the xml buffer");
204 return 1;
205 }
206
207 /*
208 * Create a new XmlWriter for memory, with no compression.
209 */
210 if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) {
211 syslog(LOG_NOTICE, "wrconfig: error creating the xml writer");
212 return 1;
213 }
214
215 /*
216 * Use indentation instead of one long line
217 */
218 if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) {
219 syslog(LOG_NOTICE, "wrconfig: error setting Indent");
220 return 1;
221 }
222
223 /*
224 * Start the document with the xml default for the version,
225 * encoding ISO 8859-1 and the default for the standalone
226 * declaration.
227 */
228 if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) {
229 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartDocument");
230 return 1;
231 }
232
233 /*
234 * Start an element named "THERMFERM". Since thist is the first
235 * element, this will be the root element of the document.
236 */
237 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "THERMFERM")) < 0) {
238 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
239 return 1;
240 }
241
242 /*
243 * Add an attribute with name "VERSION" and value "1" to THERMFERM.
244 */
245 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
246 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
247 return 1;
248 }
249
250 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "LISTEN_PORT", "%d", Config.my_port)) < 0) {
251 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
252 return 1;
253 }
254
255 #ifdef HAVE_WIRINGPI_H
256 /*
257 * Start an element named "LCDS" as child of THERMFERM.
258 */
259 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) {
260 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
261 return 1;
262 }
263 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCD")) < 0) {
264 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
265 return 1;
266 }
267 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
268 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
269 return 1;
270 }
271 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COLUMNS", "%d", Config.lcd_cols)) < 0) {
272 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
273 return 1;
274 }
275 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROWS", "%d", Config.lcd_rows)) < 0) {
276 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
277 return 1;
278 }
279 /*
280 * Close the element named LCD.
281 */
282 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
283 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
284 return 1;
285 }
286 /*
287 * Close the element LCDS.
288 */
289 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
290 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
291 return 1;
292 }
293 #endif
294
295 /*
296 * Fermenter units
297 */
298 if (Config.units) {
299 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "FERMENTERS")) < 0) {
300 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
301 return 1;
302 }
303 for (tmp3 = Config.units; tmp3; tmp3 = tmp3->next) {
304 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "UNIT")) < 0) {
305 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
306 return 1;
307 }
308 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
309 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
310 return 1;
311 }
312
313 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
314 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
315 return 1;
316 }
317 }
318 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
319 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
320 return 1;
321 }
322 }
323
324 /*
325 * All done, close any open elements
326 */
327 if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
328 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument");
329 return 1;
330 }
331 xmlFreeTextWriter(writer);
332
333 /*
334 * Now write the XML configuration
335 */
336 if (getenv((char *)"USER") == NULL) {
337 mypath = xstrcpy((char *)"/root");
338 } else {
339 mypath = xstrcpy(getenv((char *)"HOME"));
340 }
341 mypath = xstrcat(mypath, (char *)"/mbsepi-apps/");
342 mypath = xstrcat(mypath, confxml);
343
344 if (debug)
345 fprintf(stdout, "Writing %s\n", mypath);
346
347 if ((fp = fopen(mypath, "w")) == NULL) {
348 syslog(LOG_NOTICE, "could not rewrite %s", mypath);
349 return 1;
350 }
351
352 fprintf(fp, "%s", (const char *) buf->content);
353 fclose(fp);
354 xmlBufferFree(buf);
355
195 return rc; 356 return rc;
196 } 357 }
197 358
198 359
199 360

mercurial