-rw-r--r-- | lib/fields.cc | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/lib/fields.cc b/lib/fields.cc index d494098..91fbc41 100644 --- a/lib/fields.cc +++ b/lib/fields.cc | |||
@@ -1,69 +1,71 @@ | |||
1 | #include <opkele/types.h> | 1 | #include <opkele/types.h> |
2 | #include <opkele/exception.h> | 2 | #include <opkele/exception.h> |
3 | #include <opkele/util.h> | 3 | #include <opkele/util.h> |
4 | #include <algorithm> | ||
4 | 5 | ||
5 | namespace opkele { | 6 | namespace opkele { |
7 | using std::for_each; | ||
6 | using std::unary_function; | 8 | using std::unary_function; |
7 | 9 | ||
8 | struct __om_copier : public unary_function<const string&,void> { | 10 | struct __om_copier : public unary_function<const string&,void> { |
9 | public: | 11 | public: |
10 | const basic_fields& from; | 12 | const basic_fields& from; |
11 | basic_fields& to; | 13 | basic_fields& to; |
12 | 14 | ||
13 | __om_copier(basic_fields& t,const basic_fields& f) | 15 | __om_copier(basic_fields& t,const basic_fields& f) |
14 | : from(f), to(t) { } | 16 | : from(f), to(t) { } |
15 | 17 | ||
16 | result_type operator()(argument_type f) { | 18 | result_type operator()(argument_type f) { |
17 | to.set_field(f,from.get_field(f)); } | 19 | to.set_field(f,from.get_field(f)); } |
18 | }; | 20 | }; |
19 | 21 | ||
20 | basic_fields::basic_fields(const basic_fields& x) { | 22 | basic_fields::basic_fields(const basic_fields& x) { |
21 | x.copy_to(*this); | 23 | x.copy_to(*this); |
22 | } | 24 | } |
23 | void basic_fields::copy_to(basic_fields& x) const { | 25 | void basic_fields::copy_to(basic_fields& x) const { |
24 | x.reset_fields(); | 26 | x.reset_fields(); |
25 | for_each(fields_begin(),fields_end(), | 27 | for_each(fields_begin(),fields_end(), |
26 | __om_copier(x,*this) ); | 28 | __om_copier(x,*this) ); |
27 | } | 29 | } |
28 | void basic_fields::append_to(basic_fields& x) const { | 30 | void basic_fields::append_to(basic_fields& x) const { |
29 | for_each(fields_begin(),fields_end(), | 31 | for_each(fields_begin(),fields_end(), |
30 | __om_copier(x,*this) ); | 32 | __om_copier(x,*this) ); |
31 | } | 33 | } |
32 | 34 | ||
33 | struct __om_query_builder : public unary_function<const string&,void> { | 35 | struct __om_query_builder : public unary_function<const string&,void> { |
34 | public: | 36 | public: |
35 | const basic_fields& om; | 37 | const basic_fields& om; |
36 | bool first; | 38 | bool first; |
37 | string& rv; | 39 | string& rv; |
38 | const char *pfx; | 40 | const char *pfx; |
39 | 41 | ||
40 | __om_query_builder(const char *p,string& r,const basic_fields& m) | 42 | __om_query_builder(const char *p,string& r,const basic_fields& m) |
41 | : om(m), first(true), rv(r), pfx(p) { | 43 | : om(m), first(true), rv(r), pfx(p) { |
42 | for_each(om.fields_begin(),om.fields_end(),*this); | 44 | for_each(om.fields_begin(),om.fields_end(),*this); |
43 | } | 45 | } |
44 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) | 46 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) |
45 | : om(m), first(true), rv(r), pfx(p) { | 47 | : om(m), first(true), rv(r), pfx(p) { |
46 | rv = u; | 48 | rv = u; |
47 | if(rv.find('?')==string::npos) | 49 | if(rv.find('?')==string::npos) |
48 | rv += '?'; | 50 | rv += '?'; |
49 | else | 51 | else |
50 | first = false; | 52 | first = false; |
51 | for_each(om.fields_begin(),om.fields_end(),*this); | 53 | for_each(om.fields_begin(),om.fields_end(),*this); |
52 | } | 54 | } |
53 | 55 | ||
54 | result_type operator()(argument_type f) { | 56 | result_type operator()(argument_type f) { |
55 | if(first) | 57 | if(first) |
56 | first = false; | 58 | first = false; |
57 | else | 59 | else |
58 | rv += '&'; | 60 | rv += '&'; |
59 | if(pfx) rv += pfx; | 61 | if(pfx) rv += pfx; |
60 | rv+= f; | 62 | rv+= f; |
61 | rv += '='; | 63 | rv += '='; |
62 | rv += util::url_encode(om.get_field(f)); | 64 | rv += util::url_encode(om.get_field(f)); |
63 | } | 65 | } |
64 | }; | 66 | }; |
65 | 67 | ||
66 | string basic_fields::append_query(const string& url,const char *pfx) const { | 68 | string basic_fields::append_query(const string& url,const char *pfx) const { |
67 | string rv; | 69 | string rv; |
68 | return __om_query_builder(pfx,rv,*this,url).rv; | 70 | return __om_query_builder(pfx,rv,*this,url).rv; |
69 | } | 71 | } |