5f1b819e2e8d0d18615bda0e573c871b84992ecf
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactUtility.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        ContactUtility.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       
23  */
24
25 #include "ContactUtility.h"
26
27 #include <time.h>
28 #include <string>
29 #include <sstream>
30 #include <iomanip>
31 #include <Commons/Exception.h>
32 #include <Commons/Regex.h>
33
34 namespace DeviceAPI {
35 namespace Contact {
36
37 using namespace std;
38 using namespace WrtDeviceApis::Commons;
39
40 ContactUtility::ContactUtility()
41 {
42 }
43
44 ContactUtility::~ContactUtility()
45 {
46 }
47
48 bool ContactUtility::strToBool(const char *str)
49 {
50         if(str == NULL)
51                 return false;
52
53         string boolStr(str);
54
55         if (boolStr == "true" || boolStr == "TRUE")
56                 return true;
57
58         return false;
59 }
60
61 int ContactUtility::strToInt(const char* str)
62 {
63         if(str == NULL)
64                 return 0;
65
66         int result = 0;
67
68         istringstream iss(str);
69         iss >> result;
70
71         return result;
72 }
73
74 int ContactUtility::strToInt(const string &str)
75 {
76         int result = 0;
77
78         istringstream iss(str);
79         iss >> result;
80
81         return result;
82 }
83
84 string ContactUtility::intToStr(const int value)
85 {
86         stringstream oss;
87         oss << value;
88         return oss.str();
89 }
90
91 int ContactUtility::toDateDbInt(const tm &date)
92 {
93         return strToInt(toDateDbStr(date));
94 }
95
96 string ContactUtility::toDateDbStr(const tm &date)
97 {
98         stringstream ss;
99         ss << setfill('0') << setiosflags(ios::right) << setw(4) << (date.tm_year + 1900);
100         ss << setfill('0') << setiosflags(ios::right) << setw(2) << (date.tm_mon + 1);
101         ss << setfill('0') << setiosflags(ios::right) << setw(2) << date.tm_mday;
102
103         return ss.str();
104 }
105
106 tm ContactUtility::toDateTmFromDateDbInt(int arg)
107 {
108         tm date;
109         memset(&date, 0, sizeof(tm));
110
111         if(arg < 0) {
112                 ThrowMsg(InvalidArgumentException, "Invalid Date type");
113         }
114
115         int section0to3 = arg / 10000;
116         int section4to5 = (arg / 100) % 100;
117         int section6to7 = arg % 100;
118
119         date.tm_year = section0to3 - 1900;
120         date.tm_mon = section4to5 - 1;
121         date.tm_mday = section6to7;
122
123         return date;
124 }
125
126 tm ContactUtility::toDateTmFromDateDbStr(const char* arg)
127 {
128         if(arg == NULL) {
129                 ThrowMsg(InvalidArgumentException, "Invalid Date type");
130         }
131
132         int dateDb;
133         stringstream ss(arg);
134         ss >> dateDb;
135
136         return toDateTmFromDateDbInt(dateDb);
137 }
138
139 string ContactUtility::convertUriToPath(const string str)
140 {
141         string result;
142
143         if(validate("^file:///[\x20-\x7E]+$", str, VALIDATE_MATCH_CASELESS))
144                 result = str.substr(string("file://").size());
145         else if(validate("^/[\x20-\x7E]+$", str, VALIDATE_MATCH_CASELESS))
146                 result = str;
147
148         return result;
149 }
150
151 string ContactUtility::convertPathToUri(const string str)
152 {
153         string result;
154
155         if(validate("^/[\x20-\x7E]+$", str, VALIDATE_MATCH_CASELESS))
156                 result = "file://" + str;
157         else if(validate("^file:///[\x20-\x7E]+$", str, VALIDATE_MATCH_CASELESS))
158                 result = str;
159
160         return result;
161 }
162
163 bool ContactUtility::checkStrIsInt(const std::string &str)
164 {
165         return validate("^[\\-]?[0-9]+$", str, VALIDATE_MATCH_CASELESS);
166 }
167
168 bool ContactUtility::checkStrIsUInt(const std::string &str)
169 {
170         return validate("^[0-9]+$", str, VALIDATE_MATCH_CASELESS);
171 }
172
173 } // Contact
174 } // DeviceAPI