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 #define CHECK_NAN_OR_INF_AND_RETURN(value) \
30 if (std::isnan(value)) \
32 return String(L"NaN"); \
34 if (std::isinf(value)) \
36 return String(L"Infinity"); \
39 namespace Tizen { namespace Base
50 ~_CLocaleWrapper(void)
55 uselocale(LC_GLOBAL_LOCALE);
59 result Construct(const char* pLocale)
61 _locale = newlocale(LC_ALL, pLocale, null);
62 SysTryReturnResult(NID_BASE, _locale != null, E_INVALID_ARG, "Creating a new locale object is failed. pLocale is invalid.");
73 _LocalizedNumParser::ToDouble(const String& str, const char* pLocale)
77 _CLocaleWrapper clocale;
78 result r = clocale.Construct(pLocale);
79 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< double >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
84 double ret = wcstod(str.GetPointer(), &pEnd);
85 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && (pEnd[0] == 0), std::numeric_limits< double >::quiet_NaN(),
86 E_NUM_FORMAT, "[%s] wcstod() failed. Scan stopped at (%ls)", GetErrorMessage(E_NUM_FORMAT), pEnd);
87 SysTryReturn(NID_BASE, errno == 0 || (ret != HUGE_VAL && ret != -HUGE_VAL), std::numeric_limits< double >::quiet_NaN(),
88 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Double.", GetErrorMessage(E_NUM_FORMAT));
94 _LocalizedNumParser::ToFloat(const String& str, const char* pLocale)
98 _CLocaleWrapper clocale;
99 result r = clocale.Construct(pLocale);
100 SysTryReturn(NID_BASE, r == E_SUCCESS, std::numeric_limits< float >::quiet_NaN(), r, "[%s] Propagating.", GetErrorMessage(r));
102 wchar_t* pEnd = null;
105 float ret = wcstof(str.GetPointer(), &pEnd);
106 SysTryReturn(NID_BASE, !(ret == 0 && errno == EINVAL) && pEnd[0] == 0, std::numeric_limits< float >::quiet_NaN(),
107 E_NUM_FORMAT, "[%s] wcstof() failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
108 SysTryReturn(NID_BASE, errno == 0 || (ret != HUGE_VAL && ret != -HUGE_VAL), std::numeric_limits< float >::quiet_NaN(),
109 E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
115 _LocalizedNumParser::ToString(double value, const char* pLocale)
118 CHECK_NAN_OR_INF_AND_RETURN(value);
120 _CLocaleWrapper clocale;
121 result r = clocale.Construct(pLocale);
122 SysTryReturn(NID_BASE, r == E_SUCCESS, String(L""), r, "[%s] Propagating.", GetErrorMessage(r));
124 wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
125 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%#lg", value);
127 return String(sValue);
131 _LocalizedNumParser::ToString(double value, const char* pLocale, int precision)
134 CHECK_NAN_OR_INF_AND_RETURN(value);
136 _CLocaleWrapper clocale;
137 result r = clocale.Construct(pLocale);
138 SysTryReturn(NID_BASE, r == E_SUCCESS, String(L""), r, "[%s] Propagating.", GetErrorMessage(r));
140 wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
141 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%.*f", precision, value);
143 return String(sValue);
147 _LocalizedNumParser::ToString(float value, const char* pLocale)
150 CHECK_NAN_OR_INF_AND_RETURN(value);
152 _CLocaleWrapper clocale;
153 result r = clocale.Construct(pLocale);
154 SysTryReturn(NID_BASE, r == E_SUCCESS, String(L""), r, "[%s] Propagating.", GetErrorMessage(r));
156 wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
157 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%g", value);
159 return String(sValue);
163 _LocalizedNumParser::ToString(float value, const char* pLocale, int precision)
166 CHECK_NAN_OR_INF_AND_RETURN(value);
168 _CLocaleWrapper clocale;
169 result r = clocale.Construct(pLocale);
170 SysTryReturn(NID_BASE, r == E_SUCCESS, String(L""), r, "[%s] Propagating.", GetErrorMessage(r));
172 wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
173 swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%.*f", precision, value);
175 return String(sValue);