Freeciv-3.2
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/**********************************************************************/
36const char *secfile_error(void)
37{
38 return error_buffer;
39}
40
41/**********************************************************************/
44void secfile_log(const struct section_file *secfile,
45 const struct section *psection,
46 const char *file, const char *function, int line,
47 const char *format, ...)
48{
50 va_list args;
51
52 va_start(args, format);
53 fc_vsnprintf(message, sizeof(message), format, args);
54 va_end(args);
55
57 "In %s() [%s:%d]: secfile '%s' in section '%s': %s",
58 function, file, line, secfile_name(secfile),
59 psection != NULL ? section_name(psection) : "NULL", message);
60}
61
62/**********************************************************************/
65const char *section_name(const struct section *psection)
66{
67 return (NULL != psection ? psection->name : NULL);
68}
69
70/**********************************************************************/
74{
75 struct section_file *secfile = fc_malloc(sizeof(struct section_file));
76
77 secfile->name = NULL;
78 secfile->num_entries = 0;
79 secfile->num_includes = 0;
80 secfile->num_long_comments = 0;
83 secfile->allow_digital_boolean = FALSE; /* Default */
84
85 secfile->hash.sections = section_hash_new();
86 /* Maybe allocated later. */
87 secfile->hash.entries = NULL;
88
89 return secfile;
90}
91
92/**********************************************************************/
95void secfile_destroy(struct section_file *secfile)
96{
97 SECFILE_RETURN_IF_FAIL(secfile, NULL, secfile != NULL);
98
100 /* Mark it NULL to be sure to don't try to make operations when
101 * deleting the entries. */
102 secfile->hash.sections = NULL;
103 if (NULL != secfile->hash.entries) {
105 /* Mark it NULL to be sure to don't try to make operations when
106 * deleting the entries. */
107 secfile->hash.entries = NULL;
108 }
109
111
112 if (NULL != secfile->name) {
113 free(secfile->name);
114 }
115
116 free(secfile);
117}
118
119/**********************************************************************/
130
131/**********************************************************************/
134bool entry_from_token(struct section *psection, const char *name,
135 const char *tok)
136{
137 if ('*' == tok[0]) {
138 char buf[strlen(tok) + 1];
139
140 remove_escapes(tok + 1, FALSE, buf, sizeof(buf));
141 (void) section_entry_str_new(psection, name, buf, FALSE);
142 DEBUG_ENTRIES("entry %s '%s'", name, buf);
143 return TRUE;
144 }
145
146 if ('$' == tok[0] || '"' == tok[0]) {
147 char buf[strlen(tok) + 1];
148 bool escaped = ('"' == tok[0]);
149
150 remove_escapes(tok + 1, escaped, buf, sizeof(buf));
151 (void) section_entry_str_new(psection, name, buf, escaped);
152 DEBUG_ENTRIES("entry %s '%s'", name, buf);
153 return TRUE;
154 }
155
156 if (fc_isdigit(tok[0]) || (('-' == tok[0] || '+' == tok[0]) && fc_isdigit(tok[1]))) {
157 float fvalue;
158
159 if (str_to_float(tok, &fvalue)) {
161 DEBUG_ENTRIES("entry %s %d", name, fvalue);
162
163 return TRUE;
164 } else {
165 int ivalue;
166
167 if (str_to_int(tok, &ivalue)) {
169 DEBUG_ENTRIES("entry %s %d", name, ivalue);
170
171 return TRUE;
172 }
173 }
174 }
175
176 if (0 == fc_strncasecmp(tok, "FALSE", 5)
177 || 0 == fc_strncasecmp(tok, "TRUE", 4)) {
178 bool value = (0 == fc_strncasecmp(tok, "TRUE", 4));
179
180 (void) section_entry_bool_new(psection, name, value);
181 DEBUG_ENTRIES("entry %s %s", name, value ? "TRUE" : "FALSE");
182 return TRUE;
183 }
184
185 return FALSE;
186}
char * incite_cost
Definition comments.c:75
const char * name
Definition inputfile.c:127
#define fc_assert_ret(condition)
Definition log.h:191
#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:517
bool str_to_float(const char *str, float *pfloat)
Definition shared.c:584
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:974
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:900
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:333
bool fc_isdigit(char c)
Definition support.c:1232
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:238
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47