Ring Daemon 16.0.0
Loading...
Searching...
No Matches
vcard.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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
20namespace vCard {
21
22namespace utils {
23
24std::map<std::string, std::string>
25toMap(std::string_view content)
26{
27 std::map<std::string, std::string> vCard;
28
29 std::string_view line;
30 while (jami::getline(content, line)) {
31 if (line.size()) {
32 const auto dblptPos = line.find(':');
33 if (dblptPos == std::string::npos)
34 continue;
35 vCard.emplace(line.substr(0, dblptPos), line.substr(dblptPos + 1));
36 }
37 }
38 return vCard;
39}
40
41std::map<std::string, std::string>
43{
44 return {
45 {std::string(Property::VCARD_VERSION), "2.1"},
46 {std::string(Property::FORMATTED_NAME), ""},
47 {std::string(Property::PHOTO_PNG), ""},
48 };
49}
50
51
52std::string
53toString(const std::map<std::string, std::string>& vCard)
54{
55 size_t estimatedSize = 0;
56 for (const auto& [key, value] : vCard) {
58 continue;
59 estimatedSize += key.size() + value.size() + 2;
60 }
61 std::string result;
62 result.reserve(estimatedSize + Delimiter::BEGIN_TOKEN.size() + Delimiter::END_LINE_TOKEN.size() + Delimiter::END_TOKEN.size() + Delimiter::END_LINE_TOKEN.size());
63
64 result += Delimiter::BEGIN_TOKEN;
66
67 for (const auto& [key, value] : vCard) {
69 continue;
70 result += key + ':' + value + '\n';
71 }
72
73 result += Delimiter::END_TOKEN;
75
76 return result;
77}
78
79void
80removeByKey(std::map<std::string, std::string>& vCard, std::string_view key) {
81 for (auto it = vCard.begin(); it != vCard.end(); ) {
82 if (jami::starts_with(it->first, key)) {
83 it = vCard.erase(it);
84 } else {
85 ++it;
86 }
87 }
88}
89
90} // namespace utils
91} // namespace vCard
bool getline(std::string_view &str, std::string_view &line, char delim='\n')
Similar to @getline_full but skips empty results.
static bool starts_with(std::string_view str, std::string_view prefix)
std::map< std::string, std::string > toMap(std::string_view content)
Payload to vCard.
Definition vcard.cpp:25
std::map< std::string, std::string > initVcard()
Definition vcard.cpp:42
std::string toString(const std::map< std::string, std::string > &vCard)
Definition vcard.cpp:53
void removeByKey(std::map< std::string, std::string > &vCard, std::string_view key)
Definition vcard.cpp:80
static constexpr std::string_view END_LINE_TOKEN
Definition vcard.h:28
static constexpr std::string_view BEGIN_TOKEN
Definition vcard.h:29
static constexpr std::string_view END_TOKEN_KEY
Definition vcard.h:32
static constexpr std::string_view END_TOKEN
Definition vcard.h:30
static constexpr std::string_view BEGIN_TOKEN_KEY
Definition vcard.h:31
static constexpr std::string_view PHOTO_PNG
Definition vcard.h:71
static constexpr std::string_view FORMATTED_NAME
Definition vcard.h:46
static constexpr std::string_view VCARD_VERSION
Definition vcard.h:38