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
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
76
77char *fcdb_script = NULL;
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 = 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;
100 NULL, &oldopt);
101 } else {
103 }
104
105 if (removed) {
106 /* Overwritten/removed an existing value */
108 FC_FREE(oldopt->value);
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 struct stat buf;
125
126 fc_assert_ret_val(NULL != filename, FALSE);
127
128 /* does file exist? */
129 if (fc_stat(filename, &buf) != 0) {
130 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
132 return FALSE;
133 }
134
135 if (!(secfile = secfile_load(filename, FALSE))) {
136 log_error(_("Cannot load fcdb config file '%s':\n%s"), filename,
137 secfile_error());
138 return FALSE;
139 }
140
141 val = secfile_lookup_str_default(secfile, NULL, "meta.lua");
142 if (val != NULL) {
143 fcdb_script = fc_strdup(val);
144 }
145
147 "fcdb")),
148 pentry) {
150 const char *value;
151#ifndef FREECIV_NDEBUG
153#endif /* FREECIV_NDEBUG */
154 entry_str_get(pentry, &value);
155
157
159 } else {
160 log_error("Value for '%s' in '%s' is not of string type, ignoring",
161 entry_name(pentry), filename);
162 }
164
165 /* FIXME: We could arrange to call secfile_check_unused() and have it
166 * complain about unused entries (e.g. those not in [fcdb]). */
167 secfile_destroy(secfile);
168
169 return TRUE;
170}
171
172/************************************************************************/
175bool fcdb_init(const char *conf_file)
176{
178
180
181 if (conf_file && strcmp(conf_file, "-")) {
183 return FALSE;
184 }
185 } else {
186 log_debug("No fcdb config file.");
187 }
188
190}
191
192/************************************************************************/
195const char *fcdb_option_get(const char *type)
196{
197 struct fcdb_option *opt;
198
200 return opt->value;
201 } else {
202 return NULL;
203 }
204}
205
206/************************************************************************/
209void fcdb_free(void)
210{
212
214 FC_FREE(popt->value);
215 FC_FREE(popt);
217
220}
221
222#else /* HAVE_FCDB */
223
224/************************************************************************/
227bool fcdb_init(const char *conf_file)
228{
229 return TRUE;
230}
231
232/************************************************************************/
235const char *fcdb_option_get(const char *type)
236{
237 return NULL;
238}
239
240/************************************************************************/
243void fcdb_free(void)
244{
245}
246
247#endif /* HAVE_FCDB */
char * incite_cost
Definition comments.c:76
void fcdb_free(void)
Definition fcdb.c:243
bool fcdb_init(const char *conf_file)
Definition fcdb.c:227
const char * fcdb_option_get(const char *type)
Definition fcdb.c:235
#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:609
fc_errno fc_get_errno(void)
Definition support.c:592
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:574
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47