File: mpdb.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (130 lines) | stat: -rw-r--r-- 3,672 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "capability.h"
#include "fcintl.h"
#include "mem.h"
#include "registry.h"

/* modinst */
#include "download.h"

#include "mpdb.h"

#define MPDB_CAPSTR "+mpdb"

/**************************************************************************
  Construct install info list from file.
**************************************************************************/
void load_install_info_list(const char *filename,
                            struct install_info_list *list)
{
  struct section_file *file;
  const char *caps;

  file = secfile_load(filename, FALSE);

  if (file == NULL) {
    /* This happens in first run - or actually all runs until something is
     * installed. Previous run has not saved database. */
    log_debug("No install info file");

    return;
  }

  caps = secfile_lookup_str(file, "info.options");

  if (caps == NULL) {
    log_error("MPDB %s missing capability information", filename);
    secfile_destroy(file);
    return;
  }

  if (!has_capabilities(MPDB_CAPSTR, caps)) {
    log_error("Incompatible mpdb file %s:", filename);
    log_error("  file options:      %s", caps);
    log_error("  supported options: %s", MPDB_CAPSTR);

    secfile_destroy(file);

    return;
  }

  if (file != NULL) {
    bool all_read = FALSE;
    int i;

    for (i = 0 ; !all_read ; i++) {
      const char *str;
      char buf[80];

      fc_snprintf(buf, sizeof(buf), "modpacks.mp%d", i);

      str = secfile_lookup_str_default(file, NULL, "%s.name", buf);

      if (str != NULL) {
        struct install_info *ii;

        ii = fc_malloc(sizeof(*ii));

        sz_strlcpy(ii->name, str);
        str = secfile_lookup_str(file, "%s.type", buf);
        ii->type = modpack_type_by_name(str, fc_strcasecmp);
        str = secfile_lookup_str(file, "%s.version", buf);
        sz_strlcpy(ii->version, str);

        install_info_list_append(list, ii);
      } else {
        all_read = TRUE;
      }
    }

    secfile_destroy(file);
  }
}

/**************************************************************************
  Save install info list to file.
**************************************************************************/
void save_install_info_list(const char *filename,
                            struct install_info_list *list)
{
  int i = 0;
  struct section_file *file = secfile_new(TRUE);

  secfile_insert_str(file, MPDB_CAPSTR, "info.options");

  install_info_list_iterate(list, ii) {
    char buf[80];

    fc_snprintf(buf, sizeof(buf), "modpacks.mp%d", i);

    secfile_insert_str(file, ii->name, "%s.name", buf);
    secfile_insert_str(file, modpack_type_name(ii->type),
                       "%s.type", buf);
    secfile_insert_str(file, ii->version, "%s.version", buf);

    i++;
  } install_info_list_iterate_end;

  if (!secfile_save(file, filename, 0, FZ_PLAIN)) {
    log_error("Saving of install info to file %s failed.", filename);
  }

  secfile_destroy(file);
}