summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
blob: 1a81c5686d10540caf76d6454abd51293d94e03e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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,"&lt;"); screwed+=4;
			break;
		    case '>':
			rv.replace(screwed,1,"&gt;"); screwed+=4;
			break;
		    case '&':
			rv.replace(screwed,1,"&amp;"); screwed+=5;
			break;
		    case ' ':
			if(flags&html_escape_nbsp) {
			    rv.replace(screwed,1,"&nbsp;"); 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,"&quot;"); 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;
    }

}