FBaseLongComparer.cpp
FBaseLongLong.cpp
FBaseLongLongComparer.cpp
+ FBase_LocalizedNumParser.cpp
FBaseObject.cpp
FBaseResult.cpp
FBaseShort.cpp
/**
* @file FBaseDouble.cpp
* @brief This is the implementation file for Double class.
- * @see Number
+ * @see Number
*/
#include <cfloat>
#include <limits.h>
#include <FBaseDouble.h>
#include <FBaseResult.h>
-#include "FBase_NativeError.h"
#include <FBaseSysLog.h>
+#include "FBase_LocalizedNumParser.h"
namespace Tizen { namespace Base
bool
Double::Equals(const Object& obj) const
{
- const Double* pOther = dynamic_cast <const Double*>(&obj);
+ const Double* pOther = dynamic_cast< const Double* >(&obj);
if (pOther == null)
{
int
Double::GetHashCode(void) const
{
- double* pTemp = const_cast<double*> (&value);
- int* pValueLow = reinterpret_cast<int*> (pTemp);
+ double* pTemp = const_cast< double* >(&value);
+ int* pValueLow = reinterpret_cast< int* >(pTemp);
int* pValueHigh = pValueLow + 1;
return *pValueLow + *pValueHigh;
}
int
Double::GetHashCode(double val)
{
- int* pValueLow = reinterpret_cast<int*> (&val);
+ int* pValueLow = reinterpret_cast< int* >(&val);
int* pValueHigh = pValueLow + 1;
return *pValueLow + *pValueHigh;
}
result
Double::Parse(const String& s, double& ret)
{
- wchar_t* pEnd = null;
- errno = 0;
- double tmpRet = wcstod(s.GetPointer(), &pEnd);
- SysTryReturnResult(NID_BASE, ((!(!Double::Compare(tmpRet, 0) && errno == EINVAL)) && (pEnd[0] == 0)), E_NUM_FORMAT,
- "Double parse failed with reason (%s). Scan stopped at (%ls)", __ConvertNativeErrorToMessage(errno), pEnd);
- SysTryReturnResult(NID_BASE, !(errno != 0 && (!Double::Compare(tmpRet, HUGE_VAL)|| !Double::Compare(tmpRet, -HUGE_VAL))),
- E_NUM_FORMAT, "Parsed value cannot fit into a Double.");
+ double tmpRet = _LocalizedNumParser::ToDouble(s, "");
+
+ if (IsNaN(tmpRet))
+ {
+ result r = GetLastResult();
+ SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
+ return r;
+ }
ret = tmpRet;
return E_SUCCESS;
char
Double::ToChar(void) const
{
- return static_cast<char> (value);
+ return static_cast< char >(value);
}
short
Double::ToShort(void) const
{
- return static_cast<short> (value);
+ return static_cast< short >(value);
}
int
Double::ToInt(void) const
{
- return static_cast<int> (value);
+ return static_cast< int >(value);
}
long
Double::ToLong(void) const
{
- return static_cast<long> (value);
+ return static_cast< long >(value);
}
long long
Double::ToLongLong(void) const
{
- return static_cast<long long> (value);
+ return static_cast< long long >(value);
}
float
Double::ToFloat(void) const
{
- return static_cast<float> (value);
+ return static_cast< float >(value);
}
double
String
Double::ToString(void) const
{
- return(Double::ToString(value));
+ return Double::ToString(value);
}
String
Double::ToString(double value)
{
- const static unsigned int MAX_DIG = 17 + 3;
- const static unsigned int DOUBLE_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
-
- if (Double::IsNaN(value))
- {
- return String(L"NaN");
- }
- else if (Double::IsInfinity(value))
- {
- return String(L"Infinity");
- }
- else
- {
- wchar_t sValue[DOUBLE_LENGTH_MAX + 1] = {0, };
- swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%#lg", value);
- return String(sValue);
- }
+ return _LocalizedNumParser::ToString(value, "");
}
long long
bool
Double::IsFinite(double d)
{
- return((isfinite(d) != 0) ? true : false);
+ return isfinite(d);
}
bool
Double::IsInfinity(void) const
{
- return(Double::IsInfinity(value));
+ return Double::IsInfinity(value);
}
bool
Double::IsInfinity(double value)
{
- return(!Double::IsFinite(value));
+ return !Double::IsFinite(value);
}
bool
Double::IsNaN(void) const
{
- return(Double::IsNaN(value));
+ return Double::IsNaN(value);
}
bool
Double::IsNaN(double value)
{
- return((isnan(value) != 0) ? true : false);
+ return isnan(value);
}
double
/**
* @file FBaseFloat.cpp
* @brief This is the implementation file for Float class.
- * @see Number class
+ * @see Number class
*/
// Includes
#include <FBaseFloat.h>
#include <FBaseResult.h>
#include <FBaseSysLog.h>
+#include "FBase_LocalizedNumParser.h"
namespace Tizen { namespace Base
{
bool
Float::Equals(const Object& obj) const
{
- const Float* pOther = dynamic_cast <const Float*>(&obj);
+ const Float* pOther = dynamic_cast< const Float* >(&obj);
if (pOther == null)
{
int
Float::GetHashCode(void) const
{
- float* pTemp = const_cast<float*> (&value);
- int* pValue = reinterpret_cast<int*> (pTemp);
+ float* pTemp = const_cast< float* >(&value);
+ int* pValue = reinterpret_cast< int* >(pTemp);
return *pValue;
}
int
Float::GetHashCode(float val)
{
- int* pValue = reinterpret_cast<int*> (&val);
+ int* pValue = reinterpret_cast< int* >(&val);
return *pValue;
}
result
Float::Parse(const String& s, float& ret)
{
- SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
- "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
+ double tmpRet = _LocalizedNumParser::ToFloat(s, "");
- errno = 0;
- wchar_t* pEnd = null;
- float tmpRet = wcstof(s.GetPointer(), &pEnd);
- SysTryReturn(NID_BASE, (!Float::Compare(pEnd[0], 0)), E_NUM_FORMAT, E_NUM_FORMAT,
- "[%s] Float parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
- SysTryReturn(NID_BASE, !((!Float::Compare(tmpRet, HUGE_VAL) || !Float::Compare(tmpRet, -HUGE_VAL)) && (errno != 0)),
- E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
+ if (IsNaN(tmpRet))
+ {
+ result r = GetLastResult();
+ SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
+ return r;
+ }
ret = tmpRet;
return E_SUCCESS;
char
Float::ToChar(void) const
{
- return static_cast<char> (value);
+ return static_cast< char >(value);
}
short
Float::ToShort(void) const
{
- return static_cast<short> (value);
+ return static_cast< short >(value);
}
int
Float::ToInt(void) const
{
- return static_cast<int> (value);
+ return static_cast< int >(value);
}
long
Float::ToLong(void) const
{
- return static_cast<long> (value);
+ return static_cast< long >(value);
}
long long
Float::ToLongLong(void) const
{
- return static_cast<long long> (value);
+ return static_cast< long long >(value);
}
float
double
Float::ToDouble(void) const
{
- return static_cast<double> (value);
+ return static_cast< double >(value);
}
String
Float::ToString(void) const
{
- return(Float::ToString(value));
+ return Float::ToString(value);
}
String
Float::ToString(float value)
{
- const static unsigned int MAX_DIG = 7 + 3;
- const static unsigned int FLOAT_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
-
- if (Float::IsNaN(value))
- {
- return String(L"NaN");
- }
- else if (Float::IsInfinity(value))
- {
- return String(L"Infinity");
- }
- else
- {
- wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
- swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%g", value);
-
- return String(sValue);
- }
+ return _LocalizedNumParser::ToString(value, "");
}
int
bool
Float::IsFinite(float value)
{
- return((isfinite(value) != 0) ? true : false);
+ return isfinite(value);
}
bool
Float::IsInfinity(void) const
{
- return(Float::IsInfinity(value));
+ return Float::IsInfinity(value);
}
bool
Float::IsInfinity(float value)
{
- return(!Float::IsFinite(value));
+ return !Float::IsFinite(value);
}
bool
Float::IsNaN(void) const
{
- return(Float::IsNaN(value));
+ return Float::IsNaN(value);
}
bool
Float::IsNaN(float value)
{
- return((isnan(value) != 0) ? true : false);
+ return isnan(value);
}
float
--- /dev/null
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file FBase_LocalizedNumParser.cpp
+ * @brief This is the implementation file for _LocalizedNumParser class.
+ */
+
+#include <cerrno>
+#include <clocale>
+#include <cmath>
+#include <cwchar>
+#include <limits>
+#include <FBaseSysLog.h>
+#include "FBase_LocalizedNumParser.h"
+
+namespace Tizen { namespace Base
+{
+
+class _CLocaleWrapper
+{
+public:
+ _CLocaleWrapper(void)
+ : _locale(0)
+ {
+ }
+
+ ~_CLocaleWrapper(void)
+ {
+ if (_locale != null)
+ {
+ freelocale(_locale);
+ uselocale(LC_GLOBAL_LOCALE);
+ }
+ }
+
+ result
+ Construct(const char* pLocale)
+ {
+ _locale = newlocale(LC_ALL, pLocale, null);
+ SysTryReturnResult(NID_BASE, _locale != null, E_INVALID_ARG, "Creating a new locale object is failed. pLocale is invalid.");
+
+ uselocale(_locale);
+ return E_SUCCESS;
+ }
+
+private:
+ locale_t _locale;
+};
+
+double
+_LocalizedNumParser::ToDouble(const String& str, const char* pLocale)
+{
+ ClearLastResult();
+
+ _CLocaleWrapper clocale;
+ result r = clocale.Construct(pLocale);
+ SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< double >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
+
+ wchar_t* pEnd = null;
+ errno = 0;
+
+ double ret = wcstod(str.GetPointer(), &pEnd);
+ SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && (pEnd[0] == 0), std::numeric_limits< double >::quiet_NaN(),
+ E_NUM_FORMAT, "[%s] wcstod() failed. Scan stopped at (%ls)", GetErrorMessage(E_NUM_FORMAT), pEnd);
+ SysTryReturn(NID_BASE, errno == 0 && ret != HUGE_VAL && ret != -HUGE_VAL, std::numeric_limits< double >::quiet_NaN(),
+ E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Double.", GetErrorMessage(E_NUM_FORMAT));
+
+ return ret;
+}
+
+float
+_LocalizedNumParser::ToFloat(const String& str, const char* pLocale)
+{
+ ClearLastResult();
+
+ _CLocaleWrapper clocale;
+ result r = clocale.Construct(pLocale);
+ SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< float >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
+
+ wchar_t* pEnd = null;
+ errno = 0;
+
+ float ret = wcstof(str.GetPointer(), &pEnd);
+ SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && pEnd[0] == 0, std::numeric_limits< float >::quiet_NaN(),
+ E_NUM_FORMAT, "[%s] wcstof() failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
+ SysTryReturn(NID_BASE, errno == 0 && ret != HUGE_VAL && ret != -HUGE_VAL, std::numeric_limits< float >::quiet_NaN(),
+ E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
+
+ return ret;
+}
+
+String
+_LocalizedNumParser::ToString(double value, const char* pLocale)
+{
+ ClearLastResult();
+
+ if (std::isnan(value))
+ {
+ return String(L"NaN");
+ }
+
+ if (std::isinf(value))
+ {
+ return String(L"Infinity");
+ }
+
+ _CLocaleWrapper clocale;
+ result r = clocale.Construct(pLocale);
+ SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
+
+ wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
+ swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%#lg", value);
+
+ return String(sValue);
+}
+
+String
+_LocalizedNumParser::ToString(float value, const char* pLocale)
+{
+ ClearLastResult();
+
+ if (std::isnan(value))
+ {
+ return String(L"NaN");
+ }
+
+ if (std::isinf(value))
+ {
+ return String(L"Infinity");
+ }
+
+ _CLocaleWrapper clocale;
+ result r = clocale.Construct(pLocale);
+ SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
+
+ wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
+ swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%g", value);
+
+ return String(sValue);
+}
+
+}} // Tizen::Base
--- /dev/null
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file FBase_LocalizedNumParser.h
+ * @brief This is the header file for the %_LocalizedNumParser class.
+ *
+ * This header file contains the declarations of the %_LocalizedNumParser class.
+ */
+
+#ifndef _FBASE_INTERNAL_LOCALIZED_NUM_PARSER_H_
+#define _FBASE_INTERNAL_LOCALIZED_NUM_PARSER_H_
+
+#include <FBaseString.h>
+
+namespace Tizen { namespace Base
+{
+
+class _OSP_EXPORT_ _LocalizedNumParser
+{
+public:
+ /**
+ * Locale specifiable version of Double::Parse(). @n
+ * This method is affected by the specified @c pLocale.
+ *
+ * @since 2.1
+ *
+ * @return The converted numeric value
+ * @param[in] str A unicode representation of @c signed @c double value
+ * @param[in] pLocale A specific locale identifier. @n
+ * The @c pLocale can have below values.
+ * - "" : the system default locale
+ * - "C" or "POSIX" : the POSIX locale
+ * - "language[_territory][.codeset]" : an implementation-provided locale, e.g. "en_US.utf8", "fr_FR.utf8", etc.
+ * @exception E_SUCCESS The method is successful.
+ * @exception E_NUM_FORMAT The specified string does not contain a number that can be parsed.
+ * @exception E_INVALID_ARG The specified locale identifier is invalid.
+ * @remarks
+ * - When an error is occurred, this method returns Not-a-Number(NaN).
+ * - The behavior of this method is dependent on the specified locale setting.
+ * - The specific error code can be accessed using the GetLastResult() method.
+ */
+ static double ToDouble(const String& str, const char* pLocale);
+
+ /**
+ * Locale specifiable version of Float::Parse(). @n
+ * This method is affected by the specified @c pLocale.
+ *
+ * @since 2.1
+ *
+ * @return The converted numeric value
+ * @param[in] str A unicode representation of @c signed @c float value
+ * @param[in] pLocale A specific locale identifier. @n
+ * The @c pLocale can have below values.
+ * - "" : the system default locale
+ * - "C" or "POSIX" : the POSIX locale
+ * - "language[_territory][.codeset]" : an implementation-provided locale, e.g. "en_US.utf8", "fr_FR.utf8", etc.
+ * @exception E_SUCCESS The method is successful.
+ * @exception E_NUM_FORMAT The specified string does not contain a number that can be parsed.
+ * @exception E_INVALID_ARG The specified locale identifier is invalid.
+ * @remarks
+ * - When an error is occurred, this method returns Not-a-Number(NaN).
+ * - The behavior of this method is dependent on the specified locale setting.
+ * - The specific error code can be accessed using the GetLastResult() method.
+ */
+ static float ToFloat(const String& str, const char* pLocale);
+
+ /**
+ * Locale specifiable version of Double::ToString(). @n
+ * This method is affected by the specified @c pLocale.
+ *
+ * @since 2.1
+ *
+ * @return A string containing a Unicode representation of the specified @c double value
+ * @param[in] value A @c double value to convert
+ * @param[in] pLocale A specific locale identifier. @n
+ * The @c pLocale can have below values.
+ * - "" : the system default locale
+ * - "C" or "POSIX" : the POSIX locale
+ * - "language[_territory][.codeset]" : an implementation-provided locale, e.g. "en_US.utf8", "fr_FR.utf8", etc.
+ * @exception E_SUCCESS The method is successful.
+ * @exception E_INVALID_ARG The specified locale identifier is invalid.
+ * @remarks
+ * - If the input value is Not-a-Number(NaN), the result is the string "NaN".
+ * Furthermore, infinity produces the result "Infinity". @n
+ * 6 digits are given for the precision of this method. Use String::Format() to set the specific precision.
+ * - The behavior of this method is dependent on the specified locale setting.
+ * - The specific error code can be accessed using the GetLastResult() method.
+ */
+ static String ToString(double value, const char* pLocale);
+
+ /**
+ * Locale specifiable version of Float::ToString(). @n
+ * This method is affected by the specified @c pLocale.
+ *
+ * @since 2.1
+ *
+ * @return A string containing a Unicode representation of the specified @c float value
+ * @param[in] value A @c float value to convert
+ * @param[in] pLocale A specific locale identifier. @n
+ * The @c pLocale can have below values.
+ * - "" : the system default locale
+ * - "C" or "POSIX" : the POSIX locale
+ * - "language[_territory][.codeset]" : an implementation-provided locale, e.g. "en_US.utf8", "fr_FR.utf8", etc.
+ * @exception E_SUCCESS The method is successful.
+ * @exception E_INVALID_ARG The specified locale identifier is invalid.
+ * @remarks
+ * - If the input value is Not-a-Number(NaN), the result is the string "NaN".
+ * Furthermore, infinity produces the result "Infinity". @n
+ * 6 digits are given for the precision of this method. Use String::Format() to set the specific precision.
+ * - The behavior of this method is dependent on the specified locale setting.
+ * - The specific error code can be accessed using the GetLastResult() method.
+ */
+ static String ToString(float value, const char* pLocale);
+
+private:
+ //
+ // This default constructor is intentionally declared as private because this class is not constructible.
+ //
+ // @since 2.1
+ //
+ _LocalizedNumParser(void);
+
+ //
+ // This destructor is intentionally declared as private because this class is not constructible.
+ //
+ // @since 2.1
+ //
+ ~_LocalizedNumParser(void);
+
+ //
+ // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
+ //
+ // @since 2.1
+ //
+ // @param[in] rhs A reference to the %_LocalizedNumParser instance
+ //
+ _LocalizedNumParser(const _LocalizedNumParser& rhs);
+
+ //
+ // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
+ //
+ // @since 2.1
+ //
+ // @return A reference to the %_LocalizedNumParser instance
+ // @param[in] rhs A reference to the %_LocalizedNumParser instance
+ //
+ _LocalizedNumParser& operator =(const _LocalizedNumParser& rhs);
+
+ static const int FLOAT_LENGTH_MAX = 7 + 38 + 3; // significant decimal digits + exponent + extra characters
+ static const int DBL_MAX_LENGTH = 17 + 308 + 3; // significant decimal digits + exponent + extra characters
+};
+
+}} // Tizen::Base
+
+#endif // _FBASE_INTERNAL_LOCALIZED_NUM_PARSER_H_
\ No newline at end of file