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