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