2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "UrlOptions.h"
12 #include "utils/StringUtils.h"
13 #include "utils/log.h"
15 CUrlOptions::CUrlOptions() = default;
17 CUrlOptions::CUrlOptions(const std::string
&options
, const char *strLead
/* = "" */)
23 CUrlOptions::~CUrlOptions() = default;
25 std::string
CUrlOptions::GetOptionsString(bool withLeadingSeparator
/* = false */) const
28 for (const auto &opt
: m_options
)
33 options
+= CURL::Encode(opt
.first
);
34 if (!opt
.second
.empty())
35 options
+= "=" + CURL::Encode(opt
.second
.asString());
38 if (withLeadingSeparator
&& !options
.empty())
40 if (m_strLead
.empty())
41 options
= "?" + options
;
43 options
= m_strLead
+ options
;
49 void CUrlOptions::AddOption(const std::string
&key
, const char *value
)
51 if (key
.empty() || value
== NULL
)
54 return AddOption(key
, std::string(value
));
57 void CUrlOptions::AddOption(const std::string
&key
, const std::string
&value
)
62 m_options
[key
] = value
;
65 void CUrlOptions::AddOption(const std::string
&key
, int value
)
70 m_options
[key
] = value
;
73 void CUrlOptions::AddOption(const std::string
&key
, float value
)
78 m_options
[key
] = value
;
81 void CUrlOptions::AddOption(const std::string
&key
, double value
)
86 m_options
[key
] = value
;
89 void CUrlOptions::AddOption(const std::string
&key
, bool value
)
94 m_options
[key
] = value
;
97 void CUrlOptions::AddOptions(const std::string
&options
)
102 std::string strOptions
= options
;
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) == '|')
109 // remove leading ?, #, ; or | if present
110 if (!m_strLead
.empty())
111 CLog::Log(LOGWARNING
, "{}: original leading str {} overridden by {}", __FUNCTION__
, m_strLead
,
113 m_strLead
= strOptions
.at(0);
114 strOptions
.erase(0, 1);
117 // split the options by & and process them one by one
118 for (const auto &option
: StringUtils::Split(strOptions
, "&"))
123 std::string key
, value
;
125 size_t pos
= option
.find('=');
126 key
= CURL::Decode(option
.substr(0, pos
));
127 if (pos
!= std::string::npos
)
128 value
= CURL::Decode(option
.substr(pos
+ 1));
130 // the key cannot be empty
132 AddOption(key
, value
);
136 void CUrlOptions::AddOptions(const CUrlOptions
&options
)
138 m_options
.insert(options
.m_options
.begin(), options
.m_options
.end());
141 void CUrlOptions::RemoveOption(const std::string
&key
)
146 auto option
= m_options
.find(key
);
147 if (option
!= m_options
.end())
148 m_options
.erase(option
);
151 bool CUrlOptions::HasOption(const std::string
&key
) const
156 return m_options
.find(key
) != m_options
.end();
159 bool CUrlOptions::GetOption(const std::string
&key
, CVariant
&value
) const
164 auto option
= m_options
.find(key
);
165 if (option
== m_options
.end())
168 value
= option
->second
;