tizen 2.3 release
[framework/system/deviced.git] / src / logd_grabber / config.c
1 /*
2  *  deviced
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include <Eina.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include "core/log.h"
27
28 static Eina_Hash *vars;
29 static const char *path = "/etc/deviced/logd-grabber.conf";
30
31 static int is_correct(char ch)
32 {
33         if (ch >= '0' && ch <= '9')
34                 return 1;
35         if (ch >='a' && ch <= 'z')
36                 return 1;
37         if (ch >='A' && ch <= 'Z')
38                 return 1;
39         if (ch == '_' || ch == '.')
40                 return 1;
41
42         return 0;
43 }
44
45 static int split(char *str, char **key, char **value)
46 {
47         char *ptr = NULL;
48         char *_key;
49         char *_value;
50
51         _key = NULL;
52         _value = strchr(str, '=');
53         if (!_value) {
54                 _E("Wrong variable declaration: \"%s\". "
55                         "Must be like \"key = value\"\n", str);
56                 return -EINVAL;
57         }
58         ptr = str;
59         while (*str == ' ') ++str;
60         while (ptr < _value) {
61                 if (!is_correct(*ptr)) {
62                         if (*ptr == ' ') {
63                                 _key = ptr;
64                                 while (*ptr == ' ' && ptr != _value)
65                                         ++ptr;
66                                 if (ptr != _value) {
67                                         _E("Expected '=' before '%c' at %d\n",
68                                                 *ptr, ptr - str);
69                                 }
70                         } else if (ptr != _value) {
71                                 _E("wrong char '%c' at %d\n", *ptr, ptr - str);
72                                 return -EINVAL;
73                         }
74                 }
75                 ++ptr;
76         }
77         if (!_key)
78                 _key = _value;
79         ptr = _key;
80         *key = (char*) calloc(1, ptr - str + 1);
81         strncpy(*key, str, ptr - str);
82
83         ptr = _value + 1;
84         while (*ptr == ' ') ++ptr;
85         if (*ptr == 0) {
86                 _E("expected char symbol at %d", ptr - str);
87                 free(*key);
88                 return -EINVAL;
89         }
90
91         _value = ptr;
92
93         while (*ptr != 0) {
94                 if (!is_correct(*ptr)) {
95                         _E("wrong char '%c' at %d\n", *ptr, ptr - str);
96                         free(*key);
97                         return -EINVAL;
98                 }
99                 ++ptr;
100         }
101
102         ptr = _value;
103         *value = (char*) calloc(1, strlen(str) - (ptr - str) + 1);
104         strncpy(*value, str + (ptr - str) , strlen(str) - (ptr - str));
105
106         return 0;
107 }
108
109 int config_init(void)
110 {
111         FILE *fp = NULL;
112         char *key;
113         char *value;
114         char buf[100];
115
116         vars = eina_hash_string_superfast_new(free);
117
118         fp = fopen(path, "r");
119         if (!fp) {
120                 perror("fopen: ");
121                 return -errno;
122         }
123         while (fgets(buf, sizeof(buf), fp) != NULL) {
124                 if (buf[strlen(buf) - 1] == '\n')
125                         buf[strlen(buf) - 1] = 0;
126                 if (split(buf, &key, &value) < 0) {
127                         _E("split failed");
128                         if (fclose(fp) < 0) {
129                                 _E("fclose failed: %s", strerror(errno));
130                         }
131                         return -EINVAL;
132                 }
133                 if (eina_hash_add(vars, key, value) != EINA_FALSE) {
134                         _I("%s = %s added\n", key, value);
135                 }
136         }
137         if (fclose(fp) < 0) {
138                 int ret = -errno;
139                 _E("fclose failed: %s", strerror(errno));
140                 return ret;
141         }
142
143         return 0;
144 }
145
146 const char *config_get_string(const char *key, const char *def_value, int *error_code)
147 {
148         const char *value = eina_hash_find(vars, key);
149
150         if (error_code) {
151                 if (!value) {
152                         _E("key %d not exist", key);
153                         *error_code = -1;
154                         value = def_value;
155                 } else {
156                         *error_code = 0;
157                 }
158         }
159
160         return value;
161 }
162
163 int config_get_int(const char *key, int def_value, int *error_code)
164 {
165         const char *value = config_get_string(key, NULL, error_code);
166         int converted_value;
167
168         if (!value)
169                 return def_value;
170         converted_value = atoi(value);
171
172         return converted_value;
173 }
174
175 float config_get_float(const char *key, float def_value, int *error_code)
176 {
177         const char *value = config_get_string(key, NULL, error_code);
178         float converted_value;
179
180         if (!value)
181                 return def_value;
182         converted_value = atof(value);
183
184         return converted_value;
185 }
186
187 double config_get_double(const char *key, double def_value, int *error_code)
188 {
189         const char *value = config_get_string(key, NULL, error_code);
190         double converted_value;
191
192         if (!value)
193                 return def_value;
194         converted_value = atof(value);
195
196         return converted_value;
197 }
198
199 int config_exit(void)
200 {
201         eina_hash_free(vars);
202
203         return 0;
204 }