Initialize Tizen 2.3
[framework/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))) {
129                                         if (!strncmp(id, id_field, strlen(id_field))) {
130                                                 if (!string) {
131                                                         free(id);
132                                                         continue;
133                                                 }
134
135                                                 *value = strdup(string);
136                                                 free(id);
137                                                 free(string);
138                                                 xmlFreeDoc(doc);
139                                                 if (*value == NULL) {
140                                                                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
141                                                                 xmlFreeDoc(doc);
142                                                                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
143                                                 }
144                                                 return SYSTEM_INFO_ERROR_NONE;
145                                         }
146                                 }
147                                 free(id);
148                                 free(string);
149                         }
150                 }
151         }
152
153         if (*value == NULL && default_node) {
154                 cur = default_node->xmlChildrenNode;
155
156                 for (cur_node = cur; cur_node; cur_node = cur_node->next) {
157                         if (cur_node->type == XML_ELEMENT_NODE) {
158                                 id = (char *)xmlGetProp(cur_node, (const xmlChar*)"id");
159                                 string = (char *) xmlGetProp(cur_node, (const xmlChar*)"string");
160
161                                 if (!strncmp(id, id_field, strlen(id))) {
162                                         if (!strncmp(id, id_field, strlen(id_field))) {
163                                                 if (!string) {
164                                                         free(id);
165                                                         continue;
166                                                 }
167
168                                                 *value = strdup(string);
169                                                 free(id);
170                                                 free(string);
171                                                 xmlFreeDoc(doc);
172                                                 if (*value == NULL) {
173                                                                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
174                                                                 xmlFreeDoc(doc);
175                                                                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
176                                                 }
177                                                 return SYSTEM_INFO_ERROR_NONE;
178                                         }
179                                 }
180                                 free(id);
181                                 free(string);
182                         }
183                 }
184         }
185
186         LOGE("cannot find %s field from %s file!!!", id_field, xml_file_path);
187         xmlFreeDoc(doc);
188         return SYSTEM_INFO_ERROR_IO_ERROR;
189 }
190
191 int system_info_get_value_from_config_xml(char *feature_tag, const char *name_field, char *type_field, char **value)
192 {
193         xmlDocPtr doc = NULL;
194         xmlNodePtr cur = NULL;
195         xmlNodePtr model_node = NULL;
196         xmlNode *cur_node = NULL;
197         char *name = NULL;
198         char *type = NULL;
199         char *string = NULL;
200
201         doc = xmlParseFile(CONFIG_FILE_PATH);
202
203         if (doc == NULL) {
204                 LOGE("cannot file open %s file!!!", CONFIG_FILE_PATH);
205                 return SYSTEM_INFO_ERROR_IO_ERROR;
206         }
207
208         cur = xmlDocGetRootElement(doc);
209         if (cur == NULL) {
210                 LOGE("empty document %s file!!!", CONFIG_FILE_PATH);
211                 xmlFreeDoc(doc);
212                 return SYSTEM_INFO_ERROR_IO_ERROR;
213         }
214
215         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
216                 if (!xmlStrcmp(cur->name, (const xmlChar*)MODEL_CONFIG_TAG))
217                         break;
218         }
219
220         if (cur == NULL) {
221                 LOGE("cannot find %s root element file!!!", MODEL_CONFIG_TAG);
222                 xmlFreeDoc(doc);
223                 return SYSTEM_INFO_ERROR_IO_ERROR;
224         }
225
226         cur = cur->xmlChildrenNode;
227
228         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
229                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)feature_tag))
230                         model_node = cur_node;
231         }
232
233         if (model_node == NULL) {
234                 LOGE("cannot find %s field from %s file!!!", name_field, CONFIG_FILE_PATH);
235                 xmlFreeDoc(doc);
236                 return SYSTEM_INFO_ERROR_IO_ERROR;
237         }
238
239         if (model_node) {
240                 cur = model_node->xmlChildrenNode;
241
242                 for (cur_node = cur; cur_node; cur_node = cur_node->next) {
243                         if (cur_node->type == XML_ELEMENT_NODE) {
244                                 name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
245                                 type = (char *)xmlGetProp(cur_node, (const xmlChar*)"type");
246
247                                 if (!strncmp(name, name_field, strlen(name))) {
248                                         if (!strncmp(name, name_field, strlen(name_field))) {
249                                                 if (strncmp(type, type_field, strlen(type_field))) {
250                                                         LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", SYSTEM_INFO_ERROR_INVALID_PARAMETER);
251                                                         free(name);
252                                                         free(type);
253                                                         xmlFreeDoc(doc);
254                                                         return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
255                                                 }
256                                                 string = (char *)xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
257                                                 if (string) {
258                                                         *value = strdup(string);
259                                                         free(name);
260                                                         free(type);
261                                                         free(string);
262                                                         break;
263                                                 }
264                                         }
265                                 }
266                                 free(name);
267                                 free(type);
268                         }
269                 }
270         }
271
272         if (!cur_node) {
273                 LOGE("cannot find %s field from %s file!!!", name_field, CONFIG_FILE_PATH);
274                 xmlFreeDoc(doc);
275                 return SYSTEM_INFO_ERROR_IO_ERROR;
276         }
277
278         if (*value == NULL) {
279                 LOGE("OUT_OF_MEMORY(0x%08x)", SYSTEM_INFO_ERROR_OUT_OF_MEMORY);
280                 xmlFreeDoc(doc);
281                 return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
282         }
283
284         xmlFreeDoc(doc);
285         return SYSTEM_INFO_ERROR_NONE;
286 }