Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : #include "vcard.h" 18 : #include "string_utils.h" 19 : 20 : namespace vCard { 21 : 22 : namespace utils { 23 : 24 : std::map<std::string, std::string> 25 95 : toMap(std::string_view content) 26 : { 27 95 : std::map<std::string, std::string> vCard; 28 : 29 95 : std::string_view line; 30 237 : while (jami::getline(content, line)) { 31 142 : if (line.size()) { 32 142 : const auto dblptPos = line.find(':'); 33 142 : if (dblptPos == std::string::npos) 34 26 : continue; 35 116 : vCard.emplace(line.substr(0, dblptPos), line.substr(dblptPos + 1)); 36 : } 37 : } 38 190 : return vCard; 39 0 : } 40 : 41 : std::map<std::string, std::string> 42 0 : initVcard() 43 : { 44 : return { 45 0 : {std::string(Property::VCARD_VERSION), "2.1"}, 46 0 : {std::string(Property::FORMATTED_NAME), ""}, 47 0 : {std::string(Property::PHOTO_PNG), ""}, 48 0 : }; 49 : } 50 : 51 : 52 : std::string 53 0 : toString(const std::map<std::string, std::string>& vCard) 54 : { 55 0 : size_t estimatedSize = 0; 56 0 : for (const auto& [key, value] : vCard) { 57 0 : if (Delimiter::BEGIN_TOKEN_KEY == key || Delimiter::END_TOKEN_KEY == key) 58 0 : continue; 59 0 : estimatedSize += key.size() + value.size() + 2; 60 : } 61 0 : std::string result; 62 0 : result.reserve(estimatedSize + Delimiter::BEGIN_TOKEN.size() + Delimiter::END_LINE_TOKEN.size() + Delimiter::END_TOKEN.size() + Delimiter::END_LINE_TOKEN.size()); 63 : 64 0 : result += Delimiter::BEGIN_TOKEN; 65 0 : result += Delimiter::END_LINE_TOKEN; 66 : 67 0 : for (const auto& [key, value] : vCard) { 68 0 : if (Delimiter::BEGIN_TOKEN_KEY == key || Delimiter::END_TOKEN_KEY == key) 69 0 : continue; 70 0 : result += key + ':' + value + '\n'; 71 : } 72 : 73 0 : result += Delimiter::END_TOKEN; 74 0 : result += Delimiter::END_LINE_TOKEN; 75 : 76 0 : return result; 77 0 : } 78 : 79 : void 80 0 : removeByKey(std::map<std::string, std::string>& vCard, std::string_view key) { 81 0 : for (auto it = vCard.begin(); it != vCard.end(); ) { 82 0 : if (jami::starts_with(it->first, key)) { 83 0 : it = vCard.erase(it); 84 : } else { 85 0 : ++it; 86 : } 87 : } 88 0 : } 89 : 90 : } // namespace utils 91 : } // namespace vCard