summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
Unidiff
Diffstat (limited to 'lib/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc
index a6e08e2..3e7f3aa 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -171,48 +171,76 @@ namespace opkele {
171 public: 171 public:
172 string& rv; 172 string& rv;
173 173
174 __url_encoder(string& r) : rv(r) { } 174 __url_encoder(string& r) : rv(r) { }
175 175
176 result_type operator()(argument_type c) { 176 result_type operator()(argument_type c) {
177 if(isrfc3986unreserved(c)) 177 if(isrfc3986unreserved(c))
178 rv += c; 178 rv += c;
179 else{ 179 else{
180 char tmp[4]; 180 char tmp[4];
181 snprintf(tmp,sizeof(tmp),"%%%02X", 181 snprintf(tmp,sizeof(tmp),"%%%02X",
182 (c&0xff)); 182 (c&0xff));
183 rv += tmp; 183 rv += tmp;
184 } 184 }
185 } 185 }
186 }; 186 };
187 187
188 string url_encode(const string& str) { 188 string url_encode(const string& str) {
189 string rv; 189 string rv;
190 for_each(str.begin(),str.end(), 190 for_each(str.begin(),str.end(),
191 __url_encoder(rv)); 191 __url_encoder(rv));
192 return rv; 192 return rv;
193 } 193 }
194 194
195 string url_decode(const string& str) {
196 string rv;
197 back_insert_iterator<string> ii(rv);
198 for(string::const_iterator i=str.begin(),ie=str.end();
199 i!=ie;++i) {
200 switch(*i) {
201 case '+':
202 *(ii++) = ' '; break;
203 case '%':
204 ++i;
205 static char tmp[3] = {0,0,0};
206 if(i==ie)
207 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
208 tmp[0] == *(i++);
209 if(i==ie)
210 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
211 tmp[1] == *i;
212 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
213 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
214 *(ii++) = strtol(tmp,0,16);
215 break;
216 default:
217 *(ii++) = *i; break;
218 }
219 }
220 return rv;
221 }
222
195 string attr_escape(const string& str) { 223 string attr_escape(const string& str) {
196 static const char *unsafechars = "<>&\n\"'"; 224 static const char *unsafechars = "<>&\n\"'";
197 string rv; 225 string rv;
198 string::size_type p=0; 226 string::size_type p=0;
199 while(true) { 227 while(true) {
200 string::size_type us = str.find_first_of(unsafechars,p); 228 string::size_type us = str.find_first_of(unsafechars,p);
201 if(us==string::npos) { 229 if(us==string::npos) {
202 if(p!=str.length()) 230 if(p!=str.length())
203 rv.append(str,p,str.length()-p); 231 rv.append(str,p,str.length()-p);
204 return rv; 232 return rv;
205 } 233 }
206 rv.append(str,p,us-p); 234 rv.append(str,p,us-p);
207 rv += "&#"; 235 rv += "&#";
208 rv += long_to_string((long)str[us]); 236 rv += long_to_string((long)str[us]);
209 rv += ';'; 237 rv += ';';
210 p = us+1; 238 p = us+1;
211 } 239 }
212 } 240 }
213 241
214 string long_to_string(long l) { 242 string long_to_string(long l) {
215 char rv[32]; 243 char rv[32];
216 int r=snprintf(rv,sizeof(rv),"%ld",l); 244 int r=snprintf(rv,sizeof(rv),"%ld",l);
217 if(r<0 || r>=(int)sizeof(rv)) 245 if(r<0 || r>=(int)sizeof(rv))
218 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 246 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");