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
37
38static int mpdb_query(sqlite3 *handle, const char *query);
39
40/**********************************************************************/
43void 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
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
96 ver);
97 } else {
98 all_read = TRUE;
99 }
100 }
101
102 secfile_destroy(file);
103 }
104}
105
106/**********************************************************************/
109static int mpdb_query(sqlite3 *handle, const char *query)
110{
111 int ret;
113
115
116 if (ret == SQLITE_OK) {
118 }
119
120 if (ret == SQLITE_DONE) {
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/**********************************************************************/
134void 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';
148 log_error(_("Can't create directory \"%s\" for modpack database."), local_name);
149 return;
150 }
151
152 if (scenario_db) {
154 } else {
156 }
157
159 NULL);
160
161 if (ret == SQLITE_OK) {
163 "create table meta (version INTEGER default " MPDB_FORMAT_VERSION ");");
164 }
165
166 if (ret == SQLITE_OK) {
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/**********************************************************************/
181void open_mpdb(const char *filename, bool scenario_db)
182{
183 sqlite3 **handle;
184 int ret;
185
186 if (scenario_db) {
188 } else {
190 }
191
193
194 if (ret != SQLITE_OK) {
195 log_error(_("Opening \"%s\" failed: %s"), filename, sqlite3_errstr(ret));
196 }
197}
198
199/**********************************************************************/
209
210/**********************************************************************/
214 const char *version)
215{
216 sqlite3 **handle;
217 int ret;
218 char qbuf[2048];
219
220 if (type == MPT_SCENARIO) {
222 } else {
224 }
225
226 sqlite3_snprintf(sizeof(qbuf), qbuf, "select * from modpacks where name is '%q';",
227 name);
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);
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);
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/**********************************************************************/
253const char *mpdb_installed_version(const char *name,
254 enum modpack_type type)
255{
256 sqlite3 **handle;
257 int ret;
258 char qbuf[2048];
260
261 if (type == MPT_SCENARIO) {
263 } else {
265 }
266
267 sqlite3_snprintf(sizeof(qbuf), qbuf,
268 "select * from modpacks where name is '%q';",
269 name);
271
272 if (ret == SQLITE_OK) {
274 }
275
276 if (ret == SQLITE_DONE) {
278 }
279
280 if (ret == SQLITE_ROW) {
281 return (const char *)sqlite3_column_text(stmt, 2);
282 }
283
284 return NULL;
285}
#define str
Definition astring.c:76
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
char * incite_cost
Definition comments.c:76
#define _(String)
Definition fcintl.h:67
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define log_debug(message,...)
Definition log.h:116
#define log_error(message,...)
Definition log.h:104
#define fc_malloc(sz)
Definition mem.h:34
#define MPDB_FORMAT_VERSION
Definition mpdb.c:33
const char * mpdb_installed_version(const char *name, enum modpack_type type)
Definition mpdb.c:253
static sqlite3 * main_handle
Definition mpdb.c:35
static sqlite3 * scenario_handle
Definition mpdb.c:36
#define MPDB_CAPSTR
Definition mpdb.c:31
void load_install_info_list(const char *filename)
Definition mpdb.c:43
void create_mpdb(const char *filename, bool scenario_db)
Definition mpdb.c:134
void close_mpdbs(void)
Definition mpdb.c:202
void open_mpdb(const char *filename, bool scenario_db)
Definition mpdb.c:181
bool mpdb_update_modpack(const char *name, enum modpack_type type, const char *version)
Definition mpdb.c:213
static int mpdb_query(sqlite3 *handle, const char *query)
Definition mpdb.c:109
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:51
void secfile_destroy(struct section_file *secfile)
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
bool make_dir(const char *pathname, int mode)
Definition shared.c:1779
#define DIR_SEPARATOR_CHAR
Definition shared.h:128
#define DIRMODE_DEFAULT
Definition shared.h:258
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47