/* * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "vcard_decoder.h" #include "telephony_errors.h" #include "telephony_log_wrapper.h" #include "vcard_decoder_v21.h" #include "vcard_decoder_v30.h" #include "vcard_decoder_v40.h" #include "vcard_utils.h" namespace OHOS { namespace Telephony { VCardFileUtils VCardDecoder::fileUtils_; VCardDecoder::VCardDecoder() {} VCardDecoder::~VCardDecoder() {} std::shared_ptr VCardDecoder::Create(const std::string &path, int32_t &errorCode) { if (!VCardUtils::EndWith(path, VCARD_FILE_EXTENSION)) { errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID; return nullptr; } fileUtils_.Close(); errorCode = fileUtils_.Open(path); if (errorCode != TELEPHONY_SUCCESS) { TELEPHONY_LOGE("Failed to read path"); fileUtils_.Close(); errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID; return nullptr; } errorCode = TELEPHONY_SUCCESS; return GetDecoder(GetVersion()); } std::shared_ptr VCardDecoder::Create(std::shared_ptr file, int32_t &errorCode) { fileUtils_.Close(); if (file == nullptr) { TELEPHONY_LOGE("file is nullptr"); errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL; } fileUtils_.SetInputStream(file); errorCode = TELEPHONY_SUCCESS; return GetDecoder(GetVersion()); } void VCardDecoder::Close() { fileUtils_.Close(); } std::string VCardDecoder::GetVersion() { std::string line; std::string version; while (fileUtils_.ReadLine(line)) { auto index = line.find(VCARD_TYPE_VERSION); if (index == std::string::npos) { continue; } version = GetVersionFromFileUtils(line, index); break; } fileUtils_.Reset(); return version; } std::string VCardDecoder::GetVersionFromFileUtils(const std::string &line, int index) { int versionIndex = index + static_cast(std::string(VCARD_TYPE_VERSION).length()) + 1; if (versionIndex < static_cast(line.length())) { return line.substr(versionIndex); } return ""; } std::shared_ptr VCardDecoder::GetDecoder(const std::string &version) { TELEPHONY_LOGI("Get version result %{public}s", version.c_str()); if (version.find(std::string(VERSION_30)) != std::string::npos) { return std::make_shared(); } if (version.find(std::string(VERSION_40)) != std::string::npos) { return std::make_shared(); } return std::make_shared(); } void VCardDecoder::AddVCardDecodeListener(std::shared_ptr listener) {} void VCardDecoder::Decode(int32_t &errorCode) {} bool VCardDecoder::DecodeOne(int32_t &errorCode) { return false; } bool VCardDecoder::IsEnd() { return fileUtils_.IsEnd(); } } // namespace Telephony } // namespace OHOS