2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <system_info.h>
25 #define MAX_SECTION 64
26 #define WHITESPACE " \t"
27 #define NEWLINE "\n\r"
30 static inline char *trim_str(char *s)
34 s += strspn(s, WHITESPACE);
37 for (t = strchr(s, 0); t > s; t--)
38 if (!strchr(WHITESPACE, t[-1]))
44 int config_parse(const char *file_name, int cb(struct parse_result *result,
45 void *user_data), void *user_data)
48 struct parse_result result;
49 /* use stack for parsing */
51 char section[MAX_SECTION];
52 char *start, *end, *name, *value;
53 int lineno = 0, ret = 0;
55 if (!file_name || !cb) {
61 f = fopen(file_name, "r");
63 if (errno == ENOENT) {
64 _I("There is no power config file");
68 _E("Failed to open file %s", file_name);
73 /* parsing line by line */
74 while (fgets(line, MAX_LINE, f) != NULL) {
78 start[strcspn(start, NEWLINE)] = '\0';
79 start = trim_str(start);
81 if (*start == COMMENT) {
83 } else if (*start == '[') {
85 end = strchr(start, ']');
86 if (!end || *end != ']') {
92 strncpy(section, start + 1, sizeof(section));
93 section[MAX_SECTION-1] = '\0';
95 /* parse name & value */
96 end = strchr(start, '=');
97 if (!end || *end != '=') {
102 name = trim_str(start);
103 value = trim_str(end + 1);
104 end = strchr(value, COMMENT);
105 if (end && *end == COMMENT) {
107 value = trim_str(value);
110 result.section = section;
112 result.value = value;
113 /* callback with parse result */
114 ret = cb(&result, user_data);
121 _D("Success to load %s", file_name);
128 _E("Failed to read %s:%d!", file_name, lineno);
132 tizen_profile_t _get_tizen_profile()
134 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
135 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
139 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
140 switch (*profileName) {
143 profile = TIZEN_PROFILE_MOBILE;
147 profile = TIZEN_PROFILE_WEARABLE;
151 profile = TIZEN_PROFILE_TV;
155 profile = TIZEN_PROFILE_IVI;
157 default: // common or unknown ==> ALL ARE COMMON.
158 profile = TIZEN_PROFILE_COMMON;