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));
104 wchar_t* pEnd = null;
110 radix = Character::RADIX_HEXADECIMAL;
115 else if (s[0] == L'0' && (s.GetLength() >= 2))
117 if (s[1] == L'x' || s[1] == L'X')
119 radix = Character::RADIX_HEXADECIMAL;
123 radix = Character::RADIX_OCTAL;
128 radix = Character::RADIX_DECIMAL;
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));
142 Long::Parse(const String& s, long& ret)
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());
148 return Parse(s, Character::RADIX_DECIMAL, ret);
152 Long::Parse(const String& s, int radix, long& ret)
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);
158 wchar_t* pEnd = null;
160 int len = s.GetLength();
161 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
162 GetErrorMessage(E_NUM_FORMAT));
165 ret = wcstol(s.GetPointer(), &pEnd, radix);
166 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
167 GetErrorMessage(E_NUM_FORMAT), pEnd);
168 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
169 "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
175 Long::ToChar(void) const
177 return static_cast<char> (value);
181 Long::ToShort(void) const
183 return static_cast<short> (value);
187 Long::ToInt(void) const
189 return static_cast<int> (value);
193 Long::ToLong(void) const
199 Long::ToLongLong(void) const
201 return static_cast<long long> (value);
205 Long::ToFloat(void) const
207 return static_cast<float> (value);
211 Long::ToDouble(void) const
213 return static_cast<double> (value);
217 Long::ToString(void) const
219 return(Long::ToString(value));
223 Long::ToString(long value)
225 const static unsigned int LONG_LENGTH_MAX = 11;
227 wchar_t sValue[LONG_LENGTH_MAX + 1] = {0, };
228 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
230 return String(sValue);