2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief This is the implementation file for Long class.
27 #include <FBaseLong.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
32 namespace Tizen { namespace Base
35 Long::Long(long value)
41 Long::Long(const Long& value)
52 Long::operator =(const Long& rhs)
62 Long::Compare(long l1, long l2)
64 return(l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
68 Long::CompareTo(const Long& value) const
70 return(Long::Compare(this->value, value.value));
74 Long::Equals(const Object& obj) const
76 const Long* pOther = dynamic_cast <const Long*>(&obj);
82 return value == (*pOther).value;
86 Long::GetHashCode(void) const
88 return static_cast<int> (value);
92 Long::GetHashCode(long val)
94 return static_cast<int> (val);
98 Long::Decode(const String& s, long& ret)
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));
106 wchar_t* pEnd = null;
109 if (s[0] == L'-' || s[0] == L'+')
116 if (s[startIndex] == L'#')
118 radix = Character::RADIX_HEXADECIMAL;
121 str.Remove(startIndex, 1);
123 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
125 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
127 radix = Character::RADIX_HEXADECIMAL;
131 radix = Character::RADIX_OCTAL;
136 radix = Character::RADIX_DECIMAL;
140 ret = wcstol(str.GetPointer(), &pEnd, radix);
141 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
142 "[%s] Long decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
143 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
144 "[%s] Decoded value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
150 Long::Parse(const String& s, long& ret)
152 int len = s.GetLength();
153 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
154 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
156 return Parse(s, Character::RADIX_DECIMAL, ret);
160 Long::Parse(const String& s, int radix, long& ret)
162 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
163 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
164 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
166 int len = s.GetLength();
167 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
168 GetErrorMessage(E_NUM_FORMAT));
171 wchar_t* pEnd = null;
172 long tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
173 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
174 GetErrorMessage(E_NUM_FORMAT), pEnd);
175 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
176 "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
183 Long::ToChar(void) const
185 return static_cast<char> (value);
189 Long::ToShort(void) const
191 return static_cast<short> (value);
195 Long::ToInt(void) const
197 return static_cast<int> (value);
201 Long::ToLong(void) const
207 Long::ToLongLong(void) const
209 return static_cast<long long> (value);
213 Long::ToFloat(void) const
215 return static_cast<float> (value);
219 Long::ToDouble(void) const
221 return static_cast<double> (value);
225 Long::ToString(void) const
227 return(Long::ToString(value));
231 Long::ToString(long value)
233 const static unsigned int LONG_LENGTH_MAX = 11;
235 wchar_t sValue[LONG_LENGTH_MAX + 1] = {0, };
236 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
238 return String(sValue);