2 // Copyright (c) 2013 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.
18 * @file FBase_LocalizedNumParser.cpp
19 * @brief This is the implementation file for _LocalizedNumParser class.
27 #include <FBaseSysLog.h>
28 #include "FBase_LocalizedNumParser.h"
30 namespace Tizen { namespace Base
41 ~_CLocaleWrapper(void)
46 uselocale(LC_GLOBAL_LOCALE);
51 Construct(const char* pLocale)
53 _locale = newlocale(LC_ALL, pLocale, null);
54 SysTryReturnResult(NID_BASE, _locale != null, E_INVALID_ARG, "Creating a new locale object is failed. pLocale is invalid.");
65 _LocalizedNumParser::ToDouble(const String& str, const char* pLocale)
69 _CLocaleWrapper clocale;
70 result r = clocale.Construct(pLocale);
71 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< double >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
76 double ret = wcstod(str.GetPointer(), &pEnd);
77 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && (pEnd[0] == 0), std::numeric_limits< double >::quiet_NaN(),
78 E_NUM_FORMAT, "[%s] wcstod() failed. Scan stopped at (%ls)", GetErrorMessage(E_NUM_FORMAT), pEnd);
79 SysTryReturn(NID_BASE, errno == 0 && ret != HUGE_VAL && ret != -HUGE_VAL, std::numeric_limits< double >::quiet_NaN(),
80 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Double.", GetErrorMessage(E_NUM_FORMAT));
86 _LocalizedNumParser::ToFloat(const String& str, const char* pLocale)
90 _CLocaleWrapper clocale;
91 result r = clocale.Construct(pLocale);
92 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< float >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
97 float ret = wcstof(str.GetPointer(), &pEnd);
98 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && pEnd[0] == 0, std::numeric_limits< float >::quiet_NaN(),
99 E_NUM_FORMAT, "[%s] wcstof() failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
100 SysTryReturn(NID_BASE, errno == 0 && ret != HUGE_VAL && ret != -HUGE_VAL, std::numeric_limits< float >::quiet_NaN(),
101 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
107 _LocalizedNumParser::ToString(double value, const char* pLocale)
111 if (std::isnan(value))
113 return String(L"NaN");
116 if (std::isinf(value))
118 return String(L"Infinity");
121 _CLocaleWrapper clocale;
122 result r = clocale.Construct(pLocale);
123 SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
125 wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
126 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%#lg", value);
128 return String(sValue);
132 _LocalizedNumParser::ToString(float value, const char* pLocale)
136 if (std::isnan(value))
138 return String(L"NaN");
141 if (std::isinf(value))
143 return String(L"Infinity");
146 _CLocaleWrapper clocale;
147 result r = clocale.Construct(pLocale);
148 SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
150 wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
151 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%g", value);
153 return String(sValue);