Freeciv-3.2: /fast/freeciv/nightly/S3_2/utility/section_file.c Source File

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
28 static 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/**********************************************************************/
36 const char *secfile_error(void)
37{
38 return error_buffer;
39}
40
41/**********************************************************************/
44 void 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{
49 char message[MAX_LEN_ERRORBUF];
50 va_list args;
51
52 va_start(args, format);
53 fc_vsnprintf(message, sizeof(message), format, args);
54 va_end(args);
55
56 fc_snprintf(error_buffer, sizeof(error_buffer),
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/**********************************************************************/
65 const char *section_name(const struct section *psection)
66{
67 return (NULL != psection ? psection->name : NULL);
68}
69
70/**********************************************************************/
73 struct section_file *secfile_new(bool allow_duplicates)
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;
81 secfile->sections = section_list_new_full(section_destroy);
82 secfile->allow_duplicates = allow_duplicates;
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/**********************************************************************/
95 void secfile_destroy(struct section_file *secfile)
96{
97 SECFILE_RETURN_IF_FAIL(secfile, NULL, secfile != NULL);
98
99 section_hash_destroy(secfile->hash.sections);
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) {
104 entry_hash_destroy(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
110 section_list_destroy(secfile->sections);
111
112 if (NULL != secfile->name) {
113 free(secfile->name);
114 }
115
116 free(secfile);
117}
118
119/**********************************************************************/
124 void secfile_allow_digital_boolean(struct section_file *secfile,
125 bool allow_digital_boolean)
126{
127 fc_assert_ret(NULL != secfile);
128 secfile->allow_digital_boolean = allow_digital_boolean;
129}
130
131/**********************************************************************/
134 bool 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)) {
160 (void) section_entry_float_new(psection, name, 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)) {
168 (void) section_entry_int_new(psection, name, 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}
incite_cost
char * incite_cost
Definition comments.c:74
name
const char * name
Definition inputfile.c:127
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
registry.h
section_destroy
void section_destroy(struct section *psection)
Definition registry_ini.c:2969
section_entry_int_new
struct entry * section_entry_int_new(struct section *psection, const char *name, int value)
Definition registry_ini.c:3179
section_entry_str_new
struct entry * section_entry_str_new(struct section *psection, const char *name, const char *value, bool escaped)
Definition registry_ini.c:3227
section_entry_float_new
struct entry * section_entry_float_new(struct section *psection, const char *name, float value)
Definition registry_ini.c:3211
section_entry_bool_new
struct entry * section_entry_bool_new(struct section *psection, const char *name, bool value)
Definition registry_ini.c:3195
secfile_name
const char * secfile_name(const struct section_file *secfile)
Definition registry_ini.c:943
secfile_log
void secfile_log(const struct section_file *secfile, const struct section *psection, const char *file, const char *function, int line, const char *format,...)
Definition section_file.c:44
error_buffer
static char error_buffer[MAX_LEN_ERRORBUF]
Definition section_file.c:28
MAX_LEN_ERRORBUF
#define MAX_LEN_ERRORBUF
Definition section_file.c:26
secfile_allow_digital_boolean
void secfile_allow_digital_boolean(struct section_file *secfile, bool allow_digital_boolean)
Definition section_file.c:124
secfile_new
struct section_file * secfile_new(bool allow_duplicates)
Definition section_file.c:73
secfile_error
const char * secfile_error(void)
Definition section_file.c:36
entry_from_token
bool entry_from_token(struct section *psection, const char *name, const char *tok)
Definition section_file.c:134
DEBUG_ENTRIES
#define DEBUG_ENTRIES(...)
Definition section_file.c:31
section_name
const char * section_name(const struct section *psection)
Definition section_file.c:65
secfile_destroy
void secfile_destroy(struct section_file *secfile)
Definition section_file.c:95
section_file.h
SECFILE_RETURN_IF_FAIL
#define SECFILE_RETURN_IF_FAIL(secfile, psection, condition)
Definition section_file.h:62
str_to_int
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
str_to_float
bool str_to_float(const char *str, float *pfloat)
Definition shared.c:584
message
Definition messagewin_common.h:29
section_file
Definition section_file.h:36
section_file::entries
struct entry_hash * entries
Definition section_file.h:49
section_file::hash
struct section_file::@8 hash
section_file::name
char * name
Definition section_file.h:37
section_file::sections
struct section_list * sections
Definition section_file.h:44
section_file::allow_digital_boolean
bool allow_digital_boolean
Definition section_file.h:46
section_file::allow_duplicates
bool allow_duplicates
Definition section_file.h:45
section_file::num_includes
unsigned int num_includes
Definition section_file.h:42
section_file::num_long_comments
unsigned int num_long_comments
Definition section_file.h:43
section_file::num_entries
size_t num_entries
Definition section_file.h:38
section
Definition section_file.h:28
section::name
char * name
Definition section_file.h:31
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:900
remove_escapes
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
Definition support.c:333
fc_isdigit
bool fc_isdigit(char c)
Definition support.c:1232
fc_strncasecmp
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:238
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47