Freeciv-3.3
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;
38 xmlNodePtr current;
39 char *cap;
40
41 secfile = secfile_new(TRUE);
42
44 if (xmlroot == nullptr
45 || fc_strcasecmp((const char *)xmlroot->name, "Freeciv")) {
46 /* TRANS: do not translate <Freeciv> */
47 log_error(_("XML-file has no root node <Freeciv>"));
48 secfile_destroy(secfile);
49 return nullptr;
50 }
51
52 cap = (char *)xmlGetNsProp(xmlroot, (xmlChar *)"options", nullptr);
53
54 if (cap == nullptr) {
55 log_error(_("XML-file has no capabilities defined!"));
56 secfile_destroy(secfile);
57
58 return nullptr;
59 }
61 log_error(_("XML-file has incompatible capabilities."));
62 log_normal(_("Freeciv capabilities: %s"), FCXML_CAPSTR);
63 log_normal(_("File capabilities: %s"), cap);
64 secfile_destroy(secfile);
65
66 return nullptr;
67 }
68
69 if (filename) {
70 secfile->name = fc_strdup(filename);
71 } else {
72 secfile->name = nullptr;
73 }
74
75 current = xmlroot->children;
76
77 while (current != nullptr) {
78 if (current->type == XML_ELEMENT_NODE) {
79 struct section *psection;
81
82 psection = secfile_section_new(secfile, (const char *)current->name);
83 sec_node = current->children;
84
85 while (sec_node != nullptr) {
86 if (sec_node->type == XML_ELEMENT_NODE) {
87 xmlNodePtr value_node = sec_node->children;
88
89 while (value_node != nullptr) {
90 if (value_node->type == XML_TEXT_NODE) {
91 const char *content = (const char *) xmlNodeGetContent(value_node);
92 int len = strlen(content);
93 char buf[len + 1];
94
95 /* Use of strncpy() here would lead to stringop-overflow
96 * compiler warning as the target length is based on source
97 * length. */
98 strcpy(buf, content);
99 if (buf[0] == '"' && buf[len - 1] == '"') {
100 buf[len - 1] = '\0';
101 }
102
103 if (!entry_from_token(psection, (const char *) sec_node->name,
104 buf)) {
105 log_error("Cannot parse token \"%s\"", content);
107 return nullptr;
108 }
109 }
110
111 value_node = value_node->next;
112 }
113 }
114
115 sec_node = sec_node->next;
116 }
117 }
118
119 current = current->next;
120 }
121
122 return secfile;
123}
124
125#endif /* FREECIV_HAVE_XML_REGISTRY */
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
char * incite_cost
Definition comments.c:76
#define _(String)
Definition fcintl.h:67
#define log_normal(message,...)
Definition log.h:108
#define log_error(message,...)
Definition log.h:104
#define fc_strdup(str)
Definition mem.h:43
int len
Definition packhand.c:127
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
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46