Freeciv-3.3
Loading...
Searching...
No Matches
section_file.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 <stdarg.h>
19
20/* utility */
21#include "mem.h"
22#include "registry.h"
23
24#include "section_file.h"
25
26#define MAX_LEN_ERRORBUF 1024
27
28static char error_buffer[MAX_LEN_ERRORBUF] = "\0";
29
30/* Debug function for every new entry. */
31#define DEBUG_ENTRIES(...) /* log_debug(__VA_ARGS__); */
32
33/**********************************************************************/
37const char *secfile_error(void)
38{
39 return error_buffer;
40}
41
42/**********************************************************************/
45void secfile_log(const struct section_file *secfile,
46 const struct section *psection,
47 const char *file, const char *function, int line,
48 const char *format, ...)
49{
51 va_list args;
52
53 va_start(args, format);
54 fc_vsnprintf(message, sizeof(message), format, args);
55 va_end(args);
56
58 "In %s() [%s:%d]: secfile '%s' in section '%s': %s",
59 function, file, line, secfile_name(secfile),
60 psection != nullptr ? section_name(psection)
61 : "nullptr", message);
62}
63
64/**********************************************************************/
67const char *section_name(const struct section *psection)
68{
69 return (psection != nullptr ? psection->name : nullptr);
70}
71
72/**********************************************************************/
76{
77 struct section_file *secfile = fc_malloc(sizeof(struct section_file));
78
79 secfile->name = nullptr;
80 secfile->num_entries = 0;
81 secfile->num_includes = 0;
82 secfile->num_long_comments = 0;
85 secfile->allow_digital_boolean = FALSE; /* Default */
86
87 secfile->hash.sections = section_hash_new();
88 /* Maybe allocated later. */
89 secfile->hash.entries = nullptr;
90
91 return secfile;
92}
93
94/**********************************************************************/
97void secfile_destroy(struct section_file *secfile)
98{
99 SECFILE_RETURN_IF_FAIL(secfile, nullptr, secfile != nullptr);
100
102 /* Mark it nullptr to be sure to don't try to make operations when
103 * deleting the entries. */
104 secfile->hash.sections = nullptr;
105 if (secfile->hash.entries != nullptr) {
107 /* Mark it nullptr to be sure to don't try to make operations when
108 * deleting the entries. */
109 secfile->hash.entries = nullptr;
110 }
111
113
114 if (secfile->name != nullptr) {
115 free(secfile->name);
116 }
117
118 free(secfile);
119}
120
121/**********************************************************************/
131
132/**********************************************************************/
135bool entry_from_token(struct section *psection, const char *name,
136 const char *tok)
137{
138 if ('*' == tok[0]) {
139 char buf[strlen(tok) + 1];
140
141 remove_escapes(tok + 1, FALSE, buf, sizeof(buf));
142 (void) section_entry_str_new(psection, name, buf, FALSE);
143 DEBUG_ENTRIES("entry %s '%s'", name, buf);
144 return TRUE;
145 }
146
147 if ('$' == tok[0] || '"' == tok[0]) {
148 char buf[strlen(tok) + 1];
149 bool escaped = ('"' == tok[0]);
150
151 remove_escapes(tok + 1, escaped, buf, sizeof(buf));
152 (void) section_entry_str_new(psection, name, buf, escaped);
153 DEBUG_ENTRIES("entry %s '%s'", name, buf);
154 return TRUE;
155 }
156
157 if (fc_isdigit(tok[0]) || (('-' == tok[0] || '+' == tok[0]) && fc_isdigit(tok[1]))) {
158 float fvalue;
159
160 if (str_to_float(tok, &fvalue)) {
162 DEBUG_ENTRIES("entry %s %d", name, fvalue);
163
164 return TRUE;
165 } else {
166 int ivalue;
167
168 if (str_to_int(tok, &ivalue)) {
170 DEBUG_ENTRIES("entry %s %d", name, ivalue);
171
172 return TRUE;
173 }
174 }
175 }
176
177 if (0 == fc_strncasecmp(tok, "FALSE", 5)
178 || 0 == fc_strncasecmp(tok, "TRUE", 4)) {
179 bool value = (0 == fc_strncasecmp(tok, "TRUE", 4));
180
181 (void) section_entry_bool_new(psection, name, value);
182 DEBUG_ENTRIES("entry %s %s", name, value ? "TRUE" : "FALSE");
183 return TRUE;
184 }
185
186 return FALSE;
187}
char * incite_cost
Definition comments.c:76
const char * name
Definition inputfile.c:127
#define fc_malloc(sz)
Definition mem.h:34
void section_destroy(struct section *psection)
struct entry * section_entry_int_new(struct section *psection, const char *name, int value)
struct entry * section_entry_str_new(struct section *psection, const char *name, const char *value, bool escaped)
struct entry * section_entry_float_new(struct section *psection, const char *name, float value)
struct entry * section_entry_bool_new(struct section *psection, const char *name, bool value)
const char * secfile_name(const struct section_file *secfile)
void secfile_log(const struct section_file *secfile, const struct section *psection, const char *file, const char *function, int line, const char *format,...)
static char error_buffer[MAX_LEN_ERRORBUF]
#define MAX_LEN_ERRORBUF
void secfile_allow_digital_boolean(struct section_file *secfile, bool allow_digital_boolean)
struct section_file * secfile_new(bool allow_duplicates)
const char * secfile_error(void)
bool entry_from_token(struct section *psection, const char *name, const char *tok)
#define DEBUG_ENTRIES(...)
const char * section_name(const struct section *psection)
void secfile_destroy(struct section_file *secfile)
#define SECFILE_RETURN_IF_FAIL(secfile, psection, condition)
bool str_to_int(const char *str, int *pint)
Definition shared.c:515
bool str_to_float(const char *str, float *pfloat)
Definition shared.c:579
struct entry_hash * entries
struct section_file::@8 hash
struct section_list * sections
bool allow_digital_boolean
bool allow_duplicates
unsigned int num_includes
unsigned int num_long_comments
size_t num_entries
char * name
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:886
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:330
bool fc_isdigit(char c)
Definition support.c:1218
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:235
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47