feedback: Modify strncmp indexing comparison
[platform/core/system/libsvi.git] / src / feedback-config.c
1 /*
2  * libfeedback
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <stdio.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <libsyscommon/ini-parser.h>
22
23 #include "feedback-config.h"
24 #include "profiles.h"
25 #include "log.h"
26
27 #define MAX_DATA        256
28
29 #define MATCH(a, b)             (!strncmp(a, b, strlen(a) + 1))
30 #define SET_CONF(a, b)          (a = (b > 0.0 ? b : a))
31
32 static int load_config_index = 0;
33
34 static int load_config(struct parse_result *result, void *user_data)
35 {
36         struct feedback_config_info *info = (struct feedback_config_info *)user_data;
37         char *name;
38         char *value;
39         int pattern;
40
41         if (!info)
42                 return -EINVAL;
43
44         if (!MATCH(result->section, info->name))
45                 return -ENOENT;
46
47         if (!result->name || !result->value)
48                 return -ENOENT;
49
50         name = result->name;
51         value = result->value;
52
53         pattern = profile->get_pattern_enum(name);
54         if (pattern < 0 || pattern >= profile->max_pattern)
55                 return -EINVAL;
56
57         info->data[load_config_index].pattern = pattern;
58
59         info->data[load_config_index].origin = strdup(value);
60         if (!info->data[load_config_index].origin)
61                 _E("fail to copy %d sound data", //LCOV_EXCL_LINE
62                                 pattern);
63         load_config_index++;
64
65         return 0;
66 }
67
68 int feedback_load_config(const char *path,
69                 struct feedback_config_info *info)
70 {
71         int ret;
72         int i;
73
74         if (!path || !info)
75                 return -EINVAL;
76
77         info->data = calloc(1,
78                         sizeof(struct feedback_data) * profile->get_num_of_pattern());
79         if (!info->data) {
80                 _E("fail to allocate %s data", path); //LCOV_EXCL_LINE
81                 return -ENOMEM; //LCOV_EXCL_LINE System Error
82         }
83
84         for (i = 0; i < profile->get_num_of_pattern(); i++)
85                 info->data[i].pattern = -1;
86
87         load_config_index = 0;
88         ret = config_parse(path, load_config, info);
89         if (ret < 0)
90                 _E("Failed to load %s, %d Use default value!", //LCOV_EXCL_LINE
91                                 path, ret);
92
93         return ret;
94 }
95
96 void feedback_free_config(struct feedback_config_info *info)
97 {
98         int i;
99
100         if (!info || !(info->data))
101                 return;
102
103         if (!profile)
104                 return;
105
106         for (i = 0; i < profile->get_num_of_pattern(); i++) {
107                 if (info->data[i].origin) {
108                         free(info->data[i].origin);
109                         info->data[i].origin = NULL;
110                 }
111                 if (info->data[i].changed) {
112                         free(info->data[i].changed);
113                         info->data[i].changed = NULL;
114                 }
115         }
116
117         free(info->data);
118         info->data = NULL;
119         load_config_index = 0;
120 }