root/src/noit_conf.c

Revision 0268e62cb80791dd94220cf6deec916656c7cddd, 2.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

update.. prep for config system and module loading

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include <stdio.h>
8 #include "noit_conf.h"
9 #include "utils/noit_hash.h"
10
11 /* tmp hash impl, replace this with something nice */
12 static noit_hash_table _tmp_config = NOIT_HASH_EMPTY;
13
14
15 static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY;
16 static struct {
17   const char *key;
18   const char *val;
19 } config_info[] = {
20   /*
21    * These are compile-time fallbacks to be used in the event
22    * that the current running config does not have values for
23    * these config paths.
24    *
25    * PLEASE: keep them alphabetically sorted.
26    */
27   { "/global/modules/directory", MODULES_DIR },
28
29   { NULL, NULL }
30 };
31
32
33 void noit_conf_init() {
34   int i;
35   for(i = 0; config_info[i].key != NULL; i++) {
36     noit_hash_store(&_compiled_fallback,
37                     strdup(config_info[i].key), strlen(config_info[i].key),
38                     (void *)strdup(config_info[i].val));
39   }
40 }
41
42 int noit_conf_load(const char *path) {
43   return -1;
44 }
45 int noit_conf_save(const char *path) {
46   return -1;
47 }
48
49 int noit_conf_get_string(const char *path, char **value) {
50   char *str;
51   if(noit_hash_retrieve(&_tmp_config,
52                         path, strlen(path), (void **)&str)) {
53     *value = strdup(str);
54   }
55   if(noit_hash_retrieve(&_compiled_fallback,
56                         path, strlen(path), (void **)&str)) {
57     *value = strdup(str);
58   }
59   return 0;
60 }
61 int noit_conf_set_string(const char *path, const char *value) {
62   noit_hash_replace(&_tmp_config,
63                     strdup(path), strlen(path), (void *)strdup(value),
64                     free, free);
65   return 1;
66 }
67 int noit_conf_get_int(const char *path, int *value) {
68   char *str;
69   long longval;
70   if(noit_conf_get_string(path, &str)) {
71     int base = 10;
72     if(str[0] == '0') {
73       if(str[1] == 'x') base = 16;
74       else base = 8;
75     }
76     longval = strtol(str, NULL, base);
77     free(str);
78     *value = (int)longval;
79     return 1;
80   }
81   return 0;
82 }
83 int noit_conf_set_int(const char *path, int value) {
84   char buffer[32];
85   snprintf(buffer, 32, "%d", value);
86   return noit_conf_set_string(path, buffer);
87 }
88 int noit_conf_get_float(const char *path, float *value) {
89   char *str;
90   if(noit_conf_get_string(path, &str)) {
91     *value = atof(str);
92     free(str);
93     return 1;
94   }
95   return 0;
96 }
97 int noit_conf_set_float(const char *path, float value) {
98   char buffer[32];
99   snprintf(buffer, 32, "%f", value);
100   return noit_conf_set_string(path, buffer);
101 }
102 int noit_conf_get_boolean(const char *path, noit_conf_boolean *value) {
103   char *str;
104   if(noit_conf_get_string(path, &str)) {
105     if(!strcasecmp(str, "true")) *value = true;
106     else *value = false;
107     free(str);
108     return 1;
109   }
110   return 0;
111 }
112 int noit_conf_set_boolean(const char *path, noit_conf_boolean value) {
113   if(value == true)
114     return noit_conf_set_string(path, "true");
115   return noit_conf_set_string(path, "false");
116 }
117
Note: See TracBrowser for help on using the browser.