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