[3.0][Cherry-pick] Remove privacy input data in log & Remove needless log
[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 input String 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         return Parse(s, Character::RADIX_DECIMAL, ret);
152 }
153
154 result
155 Integer::Parse(const String& s, int radix, int& ret)
156 {
157         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
158                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
159                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
160
161         int len = s.GetLength();
162         SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
163                 GetErrorMessage(E_NUM_FORMAT));
164
165         errno = 0;
166         wchar_t* pEnd = null;
167         int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
168         SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT,
169                 "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
170         SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
171                 "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
172
173         ret = tmpRet;
174         return E_SUCCESS;
175 }
176
177 char
178 Integer::ToChar(void) const
179 {
180         return static_cast< char >(value);
181 }
182
183 short
184 Integer::ToShort(void) const
185 {
186         return static_cast< short >(value);
187 }
188
189 int
190 Integer::ToInt(void) const
191 {
192         return value;
193 }
194
195 long
196 Integer::ToLong(void) const
197 {
198         return static_cast< long >(value);
199 }
200
201 long long
202 Integer::ToLongLong(void) const
203 {
204         return static_cast< long long >(value);
205 }
206
207 float
208 Integer::ToFloat(void) const
209 {
210         return static_cast< float >(value);
211 }
212
213 double
214 Integer::ToDouble(void) const
215 {
216         return static_cast< double >(value);
217 }
218
219 String
220 Integer::ToString(void) const
221 {
222         return(Integer::ToString(value));
223 }
224
225 String
226 Integer::ToString(int value)
227 {
228         const static unsigned int INTEGER_LENGTH_MAX = 11;
229
230         wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
231         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
232
233         return String(sValue);
234 }
235
236 }} //Tizen::Base