webperimental: killstack decides stack protects.
[freeciv.git] / utility / registry_xml.c
blob99edd49d52499a7e9fe25c7b6f11b5c7e47164e2
1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #ifdef FREECIV_HAVE_XML_REGISTRY
20 #include <libxml/parser.h>
22 /* utility */
23 #include "capability.h"
24 #include "fcintl.h"
25 #include "mem.h"
26 #include "registry.h"
27 #include "section_file.h"
29 #include "registry_xml.h"
31 /**************************************************************************
32 Load section file from xml-file
33 **************************************************************************/
34 struct section_file *xmlfile_load(xmlDoc *sec_doc, const char *filename)
36 struct section_file *secfile;
37 xmlNodePtr xmlroot;
38 xmlNodePtr current;
39 char *cap;
41 secfile = secfile_new(TRUE);
43 xmlroot = xmlDocGetRootElement(sec_doc);
44 if (xmlroot == NULL || strcmp((const char *)xmlroot->name, "Freeciv")) {
45 /* TRANS: do not translate <Freeciv> */
46 log_error(_("XML-file has no root node <Freeciv>"));
47 secfile_destroy(secfile);
48 return NULL;
51 cap = (char *)xmlGetNsProp(xmlroot, (xmlChar *)"options", NULL);
53 if (cap == NULL) {
54 log_error(_("XML-file has no capabilities defined!"));
55 secfile_destroy(secfile);
57 return NULL;
59 if (!has_capabilities(FCXML_CAPSTR, cap)) {
60 log_error(_("XML-file has incompatible capabilities."));
61 log_normal(_("Freeciv capabilities: %s"), FCXML_CAPSTR);
62 log_normal(_("File capabilities: %s"), cap);
63 secfile_destroy(secfile);
65 return NULL;
68 if (filename) {
69 secfile->name = fc_strdup(filename);
70 } else {
71 secfile->name = NULL;
74 current = xmlroot->children;
76 while (current != NULL) {
77 if (current->type == XML_ELEMENT_NODE) {
78 struct section *psection;
79 xmlNodePtr sec_node;
81 psection = secfile_section_new(secfile, (const char *)current->name);
82 sec_node = current->children;
84 while (sec_node != NULL) {
85 if (sec_node->type == XML_ELEMENT_NODE) {
86 xmlNodePtr value_node = sec_node->children;
88 while (value_node != NULL) {
89 if (value_node->type == XML_TEXT_NODE) {
90 const char *content = (const char *) xmlNodeGetContent(value_node);
91 int len = strlen(content);
92 char buf[len + 1];
94 strncpy(buf, content, sizeof(buf));
95 if (buf[0] == '"' && buf[len - 1] == '"') {
96 buf[len - 1] = '\0';
99 if (!entry_from_token(psection, (const char *) sec_node->name,
100 buf)) {
101 log_error("Cannot parse token \"%s\"", content);
102 secfile_destroy(secfile);
103 return NULL;
107 value_node = value_node->next;
111 sec_node = sec_node->next;
115 current = current->next;
118 return secfile;
121 #endif /* FREECIV_HAVE_XML_REGISTRY */