-rw-r--r-- | lib/util.cc | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc new file mode 100644 index 0000000..1a81c56 --- a/dev/null +++ b/lib/util.cc @@ -0,0 +1,83 @@ +#ifdef USE_PCH + #include "pch.h" +#else + #include <cassert> + #include "sitecing/util.h" +#endif + +namespace sitecing { + + static const char *unsafeChars = "<>& \n\""; + + string html_escape(const string& str,int flags) { + string rv = str; + string::size_type screwed = 0; + for(;;) { + screwed = rv.find_first_of(unsafeChars,screwed); + if(screwed == string::npos) + break; + while(screwed<rv.length() && strchr(unsafeChars,rv.at(screwed))) { + char danger = rv.at(screwed); + switch(danger) { + case '<': + rv.replace(screwed,1,"<"); screwed+=4; + break; + case '>': + rv.replace(screwed,1,">"); screwed+=4; + break; + case '&': + rv.replace(screwed,1,"&"); screwed+=5; + break; + case ' ': + if(flags&html_escape_nbsp) { + rv.replace(screwed,1," "); screwed+=6; + }else + screwed++; + break; + case '\n': + if(flags&html_escape_br) { + if(flags&html_escape_br_noslash) { + rv.replace(screwed,1,"<br>\n"); screwed += 5; + }else{ + rv.replace(screwed,1,"<br/>\n"); screwed += 6; + } + }else + screwed++; + break; + case '\"': + if(flags&html_escape_quot) { + rv.replace(screwed,1,"""); screwed+=6; + }else + screwed++; + break; + default: + assert(false); + break; + } + } + } + return rv; + } + + void checkpoint::set() { + point = stream->tellp(); + if(last_will==will_intestate) + last_will = will_rollback; + } + + void checkpoint::make_will(will_t lw) { + last_will = lw; + } + + void checkpoint::rollback() { + stream->seekp(point); + if(last_will == will_rollback) + last_will = will_intestate; + } + void checkpoint::commit() { + point = stream->tellp(); + if(last_will == will_rollback) + last_will = will_intestate; + } + +} |