Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
config-typed.c
Go to the documentation of this file.
1/*
2 Config-typed - Lumiera configuration high-level interface
3
4 Copyright (C)
5 2008, Christian Thaeter <ct@pipapo.org>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
14
23#include "include/logging.h"
24#include "lib/tmpbuf.h"
25#include "common/config.h"
26
27#include <stdint.h>
28
29
30
32
33
34
35
36const char*
37lumiera_config_link_get (const char* key, const char** value)
38{
39 (void) key; (void) value;
40 TRACE (configtyped_dbg);
41 UNIMPLEMENTED();
42 return 0;
43}
44
46lumiera_config_link_set (const char* key, const char** value)
47{
48 (void) key; (void) value;
49 TRACE (configtyped_dbg);
50 UNIMPLEMENTED();
51 return 0;
52}
53
54
59const char*
60lumiera_config_number_get (const char* key, long long* value)
61{
62 TRACE (configtyped_dbg);
63
64 const char* raw_value = NULL;
65
67 {
68 if (lumiera_config_get (key, &raw_value))
69 {
70 if (raw_value)
71 {
72 /* got it, scan it */
73 if (sscanf (raw_value, "%Li", value) != 1)
74 {
75 LUMIERA_ERROR_SET (config, CONFIG_SYNTAX_VALUE, lumiera_tmpbuf_snprintf (5000, "key '%s', value '%s'", key, raw_value));
76 raw_value = NULL;
77 }
78 }
79 else
80 LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
81 }
82 }
83
84 return raw_value;
85}
86
88lumiera_config_number_set (const char* key, long long* value)
89{
90 TRACE (configtyped_dbg);
91
93
95 {
96 const char* fmt = "= %lld"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
97 item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
98 }
99
100 return item;
101}
102
103
108const char*
109lumiera_config_real_get (const char* key, long double* value)
110{
111 (void) key; (void) value;
112 TRACE (configtyped_dbg);
113 UNIMPLEMENTED();
114 return 0;
115}
116
118lumiera_config_real_set (const char* key, long double* value)
119{
120 (void) key; (void) value;
121 TRACE (configtyped_dbg);
122 UNIMPLEMENTED();
123 return 0;
124}
125
126
127
139static char*
140scan_string (const char* in)
141{
142 /* chop leading blanks */
143 in += strspn(in, " \t");
144
145 char quote = *in;
146 char* end;
147 char* ret = NULL;
148
149 if (quote == '"' || quote == '\'')
150 {
151 /* quoted string */
152 ++in;
153 end = strchr (in, quote);
154 while (end && end[1] == quote)
155 end = strchr (end + 2, quote);
156
157 if (end)
158 {
159 ret = lumiera_tmpbuf_strndup (in, end - in);
160
161 /* replace double quote chars with single one */
162 char* wpos;
163 char* rpos;
164 for (wpos = rpos = ret; *rpos; ++rpos, ++wpos)
165 {
166 if (*rpos == quote)
167 ++rpos;
168 *wpos = *rpos;
169 }
170 *wpos = '\0';
171 }
172 else
173 /* quotes doesn't match */
174 LUMIERA_ERROR_SET (config, CONFIG_SYNTAX_VALUE, "unmatched quotes");
175 }
176 else
177 {
178 /* unquoted string */
179 ret = lumiera_tmpbuf_strndup (in, SIZE_MAX);
180
181 /* chop trailing blanks */
182 end = ret + strlen (ret) - 1;
183 while (end > ret && (*end == ' ' || *end == '\t'))
184 *end-- = '\0';
185 }
186
187 return ret;
188}
189
190const char*
191lumiera_config_string_get (const char* key, const char** value)
192{
193 TRACE (configtyped_dbg);
194
195 const char* raw_value = *value = NULL;
196
198 {
199 if (lumiera_config_get (key, &raw_value))
200 {
201 if (raw_value)
202 {
203 *value = scan_string (raw_value);
204 }
205 else
206 LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
207 }
208 }
209
210 return *value;
211}
212
214lumiera_config_string_set (const char* key, const char** value)
215{
216 TRACE (configtyped_dbg);
217
218 LumieraConfigitem item = NULL;
219
221 {
222 const char* fmt = "= %s"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
223 item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
224 }
225
226 return item;
227}
228
229
235const char*
236lumiera_config_wordlist_get (const char* key, const char** value)
237{
238 TRACE (configtyped_dbg, "KEY %s", key);
239
240 const char* raw_value = *value = NULL;
241
243 {
244 if (lumiera_config_get (key, &raw_value))
245 {
246 if (raw_value)
247 {
248 *value = raw_value;
249 }
250 else
251 LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
252
254 }
255 }
256
257 return *value;
258}
259
260
262lumiera_config_wordlist_set (const char* key, const char** value)
263{
264 TRACE (configtyped_dbg);
265
266 LumieraConfigitem item = NULL;
267
269 {
270 const char* fmt = "= %s"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
271 item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
272 }
273
274 return item;
275}
276
277
278
283static char*
284scan_word (const char* in)
285{
286 /* chop leading blanks */
287 in += strspn(in, " \t");
288
289 char* ret = lumiera_tmpbuf_strndup (in, SIZE_MAX);
290 char* end = ret;
291
292 /* chop trailing blanks */
293 while (*end && *end != ' ' && *end != '\t')
294 ++end;
295
296 *end++ = '\0';
297
298 return ret;
299}
300
301const char*
302lumiera_config_word_get (const char* key, const char** value)
303{
304 TRACE (configtyped_dbg, "KEY %s", key);
305
306 const char* raw_value = *value = NULL;
307
309 {
310 if (lumiera_config_get (key, &raw_value))
311 {
312 if (raw_value)
313 {
314 *value = scan_word (raw_value);
315 }
316 else
317 LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
318 }
319 }
320
321 return *value;
322}
323
325lumiera_config_word_set (const char* key, const char** value)
326{
327 TRACE (configtyped_dbg);
328
329 LumieraConfigitem item = NULL;
330
332 {
333 const char* fmt = "= %s";
334 item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, scan_word (*value)));
335 }
336
337 return item;
338}
339
340
345const char*
346lumiera_config_bool_get (const char* key, int* value)
347{
348 (void) key; (void) value;
349 TRACE (configtyped_dbg);
350 UNIMPLEMENTED("get bool from config system");
351 return 0;
352}
353
354
356lumiera_config_bool_set (const char* key, int* value)
357{
358 (void) key; (void) value;
359 TRACE (configtyped_dbg);
360 UNIMPLEMENTED("set bool in config system");
361 return 0;
362}
363
364
365/*
366// Local Variables:
367// mode: C
368// c-file-style: "gnu"
369// indent-tabs-mode: nil
370// End:
371*/
LumieraConfigitem lumiera_config_wordlist_set(const char *key, const char **value)
const char * lumiera_config_number_get(const char *key, long long *value)
Number signed integer numbers, in different formats (decimal, hex, oct, binary(for masks))
const char * lumiera_config_bool_get(const char *key, int *value)
Bool Bool in various formats, (0,1(!1), yes/no, true/false, on/off, set/clear)
const char * lumiera_config_string_get(const char *key, const char **value)
LumieraConfigitem lumiera_config_real_set(const char *key, long double *value)
const char * lumiera_config_wordlist_get(const char *key, const char **value)
Wordlist words delimited by any of " \t,;".
static char * scan_word(const char *in)
helper function, takes a raw input string and give a tmpbuf with the word parsed back.
LumieraConfigitem lumiera_config_word_set(const char *key, const char **value)
LumieraConfigitem lumiera_config_link_set(const char *key, const char **value)
LumieraConfigitem lumiera_config_string_set(const char *key, const char **value)
LumieraConfigitem lumiera_config_number_set(const char *key, long long *value)
const char * lumiera_config_link_get(const char *key, const char **value)
LumieraConfigitem lumiera_config_bool_set(const char *key, int *value)
const char * lumiera_config_real_get(const char *key, long double *value)
Real floating point number in standard formats (see printf/scanf)
LumieraConfig lumiera_global_config
Definition config.c:73
const char * lumiera_config_word_get(const char *key, const char **value)
static char * scan_string(const char *in)
String unquoted string which covers the whole value area and gets chopped or quoted string which pres...
LumieraConfigitem lumiera_config_set(const char *key, const char *delim_value)
Definition config.c:223
const char * lumiera_config_get(const char *key, const char **value)
Definition config.c:154
Interface for a lumiera configuration system (draft).
lumiera_config * LumieraConfig
Definition config.h:67
lumiera_configitem * LumieraConfigitem
Definition configitem.h:53
#define LUMIERA_ERROR_SET(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition error.h:82
#define LUMIERA_ERROR_SET_WARNING(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition error.h:127
return NULL
Definition llist.h:586
This header is for including and configuring NoBug.
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition mutex.h:32
char * lumiera_tmpbuf_strndup(const char *src, size_t size)
Duplicate string to a tmpbuf.
Definition tmpbuf.c:102
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition tmpbuf.c:116
Round robin temporary buffers.