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 79b54060f9
commit 92f82c3608
4 changed files with 60 additions and 37 deletions

View File

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

View File

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

View File

@ -887,6 +887,53 @@ static void json_races(cJSON *json) {
const char * json_relpath; 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) { static void json_include(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type != cJSON_Array) { if (json->type != cJSON_Array) {
@ -894,39 +941,12 @@ static void json_include(cJSON *json) {
return; return;
} }
for (child = json->child; child; child = child->next) { for (child = json->child; child; child = child->next) {
FILE *F; const char * filename = child->valuestring;
if (json_relpath) { if (strstr(filename, ".xml") != NULL) {
char name[PATH_MAX]; include_xml(filename);
path_join(json_relpath, child->valuestring, name, sizeof(name));
F = fopen(name, "r");
} }
else { else {
F = fopen(child->valuestring, "r"); include_json(filename);
}
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);
} }
} }
} }

View File

@ -112,7 +112,7 @@ int read_xml(const char *filename, const char *catalog)
{ {
xml_reader *reader = xmlReaders; xml_reader *reader = xmlReaders;
xmlDocPtr doc; xmlDocPtr doc;
int result; int result = 0;
if (catalog) { if (catalog) {
xmlLoadCatalog(catalog); xmlLoadCatalog(catalog);
@ -122,8 +122,9 @@ int read_xml(const char *filename, const char *catalog)
log_error("could not open '%s'\n", filename); log_error("could not open '%s'\n", filename);
return -1; return -1;
} }
if (catalog) {
result = xmlXIncludeProcessFlags(doc, XML_PARSE_XINCLUDE | XML_PARSE_NONET | XML_PARSE_PEDANTIC | XML_PARSE_COMPACT); result = xmlXIncludeProcessFlags(doc, XML_PARSE_XINCLUDE | XML_PARSE_NONET | XML_PARSE_PEDANTIC | XML_PARSE_COMPACT);
}
if (result >= 0) { if (result >= 0) {
while (reader != NULL) { while (reader != NULL) {
int i = reader->callback(doc); int i = reader->callback(doc);