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_file_utils.h"
16 
17 #include <iostream>
18 
19 #include "telephony_errors.h"
20 #include "telephony_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 
Open(const std::string & filePath)25 int32_t VCardFileUtils::Open(const std::string &filePath)
26 {
27     char path[PATH_MAX] = { '\0' };
28     if (realpath(filePath.c_str(), path) == nullptr) {
29         TELEPHONY_LOGE("get real path fail");
30         return TELEPHONY_ERR_VCARD_FILE_INVALID;
31     }
32     std::string realPath = path;
33     file_ = std::make_shared<std::ifstream>(realPath);
34     if (!file_->is_open()) {
35         return TELEPHONY_ERR_VCARD_FILE_INVALID;
36     }
37     return TELEPHONY_SUCCESS;
38 }
39 
SetInputStream(std::shared_ptr<std::ifstream> file)40 void VCardFileUtils::SetInputStream(std::shared_ptr<std::ifstream> file)
41 {
42     file_ = file;
43 }
44 
Close()45 int32_t VCardFileUtils::Close()
46 {
47     if (file_ == nullptr) {
48         TELEPHONY_LOGE("file_ is nullptr");
49         return TELEPHONY_ERR_LOCAL_PTR_NULL;
50     }
51     if (file_->is_open()) {
52         file_->close();
53         file_ = nullptr;
54     }
55     return TELEPHONY_SUCCESS;
56 }
57 
ReadLine(std::string & line)58 bool VCardFileUtils::ReadLine(std::string &line)
59 {
60     if (file_ == nullptr) {
61         TELEPHONY_LOGE("file_ is nullptr");
62         return false;
63     }
64     std::getline(*file_, line);
65     return !IsEnd();
66 }
67 
PeekLine(std::string & line)68 bool VCardFileUtils::PeekLine(std::string &line)
69 {
70     if (file_ == nullptr) {
71         TELEPHONY_LOGE("file_ is nullptr");
72         return false;
73     }
74     std::streampos currentPosition = file_->tellg();
75     file_->peek();
76     std::getline(*file_, line);
77     file_->seekg(currentPosition);
78     return !IsEnd();
79 }
80 
Reset()81 void VCardFileUtils::Reset()
82 {
83     if (file_ == nullptr) {
84         TELEPHONY_LOGE("file_ is nullptr");
85         return;
86     }
87     file_->clear();
88     file_->seekg(0, std::ios::beg);
89 }
90 
IsEnd()91 bool VCardFileUtils::IsEnd()
92 {
93     if (file_ == nullptr) {
94         TELEPHONY_LOGE("file_ is nullptr");
95         return false;
96     }
97     return file_->peek() == EOF;
98 }
99 
SkipEmptyLines()100 void VCardFileUtils::SkipEmptyLines()
101 {
102     if (file_ == nullptr) {
103         TELEPHONY_LOGE("file_ is nullptr");
104         return;
105     }
106     std::string line;
107     std::streampos currentPosition = file_->tellg();
108     while (std::getline(*file_, line)) {
109         if (!line.empty()) {
110             file_->seekg(currentPosition);
111             break;
112         }
113         currentPosition = file_->tellg();
114     }
115 }
116 
117 } // namespace Telephony
118 } // namespace OHOS
119