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