README.graphics typofix "form" -> "from"
[freeciv.git] / tools / mpdb.c
blobf9ba9f39d40adb385527188d6f2288f24ed9deb0
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "capability.h"
20 #include "fcintl.h"
21 #include "mem.h"
22 #include "registry.h"
24 /* modinst */
25 #include "download.h"
27 #include "mpdb.h"
29 #define MPDB_CAPSTR "+mpdb"
31 /**************************************************************************
32 Construct install info list from file.
33 **************************************************************************/
34 void load_install_info_list(const char *filename,
35 struct install_info_list *list)
37 struct section_file *file;
38 const char *caps;
40 file = secfile_load(filename, FALSE);
42 if (file == NULL) {
43 /* This happens in first run - or actually all runs until something is
44 * installed. Previous run has not saved database. */
45 log_debug("No install info file");
47 return;
50 caps = secfile_lookup_str(file, "info.options");
52 if (caps == NULL) {
53 log_error("MPDB %s missing capability information", filename);
54 secfile_destroy(file);
55 return;
58 if (!has_capabilities(MPDB_CAPSTR, caps)) {
59 log_error("Incompatible mpdb file %s:", filename);
60 log_error(" file options: %s", caps);
61 log_error(" supported options: %s", MPDB_CAPSTR);
63 secfile_destroy(file);
65 return;
68 if (file != NULL) {
69 bool all_read = FALSE;
70 int i;
72 for (i = 0 ; !all_read ; i++) {
73 const char *str;
74 char buf[80];
76 fc_snprintf(buf, sizeof(buf), "modpacks.mp%d", i);
78 str = secfile_lookup_str_default(file, NULL, "%s.name", buf);
80 if (str != NULL) {
81 struct install_info *ii;
83 ii = fc_malloc(sizeof(*ii));
85 strncpy(ii->name, str, sizeof(ii->name));
86 str = secfile_lookup_str(file, "%s.type", buf);
87 ii->type = modpack_type_by_name(str, fc_strcasecmp);
88 str = secfile_lookup_str(file, "%s.version", buf);
89 strncpy(ii->version, str, sizeof(ii->version));
91 install_info_list_append(list, ii);
92 } else {
93 all_read = TRUE;
97 secfile_destroy(file);
101 /**************************************************************************
102 Save install info list to file.
103 **************************************************************************/
104 void save_install_info_list(const char *filename,
105 struct install_info_list *list)
107 int i = 0;
108 struct section_file *file = secfile_new(TRUE);
110 secfile_insert_str(file, MPDB_CAPSTR, "info.options");
112 install_info_list_iterate(list, ii) {
113 char buf[80];
115 fc_snprintf(buf, sizeof(buf), "modpacks.mp%d", i);
117 secfile_insert_str(file, ii->name, "%s.name", buf);
118 secfile_insert_str(file, modpack_type_name(ii->type),
119 "%s.type", buf);
120 secfile_insert_str(file, ii->version, "%s.version", buf);
122 i++;
123 } install_info_list_iterate_end;
125 if (!secfile_save(file, filename, 0, FZ_PLAIN)) {
126 log_error("Saving of install info to file %s failed.", filename);
129 secfile_destroy(file);