Removal of compilation warnings.
[framework/web/wrt-commons.git] / modules / widget_dao / dao / config_parser_data.cpp
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  * @file        config_parser_data.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22 #include <dpl/wrt-dao-ro/config_parser_data.h>
23 #include <dpl/log/log.h>
24 #include <libxml/xmlreader.h>
25 #include <libxml/xmlstring.h>
26
27 namespace WrtDB {
28 bool IsSpace(const xmlChar* str);
29 bool CopyChar(xmlChar* out, xmlChar* in);
30
31
32 bool IsSpace(const xmlChar* str)
33 {
34     int charlen = xmlUTF8Size(str);
35
36     switch (charlen) {
37     case 1:
38         switch (*str) {
39         case 0x09:
40         case 0x0a:
41         case 0x0b:
42         case 0x0c:
43         case 0x0d:
44         case 0x20:
45             return true;
46
47         default:
48             return false;
49         }
50
51     case 2:
52         switch (*(str + 1)) {
53         case 0x85:
54         case 0xa0:
55             return true;
56
57         default:
58             return false;
59         }
60
61     case 3:
62         switch (*str) {
63         case 0xe1:
64         {
65             unsigned char c2 = *(str + 1);
66             unsigned char c3 = *(str + 2);
67             if ((c2 == 0x9a && c3 == 0x80) || (c2 == 0xa0 && c3 == 0x8e)) {
68                 return true;
69             } else {
70                 return false;
71             }
72         }
73
74         case 0xe2:
75             switch (*(str + 1)) {
76             case 0x80:
77                 switch (*(str + 2)) {
78                 case 0x80:
79                 case 0x81:
80                 case 0x82:
81                 case 0x83:
82                 case 0x84:
83                 case 0x85:
84                 case 0x86:
85                 case 0x87:
86                 case 0x88:
87                 case 0x89:
88                 case 0x8a:
89                 case 0xa8:
90                 case 0xa9:
91                 case 0xaf:
92                     return true;
93                 default:
94                     return false;
95                 }
96             case 0x81:
97                 if (*(str + 2) == 0x9f) {
98                     return true;
99                 } else {
100                     return false;
101                 }
102
103             default:
104                 return false;
105             }
106         case 0xe3:
107             if (*(str + 1) == 0x80 && *(str + 2) == 0x80) {
108                 return true;
109             } else {
110                 return false;
111             }
112
113         default:
114             return false;
115         }
116
117     default:
118         return false;
119     }
120 }
121
122 bool CopyChar(xmlChar* out,
123         xmlChar* in)
124 {
125     int size = xmlUTF8Size(in);
126     switch (size) {
127     case 6:
128         out[5] = in[5];
129     case 5:
130         out[4] = in[4];
131     case 4:
132         out[3] = in[3];
133     case 3:
134         out[2] = in[2];
135     case 2:
136         out[1] = in[1];
137     case 1:
138         out[0] = in[0];
139         return true;
140
141     default:
142         return false;
143     }
144 }
145
146 //TODO temporary fix until commits the rewrite of this functionality.
147 void NormalizeString(DPL::String& str)
148 {
149     DPL::Optional<DPL::String> opt = str;
150     NormalizeString(opt);
151     str = *opt;
152 }
153
154 void NormalizeString (DPL::Optional<DPL::String>& txt)
155 {
156     if (!!txt) {
157         std::string tmp = DPL::ToUTF8String(*txt);
158         const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str());
159         if (!xmlCheckUTF8(str)) {
160             LogError("Not valid UTF8");
161             return;
162         }
163
164         int i = 0;
165         xmlChar* c;
166         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
167             if (!IsSpace(c)) {
168                 break;
169             }
170             ++i;
171         }
172
173         xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1);
174         bool first = false;
175         xmlChar* s = tmpnew;
176         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
177             if (IsSpace(c)) {
178                 first = true;
179                 ++i;
180             } else {
181                 if (c[0] == 0x0) {
182                     break;
183                 }
184                 if (first) {
185                     xmlChar space[6] = { 0x20 };
186                     CopyChar(s, space);
187                     s += xmlUTF8Size(s);
188                     first = false;
189                 }
190                 CopyChar(s, c);
191                 s += xmlUTF8Size(s);
192                 ++i;
193             }
194         }
195         s[0] = 0x0;
196         txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew));
197         xmlFree(tmpnew);
198     }
199 }
200
201 bool ConfigParserData::Param::operator==(const Param& other) const
202 {
203     return name == other.name && value == other.value;
204 }
205
206 bool ConfigParserData::Param::operator!=(const Param& other) const
207 {
208     return name != other.name || value != other.value;
209 }
210
211 bool ConfigParserData::Param::operator >(const Param& other) const
212 {
213     if (name == other.name) {
214         return value > other.value;
215     } else {
216         return name > other.name;
217     }
218 }
219
220 bool ConfigParserData::Param::operator>=(const Param& other) const
221 {
222     if (name >= other.name) {
223         return true;
224     } else {
225         return value >= other.value;
226     }
227 }
228
229 bool ConfigParserData::Param::operator <(const Param& other) const
230 {
231     if (name == other.name) {
232         return value < other.value;
233     } else {
234         return name < other.name;
235     }
236 }
237
238 bool ConfigParserData::Param::operator<=(const Param& other) const
239 {
240     if (name <= other.name) {
241         return true;
242     } else {
243         return value <= other.value;
244     }
245 }
246
247 bool ConfigParserData::Feature::operator==(const Feature& other) const
248 {
249     return name == other.name && paramsList == other.paramsList;
250 }
251
252 bool ConfigParserData::Feature::operator!=(const Feature& other) const
253 {
254     return name != other.name || paramsList != other.paramsList;
255 }
256
257 bool ConfigParserData::Feature::operator >(const Feature& other) const
258 {
259     if (name > other.name) {
260         return true;
261     }
262     if (name < other.name) {
263         return false;
264     }
265     return paramsList > other.paramsList;
266 }
267
268 bool ConfigParserData::Feature::operator>=(const Feature& other) const
269 {
270     if (name > other.name) {
271         return true;
272     }
273     if (name < other.name) {
274         return false;
275     }
276     return paramsList >= other.paramsList;
277 }
278
279 bool ConfigParserData::Feature::operator <(const Feature& other) const
280 {
281     if (name < other.name) {
282         return true;
283     }
284     if (name > other.name) {
285         return false;
286     }
287     return paramsList < other.paramsList;
288 }
289
290 bool ConfigParserData::Feature::operator<=(const Feature& other) const
291 {
292     if (name < other.name) {
293         return true;
294     }
295     if (name > other.name) {
296         return false;
297     }
298     return paramsList <= other.paramsList;
299 }
300
301 bool ConfigParserData::Icon::operator==(const Icon& other) const
302 {
303     return src == other.src;
304 }
305
306 bool ConfigParserData::Icon::operator!=(const Icon& other) const
307 {
308     return src != other.src;
309 }
310
311 bool ConfigParserData::Icon::operator >(const Icon& other) const
312 {
313     return src > other.src;
314 }
315
316 bool ConfigParserData::Icon::operator>=(const Icon& other) const
317 {
318     return src >= other.src;
319 }
320
321 bool ConfigParserData::Icon::operator <(const Icon& other) const
322 {
323     return src < other.src;
324 }
325
326 bool ConfigParserData::Icon::operator<=(const Icon& other) const
327 {
328     return src <= other.src;
329 }
330
331 bool ConfigParserData::Preference::operator==(const Preference& other) const
332 {
333     return name == other.name;
334 }
335
336 bool ConfigParserData::Preference::operator!=(const Preference& other) const
337 {
338     return name != other.name;
339 }
340
341 bool ConfigParserData::Preference::operator >(const Preference& other) const
342 {
343     return name > other.name;
344 }
345
346 bool ConfigParserData::Preference::operator>=(const Preference& other) const
347 {
348     return name >= other.name;
349 }
350
351 bool ConfigParserData::Preference::operator <(const Preference& other) const
352 {
353     return name < other.name;
354 }
355
356 bool ConfigParserData::Preference::operator<=(const Preference& other) const
357 {
358     return name <= other.name;
359 }
360
361 bool ConfigParserData::AccessInfo::operator== (const AccessInfo& info) const
362 {
363     return m_strIRI == info.m_strIRI && m_bSubDomainAccess ==
364            info.m_bSubDomainAccess;
365 }
366
367 bool ConfigParserData::AccessInfo::operator!= (const AccessInfo& info) const
368 {
369     return m_strIRI != info.m_strIRI || m_bSubDomainAccess !=
370            info.m_bSubDomainAccess;
371 }
372
373 bool ConfigParserData::AccessInfo::operator <(const AccessInfo& info) const
374 {
375     if (m_strIRI == info.m_strIRI) {
376         return m_bSubDomainAccess < info.m_bSubDomainAccess;
377     } else {
378         return m_strIRI < info.m_strIRI;
379     }
380 }
381
382 bool ConfigParserData::Setting::operator==(const Setting& other) const
383 {
384     return m_name == other.m_name &&
385         m_value == other.m_value;
386 }
387
388 bool ConfigParserData::Setting::operator!=(const Setting& other) const
389 {
390     return m_name != other.m_name ||
391         m_value != other.m_value;
392 }
393
394 bool ConfigParserData::Setting::operator >(const Setting& other) const
395 {
396     return m_name > other.m_name;
397 }
398
399 bool ConfigParserData::Setting::operator>=(const Setting& other) const
400 {
401     return m_name >= other.m_name;
402 }
403
404 bool ConfigParserData::Setting::operator <(const Setting& other) const
405 {
406     return m_name < other.m_name;
407 }
408
409 bool ConfigParserData::Setting::operator<=(const Setting& other) const
410 {
411     return m_name <= other.m_name;
412 }
413
414 bool ConfigParserData::ServiceInfo::operator== (const ServiceInfo& info) const
415 {
416     return m_src == info.m_src &&
417     m_operation == info.m_operation &&
418     m_scheme == info.m_scheme &&
419     m_mime == info.m_mime;
420 }
421
422 bool ConfigParserData::ServiceInfo::operator!= (const ServiceInfo& info) const
423 {
424     return m_src != info.m_src &&
425     m_operation != info.m_operation &&
426     m_scheme != info.m_scheme &&
427     m_mime != info.m_mime;
428 }
429 } // namespace WrtDB