Revert "[N_SE-33612] In Decode(), add code to check if str[0] is '+' or '-'"
[platform/framework/native/appfw.git] / src / base / FBaseInt8.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FBaseInt8.cpp
20  * @brief               This is the implementation file for Int8 class.
21  * @see                 Number class
22  */
23
24 #include <wchar.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <FBaseInt8.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
31
32
33
34 namespace Tizen { namespace Base
35 {
36
37 Int8::Int8(char value)
38         : value((signed char)value)
39         , __pInt8Impl(null)
40 {
41 }
42
43 Int8::Int8(const Int8& value)
44         : value(value.value)
45         , __pInt8Impl(null)
46 {
47 }
48
49 Int8::~Int8(void)
50 {
51 }
52
53 Int8&
54 Int8::operator =(const Int8& rhs)
55 {
56         if (&rhs != this)
57         {
58                 value = rhs.value;
59         }
60         return (*this);
61 }
62
63 int
64 Int8::Compare(char ch1, char ch2)
65 {
66         return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
67 }
68
69 int
70 Int8::CompareTo(const Int8& value) const
71 {
72         return (Int8::Compare(this->value, value.value));
73 }
74
75 bool
76 Int8::Equals(const Object& obj) const
77 {
78         const Int8* pOther = dynamic_cast <const Int8*>(&obj);
79         if (pOther == null)
80         {
81                 return (false);
82         }
83
84         return (value == (*pOther).value);
85 }
86
87 int
88 Int8::GetHashCode(void) const
89 {
90         return static_cast<int> (value);
91 }
92
93 int
94 Int8::GetHashCode(char val)
95 {
96         return static_cast<int> (val);
97 }
98
99 result
100 Int8::Decode(const String& s, char& ret)
101 {
102         SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
103                 "[%s] The length of string s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
104
105         long value = 0;
106         int radix = 0;
107         int startIndex = 0;
108         int minLength = 2;
109         wchar_t* pEnd = null;
110         String str(s);
111
112         if (s[0] == L'-')
113         {
114                 startIndex = 1;
115                 minLength = 3;
116         }
117
118         // Find radix
119         if (s[startIndex] == L'#')
120         {
121                 radix = Character::RADIX_HEXADECIMAL;
122
123                 // Remove '#'
124                 str.Remove(startIndex, 1);
125         }
126         else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
127         {
128                 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
129                 {
130                         radix = Character::RADIX_HEXADECIMAL;
131                 }
132                 else
133                 {
134                         radix = Character::RADIX_OCTAL;
135                 }
136         }
137         else
138         {
139                 radix = Character::RADIX_DECIMAL;
140         }
141
142         result r = E_SUCCESS;
143
144         errno = 0;
145         value = wcstol(str.GetPointer(), &pEnd, radix);
146         SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
147                 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
148         SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
149                 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
150
151 CATCH:
152         if (value > Int8::VALUE_MAX)
153         {
154                 ret = Int8::VALUE_MAX;
155         }
156         else if (value < (signed char) Int8::VALUE_MIN)
157         {
158                 ret = (signed char) Int8::VALUE_MIN;
159         }
160         else
161         {
162                 ret = (char) value;
163         }
164
165         return r;
166 }
167
168 result
169 Int8::Parse(const String& s, char& ret)
170 {
171         int len = s.GetLength();
172         SysTryReturn(NID_BASE, (len > 0 && len < 5), E_NUM_FORMAT, E_NUM_FORMAT,
173                                 "[%s] The length of s(%ls) MUST be greater than 0 and less than 5.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
174
175         return Parse(s, Character::RADIX_DECIMAL, ret);
176 }
177
178 result
179 Int8::Parse(const String& s, int radix, char& ret)
180 {
181         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
182                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
183                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
184
185         int len = s.GetLength();
186         SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
187                 GetErrorMessage(E_NUM_FORMAT));
188
189         result r = E_SUCCESS;
190
191         errno = 0;
192         wchar_t* pEnd = null;
193         long value = wcstol(s.GetPointer(), &pEnd, radix);
194         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
195                 GetErrorMessage(E_NUM_FORMAT), pEnd);
196         SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
197                 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
198
199         if (value > Int8::VALUE_MAX)
200         {
201                 ret = Int8::VALUE_MAX;
202         }
203         else if (value < static_cast< signed char >(Int8::VALUE_MIN))
204         {
205                 ret = static_cast< signed char >(Int8::VALUE_MIN);
206         }
207         else
208         {
209                 ret = static_cast< char >(value);
210         }
211
212         return r;
213 }
214
215 char
216 Int8::ToChar(void) const
217 {
218         return static_cast<char> (value);
219 }
220
221 short
222 Int8::ToShort(void) const
223 {
224         return static_cast<short> (value);
225 }
226
227 int
228 Int8::ToInt(void) const
229 {
230         return static_cast<int> (value);
231 }
232
233 long
234 Int8::ToLong(void) const
235 {
236         return static_cast<long> (value);
237 }
238
239 long long
240 Int8::ToLongLong(void) const
241 {
242         return static_cast<long long> (value);
243 }
244
245 float
246 Int8::ToFloat(void) const
247 {
248         return static_cast<float> (value);
249 }
250
251 double
252 Int8::ToDouble(void) const
253 {
254         return static_cast<double> (value);
255 }
256
257 String
258 Int8::ToString(void) const
259 {
260         return (Int8::ToString(value));
261 }
262
263 String
264 Int8::ToString(char value)
265 {
266         const static unsigned int INT8_LENGTH_MAX = 4;
267
268         wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
269         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
270
271         return String(sValue);
272 }
273
274 }} //Tizen::Base