query_params.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003,2004 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_QUERY_PARAMS_H
30 #define CXXTOOLS_QUERY_PARAMS_H
31 
32 #include <string>
33 #include <vector>
34 #include <set>
35 #include <map>
36 #include <algorithm>
37 #include <iostream>
38 #include <iterator>
39 
40 namespace cxxtools
41 {
42 
55 {
56  public:
57  struct value_type
58  {
59  std::string name;
60  std::string value;
61 
62  value_type() { }
63  value_type(const std::string& name_, const std::string& value_)
64  : name(name_),
65  value(value_)
66  { }
67  };
68  typedef std::vector<value_type> values_type;
69  typedef values_type::size_type size_type;
70 
75  : public std::iterator<std::bidirectional_iterator_tag, const std::string>
76  {
77  const QueryParams* params;
78  std::string name;
79  size_type pos;
80 
81  size_type size() const
82  {
83  return (name.empty() ? params->paramcount()
84  : params->paramcount(name));
85  }
86 
87  bool is_end() const
88  {
89  return params == 0 || pos >= size();
90  }
91 
92  size_type getpos() const
93  {
94  return is_end() ? size() : pos;
95  }
96 
97  public:
100  : params(0),
101  pos(0)
102  { }
103 
105  explicit const_iterator(const QueryParams& p)
106  : params(&p),
107  pos(0)
108  { }
109 
111  const_iterator(const QueryParams& p, const std::string& n)
112  : params(&p),
113  name(n),
114  pos(0)
115  { }
116 
117  bool operator== (const const_iterator& it) const
118  {
119  bool e = it.is_end();
120  return is_end() ? e
121  : !e && pos == it.pos;
122  }
123 
124  bool operator!= (const const_iterator& it) const
125  {
126  bool e = it.is_end();
127  return is_end() ? !e
128  : e || pos != it.pos;
129  }
130 
132  {
133  ++pos;
134  return *this;
135  }
136 
138  {
139  const_iterator it = *this;
140  ++pos;
141  return it;
142  }
143 
145  {
146  --pos;
147  return *this;
148  }
149 
151  {
152  const_iterator it = *this;
153  --pos;
154  return it;
155  }
156 
157  reference operator*() const
158  {
159  return name.empty() ? params->param(pos)
160  : params->param(name, pos);
161  }
162 
163  pointer operator->() const
164  {
165  return &(operator*());
166  }
167 
168  };
169 
170  private:
171  values_type _values;
172 
173  public:
176  { }
177 
178  explicit QueryParams(const std::string& url)
179  {
180  parse_url(url);
181  }
182 
183  explicit QueryParams(const char* url)
184  {
185  parse_url(url);
186  }
187 
189  void parse_url(const std::string& url);
191  void parse_url(const char* url);
193  void parse_url(std::istream& url_stream);
194 
195  //
196  // unnamed parameter
197  //
198 
200  const std::string& param(size_type n) const
201  { return param(std::string(), n); }
202 
205  { return paramcount(std::string()); }
206 
208  const std::string& operator[] (size_type n) const
209  { return param(n); }
210 
212  QueryParams& add(const std::string& value)
213  {
214  _values.push_back(value_type(std::string(), value));
215  return *this;
216  }
217 
218  //
219  // named parameter
220  //
221 
223  const std::string& param(const std::string& name, size_type n = 0) const;
224 
226  std::string param(const std::string& name, size_type n,
227  const std::string& def) const;
228 
230  std::string param(const std::string& name, const std::string& def) const
231  { return param(name, 0, def); }
232 
234  size_type paramcount(const std::string& name) const;
235 
237  std::string operator[] (const std::string& name) const
238  { return param(name, 0, std::string()); }
239 
241  bool has(const std::string& name) const;
242 
244  QueryParams& add(const std::string& name, const std::string& value)
245  {
246  _values.push_back(value_type(name, value));
247  return *this;
248  }
249 
250  QueryParams& add(const QueryParams& other)
251  {
252  _values.insert(_values.end(), other._values.begin(), other._values.end());
253  return *this;
254  }
255 
257  void clear()
258  { _values.clear(); }
259 
261  bool empty() const
262  { return _values.empty(); }
263 
264  //
265  // iterator-methods
266  //
267 
270  { return const_iterator(*this); }
272  const_iterator begin(const std::string& name) const
273  { return const_iterator(*this, name); }
276  { return const_iterator(); }
277 
279  std::string getUrl() const;
280 
281 };
282 
284 inline std::ostream& operator<< (std::ostream& out, const QueryParams& p)
285 { return out << p.getUrl(); }
286 
287 }
288 
289 #endif // CXXTOOLS_QUERY_PARAMS_H
290