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)
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 
31 extern LumieraConfig lumiera_global_config;
32 
33 
34 
35 
36 const char*
37 lumiera_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 
45 LumieraConfigitem
46 lumiera_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 
59 const char*
60 lumiera_config_number_get (const char* key, long long* value)
61 {
62  TRACE (configtyped_dbg);
63 
64  const char* raw_value = NULL;
65 
66  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
87 LumieraConfigitem
88 lumiera_config_number_set (const char* key, long long* value)
89 {
90  TRACE (configtyped_dbg);
91 
92  LumieraConfigitem item = NULL;
93 
94  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
108 const char*
109 lumiera_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 
117 LumieraConfigitem
118 lumiera_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 
139 static char*
140 scan_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 
190 const char*
191 lumiera_config_string_get (const char* key, const char** value)
192 {
193  TRACE (configtyped_dbg);
194 
195  const char* raw_value = *value = NULL;
196 
197  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
213 LumieraConfigitem
214 lumiera_config_string_set (const char* key, const char** value)
215 {
216  TRACE (configtyped_dbg);
217 
218  LumieraConfigitem item = NULL;
219 
220  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
235 const char*
236 lumiera_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 
242  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
261 LumieraConfigitem
262 lumiera_config_wordlist_set (const char* key, const char** value)
263 {
264  TRACE (configtyped_dbg);
265 
266  LumieraConfigitem item = NULL;
267 
268  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
283 static char*
284 scan_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 
301 const char*
302 lumiera_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 
308  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
324 LumieraConfigitem
325 lumiera_config_word_set (const char* key, const char** value)
326 {
327  TRACE (configtyped_dbg);
328 
329  LumieraConfigitem item = NULL;
330 
331  LUMIERA_MUTEX_SECTION (mutex_sync, &lumiera_global_config->lock)
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 
345 const char*
346 lumiera_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 
355 LumieraConfigitem
356 lumiera_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 */
#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
char * lumiera_tmpbuf_strndup(const char *src, size_t size)
Duplicate string to a tmpbuf.
Definition: tmpbuf.c:102
Interface for a lumiera configuration system (draft).
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition: mutex.h:32
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:109
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:140
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:346
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:284
const char * lumiera_config_wordlist_get(const char *key, const char **value)
Wordlist words delimited by any of " \t,;".
Definition: config-typed.c:236
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:60
LumieraConfigitem lumiera_config_set(const char *key, const char *delim_value)
Definition: config.c:223
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:116