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