Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / xml_parser_utils.cpp
1 /*
2  * Copyright (c) 2012 - 2014 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 "xml_parser_utils.h"
19 #include <string.h>
20 #include <assert.h>
21 #include <sstream>
22
23 using namespace std;
24
25 int
26 get_content_int(const xmlNodePtr cur_node) {
27     assert(cur_node != NULL);
28
29     int num = -1;
30
31     xmlChar* key = xmlNodeGetContent(cur_node);
32     if (key!= NULL) {
33         if (0 == strcmp("NOT_USED", (const char*)key)) {
34             num = -1;
35         } else {
36             num = atoi((const char*)key);
37         }
38         xmlFree(key);
39     }
40     return num;
41 }
42
43 bool
44 get_content_bool(const xmlNodePtr cur_node) {
45     assert(cur_node != NULL);
46
47     bool bVal = false;
48
49     xmlChar* key = xmlNodeGetContent(cur_node);
50
51     if (key != NULL) {
52         if (0 == xmlStrcmp(key, (const xmlChar*)"true")) {
53             bVal = true;
54         }
55         xmlFree(key);
56     }
57     return bVal;
58 }
59
60 bool
61 equal_prop(const xmlNodePtr cur_node, const char* prop, const char* str) {
62     bool val = false;
63
64     xmlChar* key = xmlGetProp(cur_node, (const xmlChar*)prop);
65
66     if (NULL != key) {
67         if (0 == xmlStrcmp(key, (const xmlChar*)str)) {
68             val = true;
69         }
70         xmlFree(key);
71     }
72
73     return val;
74 }
75
76 bool
77 get_prop_bool(const xmlNodePtr cur_node, const char* prop, sclboolean *ret) {
78     assert(cur_node != NULL);
79
80     bool succeeded = false;
81
82     xmlChar* key = xmlGetProp(cur_node, (const xmlChar*)prop);
83
84     if (ret && key != NULL) {
85         succeeded = true;
86         if (0 == xmlStrcmp(key, (const xmlChar*)"true")) {
87             *ret = true;
88         } else {
89             *ret = false;
90         }
91         xmlFree(key);
92     }
93
94     return succeeded;
95 }
96
97 int
98 dex_string_to_int(const char* str) {
99     assert(str != NULL);
100
101     int val = -1;
102     stringstream convertor(str);
103
104     convertor >> hex >> val;
105     if (convertor.fail() == true) {
106         val = -1;
107     }
108
109     return val;
110 }
111
112 int
113 get_content_dex_string_int(const xmlNodePtr cur_node) {
114     assert(NULL != cur_node);
115
116     int val = -1;
117
118     xmlChar* key = xmlNodeGetContent(cur_node);
119
120     if (key != NULL) {
121         val = dex_string_to_int((const char*)key);
122         xmlFree(key);
123     }
124
125     return val;
126 }
127