88a257a1496a222daf6e80c528f5e6ea65e2a482
[platform/core/api/system-info.git] / src / system_info_parse.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <dlog.h>
23
24 #include <system_info.h>
25 #include <system_info_private.h>
26
27 #include <iniparser.h>
28
29 #include <libxml/xmlmemory.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "CAPI_SYSTEM_INFO"
38
39 #define MODEL_CONFIG_TAG "model-config"
40
41 int system_info_ini_get_string(char *ini_file, char *key, char **output)
42 {
43         dictionary      *ini;
44         char *str;
45         char *tmp;
46
47         ini = iniparser_load(ini_file);
48
49         if (ini == NULL) {
50                 LOGE("cannot file open %s file!!!", ini_file);
51                 return SYSTEM_INFO_ERROR_IO_ERROR;
52         }
53
54         str = iniparser_getstr(ini, key);
55
56         if (str == NULL) {
57                 LOGE("NOT found %s(0x%08x)", key, SYSTEM_INFO_ERROR_IO_ERROR);
58                 iniparser_freedict(ini);
59                 return SYSTEM_INFO_ERROR_IO_ERROR;
60         }
61
62         tmp = strdup(str);
63
64         if (tmp == NULL) {
65                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
66                 iniparser_freedict(ini);
67                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
68         }
69
70         *output = tmp;
71         iniparser_freedict(ini);
72
73         return SYSTEM_INFO_ERROR_NONE;
74 }
75
76 int system_info_get_value_from_xml(char *xml_file_path, char *model, char *id_field, char **value)
77 {
78         xmlDocPtr doc = NULL;
79         xmlNodePtr cur = NULL;
80         xmlNodePtr default_node = NULL;
81         xmlNodePtr model_node = NULL;
82         xmlNode *cur_node = NULL;
83         char *id = NULL;
84         char *string = NULL;
85
86         doc = xmlParseFile(xml_file_path);
87
88         if (doc == NULL) {
89                 LOGE("cannot file open %s file!!!", xml_file_path);
90                 return SYSTEM_INFO_ERROR_IO_ERROR;
91         }
92
93         cur = xmlDocGetRootElement(doc);
94         if (cur == NULL) {
95                 LOGE("empty document %s file!!!", xml_file_path);
96                 xmlFreeDoc(doc);
97                 return SYSTEM_INFO_ERROR_IO_ERROR;
98         }
99
100         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
101                 if (!xmlStrcmp(cur->name, (const xmlChar*)"sys-info"))
102                         break;
103         }
104
105         if (cur == NULL) {
106                 LOGE("cannot find %s root element file!!!", "sys-info");
107                 xmlFreeDoc(doc);
108                 return SYSTEM_INFO_ERROR_IO_ERROR;
109         }
110
111         cur = cur->xmlChildrenNode;
112
113         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
114                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)"default"))
115                         default_node = cur_node;
116                 if (strcmp(model, "default") && !xmlStrcmp(cur_node->name, (const xmlChar*)model))
117                         model_node = cur_node;
118         }
119
120         if (model_node) {
121                 cur = model_node->xmlChildrenNode;
122
123                 for (cur_node = cur; cur_node; cur_node = cur_node->next) {
124                         if (cur_node->type == XML_ELEMENT_NODE) {
125                                 id = (char *)xmlGetProp(cur_node, (const xmlChar*)"id");
126                                 string = (char *) xmlGetProp(cur_node, (const xmlChar*)"string");
127
128                                 if (!strncmp(id, id_field, strlen(id_field))) {
129                                         if (!string) {
130                                                 free(id);
131                                                 continue;
132                                         }
133
134                                                 *value = strdup(string);
135                                         free(id);
136                                         free(string);
137                                         xmlFreeDoc(doc);
138                                         if (*value == NULL) {
139                                                         LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
140                                                         xmlFreeDoc(doc);
141                                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
142                                         }
143                                         return SYSTEM_INFO_ERROR_NONE;
144                                 }
145                                 free(id);
146                                 free(string);
147                         }
148                 }
149         }
150
151         if (*value == NULL && default_node) {
152                 cur = default_node->xmlChildrenNode;
153
154                 for (cur_node = cur; cur_node; cur_node = cur_node->next) {
155                         if (cur_node->type == XML_ELEMENT_NODE) {
156                                 id = (char *)xmlGetProp(cur_node, (const xmlChar*)"id");
157                                 string = (char *) xmlGetProp(cur_node, (const xmlChar*)"string");
158
159                                 if (!strncmp(id, id_field, strlen(id_field))) {
160                                         if (!string) {
161                                                 free(id);
162                                                 continue;
163                                         }
164
165                                                 *value = strdup(string);
166                                                 free(id);
167                                                 free(string);
168                                         xmlFreeDoc(doc);
169                                         if (*value == NULL) {
170                                                         LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
171                                                         xmlFreeDoc(doc);
172                                                         return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
173                                         }
174                                         return SYSTEM_INFO_ERROR_NONE;
175                                 }
176                                 free(id);
177                                 free(string);
178                         }
179                 }
180         }
181
182                 LOGE("cannot find %s field from %s file!!!", id_field, xml_file_path);
183                 xmlFreeDoc(doc);
184                 return SYSTEM_INFO_ERROR_IO_ERROR;
185         }
186
187 int system_info_get_value_from_config_xml(char *feature_tag, const char *name_field, char *type_field, char **value)
188 {
189         xmlDocPtr doc = NULL;
190         xmlNodePtr cur = NULL;
191         xmlNodePtr model_node = NULL;
192         xmlNode *cur_node = NULL;
193         char *name = NULL;
194         char *type = NULL;
195         char *string = NULL;
196
197         doc = xmlParseFile(CONFIG_FILE_PATH);
198
199         if (doc == NULL) {
200                 LOGE("cannot file open %s file!!!", CONFIG_FILE_PATH);
201                 return SYSTEM_INFO_ERROR_IO_ERROR;
202         }
203
204         cur = xmlDocGetRootElement(doc);
205         if (cur == NULL) {
206                 LOGE("empty document %s file!!!", CONFIG_FILE_PATH);
207                 xmlFreeDoc(doc);
208                 return SYSTEM_INFO_ERROR_IO_ERROR;
209         }
210
211         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
212                 if (!xmlStrcmp(cur->name, (const xmlChar*)MODEL_CONFIG_TAG))
213                         break;
214         }
215
216         if (cur == NULL) {
217                 LOGE("cannot find %s root element file!!!", MODEL_CONFIG_TAG);
218                 xmlFreeDoc(doc);
219                 return SYSTEM_INFO_ERROR_IO_ERROR;
220         }
221
222         cur = cur->xmlChildrenNode;
223
224         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
225                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)feature_tag))
226                         model_node = cur_node;
227         }
228
229         if (model_node == NULL) {
230                 LOGE("cannot find %s field from %s file!!!", name_field, CONFIG_FILE_PATH);
231                 xmlFreeDoc(doc);
232                 return SYSTEM_INFO_ERROR_IO_ERROR;
233         }
234
235         if (model_node) {
236                 cur = model_node->xmlChildrenNode;
237
238                 for (cur_node = cur; cur_node; cur_node = cur_node->next) {
239                         if (cur_node->type == XML_ELEMENT_NODE) {
240                                 name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
241                                 type = (char *)xmlGetProp(cur_node, (const xmlChar*)"type");
242
243                                 if (!strncmp(name, name_field, strlen(name_field))) {
244                                         if (strncmp(type, type_field, strlen(type_field))) {
245                                                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", SYSTEM_INFO_ERROR_INVALID_PARAMETER);
246                                                 free(name);
247                                                 free(type);
248                                                 xmlFreeDoc(doc);
249                                                 return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
250                                         }
251                                         string = (char *)xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
252                                         if (string) {
253                                                 *value = strdup(string);
254                                                 free(name);
255                                                 free(type);
256                                                 free(string);
257                                                 break;
258                                         }
259                                 }
260                                 free(name);
261                                 free(type);
262                         }
263                 }
264         }
265
266         if (!cur_node) {
267                 LOGE("cannot find %s field from %s file!!!", name_field, CONFIG_FILE_PATH);
268                 xmlFreeDoc(doc);
269                 return SYSTEM_INFO_ERROR_IO_ERROR;
270         }
271
272         if (*value == NULL) {
273                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
274                 xmlFreeDoc(doc);
275                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
276         }
277
278         xmlFreeDoc(doc);
279         return SYSTEM_INFO_ERROR_NONE;
280 }