4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 static Eina_Hash *vars;
29 static const char *path = "/etc/deviced/logd-grabber.conf";
31 static int is_correct(char ch)
33 if (ch >= '0' && ch <= '9')
35 if (ch >='a' && ch <= 'z')
37 if (ch >='A' && ch <= 'Z')
39 if (ch == '_' || ch == '.')
45 static int split(char *str, char **key, char **value)
52 _value = strchr(str, '=');
54 _E("Wrong variable declaration: \"%s\". "
55 "Must be like \"key = value\"\n", str);
59 while (*str == ' ') ++str;
60 while (ptr < _value) {
61 if (!is_correct(*ptr)) {
64 while (*ptr == ' ' && ptr != _value)
67 _E("Expected '=' before '%c' at %d\n",
70 } else if (ptr != _value) {
71 _E("wrong char '%c' at %d\n", *ptr, ptr - str);
80 *key = (char*) calloc(1, ptr - str + 1);
81 strncpy(*key, str, ptr - str);
84 while (*ptr == ' ') ++ptr;
86 _E("expected char symbol at %d", ptr - str);
94 if (!is_correct(*ptr)) {
95 _E("wrong char '%c' at %d\n", *ptr, ptr - str);
103 *value = (char*) calloc(1, strlen(str) - (ptr - str) + 1);
104 strncpy(*value, str + (ptr - str) , strlen(str) - (ptr - str));
109 int config_init(void)
116 vars = eina_hash_string_superfast_new(free);
118 fp = fopen(path, "r");
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) {
128 if (fclose(fp) < 0) {
129 _E("fclose failed: %s", strerror(errno));
133 if (eina_hash_add(vars, key, value) != EINA_FALSE) {
134 _I("%s = %s added\n", key, value);
137 if (fclose(fp) < 0) {
139 _E("fclose failed: %s", strerror(errno));
146 const char *config_get_string(const char *key, const char *def_value, int *error_code)
148 const char *value = eina_hash_find(vars, key);
152 _E("key %d not exist", key);
163 int config_get_int(const char *key, int def_value, int *error_code)
165 const char *value = config_get_string(key, NULL, error_code);
170 converted_value = atoi(value);
172 return converted_value;
175 float config_get_float(const char *key, float def_value, int *error_code)
177 const char *value = config_get_string(key, NULL, error_code);
178 float converted_value;
182 converted_value = atof(value);
184 return converted_value;
187 double config_get_double(const char *key, double def_value, int *error_code)
189 const char *value = config_get_string(key, NULL, error_code);
190 double converted_value;
194 converted_value = atof(value);
196 return converted_value;
199 int config_exit(void)
201 eina_hash_free(vars);