[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[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 s 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         int len = s.GetLength();
174         SysTryReturn(NID_BASE, (len > 0 && len < 7), E_NUM_FORMAT, E_NUM_FORMAT,
175                 "[%s] The length of s(%ls) MUST be greater than 0 and less than 7.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
176
177         return Parse(s, Character::RADIX_DECIMAL, ret);
178 }
179
180 result
181 Short::Parse(const String& s, int radix, short& ret)
182 {
183         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
184                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
185                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
186
187         int len = s.GetLength();
188         SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
189                 GetErrorMessage(E_NUM_FORMAT));
190
191         result r = E_SUCCESS;
192
193         errno = 0;
194         wchar_t* pEnd = null;
195         long value = wcstol(s.GetPointer(), &pEnd, radix);
196         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
197                 "[%s] Short parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
198         SysTryReturn(NID_BASE, !(value > Short::VALUE_MAX || value < Short::VALUE_MIN) || (errno != 0), E_NUM_FORMAT,
199                 E_NUM_FORMAT, "[%s] Parsed value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
200
201         if (value > Short::VALUE_MAX)
202         {
203                 ret = Short::VALUE_MAX;
204         }
205         else if (value < Short::VALUE_MIN)
206         {
207                 ret = Short::VALUE_MIN;
208         }
209         else
210         {
211                 ret = static_cast< short >(value);
212         }
213
214         return r;
215 }
216
217 char
218 Short::ToChar(void) const
219 {
220         return static_cast< char >(value);
221 }
222
223 short
224 Short::ToShort(void) const
225 {
226         return value;
227 }
228
229 int
230 Short::ToInt(void) const
231 {
232         return static_cast< int >(value);
233 }
234
235 long
236 Short::ToLong(void) const
237 {
238         return static_cast< long >(value);
239 }
240
241 long long
242 Short::ToLongLong(void) const
243 {
244         return static_cast< long long >(value);
245 }
246
247 float
248 Short::ToFloat(void) const
249 {
250         return static_cast< float >(value);
251 }
252
253 double
254 Short::ToDouble(void) const
255 {
256         return static_cast< double >(value);
257 }
258
259 String
260 Short::ToString(void) const
261 {
262         return(Short::ToString(value));
263 }
264
265 String
266 Short::ToString(short value)
267 {
268         const static unsigned int SHORT_LENGTH_MAX = 6;
269
270         wchar_t sValue[SHORT_LENGTH_MAX + 1] = {0, };
271         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
272
273         return String(sValue);
274 }
275
276 }} //Tizen::Base