11093ccb01aaed1300e6de54c0a303003dfe926b
[platform/core/system/libsyscommon.git] / src / libcommon / ini-parser.c
1 /*
2  * libsyscommon
3  *
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include "ini-parser.h"
23
24 #include "shared/log.h"
25
26 #define MAX_LINE        512
27 #define MAX_SECTION     64
28 #define WHITESPACE      " \t"
29 #define NEWLINE         "\n\r"
30 #define COMMENT         '#'
31
32 static inline char *trim_str(char *s)
33 {
34         char *t;
35         /* left trim */
36         s += strspn(s, WHITESPACE);
37
38         /* right trim */
39         for (t = strchr(s, 0); t > s; t--)
40                 if (!strchr(WHITESPACE, t[-1]))
41                         break;
42         *t = 0;
43         return s;
44 }
45
46 int config_parse(const char *file_name, int cb(struct parse_result *result,
47                         void *user_data), void *user_data)
48 {
49         FILE *f = NULL;
50         struct parse_result result;
51         /* use stack for parsing */
52         char line[MAX_LINE];
53         char section[MAX_SECTION];
54         char *start, *end, *name, *value;
55         int lineno = 0, ret = 0;
56
57         if (!file_name || !cb) {
58                 ret = -EINVAL;
59                 goto error;
60         }
61
62         /* open conf file */
63         f = fopen(file_name, "r");
64         if (!f) {
65                 _E("Failed to open file '%s'.", file_name);
66                 ret = -EIO;
67                 goto error;
68         }
69
70         /* parsing line by line */
71         while (fgets(line, MAX_LINE, f) != NULL) {
72                 lineno++;
73
74                 start = line;
75                 start[strcspn(start, NEWLINE)] = '\0';
76                 start = trim_str(start);
77
78                 if (*start == COMMENT) {
79                         continue;
80                 } else if (*start == '[') {
81                         /* parse section */
82                         end = strchr(start, ']');
83                         if (!end || *end != ']') {
84                                 ret = -EBADMSG;
85                                 goto error;
86                         }
87
88                         *end = '\0';
89                         strncpy(section, start + 1, sizeof(section)-1);
90                         section[MAX_SECTION-1] = '\0';
91                 } else if (*start) {
92                         /* parse name & value */
93                         end = strchr(start, '=');
94                         if (!end || *end != '=') {
95                                 ret = -EBADMSG;
96                                 goto error;
97                         }
98                         *end = '\0';
99                         name = trim_str(start);
100                         value = trim_str(end + 1);
101                         end = strchr(value, COMMENT);
102                         if (end && *end == COMMENT) {
103                                 *end = '\0';
104                                 value = trim_str(value);
105                         }
106
107                         result.section = section;
108                         result.name = name;
109                         result.value = value;
110                         /* callback with parse result */
111                         ret = cb(&result, user_data);
112                         if (ret < 0) {
113                                 ret = -EBADMSG;
114                                 goto error;
115                         }
116                 }
117         }
118         _D("Success to load '%s'.", file_name);
119         fclose(f);
120         return 0;
121
122 error:
123         if (f)
124                 fclose(f);
125         _E("Failed to read '%s': %d", file_name, lineno);
126         return ret;
127 }
128