query.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009 zooml.com
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #ifndef __TERRALIB_COMMON_INTERNAL_QUERY_H
23 #define __TERRALIB_COMMON_INTERNAL_QUERY_H
24 #include "../Config.h"
25 #include "utils.h"
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 namespace te
30 {
31  namespace common
32  {
33  namespace uri
34  {
35  /** \brief URI query component.
36  *
37  * This parses the query component into a list of
38  * string pairs, the first is the key and the second the value.
39  *
40  * Syntax (see uri_path for additional definitions): Both key and
41  * value are pct-decoded after parsing.
42  * <pre>
43  * query = kv-pair *( "&" kv-pair )
44  * kv-pair = key "=" [ value ]
45  * key = 1*qchar
46  * value = 1*qchar
47  * qchar = <pchar except "=" and "&"> | "/" | "?"
48  * </pre>
49  *
50  * Note that this provides optional sorting for faster finds
51  * when there are a non-trivial number of pairs.
52  * However, once the query is sorted pairs must not be inserted.
53  * @see http://tools.ietf.org/html/rfc3986#section-3.4 */
54  class TECOMMONEXPORT query : public std::vector<std::pair<std::string, std::string> > {
55  public:
56  query(); ///< Construct.
57  /// Construct from encoded string, for example "a=foo%20bar&b=1",
58  /// and sort the keys if \p dosort is true.
59  /// Note that this should not contain the leading '?'.
60  /// See \c parse() for less strict construction.
61  /// @exception std::invalid_argument if missing key or invalid encoding
62  query(const std::string& v, bool dosort = false);
63  /// Sort the pairs by key. This will speed the find methods if there
64  /// are more than a trivial number of pairs.
65  void sort();
66  bool sorted() const {return sorted_;} ///< Test if sort() has been called.
67  std::string encoding() const; ///< Calculate encoded string.
68  /// Find the const_iterator for the key. Note the search is linear
69  /// if not sorted.
70  const_iterator find(const std::string& key) const;
71  /// Find the iterator for the key. Note the search is linear
72  /// if not sorted.
73  iterator find(const std::string& key);
74  /// Find the key and convert its associated value, returning true
75  /// if the key is found and is_null true if the key is found but the
76  /// value string is empty or isspace. The value is only set if found
77  /// but not null. Note the search is linear.
78  /// @exception std::invalid_argument if not null and cannot convert
79  template<typename T> bool find(const std::string& key, T& value, bool& is_null) const {
80  const_iterator it = find(key);
81  if (it == end())
82  return false;
83  is_null = !convert(it->second, value);
84  return true;
85  }
86  static const char PAIRS_SEP_CHAR; ///< pairs separator char ('&')
87  static const char PAIRS_SEP_CHAR2; ///< pairs separator char (';') // added by Lauro
88  static const char KEY_VALUE_SEP_CHAR; ///< key-value separator char ('=')
89  private:
90  friend bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, query& v, std::string* errs);
91  bool sorted_;
92  };
93  /// Parse URI query, returning whether found or not
94  /// and advancing first and setting query if found.
95  /// Does not skip leading space.
96  ///
97  /// If \p errs is specified the following take place:<ul>
98  /// <li>Errors in parsing subcomponents, after the first, do not
99  /// cause an immediate false return and errors messages
100  /// are reported in the error string \p errs.</li>
101  /// <li>Errors are handled as follows:<ul>
102  /// <li>Key-value pairs with an empty key are ignored.</li>
103  /// <li>Keys with missing '=' are ignored.</li>
104  /// <li>Errors decoding a key or value result in no
105  /// decoding at all (i.e. assumes an unencoded '%').</li>
106  /// </ul></li>
107  /// </ul>
108  bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, query& v, std::string* errs = 0);
109  /// Stream out query encoding.
110  inline std::ostream& operator <<(std::ostream& os, const query& v) {return os << v.encoding();}
111  }
112  }
113 }
114 #endif
static const char KEY_VALUE_SEP_CHAR
key-value separator char ('=')
Definition: query.h:88
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Parse URI authority, returning whether found or not and advancing first and setting authority if foun...
std::string encoding() const
Calculate encoded string.
static const char PAIRS_SEP_CHAR2
pairs separator char (';') // added by Lauro
Definition: query.h:87
bool sorted() const
Test if sort() has been called.
Definition: query.h:66
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
static const char PAIRS_SEP_CHAR
pairs separator char ('&')
Definition: query.h:86
bool find(const std::string &key, T &value, bool &is_null) const
Find the key and convert its associated value, returning true if the key is found and is_null true if...
Definition: query.h:79
URI query component.
Definition: query.h:54
std::string TECOMMONEXPORT convert(const path &v)
URI path to string.