Stop help talking about units' ability to attack relative to non-native
[freeciv.git] / server / fcdb.c
blobb3999d81ffb1438ab8764114412ac4831b3fede4
1 /****************************************************************************
2 Freeciv - Copyright (C) 2005 - M.C. Kaufman
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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 /* utility */
23 #include "fcintl.h"
24 #include "log.h"
25 #include "md5.h"
26 #include "shared.h"
27 #include "support.h"
29 /* common */
30 #include "connection.h"
31 #include "packets.h"
33 /* server */
34 #include "connecthand.h"
35 #include "notify.h"
36 #include "sernet.h"
37 #include "srv_main.h"
39 /* server/scripting */
40 #ifdef HAVE_FCDB
41 #include "script_fcdb.h"
42 #endif /* HAVE_FCDB */
44 #include "fcdb.h"
46 /* If HAVE_FCDB is set, the freeciv database module is compiled. Else only
47 * some dummy functions are defiend. */
48 #ifdef HAVE_FCDB
50 enum fcdb_option_source {
51 AOS_DEFAULT, /* Internal default, currently not used */
52 AOS_FILE, /* Read from config file */
53 AOS_SET /* Set, currently not used */
56 struct fcdb_option {
57 enum fcdb_option_source source;
58 char *value;
61 #define SPECHASH_TAG fcdb_option
62 #define SPECHASH_KEY_TYPE char *
63 #define SPECHASH_DATA_TYPE struct fcdb_option *
64 #define SPECHASH_KEY_VAL genhash_str_val_func
65 #define SPECHASH_KEY_COMP genhash_str_comp_func
66 #define SPECHASH_KEY_COPY genhash_str_copy_func
67 #define SPECHASH_KEY_FREE genhash_str_free_func
68 #include "spechash.h"
69 #define fcdb_option_hash_data_iterate(phash, data) \
70 TYPED_HASH_DATA_ITERATE(struct fcdb_option *, phash, data)
71 #define fcdb_option_hash_data_iterate_end HASH_DATA_ITERATE_END
72 #define fcdb_option_hash_keys_iterate(phash, key) \
73 TYPED_HASH_KEYS_ITERATE(char *, phash, key)
74 #define fcdb_option_hash_keys_iterate_end HASH_KEYS_ITERATE_END
75 #define fcdb_option_hash_iterate(phash, key, data) \
76 TYPED_HASH_ITERATE(char *, struct fcdb_option *, phash, key, data)
77 #define fcdb_option_hash_iterate_end HASH_ITERATE_END
79 struct fcdb_option_hash *fcdb_config = NULL;
81 static bool fcdb_set_option(const char *key, const char *value,
82 enum fcdb_option_source source);
83 static bool fcdb_load_config(const char *filename);
86 /****************************************************************************
87 Set one fcdb option (or delete it if value==NULL).
88 Replaces any previous setting.
89 ****************************************************************************/
90 static bool fcdb_set_option(const char *key, const char *value,
91 enum fcdb_option_source source)
93 struct fcdb_option *oldopt = NULL;
94 bool removed;
96 if (value != NULL) {
97 struct fcdb_option *newopt = fc_malloc(sizeof(*newopt));
98 newopt->value = fc_strdup(value);
99 newopt->source = source;
100 removed = fcdb_option_hash_replace_full(fcdb_config, key, newopt,
101 NULL, &oldopt);
102 } else {
103 removed = fcdb_option_hash_remove_full(fcdb_config, key, NULL, &oldopt);
106 if (removed) {
107 /* Overwritten/removed an existing value */
108 fc_assert_ret_val(oldopt != NULL, FALSE);
109 FC_FREE(oldopt->value);
110 FC_FREE(oldopt);
113 return TRUE;
116 /****************************************************************************
117 Load fcdb configuration from file.
118 We deliberately don't search datadirs for filename, as we don't want this
119 overridden by modpacks etc.
120 ****************************************************************************/
121 static bool fcdb_load_config(const char *filename)
123 struct section_file *secfile;
125 fc_assert_ret_val(NULL != filename, FALSE);
127 if (!(secfile = secfile_load(filename, FALSE))) {
128 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
129 secfile_error());
130 return FALSE;
133 entry_list_iterate(section_entries(secfile_section_by_name(secfile,
134 "fcdb")),
135 pentry) {
136 if (entry_type(pentry) == ENTRY_STR) {
137 const char *value;
138 bool entry_str_get_success = entry_str_get(pentry, &value);
140 fc_assert(entry_str_get_success);
141 fcdb_set_option(entry_name(pentry), value, AOS_FILE);
142 } else {
143 log_error("Value for '%s' in '%s' is not of string type, ignoring",
144 entry_name(pentry), filename);
146 } entry_list_iterate_end;
148 /* FIXME: we could arrange to call secfile_check_unused() and have it
149 * complain about unused entries (e.g. those not in [fcdb]). */
150 secfile_destroy(secfile);
152 return TRUE;
155 /****************************************************************************
156 Initialize freeciv database system
157 ****************************************************************************/
158 bool fcdb_init(const char *conf_file)
160 fc_assert(fcdb_config == NULL);
161 fcdb_config = fcdb_option_hash_new();
163 if (conf_file && strcmp(conf_file, "-")) {
164 if (!fcdb_load_config(conf_file)) {
165 return FALSE;
167 } else {
168 log_debug("No fcdb config file.");
171 return script_fcdb_init(NULL);
174 /****************************************************************************
175 Return the selected fcdb config value.
176 ****************************************************************************/
177 const char *fcdb_option_get(const char *type)
179 struct fcdb_option *opt;
181 if (fcdb_option_hash_lookup(fcdb_config, type, &opt)) {
182 return opt->value;
183 } else {
184 return NULL;
188 /****************************************************************************
189 Free resources allocated by fcdb system.
190 ****************************************************************************/
191 void fcdb_free(void)
193 script_fcdb_free();
195 fcdb_option_hash_data_iterate(fcdb_config, popt) {
196 FC_FREE(popt->value);
197 FC_FREE(popt);
198 } fcdb_option_hash_data_iterate_end;
200 fcdb_option_hash_destroy(fcdb_config);
201 fcdb_config = NULL;
204 #else /* HAVE_FCDB */
206 /****************************************************************************
207 Dummy function - Initialize freeciv database system
208 ****************************************************************************/
209 bool fcdb_init(const char *conf_file)
211 return TRUE;
214 /****************************************************************************
215 Dummy function - Return the selected fcdb config value.
216 ****************************************************************************/
217 const char *fcdb_option_get(const char *type)
219 return NULL;
222 /****************************************************************************
223 Dummy function - Free resources allocated by fcdb system.
224 ****************************************************************************/
225 void fcdb_free(void)
227 return;
229 #endif /* HAVE_FCDB */