1 /*
2 * Copyright (C) 2023 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 #include "vcard_decoder.h"
16
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 #include "vcard_decoder_v21.h"
20 #include "vcard_decoder_v30.h"
21 #include "vcard_decoder_v40.h"
22 #include "vcard_utils.h"
23
24 namespace OHOS {
25 namespace Telephony {
26
27 VCardFileUtils VCardDecoder::fileUtils_;
28
VCardDecoder()29 VCardDecoder::VCardDecoder() {}
~VCardDecoder()30 VCardDecoder::~VCardDecoder() {}
31
Create(const std::string & path,int32_t & errorCode)32 std::shared_ptr<VCardDecoder> VCardDecoder::Create(const std::string &path, int32_t &errorCode)
33 {
34 if (!VCardUtils::EndWith(path, VCARD_FILE_EXTENSION)) {
35 errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
36 return nullptr;
37 }
38 fileUtils_.Close();
39 errorCode = fileUtils_.Open(path);
40 if (errorCode != TELEPHONY_SUCCESS) {
41 TELEPHONY_LOGE("Failed to read path %{public}s", path.c_str());
42 fileUtils_.Close();
43 errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
44 return nullptr;
45 }
46 errorCode = TELEPHONY_SUCCESS;
47 return GetDecoder(GetVersion());
48 }
49
Create(std::shared_ptr<std::ifstream> file,int32_t & errorCode)50 std::shared_ptr<VCardDecoder> VCardDecoder::Create(std::shared_ptr<std::ifstream> file, int32_t &errorCode)
51 {
52 fileUtils_.Close();
53 if (file == nullptr) {
54 TELEPHONY_LOGE("file is nullptr");
55 errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
56 }
57 fileUtils_.SetInputStream(file);
58 errorCode = TELEPHONY_SUCCESS;
59 return GetDecoder(GetVersion());
60 }
61
Close()62 void VCardDecoder::Close()
63 {
64 fileUtils_.Close();
65 }
66
GetVersion()67 std::string VCardDecoder::GetVersion()
68 {
69 std::string line;
70 std::string version;
71 while (fileUtils_.ReadLine(line)) {
72 auto index = line.find(VCARD_TYPE_VERSION);
73 if (index == std::string::npos) {
74 continue;
75 }
76 version = line.substr(index + std::string(VCARD_TYPE_VERSION).length() + 1);
77 break;
78 }
79 fileUtils_.Reset();
80 return version;
81 }
82
GetDecoder(const std::string & version)83 std::shared_ptr<VCardDecoder> VCardDecoder::GetDecoder(const std::string &version)
84 {
85 TELEPHONY_LOGI("Get version result %{public}s", version.c_str());
86 if (version.find(std::string(VERSION_30)) != std::string::npos) {
87 return std::make_shared<VCardDecoderV30>();
88 }
89 if (version.find(std::string(VERSION_40)) != std::string::npos) {
90 return std::make_shared<VCardDecoderV40>();
91 }
92 return std::make_shared<VCardDecoderV21>();
93 }
94
AddVCardDecodeListener(std::shared_ptr<VCardDecodeListener> listener)95 void VCardDecoder::AddVCardDecodeListener(std::shared_ptr<VCardDecodeListener> listener) {}
96
Decode(int32_t & errorCode)97 void VCardDecoder::Decode(int32_t &errorCode) {}
98
DecodeOne(int32_t & errorCode)99 bool VCardDecoder::DecodeOne(int32_t &errorCode)
100 {
101 return false;
102 }
103
IsEnd()104 bool VCardDecoder::IsEnd()
105 {
106 return fileUtils_.IsEnd();
107 }
108
109 } // namespace Telephony
110 } // namespace OHOS
111