summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
Unidiff
Diffstat (limited to 'lib/util.cc') (more/less context) (show whitespace changes)
-rw-r--r--lib/util.cc19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc
index 54d6535..b7bc437 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -160,8 +160,27 @@ namespace opkele {
160 curl_free(t); 160 curl_free(t);
161 return rv; 161 return rv;
162 } 162 }
163 163
164 string attr_escape(const string& str) {
165 static const char *unsafechars = "<>&\n\"'";
166 string rv;
167 string::size_type p=0;
168 while(true) {
169 string::size_type us = str.find_first_of(unsafechars,p);
170 if(us==string::npos) {
171 if(p!=str.length())
172 rv.append(str,p,str.length()-p);
173 return rv;
174 }
175 rv.append(str,p,us-p);
176 rv += "&#";
177 rv += long_to_string((long)str[us]);
178 rv += ';';
179 p = us+1;
180 }
181 }
182
164 string long_to_string(long l) { 183 string long_to_string(long l) {
165 char rv[32]; 184 char rv[32];
166 int r=snprintf(rv,sizeof(rv),"%ld",l); 185 int r=snprintf(rv,sizeof(rv),"%ld",l);
167 if(r<0 || r>=(int)sizeof(rv)) 186 if(r<0 || r>=(int)sizeof(rv))