summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/UrlOptions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/UrlOptions.cpp')
-rw-r--r--xbmc/utils/UrlOptions.cpp169
1 files changed, 169 insertions, 0 deletions
diff --git a/xbmc/utils/UrlOptions.cpp b/xbmc/utils/UrlOptions.cpp
new file mode 100644
index 0000000..2aa304b
--- /dev/null
+++ b/xbmc/utils/UrlOptions.cpp
@@ -0,0 +1,169 @@
1/*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "UrlOptions.h"
10
11#include "URL.h"
12#include "utils/StringUtils.h"
13#include "utils/log.h"
14
15CUrlOptions::CUrlOptions() = default;
16
17CUrlOptions::CUrlOptions(const std::string &options, const char *strLead /* = "" */)
18 : m_strLead(strLead)
19{
20 AddOptions(options);
21}
22
23CUrlOptions::~CUrlOptions() = default;
24
25std::string CUrlOptions::GetOptionsString(bool withLeadingSeparator /* = false */) const
26{
27 std::string options;
28 for (const auto &opt : m_options)
29 {
30 if (!options.empty())
31 options += "&";
32
33 options += CURL::Encode(opt.first);
34 if (!opt.second.empty())
35 options += "=" + CURL::Encode(opt.second.asString());
36 }
37
38 if (withLeadingSeparator && !options.empty())
39 {
40 if (m_strLead.empty())
41 options = "?" + options;
42 else
43 options = m_strLead + options;
44 }
45
46 return options;
47}
48
49void CUrlOptions::AddOption(const std::string &key, const char *value)
50{
51 if (key.empty() || value == NULL)
52 return;
53
54 return AddOption(key, std::string(value));
55}
56
57void CUrlOptions::AddOption(const std::string &key, const std::string &value)
58{
59 if (key.empty())
60 return;
61
62 m_options[key] = value;
63}
64
65void CUrlOptions::AddOption(const std::string &key, int value)
66{
67 if (key.empty())
68 return;
69
70 m_options[key] = value;
71}
72
73void CUrlOptions::AddOption(const std::string &key, float value)
74{
75 if (key.empty())
76 return;
77
78 m_options[key] = value;
79}
80
81void CUrlOptions::AddOption(const std::string &key, double value)
82{
83 if (key.empty())
84 return;
85
86 m_options[key] = value;
87}
88
89void CUrlOptions::AddOption(const std::string &key, bool value)
90{
91 if (key.empty())
92 return;
93
94 m_options[key] = value;
95}
96
97void CUrlOptions::AddOptions(const std::string &options)
98{
99 if (options.empty())
100 return;
101
102 std::string strOptions = options;
103
104 // if matching the preset leading str, remove from options.
105 if (!m_strLead.empty() && strOptions.compare(0, m_strLead.length(), m_strLead) == 0)
106 strOptions.erase(0, m_strLead.length());
107 else if (strOptions.at(0) == '?' || strOptions.at(0) == '#' || strOptions.at(0) == ';' || strOptions.at(0) == '|')
108 {
109 // remove leading ?, #, ; or | if present
110 if (!m_strLead.empty())
111 CLog::Log(LOGWARNING, "%s: original leading str %s overridden by %c", __FUNCTION__, m_strLead.c_str(), strOptions.at(0));
112 m_strLead = strOptions.at(0);
113 strOptions.erase(0, 1);
114 }
115
116 // split the options by & and process them one by one
117 for (const auto &option : StringUtils::Split(strOptions, "&"))
118 {
119 if (option.empty())
120 continue;
121
122 std::string key, value;
123
124 size_t pos = option.find('=');
125 key = CURL::Decode(option.substr(0, pos));
126 if (pos != std::string::npos)
127 value = CURL::Decode(option.substr(pos + 1));
128
129 // the key cannot be empty
130 if (!key.empty())
131 AddOption(key, value);
132 }
133}
134
135void CUrlOptions::AddOptions(const CUrlOptions &options)
136{
137 m_options.insert(options.m_options.begin(), options.m_options.end());
138}
139
140void CUrlOptions::RemoveOption(const std::string &key)
141{
142 if (key.empty())
143 return;
144
145 auto option = m_options.find(key);
146 if (option != m_options.end())
147 m_options.erase(option);
148}
149
150bool CUrlOptions::HasOption(const std::string &key) const
151{
152 if (key.empty())
153 return false;
154
155 return m_options.find(key) != m_options.end();
156}
157
158bool CUrlOptions::GetOption(const std::string &key, CVariant &value) const
159{
160 if (key.empty())
161 return false;
162
163 auto option = m_options.find(key);
164 if (option == m_options.end())
165 return false;
166
167 value = option->second;
168 return true;
169}