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