Freeciv-3.3: /fast/freeciv/nightly/main/tools/fcmp/mpdb.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
mpdb.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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 <sqlite3.h>
19
20/* utility */
21#include "capability.h"
22#include "fcintl.h"
23#include "mem.h"
24#include "registry.h"
25
26/* modinst */
27#include "download.h"
28
29#include "mpdb.h"
30
31#define MPDB_CAPSTR "+mpdb"
32
33#define MPDB_FORMAT_VERSION "1"
34
35 static sqlite3 *main_handle = NULL;
36 static sqlite3 *scenario_handle = NULL;
37
38static int mpdb_query(sqlite3 *handle, const char *query);
39
40/**********************************************************************/
43 void load_install_info_list(const char *filename)
44{
45 struct section_file *file;
46 const char *caps;
47
48 file = secfile_load(filename, FALSE);
49
50 if (file == NULL) {
51 /* This happens in first run - or actually all runs until something is
52 * installed. Previous run has not saved database. */
53 log_debug("No install info file");
54
55 return;
56 }
57
58 caps = secfile_lookup_str(file, "info.options");
59
60 if (caps == NULL) {
61 log_error("MPDB %s missing capability information", filename);
62 secfile_destroy(file);
63 return;
64 }
65
66 if (!has_capabilities(MPDB_CAPSTR, caps)) {
67 log_error("Incompatible mpdb file %s:", filename);
68 log_error(" file options: %s", caps);
69 log_error(" supported options: %s", MPDB_CAPSTR);
70
71 secfile_destroy(file);
72
73 return;
74 }
75
76 if (file != NULL) {
77 bool all_read = FALSE;
78 int i;
79
80 for (i = 0 ; !all_read ; i++) {
81 const char *str;
82 char buf[80];
83
84 fc_snprintf(buf, sizeof(buf), "modpacks.mp%d", i);
85
86 str = secfile_lookup_str_default(file, NULL, "%s.name", buf);
87
88 if (str != NULL) {
89 const char *type;
90 const char *ver;
91
92 type = secfile_lookup_str(file, "%s.type", buf);
93 ver = secfile_lookup_str(file, "%s.version", buf);
94
95 mpdb_update_modpack(str, modpack_type_by_name(type, fc_strcasecmp),
96 ver);
97 } else {
98 all_read = TRUE;
99 }
100 }
101
102 secfile_destroy(file);
103 }
104}
105
106/**********************************************************************/
109 static int mpdb_query(sqlite3 *handle, const char *query)
110{
111 int ret;
112 sqlite3_stmt *stmt;
113
114 ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
115
116 if (ret == SQLITE_OK) {
117 ret = sqlite3_step(stmt);
118 }
119
120 if (ret == SQLITE_DONE) {
121 ret = sqlite3_finalize(stmt);
122 }
123
124 if (ret != SQLITE_OK && ret != SQLITE_ROW) {
125 log_error("Query \"%s\" failed. (%d)", query, ret);
126 }
127
128 return ret;
129}
130
131/**********************************************************************/
134 void create_mpdb(const char *filename, bool scenario_db)
135{
136 sqlite3 **handle;
137 int ret;
138 int llen = strlen(filename) + 1;
139 char *local_name = fc_malloc(llen);
140 int i;
141
142 strncpy(local_name, filename, llen);
143 for (i = llen - 1 ; local_name[i] != DIR_SEPARATOR_CHAR ; i--) {
144 /* Nothing */
145 }
146 local_name[i] = '\0';
147 if (!make_dir(local_name, DIRMODE_DEFAULT)) {
148 log_error(_("Can't create directory \"%s\" for modpack database."), local_name);
149 return;
150 }
151
152 if (scenario_db) {
153 handle = &scenario_handle;
154 } else {
155 handle = &main_handle;
156 }
157
158 ret = sqlite3_open_v2(filename, handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
159 NULL);
160
161 if (ret == SQLITE_OK) {
162 ret = mpdb_query(*handle,
163 "create table meta (version INTEGER default " MPDB_FORMAT_VERSION ");");
164 }
165
166 if (ret == SQLITE_OK) {
167 ret = mpdb_query(*handle,
168 "create table modpacks (name VARCHAR(60) NOT NULL, type VARCHAR(32), version VARCHAR(32) NOT NULL);");
169 }
170
171 if (ret == SQLITE_OK) {
172 log_debug("Created %s", filename);
173 } else {
174 log_error(_("Creating \"%s\" failed: %s"), filename, sqlite3_errstr(ret));
175 }
176}
177
178/**********************************************************************/
181 void open_mpdb(const char *filename, bool scenario_db)
182{
183 sqlite3 **handle;
184 int ret;
185
186 if (scenario_db) {
187 handle = &scenario_handle;
188 } else {
189 handle = &main_handle;
190 }
191
192 ret = sqlite3_open_v2(filename, handle, SQLITE_OPEN_READWRITE, NULL);
193
194 if (ret != SQLITE_OK) {
195 log_error(_("Opening \"%s\" failed: %s"), filename, sqlite3_errstr(ret));
196 }
197}
198
199/**********************************************************************/
202 void close_mpdbs(void)
203{
204 sqlite3_close(main_handle);
205 main_handle = NULL;
206 sqlite3_close(scenario_handle);
207 scenario_handle = NULL;
208}
209
210/**********************************************************************/
213 bool mpdb_update_modpack(const char *name, enum modpack_type type,
214 const char *version)
215{
216 sqlite3 **handle;
217 int ret;
218 char qbuf[2048];
219
220 if (type == MPT_SCENARIO) {
221 handle = &scenario_handle;
222 } else {
223 handle = &main_handle;
224 }
225
226 sqlite3_snprintf(sizeof(qbuf), qbuf, "select * from modpacks where name is '%q';",
227 name);
228 ret = mpdb_query(*handle, qbuf);
229
230 if (ret == SQLITE_ROW) {
231 sqlite3_snprintf(sizeof(qbuf), qbuf,
232 "update modpacks set type = '%q', version = '%q' where name is '%q';",
233 modpack_type_name(type), version, name);
234 ret = mpdb_query(*handle, qbuf);
235 } else {
236 /* Completely new modpack */
237 sqlite3_snprintf(sizeof(qbuf), qbuf,
238 "insert into modpacks values ('%q', '%q', '%q');",
239 name, modpack_type_name(type), version);
240 ret = mpdb_query(*handle, qbuf);
241 }
242
243 if (ret != SQLITE_OK) {
244 log_error(_("Failed to insert modpack '%s' information"), name);
245 }
246
247 return ret != SQLITE_OK;
248}
249
250/**********************************************************************/
253 const char *mpdb_installed_version(const char *name,
254 enum modpack_type type)
255{
256 sqlite3 **handle;
257 int ret;
258 char qbuf[2048];
259 sqlite3_stmt *stmt;
260
261 if (type == MPT_SCENARIO) {
262 handle = &scenario_handle;
263 } else {
264 handle = &main_handle;
265 }
266
267 sqlite3_snprintf(sizeof(qbuf), qbuf,
268 "select * from modpacks where name is '%q';",
269 name);
270 ret = sqlite3_prepare_v2(*handle, qbuf, -1, &stmt, NULL);
271
272 if (ret == SQLITE_OK) {
273 ret = sqlite3_step(stmt);
274 }
275
276 if (ret == SQLITE_DONE) {
277 ret = sqlite3_finalize(stmt);
278 }
279
280 if (ret == SQLITE_ROW) {
281 return (const char *)sqlite3_column_text(stmt, 2);
282 }
283
284 return NULL;
285}
str
#define str
Definition astring.c:76
has_capabilities
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
capability.h
incite_cost
char * incite_cost
Definition comments.c:74
download.h
fcintl.h
_
#define _(String)
Definition fcintl.h:67
type
GType type
Definition repodlgs.c:1313
name
const char * name
Definition inputfile.c:127
log_debug
#define log_debug(message,...)
Definition log.h:115
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
MPDB_FORMAT_VERSION
#define MPDB_FORMAT_VERSION
Definition mpdb.c:33
mpdb_installed_version
const char * mpdb_installed_version(const char *name, enum modpack_type type)
Definition mpdb.c:253
main_handle
static sqlite3 * main_handle
Definition mpdb.c:35
scenario_handle
static sqlite3 * scenario_handle
Definition mpdb.c:36
MPDB_CAPSTR
#define MPDB_CAPSTR
Definition mpdb.c:31
load_install_info_list
void load_install_info_list(const char *filename)
Definition mpdb.c:43
create_mpdb
void create_mpdb(const char *filename, bool scenario_db)
Definition mpdb.c:134
close_mpdbs
void close_mpdbs(void)
Definition mpdb.c:202
open_mpdb
void open_mpdb(const char *filename, bool scenario_db)
Definition mpdb.c:181
mpdb_update_modpack
bool mpdb_update_modpack(const char *name, enum modpack_type type, const char *version)
Definition mpdb.c:213
mpdb_query
static int mpdb_query(sqlite3 *handle, const char *query)
Definition mpdb.c:109
mpdb.h
secfile_load
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:51
registry.h
secfile_destroy
void secfile_destroy(struct section_file *secfile)
Definition section_file.c:95
secfile_lookup_str
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
Definition registry_ini.c:2180
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
make_dir
bool make_dir(const char *pathname, int mode)
Definition shared.c:1776
DIR_SEPARATOR_CHAR
#define DIR_SEPARATOR_CHAR
Definition shared.h:128
DIRMODE_DEFAULT
#define DIRMODE_DEFAULT
Definition shared.h:258
section_file
Definition section_file.h:36
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47