Coding rule: fix coding rule violation
[platform/core/api/device.git] / src / common.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <system_info.h>
22 #include "common.h"
23
24 #define MAX_LINE        128
25 #define MAX_SECTION     64
26 #define WHITESPACE      " \t"
27 #define NEWLINE         "\n\r"
28 #define COMMENT         '#'
29
30 static inline char *trim_str(char *s)
31 {
32         char *t;
33         /* left trim */
34         s += strspn(s, WHITESPACE);
35
36         /* right trim */
37         for (t = strchr(s, 0); t > s; t--)
38                 if (!strchr(WHITESPACE, t[-1]))
39                         break;
40         *t = 0;
41         return s;
42 }
43
44 int config_parse(const char *file_name, int cb(struct parse_result *result,
45                         void *user_data), void *user_data)
46 {
47         FILE *f = NULL;
48         struct parse_result result;
49         /* use stack for parsing */
50         char line[MAX_LINE];
51         char section[MAX_SECTION];
52         char *start, *end, *name, *value;
53         int lineno = 0, ret = 0;
54
55         if (!file_name || !cb) {
56                 ret = -EINVAL;
57                 goto error;
58         }
59
60         /* open conf file */
61         f = fopen(file_name, "r");
62         if (!f) {
63                 if (errno == ENOENT) {
64                         _I("There is no power config file");
65                         return 0;
66                 }
67
68                 _E("Failed to open file %s", file_name);
69                 ret = -EIO;
70                 goto error;
71         }
72
73         /* parsing line by line */
74         while (fgets(line, MAX_LINE, f) != NULL) {
75                 lineno++;
76
77                 start = line;
78                 start[strcspn(start, NEWLINE)] = '\0';
79                 start = trim_str(start);
80
81                 if (*start == COMMENT) {
82                         continue;
83                 } else if (*start == '[') {
84                         /* parse section */
85                         end = strchr(start, ']');
86                         if (!end || *end != ']') {
87                                 ret = -EBADMSG;
88                                 goto error;
89                         }
90
91                         *end = '\0';
92                         strncpy(section, start + 1, sizeof(section));
93                         section[MAX_SECTION-1] = '\0';
94                 } else if (*start) {
95                         /* parse name & value */
96                         end = strchr(start, '=');
97                         if (!end || *end != '=') {
98                                 ret = -EBADMSG;
99                                 goto error;
100                         }
101                         *end = '\0';
102                         name = trim_str(start);
103                         value = trim_str(end + 1);
104                         end = strchr(value, COMMENT);
105                         if (end && *end == COMMENT) {
106                                 *end = '\0';
107                                 value = trim_str(value);
108                         }
109
110                         result.section = section;
111                         result.name = name;
112                         result.value = value;
113                         /* callback with parse result */
114                         ret = cb(&result, user_data);
115                         if (ret < 0) {
116                                 ret = -EBADMSG;
117                                 goto error;
118                         }
119                 }
120         }
121         _D("Success to load %s", file_name);
122         fclose(f);
123         return 0;
124
125 error:
126         if (f)
127                 fclose(f);
128         _E("Failed to read %s:%d!", file_name, lineno);
129         return ret;
130 }
131
132 tizen_profile_t _get_tizen_profile()
133 {
134         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
135         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
136                 return profile;
137
138         char *profileName;
139         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
140         switch (*profileName) {
141         case 'm':
142         case 'M':
143                 profile = TIZEN_PROFILE_MOBILE;
144                 break;
145         case 'w':
146         case 'W':
147                 profile = TIZEN_PROFILE_WEARABLE;
148                 break;
149         case 't':
150         case 'T':
151                 profile = TIZEN_PROFILE_TV;
152                 break;
153         case 'i':
154         case 'I':
155                 profile = TIZEN_PROFILE_IVI;
156                 break;
157         default: // common or unknown ==> ALL ARE COMMON.
158                 profile = TIZEN_PROFILE_COMMON;
159         }
160         free(profileName);
161
162         return profile;
163 }