summaryrefslogtreecommitdiffabout
path: root/include/sitecing/util.h
blob: 5750ab6c72b1aed5594431616bab78a6f9473016 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef __SITECING_UTIL_H
#define __SITECING_UTIL_H

#include <ostream>
#include <string>
#include "sitecing/acomponent.h"

/**
 * @file
 * @brief more or less non-internal utility classes and functions.
 */

namespace sitecing {
    using namespace std;

    /**
     * the html_escape options enumeration.
     */
    enum html_escape_options {
	/**
	 * Turn spaces into &nbsp;
	 */
	html_escape_nbsp       = 0x0001,
	/**
	 * Turn newlines into <br/> or <br>.
	 */
	html_escape_br         = 0x0002,
	/**
	 * Turn quotes to &quot;
	 */
	html_escape_quot       = 0x0004,
	/**
	 * Do not put '/' into <br/> consruct.
	 */
	html_escape_br_noslash = 0x0008
    };
    /**
     * Escape string suitable for html output.
     * @param str the string.
     * @param flags options.
     * @return the string escaped.
     * @see html_escape_options
     */
    string html_escape(const string& str,int flags=html_escape_br);

    /**
     * The output string checkpoint object, letting one to rollback output.
     */
    class checkpoint {
	public:
	    /**
	     * The object's death will enumeration.
	     */
	    enum will_t {
		/**
		 * The stream is to be rolled back at object destruction.
		 */
		will_rollback,
		/**
		 * The stream is not to be rolled back at object destruction.
		 */
		will_commit,
		/**
		 * The object will die intestate. What's the point then?
		 */
		will_intestate
	    };
	    /**
	     * The output stream in question.
	     */
	    ostream* stream;
	    /**
	     * The point at which objhect was created.
	     */
	    ostream::pos_type point;
	    /**
	     * The last will.
	     */
	    will_t last_will;

	    /**
	     * @param s reference to the stream.
	     * @param lw the last will.
	     */
	    checkpoint(ostream& s, will_t lw=will_rollback)
		: stream(&s), last_will(lw) { set(); }
	    /**
	     * @param s pointer to the stream.
	     * @param lw the last will.
	     */
	    checkpoint(ostream* s, will_t lw=will_rollback)
		: stream(s), last_will(lw) { set(); }
	    /**
	     * @param s reference to the sitecing interface where to get output
	     * stream from.
	     * @param lw the last will.
	     */
	    checkpoint(sitecing_interface& s, will_t lw=will_rollback)
		: stream(s.out), last_will(lw) { set(); }
	    /**
	     * @param s pointer to the sitecing interface where to get output
	     * stream from.
	     * @param lw the last will.
	     */
	    checkpoint(sitecing_interface* s, will_t lw=will_rollback)
		: stream(s->out), last_will(lw) { set(); }
	    /**
	     * @param c reference to the component from which the output stream
	     * is obtained.
	     * @param lw the last will.
	     */
	    checkpoint(acomponent& c, will_t lw=will_rollback)
		: stream(c.__SCIF->out), last_will(lw) { set(); }
	    /**
	     * @param c pointer to the component from which the output stream is
	     * obtained.
	     * @param lw the last will.
	     */
	    checkpoint(acomponent* c, will_t lw=will_rollback)
		: stream(c->__SCIF->out), last_will(lw) { set(); }
	    ~checkpoint() {
		if(last_will==will_rollback)
		    rollback();
	    }

	    /**
	     * Set the possible rolback point to the current position in stream.
	     */
	    void set();
	    /**
	     * Make or change will.
	     */
	    void make_will(will_t lw);
	    /**
	     * Rollback the output made so far. In case of rollback will
	     * change to intestate.
	     */
	    void rollback();
	    /**
	     * Commit output so far. In case of rollback will, change to
	     * intestate.
	     */
	    void commit();
    };

}

#endif /* __SITECING_UTIL_H */