diff -r 879bd09e2b96 -r 4b976601737d thermferm/rdconfig.c --- a/thermferm/rdconfig.c Tue Jun 24 20:21:07 2014 +0200 +++ b/thermferm/rdconfig.c Tue Jun 24 22:38:46 2014 +0200 @@ -23,12 +23,13 @@ #include "thermferm.h" -bool debug = FALSE; +int debug = FALSE; static char *mypath; static char *k, *v; static int linecnt = 0; sys_config Config; /* System configuration */ +#define MY_ENCODING "utf-8" //static int getstr(char **); @@ -100,12 +101,14 @@ -int wrconfig(char *config) +int wrconfig(char *config, char *confxml) { - int rc = 0; - FILE *fp; - w1_therm *tmp1; - units_list *tmp3; + int rc = 0; + FILE *fp; + xmlTextWriterPtr writer; + xmlBufferPtr buf; + w1_therm *tmp1; + units_list *tmp3; if (getenv((char *)"USER") == NULL) { mypath = xstrcpy((char *)"/root"); @@ -192,6 +195,164 @@ free(mypath); mypath = NULL; + + /* + * Create a new XML buffer, to which the XML document will be written + */ + if ((buf = xmlBufferCreate()) == NULL) { + syslog(LOG_NOTICE, "wrconfig: error creating the xml buffer"); + return 1; + } + + /* + * Create a new XmlWriter for memory, with no compression. + */ + if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) { + syslog(LOG_NOTICE, "wrconfig: error creating the xml writer"); + return 1; + } + + /* + * Use indentation instead of one long line + */ + if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error setting Indent"); + return 1; + } + + /* + * Start the document with the xml default for the version, + * encoding ISO 8859-1 and the default for the standalone + * declaration. + */ + if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartDocument"); + return 1; + } + + /* + * Start an element named "THERMFERM". Since thist is the first + * element, this will be the root element of the document. + */ + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "THERMFERM")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); + return 1; + } + + /* + * Add an attribute with name "VERSION" and value "1" to THERMFERM. + */ + if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); + return 1; + } + + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "LISTEN_PORT", "%d", Config.my_port)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); + return 1; + } + +#ifdef HAVE_WIRINGPI_H + /* + * Start an element named "LCDS" as child of THERMFERM. + */ + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); + return 1; + } + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCD")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); + return 1; + } + if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COLUMNS", "%d", Config.lcd_cols)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROWS", "%d", Config.lcd_rows)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); + return 1; + } + /* + * Close the element named LCD. + */ + if ((rc = xmlTextWriterEndElement(writer)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); + return 1; + } + /* + * Close the element LCDS. + */ + if ((rc = xmlTextWriterEndElement(writer)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); + return 1; + } +#endif + + /* + * Fermenter units + */ + if (Config.units) { + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "FERMENTERS")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); + return 1; + } + for (tmp3 = Config.units; tmp3; tmp3 = tmp3->next) { + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "UNIT")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); + return 1; + } + if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); + return 1; + } + + if ((rc = xmlTextWriterEndElement(writer)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); + return 1; + } + } + if ((rc = xmlTextWriterEndElement(writer)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); + return 1; + } + } + + /* + * All done, close any open elements + */ + if ((rc = xmlTextWriterEndDocument(writer)) < 0) { + syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument"); + return 1; + } + xmlFreeTextWriter(writer); + + /* + * Now write the XML configuration + */ + if (getenv((char *)"USER") == NULL) { + mypath = xstrcpy((char *)"/root"); + } else { + mypath = xstrcpy(getenv((char *)"HOME")); + } + mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); + mypath = xstrcat(mypath, confxml); + + if (debug) + fprintf(stdout, "Writing %s\n", mypath); + + if ((fp = fopen(mypath, "w")) == NULL) { + syslog(LOG_NOTICE, "could not rewrite %s", mypath); + return 1; + } + + fprintf(fp, "%s", (const char *) buf->content); + fclose(fp); + xmlBufferFree(buf); + return rc; }