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