Freeciv-3.1
Loading...
Searching...
No Matches
registry_xml.c
Go to the documentation of this file.
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.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#ifdef FREECIV_HAVE_XML_REGISTRY
19
20#include <libxml/parser.h>
21
22/* utility */
23#include "capability.h"
24#include "fcintl.h"
25#include "mem.h"
26#include "registry.h"
27#include "section_file.h"
28
29#include "registry_xml.h"
30
31/**********************************************************************/
34struct section_file *xmlfile_load(xmlDoc *sec_doc, const char *filename)
35{
36 struct section_file *secfile;
37 xmlNodePtr xmlroot;
38 xmlNodePtr current;
39 char *cap;
40
41 secfile = secfile_new(TRUE);
42
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;
49 }
50
51 cap = (char *)xmlGetNsProp(xmlroot, (xmlChar *)"options", NULL);
52
53 if (cap == NULL) {
54 log_error(_("XML-file has no capabilities defined!"));
55 secfile_destroy(secfile);
56
57 return NULL;
58 }
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);
64
65 return NULL;
66 }
67
68 if (filename) {
69 secfile->name = fc_strdup(filename);
70 } else {
71 secfile->name = NULL;
72 }
73
74 current = xmlroot->children;
75
76 while (current != NULL) {
77 if (current->type == XML_ELEMENT_NODE) {
78 struct section *psection;
79 xmlNodePtr sec_node;
80
81 psection = secfile_section_new(secfile, (const char *)current->name);
82 sec_node = current->children;
83
84 while (sec_node != NULL) {
85 if (sec_node->type == XML_ELEMENT_NODE) {
86 xmlNodePtr value_node = sec_node->children;
87
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];
93
94 /* Use of strncpy() here would lead to stringop-overflow
95 * compiler warning as the target length is based on source
96 * length. */
97 strcpy(buf, content);
98 if (buf[0] == '"' && buf[len - 1] == '"') {
99 buf[len - 1] = '\0';
100 }
101
102 if (!entry_from_token(psection, (const char *) sec_node->name,
103 buf)) {
104 log_error("Cannot parse token \"%s\"", content);
106 return NULL;
107 }
108 }
109
110 value_node = value_node->next;
111 }
112 }
113
114 sec_node = sec_node->next;
115 }
116 }
117
118 current = current->next;
119 }
120
121 return secfile;
122}
123
124#endif /* FREECIV_HAVE_XML_REGISTRY */
bool has_capabilities(const char *us, const char *them)
Definition capability.c:86
#define _(String)
Definition fcintl.h:67
#define log_normal(message,...)
Definition log.h:107
#define log_error(message,...)
Definition log.h:103
#define fc_strdup(str)
Definition mem.h:43
int len
Definition packhand.c:125
struct section_file * secfile_new(bool allow_duplicates)
void secfile_destroy(struct section_file *secfile)
struct section * secfile_section_new(struct section_file *secfile, const char *name)
bool entry_from_token(struct section *psection, const char *name, const char *tok)
struct section_file * secfile
#define TRUE
Definition support.h:46