An URL query builder.
Provides the ability to build an URL's query portion using an accumulation of key/value parameters.
Usage example:
#include <network/YiUrlQuery.h>
CYIUrlQuery::CYIUrlQuery | ( | ) |
CYIUrlQuery::CYIUrlQuery | ( | const CYIUrl & | url | ) |
CYIUrlQuery::CYIUrlQuery | ( | const CYIString & | query, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Encoded |
||
) |
Creates a CYIUrlQuery object from the query string query.
The encoding parameter can be used to specify the encoding of the query. The parameter delimiters, '&' and '=', are expected to be unencoded. By default, the provided query string is expected to be encoded.
void CYIUrlQuery::Add | ( | const CYIString & | key, |
const CYIString & | value, | ||
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) |
Add the key/value pair as a parameter using a CYIString value at the end of the parameters list.
Add("myKey", "myValue") is roughly equivalent to appending &myKey=myValue to the query.
The encoding parameter can be used to specify the encoding of the provided key and value. By default, the key and value is assumed to be decoded.
void CYIUrlQuery::Clear | ( | ) |
Remove all parameters from this query.
Returns the parameters list. The pairs in the returned vector are composed of keys and values, respectively.
The encoding parameter can be used to specify the encoding of the returned keys and values. By default, this function returns decoded keys and values.
std::vector<std::pair<CYIString, CYIString> > CYIUrlQuery::GetParameters | ( | CYIUrl::Encoding | encoding | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Finds and returns the value associated with the first parameter whose key matches key. If no such parameter exists, an empty string is returned. A pointer to a boolean, pNotFound, can optionally be provided – if the key is not found, its pointed-to boolean will be set to true.
CYIString CYIUrlQuery::GetValue | ( | const CYIString & | key, |
CYIUrl::Encoding | encoding, | ||
bool * | pNotFound = nullptr |
||
) | const |
Finds and returns the value associated with the first parameter whose key matches key. If no such parameter exists, an empty string is returned. A pointer to a boolean, pNotFound, can optionally be provided – if the key is not found, its pointed-to boolean will be set to true.
The encoding parameter can be used to specify the encoding of the provided key and of the returned value. By default, key and value are assumed to be decoded.
std::vector<CYIString> CYIUrlQuery::GetValues | ( | const CYIString & | key, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) | const |
Returns all values associated with keys that match key. If no parameter matches key key, an empty vector is returned.
The encoding parameter can be used to specify the encoding of the provided key and of the returned values. By default, the key is assumed to be decoded.
bool CYIUrlQuery::HasKey | ( | const CYIString & | key, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) | const |
Returns true if this query contains at least one parameter whose key matches key.
The encoding parameter can be used to specify the encoding of the provided key. By default, the key is assumed to be decoded.
bool CYIUrlQuery::IsEmpty | ( | ) | const |
Returns true if this query has no parameters.
bool CYIUrlQuery::operator!= | ( | const CYIUrlQuery & | other | ) | const |
bool CYIUrlQuery::operator== | ( | const CYIUrlQuery & | other | ) | const |
void CYIUrlQuery::Remove | ( | const CYIString & | key, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) |
Remove the first parameter whose key is key.
The encoding parameter can be used to specify the encoding of the provided key key. By default, the key is assumed to be decoded.
void CYIUrlQuery::RemoveAll | ( | const CYIString & | key, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) |
Remove all parameters which use the key Key.
The encoding parameter can be used to specify the encoding of the provided key. By default, the key is assumed to be decoded.
CYIString CYIUrlQuery::RetrieveAndRemoveQueryString | ( | const CYIString & | key, |
bool * | pSuccess = nullptr |
||
) |
Returns a string generated from the specified query key of this URL Query Builder and removes it.
void CYIUrlQuery::SetParameters | ( | const std::vector< std::pair< CYIString, CYIString >> & | parameters, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Decoded |
||
) |
Sets the parameters contained in this query to parameters. All preexisting parameters are removed.
The encoding parameter can be used to specify the encoding of the provided keys and values. By default, the keys and values are assumed to be decoded.
void CYIUrlQuery::SetString | ( | const CYIString & | query, |
CYIUrl::Encoding | encoding = CYIUrl::Encoding::Encoded |
||
) |
Sets the parameters of this query by parsing the provided query string.
Example: Calling this function with 'fruit=banana&vegetable=lettuce' will result in a query with the keys 'fruit' and 'vegetable' (with associated values 'banana' and 'lettuce').
The encoding parameter can be used to specify the encoding of the query. The parameter delimiters, '&' and '=', are expected to be unencoded. By default, the query is assumed to be encoded.
CYIString CYIUrlQuery::ToString | ( | CYIUrl::Encoding | encoding = CYIUrl::Encoding::Encoded | ) | const |
Returns a string generated from the components of this URL Query Builder.
If, for example, you have a parameter 'en' with a value of 'ferret', and another parameter 'latin' with the value of 'Mustela putorius furo', this function will return
The encoding parameter can be used to specify the encoding of the returned query. By default, the parameters of this query are encoded prior to being combined into a query string.