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