Merge "Fix the prevent issue" into tizen_2.1
[platform/framework/native/appfw.git] / src / base / FBaseLong.cpp
1 //
2 // Open Service Platform
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                FBaseLong.cpp
20  * @brief               This is the implementation file for Long class.
21  * @see                 Number
22  */
23
24 #include <wchar.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <FBaseLong.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
31
32 namespace Tizen { namespace Base
33 {
34
35 Long::Long(long value)
36         : value(value)
37         , __pLongImpl(null)
38 {
39 }
40
41 Long::Long(const Long& value)
42         : value(value.value)
43         , __pLongImpl(null)
44 {
45 }
46
47 Long::~Long(void)
48 {
49 }
50
51 Long&
52 Long::operator =(const Long& rhs)
53 {
54         if (&rhs != this)
55         {
56                 value = rhs.value;
57         }
58         return *this;
59 }
60
61 int
62 Long::Compare(long l1, long l2)
63 {
64         return(l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
65 }
66
67 int
68 Long::CompareTo(const Long& value) const
69 {
70         return(Long::Compare(this->value, value.value));
71 }
72
73 bool
74 Long::Equals(const Object& obj) const
75 {
76         const Long* pOther = dynamic_cast <const Long*>(&obj);
77         if (pOther == null)
78         {
79                 return false;
80         }
81
82         return value == (*pOther).value;
83 }
84
85 int
86 Long::GetHashCode(void) const
87 {
88         return static_cast<int> (value);
89 }
90
91 int
92 Long::GetHashCode(long val)
93 {
94         return static_cast<int> (val);
95 }
96
97 result
98 Long::Decode(const String& s, long& ret)
99 {
100         SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
101                 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
102
103         int radix = 0;
104         int startIndex = 0;
105         int minLength = 2;
106         wchar_t* pEnd = null;
107         String str(s);
108
109         if (s[0] == L'-' || s[0] == L'+')
110         {
111                 startIndex = 1;
112                 minLength = 3;
113         }
114
115         // Find radix
116         if (s[startIndex] == L'#')
117         {
118                 radix = Character::RADIX_HEXADECIMAL;
119
120                 // Remove '#'
121                 str.Remove(startIndex, 1);
122         }
123         else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
124         {
125                 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
126                 {
127                         radix = Character::RADIX_HEXADECIMAL;
128                 }
129                 else
130                 {
131                         radix = Character::RADIX_OCTAL;
132                 }
133         }
134         else
135         {
136                 radix = Character::RADIX_DECIMAL;
137         }
138
139         errno = 0;
140         ret = wcstol(str.GetPointer(), &pEnd, radix);
141         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
142                 "[%s] Long decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
143         SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
144                 "[%s] Decoded value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
145
146         return E_SUCCESS;
147 }
148
149 result
150 Long::Parse(const String& s, long& ret)
151 {
152         int len = s.GetLength();
153         SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
154                 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
155
156         return Parse(s, Character::RADIX_DECIMAL, ret);
157 }
158
159 result
160 Long::Parse(const String& s, int radix, long& ret)
161 {
162         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
163                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
164                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
165
166         int len = s.GetLength();
167         SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
168                 GetErrorMessage(E_NUM_FORMAT));
169
170         errno = 0;
171         wchar_t* pEnd = null;
172         long tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
173         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
174                 GetErrorMessage(E_NUM_FORMAT), pEnd);
175         SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
176                 "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
177
178         ret = tmpRet;
179         return E_SUCCESS;
180 }
181
182 char
183 Long::ToChar(void) const
184 {
185         return static_cast<char> (value);
186 }
187
188 short
189 Long::ToShort(void) const
190 {
191         return static_cast<short> (value);
192 }
193
194 int
195 Long::ToInt(void) const
196 {
197         return static_cast<int> (value);
198 }
199
200 long
201 Long::ToLong(void) const
202 {
203         return value;
204 }
205
206 long long
207 Long::ToLongLong(void) const
208 {
209         return static_cast<long long> (value);
210 }
211
212 float
213 Long::ToFloat(void) const
214 {
215         return static_cast<float> (value);
216 }
217
218 double
219 Long::ToDouble(void) const
220 {
221         return static_cast<double> (value);
222 }
223
224 String
225 Long::ToString(void) const
226 {
227         return(Long::ToString(value));
228 }
229
230 String
231 Long::ToString(long value)
232 {
233         const static unsigned int LONG_LENGTH_MAX = 11;
234
235         wchar_t sValue[LONG_LENGTH_MAX + 1] = {0, };
236         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
237
238         return String(sValue);
239 }
240
241 }} //Tizen::Base