Fix memory leaks
[platform/framework/native/appfw.git] / src / locales / FLcl_NumberFormatterImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FLclNumberFormatterEx.cpp
19  * @brief       This is the implementation file for the NumberFormatterEx class.
20  */
21 #include <unique_ptr.h>
22 #include <unicode/decimfmt.h>
23
24 #include <FBaseSysLog.h>
25 #include <FLclNumberFormatter.h>
26
27 #include "FLcl_LocaleData.h"
28 #include "FLcl_LocaleImpl.h"
29 #include "FLcl_LocaleManagerImpl.h"
30 #include "FLcl_NumberFormatterImpl.h"
31
32
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace Locales
36 {
37
38 _NumberFormatterImpl::_NumberFormatterImpl(void)
39         : __pIcuNumberFormatter(null)
40 {
41 }
42
43 _NumberFormatterImpl::~_NumberFormatterImpl(void)
44 {
45         delete __pIcuNumberFormatter;
46 }
47
48 NumberFormatter*
49 _NumberFormatterImpl::CreateNumberFormatterN(NumberFormatterStyle style)
50 {
51         return CreateNumberFormatterN(_LocaleManagerImpl::GetSystemLocale(), style);
52 }
53
54 NumberFormatter*
55 _NumberFormatterImpl::CreateNumberFormatterN(const Locale& locale, NumberFormatterStyle style)
56 {
57         IcuLocale icuLocale = _LocaleImpl::GetLocaleImpl(locale)->GetIcuLocale();
58         SysTryReturn(NID_LCL, !icuLocale.isBogus(), null, E_UNSUPPORTED_OPERATION,
59                                                 "[%s] Given locale is not supported", GetErrorMessage(E_UNSUPPORTED_OPERATION));
60
61         std::unique_ptr<NumberFormatter> pNewFormatter(new (std::nothrow) NumberFormatter);
62         SysTryReturn(NID_LCL, pNewFormatter, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
63
64         std::unique_ptr<_NumberFormatterImpl> pNumberFormatterImpl(new (std::nothrow) _NumberFormatterImpl);
65         SysTryReturn(NID_LCL, pNumberFormatterImpl, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed");
66
67         UNumberFormatStyle icuStyle = UNUM_FORMAT_STYLE_COUNT;
68         switch(style)
69         {
70         case NUM_FORMATTER_STYLE_NUMBER:
71                 icuStyle = UNUM_DECIMAL;
72                 break;
73         case NUM_FORMATTER_STYLE_CURRENCY:
74                 icuStyle = UNUM_CURRENCY;
75                 break;
76         case NUM_FORMATTER_STYLE_PERCENT:
77                 icuStyle = UNUM_PERCENT;
78                 break;
79         case NUM_FORMATTER_STYLE_INVALID:
80                 break;
81         }
82
83         UErrorCode ec = U_ZERO_ERROR;
84         std::unique_ptr<IcuNumberFormat> pIcuNumberFormatter(IcuNumberFormat::createInstance(icuLocale, icuStyle, ec));
85         if (pIcuNumberFormatter)
86         {
87                 IcuDecimalFormat* pDecimalFmt = dynamic_cast<IcuDecimalFormat*>(pIcuNumberFormatter.get());
88                 if (pDecimalFmt)
89                 {
90                         pIcuNumberFormatter.release();
91                         pNumberFormatterImpl->__pIcuNumberFormatter = pDecimalFmt;
92                         pNumberFormatterImpl->__currency.Construct(locale);
93                         pNewFormatter->__pNumberFormatterImpl = pNumberFormatterImpl.release();
94
95                         SetLastResult(E_SUCCESS);
96                         return pNewFormatter.release();
97                 }
98         }
99
100         SetLastResult(E_INVALID_ARG);
101         return null;
102 }
103
104 result
105 _NumberFormatterImpl::ApplyPattern(const String& pattern, bool localized)
106 {
107         UErrorCode ec = U_ZERO_ERROR;
108         if (localized)
109         {
110                 __pIcuNumberFormatter->applyLocalizedPattern(_LocaleData::GetIcuString(pattern), ec);
111         }
112         else
113         {
114                 __pIcuNumberFormatter->applyPattern(_LocaleData::GetIcuString(pattern), ec);
115         }
116
117         return U_SUCCESS(ec) ? E_SUCCESS : E_INVALID_ARG;
118 }
119
120 result
121 _NumberFormatterImpl::Format(long number, String& str, _FieldPosition pos) const
122 {
123         IcuUnicodeString icuStr;
124         IcuFieldPosition icuPos = _LocaleData::GetIcuFieldPosition(pos);
125         icuStr = __pIcuNumberFormatter->format((int64_t)number, icuStr, icuPos);
126         str = _LocaleData::GetOspString(icuStr);
127         return E_SUCCESS;
128 }
129
130 result
131 _NumberFormatterImpl::Format(double number, String& str, _FieldPosition pos) const
132 {
133         IcuUnicodeString icuStr;
134         IcuFieldPosition icuPos = _LocaleData::GetIcuFieldPosition(pos);
135         icuStr = __pIcuNumberFormatter->format(number, icuStr, icuPos);
136         str = _LocaleData::GetOspString(icuStr);
137         return E_SUCCESS;
138 }
139
140 const Currency*
141 _NumberFormatterImpl::GetCurrency(void)
142 {
143         return &__currency;
144 }
145
146 void
147 _NumberFormatterImpl::SetCurrency(const Currency& newValue)
148 {
149         __currency.Construct(newValue.GetCurrencyCode());
150         __pIcuNumberFormatter->setCurrency(_LocaleData::GetIcuString(newValue.GetCurrencyCode()).getBuffer());
151 }
152
153 int
154 _NumberFormatterImpl::GetMaxIntegerDigits(void) const
155 {
156         return __pIcuNumberFormatter->getMaximumIntegerDigits();
157 }
158
159 void
160 _NumberFormatterImpl::SetMaxIntegerDigits(int newValue)
161 {
162         __pIcuNumberFormatter->setMaximumIntegerDigits(newValue);
163 }
164
165 int
166 _NumberFormatterImpl::GetMinIntegerDigits(void) const
167 {
168         return __pIcuNumberFormatter->getMinimumIntegerDigits();
169 }
170
171 void
172 _NumberFormatterImpl::SetMinIntegerDigits(int newValue)
173 {
174         __pIcuNumberFormatter->setMinimumIntegerDigits(newValue);
175 }
176
177 int
178 _NumberFormatterImpl::GetMaxFractionDigits(void) const
179 {
180         return __pIcuNumberFormatter->getMaximumFractionDigits();
181 }
182
183 void
184 _NumberFormatterImpl::SetMaxFractionDigits(int newValue)
185 {
186         __pIcuNumberFormatter->setMaximumFractionDigits(newValue);
187 }
188
189 int
190 _NumberFormatterImpl::GetMinFractionDigits(void) const
191 {
192         return __pIcuNumberFormatter->getMinimumFractionDigits();
193 }
194
195 void
196 _NumberFormatterImpl::SetMinFractionDigits(int newValue)
197 {
198         __pIcuNumberFormatter->setMinimumFractionDigits(newValue);
199 }
200
201 bool
202 _NumberFormatterImpl::IsGroupingUsed(void) const
203 {
204         return __pIcuNumberFormatter->isGroupingUsed();
205 }
206
207 void
208 _NumberFormatterImpl::SetGroupingUsed(bool newValue)
209 {
210         __pIcuNumberFormatter->setGroupingUsed(newValue);
211 }
212
213 String
214 _NumberFormatterImpl::GetPositivePrefix(void) const
215 {
216         IcuUnicodeString icuStr;
217         return _LocaleData::GetOspString(__pIcuNumberFormatter->getPositivePrefix(icuStr));
218 }
219
220 void
221 _NumberFormatterImpl::SetPositivePrefix(const String& newValue)
222 {
223         __pIcuNumberFormatter->setPositivePrefix(_LocaleData::GetIcuString(newValue));
224 }
225
226 String
227 _NumberFormatterImpl::GetNegativePrefix(void) const
228 {
229         IcuUnicodeString icuStr;
230         return _LocaleData::GetOspString(__pIcuNumberFormatter->getNegativePrefix(icuStr));
231 }
232
233 void
234 _NumberFormatterImpl::SetNegativePrefix(const String& newValue)
235 {
236         __pIcuNumberFormatter->setNegativePrefix(_LocaleData::GetIcuString(newValue));
237 }
238
239 String
240 _NumberFormatterImpl::GetPositiveSuffix(void) const
241 {
242         IcuUnicodeString icuStr;
243         return _LocaleData::GetOspString(__pIcuNumberFormatter->getPositiveSuffix(icuStr));
244 }
245
246 void
247 _NumberFormatterImpl::SetPositiveSuffix(const String& newValue)
248 {
249         __pIcuNumberFormatter->setPositiveSuffix(_LocaleData::GetIcuString(newValue));
250 }
251
252 String
253 _NumberFormatterImpl::GetNegativeSuffix(void) const
254 {
255         IcuUnicodeString icuStr;
256         return _LocaleData::GetOspString(__pIcuNumberFormatter->getNegativeSuffix(icuStr));
257 }
258
259 void
260 _NumberFormatterImpl::SetNegativeSuffix(const String& newValue)
261 {
262         __pIcuNumberFormatter->setNegativeSuffix(_LocaleData::GetIcuString(newValue));
263 }
264
265 int
266 _NumberFormatterImpl::GetMultiplier(void) const
267 {
268         return __pIcuNumberFormatter->getMultiplier();
269 }
270
271 result
272 _NumberFormatterImpl::SetMultiplier(int newValue)
273 {
274         __pIcuNumberFormatter->setMultiplier(newValue);
275         return E_SUCCESS;
276 }
277
278 int
279 _NumberFormatterImpl::GetGroupingSize(void) const
280 {
281         return __pIcuNumberFormatter->getGroupingSize();
282 }
283
284 result
285 _NumberFormatterImpl::SetGroupingSize(int newValue)
286 {
287         __pIcuNumberFormatter->setGroupingSize(newValue);
288         return E_SUCCESS;
289 }
290
291 bool
292 _NumberFormatterImpl::IsDecimalSeparatorAlwaysShown(void) const
293 {
294         return __pIcuNumberFormatter->isDecimalSeparatorAlwaysShown();
295 }
296
297 void
298 _NumberFormatterImpl::SetDecimalSeparatorAlwaysShown(bool newValue)
299 {
300         __pIcuNumberFormatter->setDecimalSeparatorAlwaysShown(newValue);
301 }
302
303 bool
304 _NumberFormatterImpl::IsPositiveSignAlwaysShown(void) const
305 {
306         IcuUnicodeString ps("+");
307         IcuUnicodeString pp;
308         pp = __pIcuNumberFormatter->getPositivePrefix(pp);
309
310         return (pp == ps);
311 }
312
313 void
314 _NumberFormatterImpl::SetPositiveSignAlwaysShown(bool newValue)
315 {
316         IcuUnicodeString ps("+");
317         IcuUnicodeString pp;
318         pp = __pIcuNumberFormatter->getPositivePrefix(pp);
319
320         if (newValue)
321         {
322                 __pIcuNumberFormatter->setPositivePrefix(ps);
323         }
324         else
325         {
326                 if (pp == ps)
327                 {
328                         __pIcuNumberFormatter->setPositivePrefix("");
329                 }
330         }
331 }
332
333 } } // Tizen::Locales