2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 * @brief This is the implementation file for Long class.
25 #include <FBaseLong.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 Long::Long(long value)
39 Long::Long(const Long& value)
50 Long::operator =(const Long& rhs)
60 Long::Compare(long l1, long l2)
62 return(l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
66 Long::CompareTo(const Long& value) const
68 return(Long::Compare(this->value, value.value));
72 Long::Equals(const Object& obj) const
74 const Long* pOther = dynamic_cast< const Long* >(&obj);
80 return value == (*pOther).value;
84 Long::GetHashCode(void) const
86 return static_cast< int >(value);
90 Long::GetHashCode(long val)
92 return static_cast< int >(val);
96 Long::Decode(const String& s, long& ret)
98 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
99 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
104 wchar_t* pEnd = null;
107 if (s[0] == L'-' || s[0] == L'+')
114 if (s[startIndex] == L'#')
116 radix = Character::RADIX_HEXADECIMAL;
119 str.Remove(startIndex, 1);
121 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
123 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
125 radix = Character::RADIX_HEXADECIMAL;
129 radix = Character::RADIX_OCTAL;
134 radix = Character::RADIX_DECIMAL;
138 ret = wcstol(str.GetPointer(), &pEnd, radix);
139 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
140 "[%s] Long decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
141 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
142 "[%s] Decoded value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
148 Long::Parse(const String& s, long& ret)
150 int len = s.GetLength();
151 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
152 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
154 return Parse(s, Character::RADIX_DECIMAL, ret);
158 Long::Parse(const String& s, int radix, long& ret)
160 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
161 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
162 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
164 int len = s.GetLength();
165 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
166 GetErrorMessage(E_NUM_FORMAT));
169 wchar_t* pEnd = null;
170 long tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
171 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
172 GetErrorMessage(E_NUM_FORMAT), pEnd);
173 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
174 "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
181 Long::ToChar(void) const
183 return static_cast< char >(value);
187 Long::ToShort(void) const
189 return static_cast< short >(value);
193 Long::ToInt(void) const
195 return static_cast< int >(value);
199 Long::ToLong(void) const
205 Long::ToLongLong(void) const
207 return static_cast< long long >(value);
211 Long::ToFloat(void) const
213 return static_cast< float >(value);
217 Long::ToDouble(void) const
219 return static_cast< double >(value);
223 Long::ToString(void) const
225 return(Long::ToString(value));
229 Long::ToString(long value)
231 const static unsigned int LONG_LENGTH_MAX = 11;
233 wchar_t sValue[LONG_LENGTH_MAX + 1] = {0, };
234 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
236 return String(sValue);