Freeciv-3.4
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
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 {
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 = nullptr;
76
77char *fcdb_script = nullptr;
78
79static bool fcdb_set_option(const char *key, const char *value,
81static bool fcdb_load_config(const char *filename);
82
83
84/************************************************************************/
88static bool fcdb_set_option(const char *key, const char *value,
90{
91 struct fcdb_option *oldopt = nullptr;
92 bool removed;
93
94 if (value != nullptr) {
95 struct fcdb_option *newopt = fc_malloc(sizeof(*newopt));
96
97 newopt->value = fc_strdup(value);
98 newopt->source = source;
100 nullptr, &oldopt);
101 } else {
102 removed = fcdb_option_hash_remove_full(fcdb_config, key, nullptr, &oldopt);
103 }
104
105 if (removed) {
106 /* Overwritten/removed an existing value */
107 fc_assert_ret_val(oldopt != nullptr, FALSE);
108
109 FC_FREE(oldopt->value);
111 }
112
113 return TRUE;
114}
115
116/************************************************************************/
121static bool fcdb_load_config(const char *filename)
122{
123 struct section_file *secfile;
124 const char *val;
125 struct stat buf;
126
127 fc_assert_ret_val(filename != nullptr, FALSE);
128
129 /* does file exist? */
130 if (fc_stat(filename, &buf) != 0) {
131 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
133 return FALSE;
134 }
135
136 if (!(secfile = secfile_load(filename, FALSE))) {
137 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
138 secfile_error());
139 return FALSE;
140 }
141
142 val = secfile_lookup_str_default(secfile, nullptr, "meta.lua");
143 if (val != nullptr) {
144 fcdb_script = fc_strdup(val);
145 }
146
148 "fcdb")),
149 pentry) {
151 const char *value;
152#ifndef FREECIV_NDEBUG
154#endif /* FREECIV_NDEBUG */
155 entry_str_get(pentry, &value);
156
158
160 } else {
161 log_error("Value for '%s' in '%s' is not of string type, ignoring",
162 entry_name(pentry), filename);
163 }
165
166 /* FIXME: We could arrange to call secfile_check_unused() and have it
167 * complain about unused entries (e.g. those not in [fcdb]). */
168 secfile_destroy(secfile);
169
170 return TRUE;
171}
172
173/************************************************************************/
176bool fcdb_init(const char *conf_file)
177{
178 fc_assert(fcdb_config == nullptr);
179
181
182 if (conf_file && strcmp(conf_file, "-")) {
184 return FALSE;
185 }
186 } else {
187 log_debug("No fcdb config file.");
188 }
189
191}
192
193/************************************************************************/
196const char *fcdb_option_get(const char *type)
197{
198 struct fcdb_option *opt;
199
201 return opt->value;
202 } else {
203 return nullptr;
204 }
205}
206
207/************************************************************************/
210void fcdb_free(void)
211{
213
215 FC_FREE(popt->value);
216 FC_FREE(popt);
218
220 fcdb_config = nullptr;
221}
222
223#else /* HAVE_FCDB */
224
225/************************************************************************/
228bool fcdb_init(const char *conf_file)
229{
230 return TRUE;
231}
232
233/************************************************************************/
236const char *fcdb_option_get(const char *type)
237{
238 return nullptr;
239}
240
241/************************************************************************/
244void fcdb_free(void)
245{
246}
247
248#endif /* HAVE_FCDB */
char * incite_cost
Definition comments.c:77
void fcdb_free(void)
Definition fcdb.c:244
bool fcdb_init(const char *conf_file)
Definition fcdb.c:228
const char * fcdb_option_get(const char *type)
Definition fcdb.c:236
#define _(String)
Definition fcintl.h:67
static GtkWidget * source
Definition gotodlg.c:58
GType type
Definition repodlgs.c:1313
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_debug(message,...)
Definition log.h:116
#define log_error(message,...)
Definition log.h:104
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
#define fc_malloc(sz)
Definition mem.h:34
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:51
const char * secfile_error(void)
void secfile_destroy(struct section_file *secfile)
const struct entry_list * section_entries(const struct section *psection)
const char * entry_name(const struct entry *pentry)
bool entry_str_get(const struct entry *pentry, const char **value)
struct section * secfile_section_by_name(const struct section_file *secfile, const char *name)
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
enum entry_type entry_type_get(const struct entry *pentry)
@ ENTRY_STR
#define entry_list_iterate_end
#define entry_list_iterate(entlist, pentry)
void script_fcdb_free(void)
bool script_fcdb_init(const char *fcdb_luafile)
const char * fc_strerror(fc_errno err)
Definition support.c:574
fc_errno fc_get_errno(void)
Definition support.c:557
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:539
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47