Freeciv-3.3: /fast/freeciv/nightly/main/server/fcdb.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
fcdb.c
Go to the documentation of this file.
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.
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#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22/* utility */
23#include "fcintl.h"
24#include "log.h"
25#include "md5.h"
26#include "shared.h"
27#include "support.h"
28
29/* common */
30#include "connection.h"
31#include "packets.h"
32
33/* server */
34#include "connecthand.h"
35#include "notify.h"
36#include "sernet.h"
37#include "srv_main.h"
38
39/* server/scripting */
40#ifdef HAVE_FCDB
41#include "script_fcdb.h"
42#endif /* HAVE_FCDB */
43
44#include "fcdb.h"
45
46/* If HAVE_FCDB is set, the freeciv database module is compiled. Else only
47 * some dummy functions are defiend. */
48#ifdef HAVE_FCDB
49
50enum 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 */
54};
55
56struct fcdb_option {
57 enum fcdb_option_source source;
58 char *value;
59};
60
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
74
75struct fcdb_option_hash *fcdb_config = NULL;
76
77char *fcdb_script = NULL;
78
79static bool fcdb_set_option(const char *key, const char *value,
80 enum fcdb_option_source source);
81static bool fcdb_load_config(const char *filename);
82
83
84/************************************************************************/
88static bool fcdb_set_option(const char *key, const char *value,
89 enum fcdb_option_source source)
90{
91 struct fcdb_option *oldopt = NULL;
92 bool removed;
93
94 if (value != NULL) {
95 struct fcdb_option *newopt = fc_malloc(sizeof(*newopt));
96
97 newopt->value = fc_strdup(value);
98 newopt->source = source;
99 removed = fcdb_option_hash_replace_full(fcdb_config, key, newopt,
100 NULL, &oldopt);
101 } else {
102 removed = fcdb_option_hash_remove_full(fcdb_config, key, NULL, &oldopt);
103 }
104
105 if (removed) {
106 /* Overwritten/removed an existing value */
107 fc_assert_ret_val(oldopt != NULL, FALSE);
108 FC_FREE(oldopt->value);
109 FC_FREE(oldopt);
110 }
111
112 return TRUE;
113}
114
115/************************************************************************/
120static bool fcdb_load_config(const char *filename)
121{
122 struct section_file *secfile;
123 const char *val;
124
125 fc_assert_ret_val(NULL != filename, FALSE);
126
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;
131 }
132
133 val = secfile_lookup_str_default(secfile, NULL, "meta.lua");
134 if (val != NULL) {
135 fcdb_script = fc_strdup(val);
136 }
137
138 entry_list_iterate(section_entries(secfile_section_by_name(secfile,
139 "fcdb")),
140 pentry) {
141 if (entry_type_get(pentry) == ENTRY_STR) {
142 const char *value;
143#ifndef FREECIV_NDEBUG
144 bool entry_str_get_success =
145#endif /* FREECIV_NDEBUG */
146 entry_str_get(pentry, &value);
147
148 fc_assert(entry_str_get_success);
149
150 fcdb_set_option(entry_name(pentry), value, AOS_FILE);
151 } else {
152 log_error("Value for '%s' in '%s' is not of string type, ignoring",
153 entry_name(pentry), filename);
154 }
155 } entry_list_iterate_end;
156
157 /* FIXME: we could arrange to call secfile_check_unused() and have it
158 * complain about unused entries (e.g. those not in [fcdb]). */
159 secfile_destroy(secfile);
160
161 return TRUE;
162}
163
164/************************************************************************/
167bool fcdb_init(const char *conf_file)
168{
169 fc_assert(fcdb_config == NULL);
170 fcdb_config = fcdb_option_hash_new();
171
172 if (conf_file && strcmp(conf_file, "-")) {
173 if (!fcdb_load_config(conf_file)) {
174 return FALSE;
175 }
176 } else {
177 log_debug("No fcdb config file.");
178 }
179
180 return script_fcdb_init(fcdb_script);
181}
182
183/************************************************************************/
186const char *fcdb_option_get(const char *type)
187{
188 struct fcdb_option *opt;
189
190 if (fcdb_option_hash_lookup(fcdb_config, type, &opt)) {
191 return opt->value;
192 } else {
193 return NULL;
194 }
195}
196
197/************************************************************************/
200void fcdb_free(void)
201{
202 script_fcdb_free();
203
204 fcdb_option_hash_data_iterate(fcdb_config, popt) {
205 FC_FREE(popt->value);
206 FC_FREE(popt);
207 } fcdb_option_hash_data_iterate_end;
208
209 fcdb_option_hash_destroy(fcdb_config);
210 fcdb_config = NULL;
211}
212
213#else /* HAVE_FCDB */
214
215/************************************************************************/
218 bool fcdb_init(const char *conf_file)
219{
220 return TRUE;
221}
222
223/************************************************************************/
226 const char *fcdb_option_get(const char *type)
227{
228 return NULL;
229}
230
231/************************************************************************/
234 void fcdb_free(void)
235{
236 return;
237}
238#endif /* HAVE_FCDB */
incite_cost
char * incite_cost
Definition comments.c:74
connecthand.h
connection.h
fcdb_free
void fcdb_free(void)
Definition fcdb.c:234
fcdb_init
bool fcdb_init(const char *conf_file)
Definition fcdb.c:218
fcdb_option_get
const char * fcdb_option_get(const char *type)
Definition fcdb.c:226
fcdb.h
fcintl.h
_
#define _(String)
Definition fcintl.h:67
source
static GtkWidget * source
Definition gotodlg.c:58
type
GType type
Definition repodlgs.c:1313
log.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_debug
#define log_debug(message,...)
Definition log.h:115
log_error
#define log_error(message,...)
Definition log.h:103
md5.h
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
notify.h
packets.h
secfile_load
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:51
secfile_error
const char * secfile_error(void)
Definition section_file.c:36
secfile_destroy
void secfile_destroy(struct section_file *secfile)
Definition section_file.c:95
section_entries
const struct entry_list * section_entries(const struct section *psection)
Definition registry_ini.c:3079
entry_name
const char * entry_name(const struct entry *pentry)
Definition registry_ini.c:3363
entry_str_get
bool entry_str_get(const struct entry *pentry, const char **value)
Definition registry_ini.c:3561
secfile_section_by_name
struct section * secfile_section_by_name(const struct section_file *secfile, const char *name)
Definition registry_ini.c:2829
secfile_lookup_str_default
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
Definition registry_ini.c:2210
entry_type_get
enum entry_type entry_type_get(const struct entry *pentry)
Definition registry_ini.c:3345
ENTRY_STR
@ ENTRY_STR
Definition registry_ini.h:635
entry_list_iterate_end
#define entry_list_iterate_end
Definition registry_ini.h:57
entry_list_iterate
#define entry_list_iterate(entlist, pentry)
Definition registry_ini.h:55
script_fcdb_free
void script_fcdb_free(void)
Definition script_fcdb.c:377
script_fcdb_init
bool script_fcdb_init(const char *fcdb_luafile)
Definition script_fcdb.c:239
script_fcdb.h
sernet.h
shared.h
spechash.h
srv_main.h
section_file
Definition section_file.h:36
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47