diff options
author | Rasie1 <rasielll@gmail.com> | 2017-03-16 19:25:27 +0300 |
---|---|---|
committer | Rasie1 <rasielll@gmail.com> | 2017-03-16 19:25:27 +0300 |
commit | 01b21296bcd855cc851417ab704081c49c1b7c90 (patch) | |
tree | ff85e7f30da89e855e3015ec682f865f24affeda /include | |
parent | 32d66d6fd1ac7f90512a573991de4940b5063374 (diff) | |
download | crow-01b21296bcd855cc851417ab704081c49c1b7c90.tar.gz crow-01b21296bcd855cc851417ab704081c49c1b7c90.zip |
Add get_dict method
Diffstat (limited to 'include')
-rw-r--r-- | include/crow/query_string.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/include/crow/query_string.h b/include/crow/query_string.h index 3f8bffe..2740441 100644 --- a/include/crow/query_string.h +++ b/include/crow/query_string.h @@ -5,6 +5,7 @@ #include <string> #include <vector> #include <iostream> +#include <boost/optional.hpp> namespace crow { @@ -197,6 +198,50 @@ inline char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int return NULL; } +inline boost::optional<std::pair<std::string, std::string>> qs_dict_name2kv(const char * dict_name, char * const * qs_kv, int qs_kv_size, int nth = 0) +{ + int i; + size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close; + + name_len = strlen(dict_name); + +#ifdef _qsSORTING +// TODO: binary search for key in the sorted qs_kv +#else // _qsSORTING + for(i=0; i<qs_kv_size; i++) + { + if ( strncmp(dict_name, qs_kv[i], name_len) == 0 ) + { + skip_to_eq = strcspn(qs_kv[i], "="); + if ( qs_kv[i][skip_to_eq] == '=' ) + skip_to_eq++; + skip_to_brace_open = strcspn(qs_kv[i], "["); + if ( qs_kv[i][skip_to_brace_open] == '[' ) + skip_to_brace_open++; + skip_to_brace_close = strcspn(qs_kv[i], "]"); + + std::cout << skip_to_brace_open << " FUFUU " << skip_to_brace_close << std::endl; + + if ( skip_to_brace_open <= skip_to_brace_close && + skip_to_brace_open > 0 && + skip_to_brace_close > 0 && + nth == 0 ) + { + auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open); + auto value = std::string(qs_kv[i] + skip_to_eq); + return boost::make_optional(std::make_pair(key, value)); + } + else + { + --nth; + } + } + } +#endif // _qsSORTING + + return boost::none; +} + inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len) { @@ -336,6 +381,21 @@ namespace crow return ret; } + std::vector<std::pair<std::string, std::string>> get_dict (const std::string& name) const + { + std::vector<std::pair<std::string, std::string>> ret; + std::string plus = name; + + int count = 0; + while(1) + { + if (auto element = qs_dict_name2kv(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++)) + ret.push_back(*element); + else + break; + } + return ret; + } private: std::string url_; |