author | Michael Krelin <hacker@klever.net> | 2005-01-29 21:21:05 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2005-01-29 21:21:05 (UTC) |
commit | ce1f37aae46ea95020d7b865f7a80e8abdfad0d8 (patch) (unidiff) | |
tree | 4964383ab8cd7e6d8ea821f1a615d1bbcf98dad8 /include/sitecing/sitecing_util.h | |
parent | 3c75c860fc1ad5b3f5185e23ec6f438dd2528958 (diff) | |
download | sitecing-0.0.zip sitecing-0.0.tar.gz sitecing-0.0.tar.bz2 |
initial commit into repository0.0
Diffstat (limited to 'include/sitecing/sitecing_util.h') (more/less context) (show whitespace changes)
-rw-r--r-- | include/sitecing/sitecing_util.h | 341 |
1 files changed, 341 insertions, 0 deletions
diff --git a/include/sitecing/sitecing_util.h b/include/sitecing/sitecing_util.h new file mode 100644 index 0000000..d1a6c4a --- a/dev/null +++ b/include/sitecing/sitecing_util.h | |||
@@ -0,0 +1,341 @@ | |||
1 | #ifndef __SITECING_SITECING_UTIL_H | ||
2 | #define __SITECING_SITECING_UTIL_H | ||
3 | |||
4 | #include <sys/types.h> | ||
5 | #include <string> | ||
6 | #include <konforka/exception.h> | ||
7 | |||
8 | /** | ||
9 | * @file | ||
10 | * @brief utility classes and functions. | ||
11 | */ | ||
12 | |||
13 | namespace sitecing { | ||
14 | using namespace std; | ||
15 | |||
16 | /** | ||
17 | * Base class for utility exceptions. | ||
18 | */ | ||
19 | class utility_error : public konforka::exception { | ||
20 | public: | ||
21 | utility_error(const string& fi,const string& fu,int l,const string& w) | ||
22 | : konforka::exception(fi,fu,l,w) { } | ||
23 | }; | ||
24 | /** | ||
25 | * Restricted sequence encountered. | ||
26 | */ | ||
27 | class utility_restricted_sequence : public utility_error { | ||
28 | public: | ||
29 | utility_restricted_sequence(const string& fi,const string& fu,int l,const string& w) | ||
30 | : utility_error(fi,fu,l,w) { } | ||
31 | }; | ||
32 | /** | ||
33 | * No prefix or suffix found to strip out. | ||
34 | */ | ||
35 | class utility_no_affix : public utility_error { | ||
36 | public: | ||
37 | utility_no_affix(const string& fi,const string& fu,int l,const string& w) | ||
38 | : utility_error(fi,fu,l,w) { } | ||
39 | }; | ||
40 | /** | ||
41 | * No prefix to strip found. | ||
42 | */ | ||
43 | class utility_no_prefix : public utility_no_affix { | ||
44 | public: | ||
45 | utility_no_prefix(const string& fi,const string& fu,int l,const string& w) | ||
46 | : utility_no_affix(fi,fu,l,w) { } | ||
47 | }; | ||
48 | /** | ||
49 | * No suffix to strip found. | ||
50 | */ | ||
51 | class utility_no_suffix : public utility_no_affix { | ||
52 | public: | ||
53 | utility_no_suffix(const string& fi,const string& fu,int l,const string& w) | ||
54 | : utility_no_affix(fi,fu,l,w) { } | ||
55 | }; | ||
56 | |||
57 | /** | ||
58 | * Went up beyond root. | ||
59 | * @todo TODO: wish I could remember the details -- document me. | ||
60 | */ | ||
61 | class utility_beyond_root : public utility_error { | ||
62 | public: | ||
63 | utility_beyond_root(const string& fi,const string& fu,int l,const string& w) | ||
64 | : utility_error(fi,fu,l,w) { } | ||
65 | }; | ||
66 | |||
67 | /** | ||
68 | * The file lock object. Released at the object destruction. | ||
69 | */ | ||
70 | class file_lock { | ||
71 | public: | ||
72 | /** | ||
73 | * The file descriptor. | ||
74 | */ | ||
75 | int fd; | ||
76 | |||
77 | file_lock() | ||
78 | : fd(-1) { } | ||
79 | /** | ||
80 | * @param f file name. | ||
81 | */ | ||
82 | file_lock(const string& f) | ||
83 | : fd(-1) { lock(f); } | ||
84 | ~file_lock() { unlock(); } | ||
85 | |||
86 | /** | ||
87 | * Do lock. | ||
88 | * @param f file name. | ||
89 | */ | ||
90 | void lock(const string& f); | ||
91 | /** | ||
92 | * @todo TODO: wish I could remember the details -- document me. | ||
93 | */ | ||
94 | void lock(); | ||
95 | /** | ||
96 | * Release the lock obtained. | ||
97 | */ | ||
98 | void unlock(); | ||
99 | }; | ||
100 | |||
101 | /** | ||
102 | * The pid file. Removed at object destruction. | ||
103 | */ | ||
104 | class pid_file { | ||
105 | public: | ||
106 | /** | ||
107 | * The file name. | ||
108 | */ | ||
109 | string file_name; | ||
110 | /** | ||
111 | * Do we unlink the file after we're done? | ||
112 | */ | ||
113 | bool unlink_pid; | ||
114 | |||
115 | pid_file() | ||
116 | : unlink_pid(false) { } | ||
117 | ~pid_file() { unlink(); } | ||
118 | |||
119 | /** | ||
120 | * @param f file name. | ||
121 | * @param u whether we want to unlink the file. | ||
122 | */ | ||
123 | void set(const string& f,bool u=true); | ||
124 | /** | ||
125 | * Unlink the file if we wanted to in the first place. | ||
126 | */ | ||
127 | void unlink(); | ||
128 | }; | ||
129 | |||
130 | /** | ||
131 | * The semaphore object. | ||
132 | */ | ||
133 | class semaphore { | ||
134 | public: | ||
135 | /** | ||
136 | * The semaphore id. | ||
137 | */ | ||
138 | int semid; | ||
139 | |||
140 | semaphore() | ||
141 | : semid(-1) { } | ||
142 | /** | ||
143 | * @param sid semaphore id. | ||
144 | */ | ||
145 | semaphore(int sid) | ||
146 | : semid(sid) { } | ||
147 | ~semaphore() { | ||
148 | deinit(); | ||
149 | } | ||
150 | |||
151 | /** | ||
152 | * Init semaphore. | ||
153 | */ | ||
154 | void init(); | ||
155 | /** | ||
156 | * Undo the init. | ||
157 | */ | ||
158 | void deinit(); | ||
159 | |||
160 | /** | ||
161 | * Semaphore on. | ||
162 | */ | ||
163 | void on(); | ||
164 | /** | ||
165 | * Semaphore off. | ||
166 | */ | ||
167 | void off(); | ||
168 | }; | ||
169 | |||
170 | /** | ||
171 | * The semaphor lock object, released at object destruction. | ||
172 | */ | ||
173 | class semaphore_lock { | ||
174 | public: | ||
175 | /** | ||
176 | * Pointer to the semaphore we're operating on. | ||
177 | */ | ||
178 | semaphore* sem; | ||
179 | /** | ||
180 | * Whether it is locked. | ||
181 | */ | ||
182 | bool locked; | ||
183 | |||
184 | semaphore_lock() | ||
185 | : sem(NULL), locked(false) {} | ||
186 | /** | ||
187 | * @param s pointer to the semaphore. | ||
188 | * @param l lock at creation? | ||
189 | */ | ||
190 | semaphore_lock(semaphore* s,bool l=true) | ||
191 | : sem(s), locked(false) { | ||
192 | if(l) lock(); | ||
193 | } | ||
194 | /** | ||
195 | * @param s reference to the semaphore. | ||
196 | * @param l lock at creation? | ||
197 | */ | ||
198 | semaphore_lock(semaphore& s,bool l=true) | ||
199 | : sem(&s), locked(false) { | ||
200 | if(l) lock(); | ||
201 | } | ||
202 | |||
203 | ~semaphore_lock() { | ||
204 | unlock(); | ||
205 | } | ||
206 | |||
207 | /** | ||
208 | * Lock it. | ||
209 | */ | ||
210 | void lock(); | ||
211 | /** | ||
212 | * Unlock it. | ||
213 | */ | ||
214 | void unlock(); | ||
215 | }; | ||
216 | |||
217 | /** | ||
218 | * normalize_path options enumeration. | ||
219 | * @see normalize_path() | ||
220 | */ | ||
221 | enum normalize_path_options { | ||
222 | /** | ||
223 | * Restrict the /../ sequence. | ||
224 | */ | ||
225 | restrict_dotdot = 1, | ||
226 | /** | ||
227 | * Strip out the leading slash. | ||
228 | */ | ||
229 | strip_leading_slash = 2, | ||
230 | /** | ||
231 | * Strip out the trailing slash. | ||
232 | */ | ||
233 | strip_trailing_slash = 4 | ||
234 | }; | ||
235 | /** | ||
236 | * combine_path options enumeration. | ||
237 | * @see combine_path() | ||
238 | */ | ||
239 | enum combine_path_options { | ||
240 | /** | ||
241 | * The origin is file. Otherwise it is directory. | ||
242 | */ | ||
243 | origin_is_file = 1, | ||
244 | /** | ||
245 | * Fail if we've gone up beyond root. | ||
246 | */ | ||
247 | fail_beyond_root = 2 | ||
248 | }; | ||
249 | |||
250 | /** | ||
251 | * Normalize pathname by stripping duplicate slashes, etc. | ||
252 | * @param path the path name. | ||
253 | * @param opts options. | ||
254 | * @return the normalized path. | ||
255 | * @see normalize_path_options | ||
256 | * @todo TODO: document exceptions. | ||
257 | */ | ||
258 | string normalize_path(const string& path,int opts=(restrict_dotdot|strip_trailing_slash)); | ||
259 | /** | ||
260 | * Strip prefix from the string. | ||
261 | * @param str the string. | ||
262 | * @param prefix prefix to strip. | ||
263 | * @return the string without prefix. | ||
264 | * @todo TODO: document exceptions. | ||
265 | */ | ||
266 | string strip_prefix(const string& str,const string& prefix); | ||
267 | /** | ||
268 | * Strip suffix from the string. | ||
269 | * @param str the string. | ||
270 | * @param suffix suffix to strip. | ||
271 | * @return the string without suffix. | ||
272 | * @todo TODO: document exceptions. | ||
273 | */ | ||
274 | string strip_suffix(const string& str,const string& suffix); | ||
275 | /** | ||
276 | * Get the directory part of the filename. | ||
277 | * @param filename the full file name. | ||
278 | * @return the directory part. | ||
279 | */ | ||
280 | string dir_name(const string& filename); | ||
281 | /** | ||
282 | * Combine path with the relative path. | ||
283 | * @param origin the origin. | ||
284 | * @param relative relative path to combine origin with. | ||
285 | * @param opts options. | ||
286 | * @return the pathc combined. | ||
287 | * @see combine_path_options | ||
288 | * @todo TODO: document exceptions. | ||
289 | */ | ||
290 | string combine_path(const string& origin,const string& relative,int opts=origin_is_file); | ||
291 | |||
292 | /** | ||
293 | * Create directory and parent directories if needed. | ||
294 | * @param path the pathname. | ||
295 | * @param mode the mode for newly created directories. | ||
296 | */ | ||
297 | void make_path(const string& path,mode_t mode); | ||
298 | |||
299 | /** | ||
300 | * Change to the directory and pop back at object's destruction (e.g. when | ||
301 | * the object goes out of scope). | ||
302 | */ | ||
303 | class auto_chdir { | ||
304 | public: | ||
305 | /** | ||
306 | * Saved working directory. | ||
307 | */ | ||
308 | string saved_pwd; | ||
309 | /** | ||
310 | * Whether we want to change back automatically. | ||
311 | */ | ||
312 | bool autopop; | ||
313 | |||
314 | auto_chdir() | ||
315 | : autopop(false) { } | ||
316 | /** | ||
317 | * @param td destination path. | ||
318 | * @param ap automatically come back? | ||
319 | */ | ||
320 | auto_chdir(const string& td,bool ap=true) | ||
321 | : autopop(false) { pushdir(td,ap); } | ||
322 | ~auto_chdir() { | ||
323 | if(autopop) | ||
324 | popdir(); | ||
325 | } | ||
326 | |||
327 | /** | ||
328 | * Change into directory. | ||
329 | * @param td the directory. | ||
330 | * @param ap automaticall pop back? | ||
331 | */ | ||
332 | void pushdir(const string& td,bool ap=true); | ||
333 | /** | ||
334 | * Change to the saved directory. | ||
335 | */ | ||
336 | void popdir(); | ||
337 | }; | ||
338 | |||
339 | } | ||
340 | |||
341 | #endif /* __SITECING_SITECING_UTIL_H */ | ||