3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #define MAX_SECTION 64
29 #define WHITESPACE " \t"
30 #define NEWLINE "\n\r"
33 #define MATCH(a, b) (!strncmp(a, b, strlen(a)))
34 #define SET_CONF(a, b) (a = (b > 0.0 ? b : a))
42 static inline char *trim_str(char *s)
46 s += strspn(s, WHITESPACE);
49 for (t = strchr(s, 0); t > s; t--)
50 if (!strchr(WHITESPACE, t[-1]))
56 static int config_parse(const char *file_name,
57 int cb(struct parse_result *result, void *user_data),
61 struct parse_result result;
62 /* use stack for parsing */
64 char section[MAX_SECTION];
65 char *start, *end, *name, *value;
66 int lineno = 0, ret = 0;
68 if (!file_name || !cb) {
74 f = fopen(file_name, "r");
76 _E("Failed to open file %s", file_name);
81 /* parsing line by line */
82 while (fgets(line, MAX_LINE, f) != NULL) {
86 start[strcspn(start, NEWLINE)] = '\0';
87 start = trim_str(start);
89 if (*start == COMMENT) {
91 } else if (*start == '[') {
93 end = strchr(start, ']');
94 if (!end || *end != ']') {
100 strncpy(section, start + 1, sizeof(section));
101 section[MAX_SECTION-1] = '\0';
103 /* parse name & value */
104 end = strchr(start, '=');
105 if (!end || *end != '=') {
110 name = trim_str(start);
111 value = trim_str(end + 1);
112 end = strchr(value, COMMENT);
113 if (end && *end == COMMENT) {
115 value = trim_str(value);
118 result.section = section;
120 result.value = value;
121 /* callback with parse result */
122 ret = cb(&result, user_data);
129 _D("Success to load %s", file_name);
136 _E("Failed to read %s:%d!", file_name, lineno);
140 static int load_config(struct parse_result *result, void *user_data)
142 struct feedback_config_info *info = (struct feedback_config_info *)user_data;
150 if (!MATCH(result->section, info->name))
153 if (!result->name || !result->value)
157 value = result->value;
159 for (i = 0; i < profile->max_pattern; i++) {
160 if (!MATCH(name, (char *)profile->str_pattern[i]))
163 info->data[i].origin = strdup(value);
164 if (!info->data[i].origin)
165 _E("fail to copy %s sound data",
166 profile->str_pattern[i]);
173 int feedback_load_config(const char *path,
174 struct feedback_config_info *info)
181 info->data = calloc(1,
182 sizeof(struct feedback_data) * profile->max_pattern);
184 _E("fail to allocate %s data", path);
188 ret = config_parse(path, load_config, info);
190 _E("Failed to load %s, %d Use default value!",
196 void feedback_free_config(struct feedback_config_info *info)
200 if (!info || !(info->data))
206 for (i = 0; i < profile->max_pattern; i++) {
207 if (info->data[i].origin) {
208 free(info->data[i].origin);
209 info->data[i].origin = NULL;
211 if (info->data[i].changed) {
212 free(info->data[i].changed);
213 info->data[i].changed = NULL;