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.
26 #include <FBaseSysLog.h>
27 #include "FBase_LocalizedNumParser.h"
29 namespace Tizen { namespace Base
40 ~_CLocaleWrapper(void)
45 uselocale(LC_GLOBAL_LOCALE);
49 result Construct(const char* pLocale)
51 _locale = newlocale(LC_ALL, pLocale, null);
52 SysTryReturnResult(NID_BASE, _locale != null, E_INVALID_ARG, "Creating a new locale object is failed. pLocale is invalid.");
63 _LocalizedNumParser::ToDouble(const String& str, const char* pLocale)
67 _CLocaleWrapper clocale;
68 result r = clocale.Construct(pLocale);
69 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< double >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
74 double ret = wcstod(str.GetPointer(), &pEnd);
75 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && (pEnd[0] == 0), std::numeric_limits< double >::quiet_NaN(),
76 E_NUM_FORMAT, "[%s] wcstod() failed. Scan stopped at (%ls)", GetErrorMessage(E_NUM_FORMAT), pEnd);
77 SysTryReturn(NID_BASE, errno == 0 || (ret != HUGE_VAL && ret != -HUGE_VAL), std::numeric_limits< double >::quiet_NaN(),
78 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Double.", GetErrorMessage(E_NUM_FORMAT));
84 _LocalizedNumParser::ToFloat(const String& str, const char* pLocale)
88 _CLocaleWrapper clocale;
89 result r = clocale.Construct(pLocale);
90 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< float >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
95 float ret = wcstof(str.GetPointer(), &pEnd);
96 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && pEnd[0] == 0, std::numeric_limits< float >::quiet_NaN(),
97 E_NUM_FORMAT, "[%s] wcstof() failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
98 SysTryReturn(NID_BASE, errno == 0 || (ret != HUGE_VAL && ret != -HUGE_VAL), std::numeric_limits< float >::quiet_NaN(),
99 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
105 _LocalizedNumParser::ToString(double value, const char* pLocale)
109 if (std::isnan(value))
111 return String(L"NaN");
114 if (std::isinf(value))
116 return String(L"Infinity");
119 _CLocaleWrapper clocale;
120 result r = clocale.Construct(pLocale);
121 SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
123 wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
124 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%#lg", value);
126 return String(sValue);
130 _LocalizedNumParser::ToString(float value, const char* pLocale)
134 if (std::isnan(value))
136 return String(L"NaN");
139 if (std::isinf(value))
141 return String(L"Infinity");
144 _CLocaleWrapper clocale;
145 result r = clocale.Construct(pLocale);
146 SysTryReturn(NID_BASE, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
148 wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
149 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%g", value);
151 return String(sValue);