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