-rw-r--r-- | include/opkele/iterator.h | 2 | ||||
-rw-r--r-- | lib/util.cc | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/include/opkele/iterator.h b/include/opkele/iterator.h index 8f86234..94da7e4 100644 --- a/include/opkele/iterator.h +++ b/include/opkele/iterator.h | |||
@@ -58,97 +58,97 @@ namespace opkele { | |||
58 | 58 | ||
59 | template <typename T,typename TR=T&,typename TP=T*> | 59 | template <typename T,typename TR=T&,typename TP=T*> |
60 | class basic_forward_iterator_proxy_impl : public iterator<forward_iterator_tag,T,void,TP,TR> { | 60 | class basic_forward_iterator_proxy_impl : public iterator<forward_iterator_tag,T,void,TP,TR> { |
61 | public: | 61 | public: |
62 | virtual ~basic_forward_iterator_proxy_impl() { } | 62 | virtual ~basic_forward_iterator_proxy_impl() { } |
63 | 63 | ||
64 | virtual basic_forward_iterator_proxy_impl<T,TR,TP>* dup() const = 0; | 64 | virtual basic_forward_iterator_proxy_impl<T,TR,TP>* dup() const = 0; |
65 | 65 | ||
66 | virtual bool operator==(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const = 0; | 66 | virtual bool operator==(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const = 0; |
67 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const { | 67 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const { |
68 | return !((*this)==x); } | 68 | return !((*this)==x); } |
69 | virtual TR operator*() const = 0; | 69 | virtual TR operator*() const = 0; |
70 | virtual TP operator->() const = 0; | 70 | virtual TP operator->() const = 0; |
71 | virtual void advance() = 0; | 71 | virtual void advance() = 0; |
72 | }; | 72 | }; |
73 | 73 | ||
74 | template <typename IT> | 74 | template <typename IT> |
75 | class forward_iterator_proxy_impl : public basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer> { | 75 | class forward_iterator_proxy_impl : public basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer> { |
76 | public: | 76 | public: |
77 | IT i; | 77 | IT i; |
78 | 78 | ||
79 | forward_iterator_proxy_impl(const IT& _i) : i(_i) { } | 79 | forward_iterator_proxy_impl(const IT& _i) : i(_i) { } |
80 | 80 | ||
81 | virtual basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>* dup() const { | 81 | virtual basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>* dup() const { |
82 | return new forward_iterator_proxy_impl<IT>(i); } | 82 | return new forward_iterator_proxy_impl<IT>(i); } |
83 | 83 | ||
84 | virtual bool operator==(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { | 84 | virtual bool operator==(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { |
85 | return i==static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } | 85 | return i==static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } |
86 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { | 86 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { |
87 | return i!=static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } | 87 | return i!=static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } |
88 | virtual typename IT::reference operator*() const { return *i; } | 88 | virtual typename IT::reference operator*() const { return *i; } |
89 | virtual typename IT::pointer operator->() const { return i.operator->(); } | 89 | virtual typename IT::pointer operator->() const { return i.operator->(); } |
90 | virtual void advance() { ++i; } | 90 | virtual void advance() { ++i; } |
91 | }; | 91 | }; |
92 | 92 | ||
93 | template<typename T,typename TR=T&,typename TP=T*> | 93 | template<typename T,typename TR=T&,typename TP=T*> |
94 | class forward_iterator_proxy : public iterator<forward_iterator_tag,T,void,TP,TR> { | 94 | class forward_iterator_proxy : public iterator<forward_iterator_tag,T,void,TP,TR> { |
95 | public: | 95 | public: |
96 | basic_forward_iterator_proxy_impl<T,TR,TP> *I; | 96 | basic_forward_iterator_proxy_impl<T,TR,TP> *I; |
97 | 97 | ||
98 | template<typename IT> | 98 | template<typename IT> |
99 | forward_iterator_proxy(const IT& i) | 99 | forward_iterator_proxy(const IT& i) |
100 | : I(new forward_iterator_proxy_impl<IT>(i)) { } | 100 | : I(new forward_iterator_proxy_impl<IT>(i)) { } |
101 | forward_iterator_proxy(const forward_iterator_proxy<T,TR,TP>& x) | 101 | forward_iterator_proxy(const forward_iterator_proxy<T,TR,TP>& x) |
102 | : I(x.I->dup()) { } | 102 | : I(x.I->dup()) { } |
103 | ~forward_iterator_proxy() { delete I; } | 103 | ~forward_iterator_proxy() { delete I; } |
104 | 104 | ||
105 | forward_iterator_proxy& operator=(const forward_iterator_proxy<T,TR,TP>& x) { | 105 | forward_iterator_proxy& operator=(const forward_iterator_proxy<T,TR,TP>& x) { |
106 | delete I; I = x.I->dup(); } | 106 | delete I; I = x.I->dup(); return *this; } |
107 | 107 | ||
108 | bool operator==(const forward_iterator_proxy<T,TR,TP>& x) const { | 108 | bool operator==(const forward_iterator_proxy<T,TR,TP>& x) const { |
109 | return (*I)==(*(x.I)); } | 109 | return (*I)==(*(x.I)); } |
110 | bool operator!=(const forward_iterator_proxy<T,TR,TP>& x) const { | 110 | bool operator!=(const forward_iterator_proxy<T,TR,TP>& x) const { |
111 | return (*I)!=(*(x.I)); } | 111 | return (*I)!=(*(x.I)); } |
112 | 112 | ||
113 | TR operator*() const { | 113 | TR operator*() const { |
114 | return **I; } | 114 | return **I; } |
115 | TP operator->() const { | 115 | TP operator->() const { |
116 | return I->operator->(); } | 116 | return I->operator->(); } |
117 | 117 | ||
118 | forward_iterator_proxy<T,TR,TP>& operator++() { | 118 | forward_iterator_proxy<T,TR,TP>& operator++() { |
119 | I->advance(); return *this; } | 119 | I->advance(); return *this; } |
120 | forward_iterator_proxy<T,TR,TP>& operator++(int) { | 120 | forward_iterator_proxy<T,TR,TP>& operator++(int) { |
121 | forward_iterator_proxy<T,TR,TP> rv(*this); | 121 | forward_iterator_proxy<T,TR,TP> rv(*this); |
122 | I->advance(); return rv; } | 122 | I->advance(); return rv; } |
123 | }; | 123 | }; |
124 | 124 | ||
125 | template<typename IT> | 125 | template<typename IT> |
126 | class basic_filterator : public iterator< | 126 | class basic_filterator : public iterator< |
127 | typename IT::iterator_category, | 127 | typename IT::iterator_category, |
128 | typename IT::value_type, | 128 | typename IT::value_type, |
129 | typename IT::difference_type, | 129 | typename IT::difference_type, |
130 | typename IT::pointer, | 130 | typename IT::pointer, |
131 | typename IT::reference> { | 131 | typename IT::reference> { |
132 | public: | 132 | public: |
133 | IT it; | 133 | IT it; |
134 | IT ei; | 134 | IT ei; |
135 | bool empty; | 135 | bool empty; |
136 | 136 | ||
137 | basic_filterator() : empty(true) { } | 137 | basic_filterator() : empty(true) { } |
138 | basic_filterator(const IT& _bi,const IT& _ei) | 138 | basic_filterator(const IT& _bi,const IT& _ei) |
139 | : it(_bi), ei(_ei) { empty = (it==ei); } | 139 | : it(_bi), ei(_ei) { empty = (it==ei); } |
140 | basic_filterator(const basic_filterator<IT>& x) | 140 | basic_filterator(const basic_filterator<IT>& x) |
141 | : it(x.it), ei(x.ei), empty(x.empty) { } | 141 | : it(x.it), ei(x.ei), empty(x.empty) { } |
142 | virtual ~basic_filterator() { } | 142 | virtual ~basic_filterator() { } |
143 | 143 | ||
144 | bool operator==(const basic_filterator<IT>& x) const { | 144 | bool operator==(const basic_filterator<IT>& x) const { |
145 | return empty?x.empty:(it==x.it); } | 145 | return empty?x.empty:(it==x.it); } |
146 | bool operator!=(const basic_filterator<IT>& x) const { | 146 | bool operator!=(const basic_filterator<IT>& x) const { |
147 | return empty!=x.empty || it!=x.it; } | 147 | return empty!=x.empty || it!=x.it; } |
148 | 148 | ||
149 | typename IT::reference operator*() const { | 149 | typename IT::reference operator*() const { |
150 | assert(!empty); | 150 | assert(!empty); |
151 | return *it; } | 151 | return *it; } |
152 | typename IT::pointer operator->() const { | 152 | typename IT::pointer operator->() const { |
153 | assert(!empty); | 153 | assert(!empty); |
154 | return it.operator->(); } | 154 | return it.operator->(); } |
diff --git a/lib/util.cc b/lib/util.cc index a46ba2a..249eeed 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -165,104 +165,104 @@ namespace opkele { | |||
165 | throw failed_conversion(OPKELE_CP_ "failed to gmtime()"); | 165 | throw failed_conversion(OPKELE_CP_ "failed to gmtime()"); |
166 | return rv; | 166 | return rv; |
167 | } | 167 | } |
168 | 168 | ||
169 | /* | 169 | /* |
170 | * | 170 | * |
171 | */ | 171 | */ |
172 | 172 | ||
173 | static inline bool isrfc3986unreserved(int c) { | 173 | static inline bool isrfc3986unreserved(int c) { |
174 | if(c<'-') return false; | 174 | if(c<'-') return false; |
175 | if(c<='.') return true; | 175 | if(c<='.') return true; |
176 | if(c<'0') return false; if(c<='9') return true; | 176 | if(c<'0') return false; if(c<='9') return true; |
177 | if(c<'A') return false; if(c<='Z') return true; | 177 | if(c<'A') return false; if(c<='Z') return true; |
178 | if(c<'_') return false; | 178 | if(c<'_') return false; |
179 | if(c=='_') return true; | 179 | if(c=='_') return true; |
180 | if(c<'a') return false; if(c<='z') return true; | 180 | if(c<'a') return false; if(c<='z') return true; |
181 | if(c=='~') return true; | 181 | if(c=='~') return true; |
182 | return false; | 182 | return false; |
183 | } | 183 | } |
184 | 184 | ||
185 | struct __url_encoder : public unary_function<char,void> { | 185 | struct __url_encoder : public unary_function<char,void> { |
186 | public: | 186 | public: |
187 | string& rv; | 187 | string& rv; |
188 | 188 | ||
189 | __url_encoder(string& r) : rv(r) { } | 189 | __url_encoder(string& r) : rv(r) { } |
190 | 190 | ||
191 | result_type operator()(argument_type c) { | 191 | result_type operator()(argument_type c) { |
192 | if(isrfc3986unreserved(c)) | 192 | if(isrfc3986unreserved(c)) |
193 | rv += c; | 193 | rv += c; |
194 | else{ | 194 | else{ |
195 | char tmp[4]; | 195 | char tmp[4]; |
196 | snprintf(tmp,sizeof(tmp),"%%%02X", | 196 | snprintf(tmp,sizeof(tmp),"%%%02X", |
197 | (c&0xff)); | 197 | (c&0xff)); |
198 | rv += tmp; | 198 | rv += tmp; |
199 | } | 199 | } |
200 | } | 200 | } |
201 | }; | 201 | }; |
202 | 202 | ||
203 | string url_encode(const string& str) { | 203 | string url_encode(const string& str) { |
204 | string rv; | 204 | string rv; |
205 | for_each(str.begin(),str.end(), | 205 | for_each(str.begin(),str.end(), |
206 | __url_encoder(rv)); | 206 | __url_encoder(rv)); |
207 | return rv; | 207 | return rv; |
208 | } | 208 | } |
209 | 209 | ||
210 | string url_decode(const string& str) { | 210 | string url_decode(const string& str) { |
211 | string rv; | 211 | string rv; |
212 | back_insert_iterator<string> ii(rv); | 212 | back_insert_iterator<string> ii(rv); |
213 | char tmp[3]; tmp[2] = 0; | ||
213 | for(string::const_iterator i=str.begin(),ie=str.end(); | 214 | for(string::const_iterator i=str.begin(),ie=str.end(); |
214 | i!=ie;++i) { | 215 | i!=ie;++i) { |
215 | switch(*i) { | 216 | switch(*i) { |
216 | case '+': | 217 | case '+': |
217 | *(ii++) = ' '; break; | 218 | *(ii++) = ' '; break; |
218 | case '%': | 219 | case '%': |
219 | ++i; | 220 | ++i; |
220 | static char tmp[3] = {0,0,0}; | ||
221 | if(i==ie) | 221 | if(i==ie) |
222 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); | 222 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); |
223 | tmp[0] = *(i++); | 223 | tmp[0] = *(i++); |
224 | if(i==ie) | 224 | if(i==ie) |
225 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); | 225 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); |
226 | tmp[1] = *i; | 226 | tmp[1] = *i; |
227 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) | 227 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) |
228 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); | 228 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); |
229 | *(ii++) = (char)strtol(tmp,0,16); | 229 | *(ii++) = (char)strtol(tmp,0,16); |
230 | break; | 230 | break; |
231 | default: | 231 | default: |
232 | *(ii++) = *i; break; | 232 | *(ii++) = *i; break; |
233 | } | 233 | } |
234 | } | 234 | } |
235 | return rv; | 235 | return rv; |
236 | } | 236 | } |
237 | 237 | ||
238 | string attr_escape(const string& str) { | 238 | string attr_escape(const string& str) { |
239 | static const char *unsafechars = "<>&\n\"'"; | 239 | static const char *unsafechars = "<>&\n\"'"; |
240 | string rv; | 240 | string rv; |
241 | string::size_type p=0; | 241 | string::size_type p=0; |
242 | while(true) { | 242 | while(true) { |
243 | string::size_type us = str.find_first_of(unsafechars,p); | 243 | string::size_type us = str.find_first_of(unsafechars,p); |
244 | if(us==string::npos) { | 244 | if(us==string::npos) { |
245 | if(p!=str.length()) | 245 | if(p!=str.length()) |
246 | rv.append(str,p,str.length()-p); | 246 | rv.append(str,p,str.length()-p); |
247 | return rv; | 247 | return rv; |
248 | } | 248 | } |
249 | rv.append(str,p,us-p); | 249 | rv.append(str,p,us-p); |
250 | rv += "&#"; | 250 | rv += "&#"; |
251 | rv += long_to_string((long)str[us]); | 251 | rv += long_to_string((long)str[us]); |
252 | rv += ';'; | 252 | rv += ';'; |
253 | p = us+1; | 253 | p = us+1; |
254 | } | 254 | } |
255 | } | 255 | } |
256 | 256 | ||
257 | string long_to_string(long l) { | 257 | string long_to_string(long l) { |
258 | char rv[32]; | 258 | char rv[32]; |
259 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 259 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
260 | if(r<0 || r>=(int)sizeof(rv)) | 260 | if(r<0 || r>=(int)sizeof(rv)) |
261 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 261 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
262 | return rv; | 262 | return rv; |
263 | } | 263 | } |
264 | 264 | ||
265 | long string_to_long(const string& s) { | 265 | long string_to_long(const string& s) { |
266 | char *endptr = 0; | 266 | char *endptr = 0; |
267 | long rv = strtol(s.c_str(),&endptr,10); | 267 | long rv = strtol(s.c_str(),&endptr,10); |
268 | if((!endptr) || endptr==s.c_str()) | 268 | if((!endptr) || endptr==s.c_str()) |