plan to eliminate xinclude use, move to single config file with includes.

This commit is contained in:
Enno Rehling 2018-02-04 18:25:38 +01:00
parent 2d7d46d3ac
commit a316283a6d
4 changed files with 60 additions and 37 deletions

View File

@ -3,7 +3,9 @@
"keywords.json",
"calendar.json",
"prefixes.json",
"e2/terrains.json"
"e2/terrains.json",
"e2/rules.xml",
"e2/locales.xml"
],
"disabled": [
"jsreport"

View File

@ -5,7 +5,7 @@ end
if config.rules then
local rules = config.rules .. '/'
assert(0 == eressea.config.read(rules .. 'config.json', confdir), "could not read JSON data")
assert(0 == read_xml(confdir .. rules .. 'rules.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
assert(0 == read_xml(confdir .. rules .. 'locales.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
-- assert(0 == read_xml(confdir .. rules .. 'rules.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
-- assert(0 == read_xml(confdir .. rules .. 'locales.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?")
end
eressea.game.reset()

View File

@ -887,6 +887,53 @@ static void json_races(cJSON *json) {
const char * json_relpath;
static void include_json(const char *filename) {
FILE *F;
if (json_relpath) {
char name[PATH_MAX];
path_join(json_relpath, filename, name, sizeof(name));
F = fopen(name, "r");
}
else {
F = fopen(filename, "r");
}
if (F) {
long pos;
fseek(F, 0, SEEK_END);
pos = ftell(F);
rewind(F);
if (pos > 0) {
cJSON *config;
char *data;
size_t sz;
data = malloc(pos + 1);
sz = fread(data, 1, (size_t)pos, F);
data[sz] = 0;
config = cJSON_Parse(data);
free(data);
if (config) {
json_config(config);
cJSON_Delete(config);
}
else {
log_error("invalid JSON, could not parse %s", filename);
}
}
fclose(F);
}
}
static void include_xml(const char *filename) {
char name[PATH_MAX];
if (json_relpath) {
path_join(json_relpath, filename, name, sizeof(name));
filename = name;
}
read_xml(filename, NULL);
}
static void json_include(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Array) {
@ -894,39 +941,12 @@ static void json_include(cJSON *json) {
return;
}
for (child = json->child; child; child = child->next) {
FILE *F;
if (json_relpath) {
char name[PATH_MAX];
path_join(json_relpath, child->valuestring, name, sizeof(name));
F = fopen(name, "r");
const char * filename = child->valuestring;
if (strstr(filename, ".xml") != NULL) {
include_xml(filename);
}
else {
F = fopen(child->valuestring, "r");
}
if (F) {
long pos;
fseek(F, 0, SEEK_END);
pos = ftell(F);
rewind(F);
if (pos > 0) {
cJSON *config;
char *data;
size_t sz;
data = malloc(pos + 1);
sz = fread(data, 1, (size_t)pos, F);
data[sz] = 0;
config = cJSON_Parse(data);
free(data);
if (config) {
json_config(config);
cJSON_Delete(config);
}
else {
log_error("invalid JSON, could not parse %s", child->valuestring);
}
}
fclose(F);
include_json(filename);
}
}
}

View File

@ -112,7 +112,7 @@ int read_xml(const char *filename, const char *catalog)
{
xml_reader *reader = xmlReaders;
xmlDocPtr doc;
int result;
int result = 0;
if (catalog) {
xmlLoadCatalog(catalog);
@ -122,8 +122,9 @@ int read_xml(const char *filename, const char *catalog)
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 (catalog) {
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);