[2.2.1] Remove privacy input data in log & Remove needless log
[platform/framework/native/appfw.git] / src / base / FBaseInt8.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                FBaseInt8.cpp
19  * @brief               This is the implementation file for Int8 class.
20  * @see                 Number class
21  */
22 #include <wchar.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <FBaseInt8.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
29
30 namespace Tizen { namespace Base
31 {
32
33 Int8::Int8(char value)
34         : value((signed char)value)
35         , __pInt8Impl(null)
36 {
37 }
38
39 Int8::Int8(const Int8& value)
40         : value(value.value)
41         , __pInt8Impl(null)
42 {
43 }
44
45 Int8::~Int8(void)
46 {
47 }
48
49 Int8&
50 Int8::operator =(const Int8& rhs)
51 {
52         if (&rhs != this)
53         {
54                 value = rhs.value;
55         }
56         return (*this);
57 }
58
59 int
60 Int8::Compare(char ch1, char ch2)
61 {
62         return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
63 }
64
65 int
66 Int8::CompareTo(const Int8& value) const
67 {
68         return (Int8::Compare(this->value, value.value));
69 }
70
71 bool
72 Int8::Equals(const Object& obj) const
73 {
74         const Int8* pOther = dynamic_cast< const Int8* >(&obj);
75         if (pOther == null)
76         {
77                 return (false);
78         }
79
80         return (value == (*pOther).value);
81 }
82
83 int
84 Int8::GetHashCode(void) const
85 {
86         return static_cast< int >(value);
87 }
88
89 int
90 Int8::GetHashCode(char val)
91 {
92         return static_cast< int >(val);
93 }
94
95 result
96 Int8::Decode(const String& s, char& 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         long value = 0;
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         result r = E_SUCCESS;
139
140         errno = 0;
141         value = wcstol(str.GetPointer(), &pEnd, radix);
142         SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
143                 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
144         SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
145                 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
146
147 CATCH:
148         if (value > Int8::VALUE_MAX)
149         {
150                 ret = Int8::VALUE_MAX;
151         }
152         else if (value < (signed char) Int8::VALUE_MIN)
153         {
154                 ret = (signed char) Int8::VALUE_MIN;
155         }
156         else
157         {
158                 ret = (char) value;
159         }
160
161         return r;
162 }
163
164 result
165 Int8::Parse(const String& s, char& ret)
166 {
167         return Parse(s, Character::RADIX_DECIMAL, ret);
168 }
169
170 result
171 Int8::Parse(const String& s, int radix, char& ret)
172 {
173         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
174                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
175                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
176
177         int len = s.GetLength();
178         SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
179                 GetErrorMessage(E_NUM_FORMAT));
180
181         errno = 0;
182         wchar_t* pEnd = null;
183         long value = wcstol(s.GetPointer(), &pEnd, radix);
184         SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
185                 GetErrorMessage(E_NUM_FORMAT), pEnd);
186         SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
187                 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
188
189         if (value > Int8::VALUE_MAX)
190         {
191                 ret = Int8::VALUE_MAX;
192         }
193         else if (value < static_cast< signed char >(Int8::VALUE_MIN))
194         {
195                 ret = static_cast< signed char >(Int8::VALUE_MIN);
196         }
197         else
198         {
199                 ret = static_cast< char >(value);
200         }
201
202         return E_SUCCESS;
203 }
204
205 char
206 Int8::ToChar(void) const
207 {
208         return static_cast< char >(value);
209 }
210
211 short
212 Int8::ToShort(void) const
213 {
214         return static_cast< short >(value);
215 }
216
217 int
218 Int8::ToInt(void) const
219 {
220         return static_cast< int >(value);
221 }
222
223 long
224 Int8::ToLong(void) const
225 {
226         return static_cast< long >(value);
227 }
228
229 long long
230 Int8::ToLongLong(void) const
231 {
232         return static_cast< long long >(value);
233 }
234
235 float
236 Int8::ToFloat(void) const
237 {
238         return static_cast< float >(value);
239 }
240
241 double
242 Int8::ToDouble(void) const
243 {
244         return static_cast< double >(value);
245 }
246
247 String
248 Int8::ToString(void) const
249 {
250         return (Int8::ToString(value));
251 }
252
253 String
254 Int8::ToString(char value)
255 {
256         const static unsigned int INT8_LENGTH_MAX = 4;
257
258         wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
259         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
260
261         return String(sValue);
262 }
263
264 }} //Tizen::Base