Do not build gtk3.22-client by default
[freeciv.git] / server / fcdb.c
blob4956e1a89f18e568b34f8e1c382cce56a1f0d1e4
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_ASTR_KEY_TYPE
63 #define SPECHASH_IDATA_TYPE struct fcdb_option *
64 #include "spechash.h"
65 #define fcdb_option_hash_data_iterate(phash, data) \
66 TYPED_HASH_DATA_ITERATE(struct fcdb_option *, phash, data)
67 #define fcdb_option_hash_data_iterate_end HASH_DATA_ITERATE_END
68 #define fcdb_option_hash_keys_iterate(phash, key) \
69 TYPED_HASH_KEYS_ITERATE(char *, phash, key)
70 #define fcdb_option_hash_keys_iterate_end HASH_KEYS_ITERATE_END
71 #define fcdb_option_hash_iterate(phash, key, data) \
72 TYPED_HASH_ITERATE(char *, struct fcdb_option *, phash, key, data)
73 #define fcdb_option_hash_iterate_end HASH_ITERATE_END
75 struct fcdb_option_hash *fcdb_config = NULL;
77 static bool fcdb_set_option(const char *key, const char *value,
78 enum fcdb_option_source source);
79 static bool fcdb_load_config(const char *filename);
82 /****************************************************************************
83 Set one fcdb option (or delete it if value==NULL).
84 Replaces any previous setting.
85 ****************************************************************************/
86 static bool fcdb_set_option(const char *key, const char *value,
87 enum fcdb_option_source source)
89 struct fcdb_option *oldopt = NULL;
90 bool removed;
92 if (value != NULL) {
93 struct fcdb_option *newopt = fc_malloc(sizeof(*newopt));
94 newopt->value = fc_strdup(value);
95 newopt->source = source;
96 removed = fcdb_option_hash_replace_full(fcdb_config, key, newopt,
97 NULL, &oldopt);
98 } else {
99 removed = fcdb_option_hash_remove_full(fcdb_config, key, NULL, &oldopt);
102 if (removed) {
103 /* Overwritten/removed an existing value */
104 fc_assert_ret_val(oldopt != NULL, FALSE);
105 FC_FREE(oldopt->value);
106 FC_FREE(oldopt);
109 return TRUE;
112 /****************************************************************************
113 Load fcdb configuration from file.
114 We deliberately don't search datadirs for filename, as we don't want this
115 overridden by modpacks etc.
116 ****************************************************************************/
117 static bool fcdb_load_config(const char *filename)
119 struct section_file *secfile;
121 fc_assert_ret_val(NULL != filename, FALSE);
123 if (!(secfile = secfile_load(filename, FALSE))) {
124 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
125 secfile_error());
126 return FALSE;
129 entry_list_iterate(section_entries(secfile_section_by_name(secfile,
130 "fcdb")),
131 pentry) {
132 if (entry_type(pentry) == ENTRY_STR) {
133 const char *value;
134 #ifndef FREECIV_NDEBUG
135 bool entry_str_get_success =
136 #endif
137 entry_str_get(pentry, &value);
139 fc_assert(entry_str_get_success);
140 fcdb_set_option(entry_name(pentry), value, AOS_FILE);
141 } else {
142 log_error("Value for '%s' in '%s' is not of string type, ignoring",
143 entry_name(pentry), filename);
145 } entry_list_iterate_end;
147 /* FIXME: we could arrange to call secfile_check_unused() and have it
148 * complain about unused entries (e.g. those not in [fcdb]). */
149 secfile_destroy(secfile);
151 return TRUE;
154 /****************************************************************************
155 Initialize freeciv database system
156 ****************************************************************************/
157 bool fcdb_init(const char *conf_file)
159 fc_assert(fcdb_config == NULL);
160 fcdb_config = fcdb_option_hash_new();
162 if (conf_file && strcmp(conf_file, "-")) {
163 if (!fcdb_load_config(conf_file)) {
164 return FALSE;
166 } else {
167 log_debug("No fcdb config file.");
170 return script_fcdb_init(NULL);
173 /****************************************************************************
174 Return the selected fcdb config value.
175 ****************************************************************************/
176 const char *fcdb_option_get(const char *type)
178 struct fcdb_option *opt;
180 if (fcdb_option_hash_lookup(fcdb_config, type, &opt)) {
181 return opt->value;
182 } else {
183 return NULL;
187 /****************************************************************************
188 Free resources allocated by fcdb system.
189 ****************************************************************************/
190 void fcdb_free(void)
192 script_fcdb_free();
194 fcdb_option_hash_data_iterate(fcdb_config, popt) {
195 FC_FREE(popt->value);
196 FC_FREE(popt);
197 } fcdb_option_hash_data_iterate_end;
199 fcdb_option_hash_destroy(fcdb_config);
200 fcdb_config = NULL;
203 #else /* HAVE_FCDB */
205 /****************************************************************************
206 Dummy function - Initialize freeciv database system
207 ****************************************************************************/
208 bool fcdb_init(const char *conf_file)
210 return TRUE;
213 /****************************************************************************
214 Dummy function - Return the selected fcdb config value.
215 ****************************************************************************/
216 const char *fcdb_option_get(const char *type)
218 return NULL;
221 /****************************************************************************
222 Dummy function - Free resources allocated by fcdb system.
223 ****************************************************************************/
224 void fcdb_free(void)
226 return;
228 #endif /* HAVE_FCDB */