Lumiera  0.pre.03
»edit your freedom«
config-typed.c
Go to the documentation of this file.
1 /*
2  Config-typed - Lumiera configuration high-level interface
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
32 #include "include/logging.h"
33 #include "lib/tmpbuf.h"
34 #include "common/config.h"
35 
36 #include <stdint.h>
37 
38 
39 
40 extern LumieraConfig lumiera_global_config;
41 
42 
43 
44 
45 const char*
46 lumiera_config_link_get (const char* key, const char** value)
47 {
48  (void) key; (void) value;
49  TRACE (configtyped_dbg);
50  UNIMPLEMENTED();
51  return 0;
52 }
53 
54 LumieraConfigitem
55 lumiera_config_link_set (const char* key, const char** value)
56 {
57  (void) key; (void) value;
58  TRACE (configtyped_dbg);
59  UNIMPLEMENTED();
60  return 0;
61 }
62 
63 
68 const char*
69 lumiera_config_number_get (const char* key, long long* value)
70 {
71  TRACE (configtyped_dbg);
72 
73  const char* raw_value = NULL;
74 
75  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
76  {
77  if (lumiera_config_get (key, &raw_value))
78  {
79  if (raw_value)
80  {
81  /* got it, scan it */
82  if (sscanf (raw_value, "%Li", value) != 1)
83  {
84  LUMIERA_ERROR_SET (config, CONFIG_SYNTAX_VALUE, lumiera_tmpbuf_snprintf (5000, "key '%s', value '%s'", key, raw_value));
85  raw_value = NULL;
86  }
87  }
88  else
89  LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
90  }
91  }
92 
93  return raw_value;
94 }
95 
96 LumieraConfigitem
97 lumiera_config_number_set (const char* key, long long* value)
98 {
99  TRACE (configtyped_dbg);
100 
101  LumieraConfigitem item = NULL;
102 
103  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
104  {
105  const char* fmt = "= %lld"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
106  item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
107  }
108 
109  return item;
110 }
111 
112 
117 const char*
118 lumiera_config_real_get (const char* key, long double* value)
119 {
120  (void) key; (void) value;
121  TRACE (configtyped_dbg);
122  UNIMPLEMENTED();
123  return 0;
124 }
125 
126 LumieraConfigitem
127 lumiera_config_real_set (const char* key, long double* value)
128 {
129  (void) key; (void) value;
130  TRACE (configtyped_dbg);
131  UNIMPLEMENTED();
132  return 0;
133 }
134 
135 
136 
148 static char*
149 scan_string (const char* in)
150 {
151  /* chop leading blanks */
152  in += strspn(in, " \t");
153 
154  char quote = *in;
155  char* end;
156  char* ret = NULL;
157 
158  if (quote == '"' || quote == '\'')
159  {
160  /* quoted string */
161  ++in;
162  end = strchr (in, quote);
163  while (end && end[1] == quote)
164  end = strchr (end + 2, quote);
165 
166  if (end)
167  {
168  ret = lumiera_tmpbuf_strndup (in, end - in);
169 
170  /* replace double quote chars with single one */
171  char* wpos;
172  char* rpos;
173  for (wpos = rpos = ret; *rpos; ++rpos, ++wpos)
174  {
175  if (*rpos == quote)
176  ++rpos;
177  *wpos = *rpos;
178  }
179  *wpos = '\0';
180  }
181  else
182  /* quotes doesn't match */
183  LUMIERA_ERROR_SET (config, CONFIG_SYNTAX_VALUE, "unmatched quotes");
184  }
185  else
186  {
187  /* unquoted string */
188  ret = lumiera_tmpbuf_strndup (in, SIZE_MAX);
189 
190  /* chop trailing blanks */
191  end = ret + strlen (ret) - 1;
192  while (end > ret && (*end == ' ' || *end == '\t'))
193  *end-- = '\0';
194  }
195 
196  return ret;
197 }
198 
199 const char*
200 lumiera_config_string_get (const char* key, const char** value)
201 {
202  TRACE (configtyped_dbg);
203 
204  const char* raw_value = *value = NULL;
205 
206  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
207  {
208  if (lumiera_config_get (key, &raw_value))
209  {
210  if (raw_value)
211  {
212  *value = scan_string (raw_value);
213  }
214  else
215  LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
216  }
217  }
218 
219  return *value;
220 }
221 
222 LumieraConfigitem
223 lumiera_config_string_set (const char* key, const char** value)
224 {
225  TRACE (configtyped_dbg);
226 
227  LumieraConfigitem item = NULL;
228 
229  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
230  {
231  const char* fmt = "= %s"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
232  item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
233  }
234 
235  return item;
236 }
237 
238 
244 const char*
245 lumiera_config_wordlist_get (const char* key, const char** value)
246 {
247  TRACE (configtyped_dbg, "KEY %s", key);
248 
249  const char* raw_value = *value = NULL;
250 
251  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
252  {
253  if (lumiera_config_get (key, &raw_value))
254  {
255  if (raw_value)
256  {
257  *value = raw_value;
258  }
259  else
260  LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
261 
263  }
264  }
265 
266  return *value;
267 }
268 
269 
270 LumieraConfigitem
271 lumiera_config_wordlist_set (const char* key, const char** value)
272 {
273  TRACE (configtyped_dbg);
274 
275  LumieraConfigitem item = NULL;
276 
277  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
278  {
279  const char* fmt = "= %s"; TODO ("use the config system (config.format*...) to deduce the desired format for this key");
280  item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, *value));
281  }
282 
283  return item;
284 }
285 
286 
287 
292 static char*
293 scan_word (const char* in)
294 {
295  /* chop leading blanks */
296  in += strspn(in, " \t");
297 
298  char* ret = lumiera_tmpbuf_strndup (in, SIZE_MAX);
299  char* end = ret;
300 
301  /* chop trailing blanks */
302  while (*end && *end != ' ' && *end != '\t')
303  ++end;
304 
305  *end++ = '\0';
306 
307  return ret;
308 }
309 
310 const char*
311 lumiera_config_word_get (const char* key, const char** value)
312 {
313  TRACE (configtyped_dbg, "KEY %s", key);
314 
315  const char* raw_value = *value = NULL;
316 
317  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
318  {
319  if (lumiera_config_get (key, &raw_value))
320  {
321  if (raw_value)
322  {
323  *value = scan_word (raw_value);
324  }
325  else
326  LUMIERA_ERROR_SET_WARNING (config, CONFIG_NO_ENTRY, key);
327  }
328  }
329 
330  return *value;
331 }
332 
333 LumieraConfigitem
334 lumiera_config_word_set (const char* key, const char** value)
335 {
336  TRACE (configtyped_dbg);
337 
338  LumieraConfigitem item = NULL;
339 
340  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
341  {
342  const char* fmt = "= %s";
343  item = lumiera_config_set (key, lumiera_tmpbuf_snprintf (SIZE_MAX, fmt, scan_word (*value)));
344  }
345 
346  return item;
347 }
348 
349 
354 const char*
355 lumiera_config_bool_get (const char* key, int* value)
356 {
357  (void) key; (void) value;
358  TRACE (configtyped_dbg);
359  UNIMPLEMENTED("get bool from config system");
360  return 0;
361 }
362 
363 
364 LumieraConfigitem
365 lumiera_config_bool_set (const char* key, int* value)
366 {
367  (void) key; (void) value;
368  TRACE (configtyped_dbg);
369  UNIMPLEMENTED("set bool in config system");
370  return 0;
371 }
372 
373 
374 /*
375 // Local Variables:
376 // mode: C
377 // c-file-style: "gnu"
378 // indent-tabs-mode: nil
379 // End:
380 */
#define LUMIERA_ERROR_SET(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition: error.h:91
#define LUMIERA_ERROR_SET_WARNING(flag, err, extra)
Helper macro to raise an error for the current thread.
Definition: error.h:136
char * lumiera_tmpbuf_strndup(const char *src, size_t size)
Duplicate string to a tmpbuf.
Definition: tmpbuf.c:111
Interface for a lumiera configuration system (draft).
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition: mutex.h:41
This header is for including and configuring NoBug.
const char * lumiera_config_real_get(const char *key, long double *value)
Real floating point number in standard formats (see printf/scanf)
Definition: config-typed.c:118
Round robin temporary buffers.
static char * scan_string(const char *in)
String unquoted string which covers the whole value area and gets chopped or quoted string which pres...
Definition: config-typed.c:149
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)
Definition: config-typed.c:355
static char * scan_word(const char *in)
helper function, takes a raw input string and give a tmpbuf with the word parsed back.
Definition: config-typed.c:293
const char * lumiera_config_wordlist_get(const char *key, const char **value)
Wordlist words delimited by any of " \t,;".
Definition: config-typed.c:245
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)) ...
Definition: config-typed.c:69
LumieraConfigitem lumiera_config_set(const char *key, const char *delim_value)
Definition: config.c:232
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:125