mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
formatting
This commit is contained in:
parent
94a05b854a
commit
cd67331fad
@ -16,14 +16,14 @@ namespace crow
|
||||
// https://github.com/bartgrantham/qs_parse
|
||||
// ----------------------------------------------------------------------------
|
||||
/* Similar to strncmp, but handles URL-encoding for either string */
|
||||
int qs_strncmp(const char * s, const char * qs, size_t n);
|
||||
int qs_strncmp(const char* s, const char* qs, size_t n);
|
||||
|
||||
|
||||
/* Finds the beginning of each key/value pair and stores a pointer in qs_kv.
|
||||
* Also decodes the value portion of the k/v pair *in-place*. In a future
|
||||
* enhancement it will also have a compile-time option of sorting qs_kv
|
||||
* alphabetically by key. */
|
||||
int qs_parse(char * qs, char * qs_kv[], int qs_kv_size, bool parse_url);
|
||||
int qs_parse(char* qs, char* qs_kv[], int qs_kv_size, bool parse_url);
|
||||
|
||||
|
||||
/* Used by qs_parse to decode the value portion of a k/v pair */
|
||||
@ -97,7 +97,7 @@ inline int qs_strncmp(const char * s, const char * qs, size_t n)
|
||||
}
|
||||
|
||||
|
||||
inline int qs_parse(char * qs, char * qs_kv[], int qs_kv_size, bool parse_url = true)
|
||||
inline int qs_parse(char* qs, char* qs_kv[], int qs_kv_size, bool parse_url = true)
|
||||
{
|
||||
int i, j;
|
||||
char * substr_ptr;
|
||||
@ -138,7 +138,7 @@ inline int qs_parse(char * qs, char * qs_kv[], int qs_kv_size, bool parse_url =
|
||||
#endif
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline int qs_decode(char * qs)
|
||||
@ -286,7 +286,7 @@ inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace crow
|
||||
namespace crow
|
||||
{
|
||||
struct request;
|
||||
/// A class to represent any data coming after the `?` in the request URL into key-value pairs.
|
||||
@ -297,35 +297,34 @@ namespace crow
|
||||
|
||||
query_string()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
query_string(const query_string& qs)
|
||||
: url_(qs.url_)
|
||||
query_string(const query_string& qs):
|
||||
url_(qs.url_)
|
||||
{
|
||||
for(auto p:qs.key_value_pairs_)
|
||||
for (auto p : qs.key_value_pairs_)
|
||||
{
|
||||
key_value_pairs_.push_back((char*)(p-qs.url_.c_str()+url_.c_str()));
|
||||
key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
query_string& operator = (const query_string& qs)
|
||||
query_string& operator=(const query_string& qs)
|
||||
{
|
||||
url_ = qs.url_;
|
||||
key_value_pairs_.clear();
|
||||
for(auto p:qs.key_value_pairs_)
|
||||
for (auto p : qs.key_value_pairs_)
|
||||
{
|
||||
key_value_pairs_.push_back((char*)(p-qs.url_.c_str()+url_.c_str()));
|
||||
key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str()));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
query_string& operator = (query_string&& qs)
|
||||
query_string& operator=(query_string&& qs)
|
||||
{
|
||||
key_value_pairs_ = std::move(qs.key_value_pairs_);
|
||||
char* old_data = (char*)qs.url_.c_str();
|
||||
url_ = std::move(qs.url_);
|
||||
for(auto& p:key_value_pairs_)
|
||||
for (auto& p : key_value_pairs_)
|
||||
{
|
||||
p += (char*)url_.c_str() - old_data;
|
||||
}
|
||||
@ -333,8 +332,8 @@ namespace crow
|
||||
}
|
||||
|
||||
|
||||
query_string(std::string params, bool url = true)
|
||||
: url_(std::move(params))
|
||||
query_string(std::string params, bool url = true):
|
||||
url_(std::move(params))
|
||||
{
|
||||
if (url_.empty())
|
||||
return;
|
||||
@ -345,7 +344,7 @@ namespace crow
|
||||
key_value_pairs_.resize(count);
|
||||
}
|
||||
|
||||
void clear()
|
||||
void clear()
|
||||
{
|
||||
key_value_pairs_.clear();
|
||||
url_.clear();
|
||||
@ -354,38 +353,38 @@ namespace crow
|
||||
friend std::ostream& operator<<(std::ostream& os, const query_string& qs)
|
||||
{
|
||||
os << "[ ";
|
||||
for(size_t i = 0; i < qs.key_value_pairs_.size(); ++i) {
|
||||
for (size_t i = 0; i < qs.key_value_pairs_.size(); ++i)
|
||||
{
|
||||
if (i)
|
||||
os << ", ";
|
||||
os << qs.key_value_pairs_[i];
|
||||
}
|
||||
os << " ]";
|
||||
return os;
|
||||
|
||||
}
|
||||
|
||||
/// Get a value from a name, used for `?name=value`.
|
||||
|
||||
///
|
||||
/// Note: this method returns the value of the first occurrence of the key only, to return all occurrences, see \ref get_list().
|
||||
char* get (const std::string& name) const
|
||||
char* get(const std::string& name) const
|
||||
{
|
||||
char* ret = qs_k2v(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size());
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Works similar to \ref get() except it removes the item from the query string.
|
||||
char* pop (const std::string& name)
|
||||
char* pop(const std::string& name)
|
||||
{
|
||||
char* ret = get(name);
|
||||
if (ret != nullptr)
|
||||
{
|
||||
for (unsigned int i = 0; i<key_value_pairs_.size(); i++)
|
||||
for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
|
||||
{
|
||||
std::string str_item(key_value_pairs_[i]);
|
||||
if (str_item.substr(0, name.size()+1) == name+'=')
|
||||
if (str_item.substr(0, name.size() + 1) == name + '=')
|
||||
{
|
||||
key_value_pairs_.erase(key_value_pairs_.begin()+i);
|
||||
key_value_pairs_.erase(key_value_pairs_.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -397,14 +396,14 @@ namespace crow
|
||||
|
||||
///
|
||||
/// Note: Square brackets in the above example are controlled by `use_brackets` boolean (true by default). If set to false, the example becomes `?name=value1,name=value2...name=valuen`
|
||||
std::vector<char*> get_list (const std::string& name, bool use_brackets = true) const
|
||||
std::vector<char*> get_list(const std::string& name, bool use_brackets = true) const
|
||||
{
|
||||
std::vector<char*> ret;
|
||||
std::string plus = name + (use_brackets ? "[]" : "");
|
||||
char* element = nullptr;
|
||||
|
||||
int count = 0;
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
element = qs_k2v(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++);
|
||||
if (!element)
|
||||
@ -415,17 +414,17 @@ namespace crow
|
||||
}
|
||||
|
||||
/// Similar to \ref get_list() but it removes the
|
||||
std::vector<char*> pop_list (const std::string& name, bool use_brackets = true)
|
||||
std::vector<char*> pop_list(const std::string& name, bool use_brackets = true)
|
||||
{
|
||||
std::vector<char*> ret = get_list(name, use_brackets);
|
||||
if (!ret.empty())
|
||||
{
|
||||
for (unsigned int i = 0; i<key_value_pairs_.size(); i++)
|
||||
for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
|
||||
{
|
||||
std::string str_item(key_value_pairs_[i]);
|
||||
if ((use_brackets ? (str_item.substr(0, name.size()+3) == name+"[]=") : (str_item.substr(0, name.size()+1) == name+'=')))
|
||||
if ((use_brackets ? (str_item.substr(0, name.size() + 3) == name + "[]=") : (str_item.substr(0, name.size() + 1) == name + '=')))
|
||||
{
|
||||
key_value_pairs_.erase(key_value_pairs_.begin()+i--);
|
||||
key_value_pairs_.erase(key_value_pairs_.begin() + i--);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -438,12 +437,12 @@ namespace crow
|
||||
/// For example calling `get_dict(yourname)` on `?yourname[sub1]=42&yourname[sub2]=84` would give a map containing `{sub1 : 42, sub2 : 84}`.
|
||||
///
|
||||
/// if your query string has both empty brackets and ones with a key inside, use pop_list() to get all the values without a key before running this method.
|
||||
std::unordered_map<std::string, std::string> get_dict (const std::string& name) const
|
||||
std::unordered_map<std::string, std::string> get_dict(const std::string& name) const
|
||||
{
|
||||
std::unordered_map<std::string, std::string> ret;
|
||||
|
||||
int count = 0;
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++))
|
||||
ret.insert(*element);
|
||||
@ -454,17 +453,17 @@ namespace crow
|
||||
}
|
||||
|
||||
/// Works the same as \ref get_dict() but removes the values from the query string.
|
||||
std::unordered_map<std::string, std::string> pop_dict (const std::string& name)
|
||||
std::unordered_map<std::string, std::string> pop_dict(const std::string& name)
|
||||
{
|
||||
std::unordered_map<std::string, std::string> ret = get_dict(name);
|
||||
if (!ret.empty())
|
||||
{
|
||||
for (unsigned int i = 0; i<key_value_pairs_.size(); i++)
|
||||
for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
|
||||
{
|
||||
std::string str_item(key_value_pairs_[i]);
|
||||
if (str_item.substr(0, name.size()+1) == name+'[')
|
||||
if (str_item.substr(0, name.size() + 1) == name + '[')
|
||||
{
|
||||
key_value_pairs_.erase(key_value_pairs_.begin()+i--);
|
||||
key_value_pairs_.erase(key_value_pairs_.begin() + i--);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -474,7 +473,7 @@ namespace crow
|
||||
std::vector<std::string> keys() const
|
||||
{
|
||||
std::vector<std::string> ret;
|
||||
for (auto element: key_value_pairs_)
|
||||
for (auto element : key_value_pairs_)
|
||||
{
|
||||
std::string str_element(element);
|
||||
ret.emplace_back(str_element.substr(0, str_element.find('=')));
|
||||
@ -487,4 +486,4 @@ namespace crow
|
||||
std::vector<char*> key_value_pairs_;
|
||||
};
|
||||
|
||||
} // end namespace
|
||||
} // namespace crow
|
||||
|
Loading…
Reference in New Issue
Block a user