• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_v30.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 #include "vcard_constant.h"
20 #include "vcard_file_utils.h"
21 #include "vcard_utils.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 
ReadBegin()26 bool VCardDecoderV30::ReadBegin()
27 {
28     return VCardDecoderV21::ReadBegin();
29 }
30 
GetLine()31 std::string VCardDecoderV30::GetLine()
32 {
33     std::string line = "";
34     if (!preLine_.empty()) {
35         line = preLine_;
36         preLine_ = "";
37         return line;
38     }
39     fileUtils_.ReadLine(line);
40     return line;
41 }
42 
PeekLine()43 std::string VCardDecoderV30::PeekLine()
44 {
45     if (!preLine_.empty()) {
46         return preLine_;
47     }
48     std::string line = "";
49     fileUtils_.PeekLine(line);
50     return line;
51 }
52 
GetNonEmptyLine()53 std::string VCardDecoderV30::GetNonEmptyLine()
54 {
55     std::string line;
56     std::string ret = "";
57     while (fileUtils_.ReadLine(line)) {
58         if (line.empty()) {
59             continue;
60         }
61         if (line[0] == ' ' || line[0] == '\t') {
62             if (!preLine_.empty()) {
63                 ret += preLine_;
64                 preLine_ = "";
65             }
66             ret += line.substr(1);
67             continue;
68         }
69         std::string tempPreLine = preLine_;
70         VCardUtils::Trim(tempPreLine);
71         if (!ret.empty() || !tempPreLine.empty()) {
72             break;
73         }
74         preLine_ = line;
75     }
76     if (ret.empty()) {
77         ret = preLine_;
78     }
79     preLine_ = line;
80     return VCardUtils::Trim(ret);
81 }
82 
DealParams(const std::string & params,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)83 void VCardDecoderV30::DealParams(const std::string &params, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
84 {
85     VCardDecoderV21::DealParams(params, rawData, errorCode);
86     if (errorCode == TELEPHONY_SUCCESS) {
87         return;
88     }
89     auto strs = VCardUtils::Split(params, "=");
90     if (static_cast<int32_t>(strs.size()) == SIZE_TWO) {
91         std::string name = VCardUtils::ToUpper(VCardUtils::Trim(strs[0]));
92         std::string value = VCardUtils::Trim(strs[1]);
93         DealAnyParam(name, value, rawData, errorCode);
94         errorCode = TELEPHONY_SUCCESS;
95         return;
96     }
97     errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
98 }
99 
EncodeParamValue(const std::string & value)100 std::string VCardDecoderV30::EncodeParamValue(const std::string &value)
101 {
102     int32_t errorCode = TELEPHONY_SUCCESS;
103     return VCardUtils::ConvertCharset(value, "", DEFAULT_IMPORT_CHARSET, errorCode);
104 }
105 
DealAnyParam(const std::string & param,const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)106 void VCardDecoderV30::DealAnyParam(
107     const std::string &param, const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
108 {
109     DealParamV30(param, paramValue, rawData, errorCode);
110 }
111 
DealNoNameParam(const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)112 void VCardDecoderV30::DealNoNameParam(
113     const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
114 {
115     DealTypeParam(paramValue, rawData, errorCode);
116 }
117 
DealTypeParam(const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)118 void VCardDecoderV30::DealTypeParam(
119     const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
120 {
121     DealParamV30(VCARD_PARAM_TYPE, paramValue, rawData, errorCode);
122 }
123 
DealAgent(std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)124 void VCardDecoderV30::DealAgent(std::shared_ptr<VCardRawData> rawData, int32_t &errorCode) {}
125 
UnescapeText(const std::string & from)126 std::string VCardDecoderV30::UnescapeText(const std::string &from)
127 {
128     std::string result;
129     for (size_t i = 0; i < from.length(); i++) {
130         auto ch = from[i];
131         if (ch == '\\' && i < from.length() - 1) {
132             char nextCh = from[++i];
133             if (nextCh == 'n' || nextCh == 'N') {
134                 result.append("\n");
135                 continue;
136             }
137             result += nextCh;
138             continue;
139         }
140         result += ch;
141     }
142     return result;
143 }
144 
UnescapeChar(char ch)145 std::string VCardDecoderV30::UnescapeChar(char ch)
146 {
147     if (ch == 'n' || ch == 'N') {
148         return "\n";
149     }
150     return std::string(1, ch);
151 }
152 
DealParamV30(const std::string & param,const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)153 void VCardDecoderV30::DealParamV30(
154     const std::string &param, const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
155 {
156     auto quoted = false;
157     std::string value = "";
158     for (size_t i = 0; i < paramValue.length(); i++) {
159         auto ch = paramValue[i];
160         if (ch == '"') {
161             if (quoted) {
162                 rawData->AppendParameter(param, EncodeParamValue(value));
163                 value.clear();
164                 quoted = false;
165                 continue;
166             }
167             quoted = true;
168             continue;
169         }
170         if (ch == ',' && !quoted) {
171             if (value.empty()) {
172                 continue;
173             }
174             rawData->AppendParameter(param, EncodeParamValue(value));
175             value.clear();
176             continue;
177         }
178         value += ch;
179     }
180     if (quoted) {
181         TELEPHONY_LOGI("non quote at end");
182     }
183     if (value.empty()) {
184         TELEPHONY_LOGI("value is empty");
185         return;
186     }
187     rawData->AppendParameter(param, EncodeParamValue(value));
188 }
189 
GetVersion()190 std::string VCardDecoderV30::GetVersion()
191 {
192     return VERSION_30;
193 }
194 
GetBase64(const std::string & value,int32_t & errorCode)195 std::string VCardDecoderV30::GetBase64(const std::string &value, int32_t &errorCode)
196 {
197     return value;
198 }
199 
GetSupportType()200 std::vector<std::string> VCardDecoderV30::GetSupportType()
201 {
202     return { VCARD_TYPE_BEGIN, VCARD_TYPE_END, VCARD_TYPE_LOGO, VCARD_TYPE_PHOTO, "LABEL", VCARD_TYPE_FN, "TITLE",
203         "SOUND", VCARD_TYPE_VERSION, VCARD_TYPE_TEL, VCARD_TYPE_EMAIL, "TZ", "GEO", VCARD_TYPE_NOTE, VCARD_TYPE_URL,
204         VCARD_TYPE_BDAY, "ROLE", "REV", "UID", "KEY", "MAILER", "NAME", "PROFILE", "SOURCE", VCARD_TYPE_NICKNAME,
205         "CLASS", "SORT-STRING", "CATEGORIES", "PRODID", "IMPP" };
206 }
207 } // namespace Telephony
208 } // namespace OHOS
209