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