check: Relocate line coverage comment at profile_get_num_of_standard()
[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 //LCOV_EXCL_START
35 static int load_config(struct parse_result *result, void *user_data)
36 {
37         struct feedback_config_info *info = (struct feedback_config_info *)user_data;
38         char *name;
39         char *value;
40         int pattern;
41
42         if (!info)
43                 return -EINVAL;
44
45         if (!MATCH(result->section, info->name))
46                 return -ENOENT;
47
48         if (!result->name || !result->value)
49                 return -ENOENT;
50
51         name = result->name;
52         value = result->value;
53
54         pattern = profile->get_pattern_enum(name);
55         if (pattern < 0 || pattern >= profile->max_pattern)
56                 return -EINVAL;
57
58         info->data[load_config_index].pattern = pattern;
59
60         info->data[load_config_index].origin = strdup(value);
61         if (!info->data[load_config_index].origin)
62                 _E("fail to copy %d sound data", //LCOV_EXCL_LINE
63                                 pattern);
64         load_config_index++;
65
66         return 0;
67 }
68
69 int feedback_load_config(const char *path,
70                 struct feedback_config_info *info)
71 {
72         int ret;
73         int i;
74
75         if (!path || !info)
76                 return -EINVAL;
77
78         info->data = calloc(1,
79                         sizeof(struct feedback_data) * profile->get_num_of_pattern());
80         if (!info->data) {
81                 _E("fail to allocate %s data", path); //LCOV_EXCL_LINE
82                 return -ENOMEM; //LCOV_EXCL_LINE System Error
83         }
84
85         for (i = 0; i < profile->get_num_of_pattern(); i++)
86                 info->data[i].pattern = -1;
87
88         load_config_index = 0;
89         ret = config_parse(path, load_config, info);
90         if (ret < 0)
91                 _E("Failed to load %s, %d Use default value!", //LCOV_EXCL_LINE
92                                 path, ret);
93
94         return ret;
95 }
96
97 void feedback_free_config(struct feedback_config_info *info)
98 {
99         int i;
100
101         if (!info || !(info->data))
102                 return;
103
104         if (!profile)
105                 return;
106
107         for (i = 0; i < profile->get_num_of_pattern(); i++) {
108                 if (info->data[i].origin) {
109                         free(info->data[i].origin);
110                         info->data[i].origin = NULL;
111                 }
112         }
113
114         free(info->data);
115         info->data = NULL;
116         load_config_index = 0;
117 }
118 //LCOV_EXCL_STOP