1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "measure_format.h"
17
18 using namespace OHOS::I18N;
19
MeasureFormat(LocaleInfo & localeinfo,I18nStatus & status)20 MeasureFormat::MeasureFormat(LocaleInfo &localeinfo, I18nStatus &status)
21 {
22 if (localeinfo.GetId() == nullptr) {
23 status = I18nStatus::IERROR;
24 return;
25 }
26 locale = localeinfo;
27 }
28
Init()29 bool MeasureFormat::Init()
30 {
31 I18nStatus status = I18nStatus::ISUCCESS;
32 if (measureFormatImpl != nullptr) {
33 delete measureFormatImpl;
34 }
35 measureFormatImpl = new MeasureFormatImpl(locale, status);
36 if (measureFormatImpl == nullptr || status != I18nStatus::ISUCCESS) {
37 return false;
38 }
39 DataResource resource(&locale);
40 bool isSuccess = resource.Init();
41 if (!isSuccess) {
42 return false;
43 }
44 return measureFormatImpl->Init(resource);
45 }
46
~MeasureFormat()47 MeasureFormat::~MeasureFormat()
48 {
49 if (measureFormatImpl != nullptr) {
50 delete measureFormatImpl;
51 measureFormatImpl = nullptr;
52 }
53 }
54
Format(double num,std::string unit,I18nStatus & status,MeasureFormatType type)55 std::string MeasureFormat::Format(double num, std::string unit, I18nStatus &status, MeasureFormatType type)
56 {
57 if (!ReInitImpl()) {
58 status = I18nStatus::IERROR;
59 return "";
60 }
61 return measureFormatImpl->Format(num, unit, type, status);
62 }
63
Format(int num,std::string unit,I18nStatus & status,MeasureFormatType type)64 std::string MeasureFormat::Format(int num, std::string unit, I18nStatus &status, MeasureFormatType type)
65 {
66 if (!ReInitImpl()) {
67 status = I18nStatus::IERROR;
68 return "";
69 }
70 return measureFormatImpl->Format(num, unit, type, status);
71 }
72
ReInitImpl()73 bool MeasureFormat::ReInitImpl()
74 {
75 if (measureFormatImpl == nullptr) {
76 bool isSuccess = Init();
77 if (!isSuccess) {
78 return false;
79 }
80 }
81 return true;
82 }