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