summaryrefslogtreecommitdiffabout
path: root/include/sitecing/util.h
Unidiff
Diffstat (limited to 'include/sitecing/util.h') (more/less context) (ignore whitespace changes)
-rw-r--r--include/sitecing/util.h148
1 files changed, 148 insertions, 0 deletions
diff --git a/include/sitecing/util.h b/include/sitecing/util.h
new file mode 100644
index 0000000..5750ab6
--- a/dev/null
+++ b/include/sitecing/util.h
@@ -0,0 +1,148 @@
1#ifndef __SITECING_UTIL_H
2#define __SITECING_UTIL_H
3
4#include <ostream>
5#include <string>
6#include "sitecing/acomponent.h"
7
8/**
9 * @file
10 * @brief more or less non-internal utility classes and functions.
11 */
12
13namespace sitecing {
14 using namespace std;
15
16 /**
17 * the html_escape options enumeration.
18 */
19 enum html_escape_options {
20 /**
21 * Turn spaces into &nbsp;
22 */
23 html_escape_nbsp = 0x0001,
24 /**
25 * Turn newlines into <br/> or <br>.
26 */
27 html_escape_br = 0x0002,
28 /**
29 * Turn quotes to &quot;
30 */
31 html_escape_quot = 0x0004,
32 /**
33 * Do not put '/' into <br/> consruct.
34 */
35 html_escape_br_noslash = 0x0008
36 };
37 /**
38 * Escape string suitable for html output.
39 * @param str the string.
40 * @param flags options.
41 * @return the string escaped.
42 * @see html_escape_options
43 */
44 string html_escape(const string& str,int flags=html_escape_br);
45
46 /**
47 * The output string checkpoint object, letting one to rollback output.
48 */
49 class checkpoint {
50 public:
51 /**
52 * The object's death will enumeration.
53 */
54 enum will_t {
55 /**
56 * The stream is to be rolled back at object destruction.
57 */
58 will_rollback,
59 /**
60 * The stream is not to be rolled back at object destruction.
61 */
62 will_commit,
63 /**
64 * The object will die intestate. What's the point then?
65 */
66 will_intestate
67 };
68 /**
69 * The output stream in question.
70 */
71 ostream* stream;
72 /**
73 * The point at which objhect was created.
74 */
75 ostream::pos_type point;
76 /**
77 * The last will.
78 */
79 will_t last_will;
80
81 /**
82 * @param s reference to the stream.
83 * @param lw the last will.
84 */
85 checkpoint(ostream& s, will_t lw=will_rollback)
86 : stream(&s), last_will(lw) { set(); }
87 /**
88 * @param s pointer to the stream.
89 * @param lw the last will.
90 */
91 checkpoint(ostream* s, will_t lw=will_rollback)
92 : stream(s), last_will(lw) { set(); }
93 /**
94 * @param s reference to the sitecing interface where to get output
95 * stream from.
96 * @param lw the last will.
97 */
98 checkpoint(sitecing_interface& s, will_t lw=will_rollback)
99 : stream(s.out), last_will(lw) { set(); }
100 /**
101 * @param s pointer to the sitecing interface where to get output
102 * stream from.
103 * @param lw the last will.
104 */
105 checkpoint(sitecing_interface* s, will_t lw=will_rollback)
106 : stream(s->out), last_will(lw) { set(); }
107 /**
108 * @param c reference to the component from which the output stream
109 * is obtained.
110 * @param lw the last will.
111 */
112 checkpoint(acomponent& c, will_t lw=will_rollback)
113 : stream(c.__SCIF->out), last_will(lw) { set(); }
114 /**
115 * @param c pointer to the component from which the output stream is
116 * obtained.
117 * @param lw the last will.
118 */
119 checkpoint(acomponent* c, will_t lw=will_rollback)
120 : stream(c->__SCIF->out), last_will(lw) { set(); }
121 ~checkpoint() {
122 if(last_will==will_rollback)
123 rollback();
124 }
125
126 /**
127 * Set the possible rolback point to the current position in stream.
128 */
129 void set();
130 /**
131 * Make or change will.
132 */
133 void make_will(will_t lw);
134 /**
135 * Rollback the output made so far. In case of rollback will
136 * change to intestate.
137 */
138 void rollback();
139 /**
140 * Commit output so far. In case of rollback will, change to
141 * intestate.
142 */
143 void commit();
144 };
145
146}
147
148#endif /* __SITECING_UTIL_H */