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.
26 #include <FBaseLong.h>
27 #include <FBaseResult.h>
28 #include <FBaseCharacter.h>
29 #include <FBaseSysLog.h>
31 namespace Tizen { namespace Base
34 Long::Long(long value)
40 Long::Long(const Long& value)
51 Long::operator =(const Long& rhs)
61 Long::Compare(long l1, long l2)
63 return(l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
67 Long::CompareTo(const Long& value) const
69 return(Long::Compare(this->value, value.value));
73 Long::Equals(const Object& obj) const
75 const Long* pOther = dynamic_cast <const Long*>(&obj);
81 return value == (*pOther).value;
85 Long::GetHashCode(void) const
87 return static_cast<int> (value);
91 Long::GetHashCode(long val)
93 return static_cast<int> (val);
97 Long::Decode(const String& s, long& ret)
99 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
100 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
105 wchar_t* pEnd = null;
108 if (s[0] == L'-' || s[0] == L'+')
115 if (s[startIndex] == L'#')
117 radix = Character::RADIX_HEXADECIMAL;
120 str.Remove(startIndex, 1);
122 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
124 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
126 radix = Character::RADIX_HEXADECIMAL;
130 radix = Character::RADIX_OCTAL;
135 radix = Character::RADIX_DECIMAL;
139 ret = wcstol(str.GetPointer(), &pEnd, radix);
140 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
141 "[%s] Long decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
142 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
143 "[%s] Decoded value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
149 Long::Parse(const String& s, long& ret)
151 int len = s.GetLength();
152 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
153 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
155 return Parse(s, Character::RADIX_DECIMAL, ret);
159 Long::Parse(const String& s, int radix, long& ret)
161 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
162 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
163 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
165 int len = s.GetLength();
166 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
167 GetErrorMessage(E_NUM_FORMAT));
170 wchar_t* pEnd = null;
171 long tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
172 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
173 GetErrorMessage(E_NUM_FORMAT), pEnd);
174 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
175 "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
182 Long::ToChar(void) const
184 return static_cast<char> (value);
188 Long::ToShort(void) const
190 return static_cast<short> (value);
194 Long::ToInt(void) const
196 return static_cast<int> (value);
200 Long::ToLong(void) const
206 Long::ToLongLong(void) const
208 return static_cast<long long> (value);
212 Long::ToFloat(void) const
214 return static_cast<float> (value);
218 Long::ToDouble(void) const
220 return static_cast<double> (value);
224 Long::ToString(void) const
226 return(Long::ToString(value));
230 Long::ToString(long value)
232 const static unsigned int LONG_LENGTH_MAX = 11;
234 wchar_t sValue[LONG_LENGTH_MAX + 1] = {0, };
235 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
237 return String(sValue);