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");
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 = GetVersionFromFileUtils(line, index);
77 break;
78 }
79 fileUtils_.Reset();
80 return version;
81 }
82
GetVersionFromFileUtils(const std::string & line,int index)83 std::string VCardDecoder::GetVersionFromFileUtils(const std::string &line, int index)
84 {
85 int versionIndex = index + static_cast<int>(std::string(VCARD_TYPE_VERSION).length()) + 1;
86 if (versionIndex < static_cast<int>(line.length())) {
87 return line.substr(versionIndex);
88 }
89 return "";
90 }
91
GetDecoder(const std::string & version)92 std::shared_ptr<VCardDecoder> VCardDecoder::GetDecoder(const std::string &version)
93 {
94 TELEPHONY_LOGI("Get version result %{public}s", version.c_str());
95 if (version.find(std::string(VERSION_30)) != std::string::npos) {
96 return std::make_shared<VCardDecoderV30>();
97 }
98 if (version.find(std::string(VERSION_40)) != std::string::npos) {
99 return std::make_shared<VCardDecoderV40>();
100 }
101 return std::make_shared<VCardDecoderV21>();
102 }
103
AddVCardDecodeListener(std::shared_ptr<VCardDecodeListener> listener)104 void VCardDecoder::AddVCardDecodeListener(std::shared_ptr<VCardDecodeListener> listener) {}
105
Decode(int32_t & errorCode)106 void VCardDecoder::Decode(int32_t &errorCode) {}
107
DecodeOne(int32_t & errorCode)108 bool VCardDecoder::DecodeOne(int32_t &errorCode)
109 {
110 return false;
111 }
112
IsEnd()113 bool VCardDecoder::IsEnd()
114 {
115 return fileUtils_.IsEnd();
116 }
117
118 } // namespace Telephony
119 } // namespace OHOS
120