Arax -8d09c51940345c86062e8ef2427c705ae66e5926
A Runtime Framework for Decoupling Applications from Heterogeneous Accelerators
Loading...
Searching...
No Matches
config.c
Go to the documentation of this file.
1#include "config.h"
2#include "system.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include <errno.h>
8#include <limits.h>
9#include <stdint.h>
10#include "utils/arax_assert.h"
11#include <pwd.h>
12
13const char* conf_get(const char *path)
14{
15 switch (utils_config_get_source()) {
16 case CONFIG_ENV: {
17 char *conf_str;
18 conf_str = strdup(system_env_var("ARAX_CONF"));
19 return conf_str;
20 }
21 case CONFIG_FILE: {
22 FILE *conf = 0;
23
24 size_t size = system_file_size(path);
25 char *conf_str = malloc(size + 1);
26 memset(conf_str, 0, size + 1);
27
28 if (!size)
29 return conf_str;
30
31 conf = fopen(path, "r");
32
33 if (conf) {
34 char *read_ptr = conf_str;
35 size_t read_bytes = 0;
36 while ( (read_bytes = fread(conf_str, 1, size, conf) ) ) {
37 read_ptr += read_bytes;
38 size -= read_bytes;
39 }
40 fclose(conf);
41 }
42 return conf_str;
43 }
44 }
45 arax_assert(!"Unkown config source!");
46 return 0;
47} /* conf_get */
48
49void conf_set(const char *path, const char *conf_str)
50{
51 switch (utils_config_get_source()) {
52 case CONFIG_ENV: {
53 setenv("ARAX_CONF", conf_str, 1);
54 return;
55 }
56 case CONFIG_FILE: {
57 FILE *conf = 0;
58
59 size_t size = strlen(conf_str);
60 conf = fopen(path, "w");
61 if (conf) {
62 fwrite(conf_str, 1, size, conf);
63 fclose(conf);
64 }
65 return;
66 }
67 }
68 arax_assert(!"Unkown config source!");
69}
70
71void utils_config_write_str(char *path, const char *key, const char *value)
72{
73 const char *prev = conf_get(path);
74 char *next = malloc(strlen(prev) + strlen(key) + strlen(value) + 3);
75
76 strcpy(next, prev);
77
78 sprintf(next + strlen(next), "%s %s\n", key, value);
79
80 conf_set(path, next);
81
82 free(next);
83}
84
85void utils_config_write_long(char *path, const char *key, long value)
86{
87 char svalue[1024];
88
89 sprintf(svalue, "%ld", value);
90
91 utils_config_write_str(path, key, svalue);
92}
93
94char* utils_config_alloc_path(const char *path)
95{
96 char temp[4096] = { 0 };
97 char *tp = temp;
98 size_t size = sizeof(temp);
99
100 if (!path)
101 return 0;
102
103 do{
104 if (!size)
105 return 0;
106
107 switch (*path) {
108 case '~': {
109 const char *home = system_home_path();
110 size_t home_len = strlen(home);
111 arax_assert(size - home_len <= sizeof(temp)); // would have overflowed
112 strncat(tp, home, size);
113 tp += home_len;
114 size -= home_len;
115 break;
116 }
117 default:
118 *tp = *path;
119 tp++;
120 size--;
121 }
122 }while (*(path++)); // ensure \0 gets copied
123 tp = malloc(strlen(temp) + 1);
124 strcpy(tp, temp);
125 return tp;
126}
127
129{
130 free(path);
131}
132
133int _utils_config_get_str(char *path, const char *key, char *value, size_t value_size)
134{
135 const char *conf = conf_get(path);
136 const char *cleanup = conf;
137 char ckey[128];
138 char cval[896];
139 int line = 0;
140 int len = 0;
141
142 if (!conf)
143 return 0;
144
145 while (++line) {
146 if (sscanf(conf, "%127s %895s%n", ckey, cval, &len) < 2) {
147 break;
148 }
149 conf += len;
150 len = 0;
151 if (!strncmp(ckey, key, sizeof(ckey) ) ) {
152 /* Found the key i was looking for */
153 strncpy(value, cval, value_size);
154 len = strlen(cval);
155 break;
156 }
157 }
158
159 free((void *) cleanup);
160
161 return len;
162}
163
164int utils_config_get_str(char *path, const char *key, char *value, size_t value_size, const char *def_val)
165{
166 if (!_utils_config_get_str(path, key, value, value_size)) {
167 if (def_val) { // Not found, but have default, update with default
168 utils_config_write_str(path, key, def_val);
169 strncpy(value, def_val, value_size);
170 } else {
171 fprintf(stderr, "No default value for \'%s\' config key\n", key);
172 }
173 return 0;
174 }
175 return 1;
176}
177
178int utils_config_get_bool(char *path, const char *key, int *value, int def_val)
179{
180 if (utils_config_get_int(path, key, value, def_val) ) {
181 if (*value == 0 || *value == 1)
182 return 1;
183 }
184
185
186 *value = def_val;
187 return 0;
188}
189
190int utils_config_get_int(char *path, const char *key, int *value, int def_val)
191{
192 long cval;
193
194 if (utils_config_get_long(path, key, &cval, def_val) ) {
195 if (INT_MAX >= cval && INT_MIN <= cval) {
196 *value = cval;
197 return 1; /* Value was an int */
198 }
199 }
200
201
202 *value = def_val;
203 return 0;
204}
205
206int utils_config_get_long(char *path, const char *key, long *value, long def_val)
207{
208 char cval[22];
209 char *end;
210
211 if (_utils_config_get_str(path, key, cval, sizeof(cval) ) ) {
212 /* Key exists */
213 errno = 0;
214 *value = strtol(cval, &end, 0);
215 if (errno || end == cval) {
216 utils_config_write_long(path, key, def_val);
217 *value = def_val;
218 return 0;
219 }
220 return 1;
221 }
222 *value = def_val;
223 return 0;
224}
225
226int utils_config_get_size(char *path, const char *key, size_t *value, size_t def_val)
227{
228 long cval;
229
230 if (utils_config_get_long(path, key, &cval, def_val) ) {
231 if (SIZE_MAX >= cval && 0 <= cval) {
232 *value = cval;
233 return 1; /* Value was an size_t */
234 }
235 }
236
237
238 *value = def_val;
239 return 0;
240}
241
243{
244 if (system_env_var("ARAX_CONF"))
245 return CONFIG_ENV;
246 else
247 return CONFIG_FILE;
248}
#define arax_assert(EXPR)
Definition arax_assert.h:7
utils_config_source
Definition config.h:142
@ CONFIG_FILE
Definition config.h:143
@ CONFIG_ENV
Definition config.h:144
int _utils_config_get_str(char *path, const char *key, char *value, size_t value_size)
Definition config.c:133
enum utils_config_source utils_config_get_source()
Definition config.c:242
int utils_config_get_long(char *path, const char *key, long *value, long def_val)
Definition config.c:206
int utils_config_get_str(char *path, const char *key, char *value, size_t value_size, const char *def_val)
Definition config.c:164
int utils_config_get_int(char *path, const char *key, int *value, int def_val)
Definition config.c:190
int utils_config_get_size(char *path, const char *key, size_t *value, size_t def_val)
Definition config.c:226
void utils_config_write_str(char *path, const char *key, const char *value)
Definition config.c:71
void utils_config_free_path(char *path)
Definition config.c:128
void utils_config_write_long(char *path, const char *key, long value)
Definition config.c:85
const char * conf_get(const char *path)
Definition config.c:13
int utils_config_get_bool(char *path, const char *key, int *value, int def_val)
Definition config.c:178
void conf_set(const char *path, const char *conf_str)
Definition config.c:49
char * utils_config_alloc_path(const char *path)
Definition config.c:94
char * system_home_path()
Definition system.c:21
off_t system_file_size(const char *file)
Definition system.c:68
const char * system_env_var(const char *var)
Definition system.c:32