2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
+-------------------+ 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.
|
2014-07-26 22:52:25 +02:00
|
|
|
*/
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "xml.h"
|
|
|
|
|
|
|
|
/* util includes */
|
|
|
|
#include "log.h"
|
2016-08-29 10:20:08 +02:00
|
|
|
#include "assert.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-06-10 05:14:08 +02:00
|
|
|
#ifdef USE_LIBXML2
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <libxml/catalog.h>
|
|
|
|
#include <libxml/xmlstring.h>
|
2014-06-10 05:14:08 +02:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2014-06-10 05:14:08 +02:00
|
|
|
#ifdef USE_LIBXML2
|
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
|
|
|
{
|
2014-07-26 22:52:25 +02:00
|
|
|
int i = dflt;
|
|
|
|
xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
|
|
|
|
if (propValue != NULL) {
|
|
|
|
i = atoi((const char *)propValue);
|
|
|
|
xmlFree(propValue);
|
|
|
|
}
|
|
|
|
return i;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 07:41:07 +02:00
|
|
|
bool xml_bvalue(xmlNodePtr node, const char *name, bool dflt)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-26 22:52:25 +02:00
|
|
|
bool result = dflt;
|
|
|
|
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) {
|
|
|
|
log_warning("bool value is '1': %s::%s\n", node->name, name);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else if (strcmp((const char *)propValue, "0") == 0) {
|
|
|
|
log_warning("bool value is '0': %s::%s\n", node->name, name);
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
xmlFree(propValue);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-07-26 22:52:25 +02:00
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-26 22:52:25 +02:00
|
|
|
double result = dflt;
|
|
|
|
xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
|
|
|
|
if (propValue != NULL) {
|
|
|
|
result = atof((const char *)propValue);
|
|
|
|
xmlFree(propValue);
|
|
|
|
}
|
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* new xml functions */
|
|
|
|
/* libxml includes */
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/xinclude.h>
|
|
|
|
|
|
|
|
typedef struct xml_reader {
|
2014-07-26 22:52:25 +02:00
|
|
|
struct xml_reader *next;
|
|
|
|
xml_callback callback;
|
2010-08-08 10:06:34 +02:00
|
|
|
} xml_reader;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static xml_reader *xmlReaders;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-09-07 21:15:24 +02:00
|
|
|
void xml_done(void) {
|
|
|
|
xml_reader ** xrp = &xmlReaders;
|
|
|
|
while (*xrp) {
|
|
|
|
xml_reader *xr = *xrp;
|
|
|
|
*xrp = xr->next;
|
|
|
|
free(xr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void xml_register_callback(xml_callback callback)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-26 22:52:25 +02:00
|
|
|
xml_reader *reader = (xml_reader *)malloc(sizeof(xml_reader));
|
|
|
|
xml_reader **insert = &xmlReaders;
|
2016-08-29 10:20:08 +02:00
|
|
|
assert_alloc(reader);
|
2014-07-26 22:52:25 +02:00
|
|
|
reader->callback = callback;
|
|
|
|
reader->next = NULL;
|
|
|
|
|
|
|
|
while (*insert)
|
|
|
|
insert = &(*insert)->next;
|
|
|
|
*insert = reader;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-06-10 04:04:11 +02:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
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
|
|
|
{
|
2014-06-10 04:04:11 +02:00
|
|
|
#ifdef USE_LIBXML2
|
2014-07-26 22:52:25 +02:00
|
|
|
xml_reader *reader = xmlReaders;
|
|
|
|
xmlDocPtr doc;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (catalog) {
|
|
|
|
xmlLoadCatalog(catalog);
|
|
|
|
}
|
|
|
|
doc = xmlReadFile(filename, NULL, XML_PARSE_XINCLUDE | XML_PARSE_NONET | XML_PARSE_PEDANTIC | XML_PARSE_COMPACT);
|
|
|
|
if (doc == NULL) {
|
|
|
|
log_error("could not open '%s'\n", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = xmlXIncludeProcessFlags(doc, XML_PARSE_XINCLUDE | XML_PARSE_NONET | XML_PARSE_PEDANTIC | XML_PARSE_COMPACT);
|
|
|
|
if (result >= 0) {
|
|
|
|
while (reader != NULL) {
|
|
|
|
int i = reader->callback(doc);
|
|
|
|
if (i != 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
reader = reader->next;
|
|
|
|
}
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
return result;
|
2014-06-10 04:04:11 +02:00
|
|
|
#else
|
|
|
|
log_error("LIBXML2 disabled, cannot read %s.\n", filename);
|
|
|
|
return -1;
|
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|