server/src/util/xml.c

137 lines
3.3 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
+-------------------+ Stefan Reich <reich@halbling.de>
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include "xml.h"
/* util includes */
#include "log.h"
#include <libxml/catalog.h>
#include <libxml/xmlstring.h>
/* libc includes */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
2011-03-07 08:02:35 +01:00
const xmlChar *xml_i(double number)
2010-08-08 10:06:34 +02:00
{
static char buffer[128];
2011-03-07 17:26:50 +01:00
snprintf(buffer, sizeof(buffer), "%.0f", number);
2010-08-08 10:06:34 +02:00
return (const xmlChar *)buffer;
}
2011-03-07 08:02:35 +01:00
int xml_ivalue(xmlNodePtr node, const char *name, int dflt)
2010-08-08 10:06:34 +02:00
{
int i = dflt;
2011-03-07 08:02:35 +01:00
xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
if (propValue != NULL) {
i = atoi((const char *)propValue);
2010-08-08 10:06:34 +02:00
xmlFree(propValue);
}
return i;
}
2011-03-07 08:02:35 +01:00
boolean xml_bvalue(xmlNodePtr node, const char *name, boolean dflt)
2010-08-08 10:06:34 +02:00
{
boolean result = dflt;
2011-03-07 08:02:35 +01:00
xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
if (propValue != NULL) {
if (strcmp((const char *)propValue, "no") == 0)
result = false;
else if (strcmp((const char *)propValue, "yes") == 0)
result = true;
else if (strcmp((const char *)propValue, "false") == 0)
result = false;
else if (strcmp((const char *)propValue, "true") == 0)
result = true;
else if (strcmp((const char *)propValue, "1") == 0) {
2010-08-08 10:06:34 +02:00
log_warning(("boolean value is '1': %s::%s\n", node->name, name));
result = true;
2011-03-07 08:02:35 +01:00
} else if (strcmp((const char *)propValue, "0") == 0) {
2010-08-08 10:06:34 +02:00
log_warning(("boolean value is '0': %s::%s\n", node->name, name));
result = false;
}
xmlFree(propValue);
}
return result;
}
2011-03-07 08:02:35 +01:00
double xml_fvalue(xmlNodePtr node, const char *name, double dflt)
2010-08-08 10:06:34 +02:00
{
double result = dflt;
2011-03-07 08:02:35 +01:00
xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
if (propValue != NULL) {
result = atof((const char *)propValue);
2010-08-08 10:06:34 +02:00
xmlFree(propValue);
}
return result;
}
/* new xml functions */
/* libxml includes */
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xinclude.h>
typedef struct xml_reader {
2011-03-07 08:02:35 +01:00
struct xml_reader *next;
2010-08-08 10:06:34 +02:00
xml_callback callback;
} xml_reader;
2011-03-07 08:02:35 +01:00
static xml_reader *xmlReaders;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
void xml_register_callback(xml_callback callback)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
xml_reader *reader = (xml_reader *) malloc(sizeof(xml_reader));
xml_reader **insert = &xmlReaders;
2010-08-08 10:06:34 +02:00
reader->callback = callback;
reader->next = NULL;
2011-03-07 08:02:35 +01:00
while (*insert)
insert = &(*insert)->next;
2010-08-08 10:06:34 +02:00
*insert = reader;
}
2011-03-07 08:02:35 +01:00
int read_xml(const char *filename, const char *catalog)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
xml_reader *reader = xmlReaders;
2010-08-08 10:06:34 +02:00
xmlDocPtr doc;
if (catalog) {
xmlLoadCatalog(catalog);
}
#ifdef XML_PARSE_XINCLUDE
doc = xmlReadFile(filename, NULL, XML_PARSE_XINCLUDE);
#else
doc = xmlParseFile(filename);
#endif
2011-03-07 08:02:35 +01:00
if (doc == NULL) {
2010-08-08 10:06:34 +02:00
log_error(("could not open %s\n", filename));
return -1;
}
xmlXIncludeProcess(doc);
2011-03-07 08:02:35 +01:00
while (reader != NULL) {
2010-08-08 10:06:34 +02:00
int i = reader->callback(doc);
2011-03-07 08:02:35 +01:00
if (i != 0) {
2010-08-08 10:06:34 +02:00
return i;
}
reader = reader->next;
}
xmlFreeDoc(doc);
return 0;
}