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 101 : toMap(std::string_view content) 26 : { 27 101 : std::map<std::string, std::string> vCard; 28 : 29 101 : std::string_view line; 30 255 : while (jami::getline(content, line)) { 31 154 : if (line.size()) { 32 154 : const auto dblptPos = line.find(':'); 33 154 : if (dblptPos == std::string::npos) 34 28 : continue; 35 126 : vCard.emplace(line.substr(0, dblptPos), line.substr(dblptPos + 1)); 36 : } 37 : } 38 202 : 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 : } // namespace utils 80 : 81 : } // namespace vCard