-rw-r--r-- | include/sitecing/sitecing_util.h | 87 |
1 files changed, 3 insertions, 84 deletions
diff --git a/include/sitecing/sitecing_util.h b/include/sitecing/sitecing_util.h index d1a6c4a..f642c74 100644 --- a/include/sitecing/sitecing_util.h +++ b/include/sitecing/sitecing_util.h | |||
@@ -1,91 +1,75 @@ | |||
1 | #ifndef __SITECING_SITECING_UTIL_H | 1 | #ifndef __SITECING_SITECING_UTIL_H |
2 | #define __SITECING_SITECING_UTIL_H | 2 | #define __SITECING_SITECING_UTIL_H |
3 | 3 | ||
4 | #include <sys/types.h> | 4 | #include <sys/types.h> |
5 | #include <string> | 5 | #include <string> |
6 | #include <konforka/exception.h> | 6 | #include <konforka/exception.h> |
7 | #include <konforka/util.h> | ||
7 | 8 | ||
8 | /** | 9 | /** |
9 | * @file | 10 | * @file |
10 | * @brief utility classes and functions. | 11 | * @brief utility classes and functions. |
11 | */ | 12 | */ |
12 | 13 | ||
13 | namespace sitecing { | 14 | namespace sitecing { |
14 | using namespace std; | 15 | using namespace std; |
15 | 16 | ||
16 | /** | 17 | /** |
17 | * Base class for utility exceptions. | 18 | * Base class for utility exceptions. |
18 | */ | 19 | */ |
19 | class utility_error : public konforka::exception { | 20 | class utility_error : public konforka::exception { |
20 | public: | 21 | public: |
21 | utility_error(const string& fi,const string& fu,int l,const string& w) | 22 | utility_error(const string& fi,const string& fu,int l,const string& w) |
22 | : konforka::exception(fi,fu,l,w) { } | 23 | : konforka::exception(fi,fu,l,w) { } |
23 | }; | 24 | }; |
24 | /** | 25 | |
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 | /** | 26 | /** |
33 | * No prefix or suffix found to strip out. | 27 | * No prefix or suffix found to strip out. |
34 | */ | 28 | */ |
35 | class utility_no_affix : public utility_error { | 29 | class utility_no_affix : public utility_error { |
36 | public: | 30 | public: |
37 | utility_no_affix(const string& fi,const string& fu,int l,const string& w) | 31 | utility_no_affix(const string& fi,const string& fu,int l,const string& w) |
38 | : utility_error(fi,fu,l,w) { } | 32 | : utility_error(fi,fu,l,w) { } |
39 | }; | 33 | }; |
40 | /** | 34 | /** |
41 | * No prefix to strip found. | 35 | * No prefix to strip found. |
42 | */ | 36 | */ |
43 | class utility_no_prefix : public utility_no_affix { | 37 | class utility_no_prefix : public utility_no_affix { |
44 | public: | 38 | public: |
45 | utility_no_prefix(const string& fi,const string& fu,int l,const string& w) | 39 | utility_no_prefix(const string& fi,const string& fu,int l,const string& w) |
46 | : utility_no_affix(fi,fu,l,w) { } | 40 | : utility_no_affix(fi,fu,l,w) { } |
47 | }; | 41 | }; |
48 | /** | 42 | /** |
49 | * No suffix to strip found. | 43 | * No suffix to strip found. |
50 | */ | 44 | */ |
51 | class utility_no_suffix : public utility_no_affix { | 45 | class utility_no_suffix : public utility_no_affix { |
52 | public: | 46 | public: |
53 | utility_no_suffix(const string& fi,const string& fu,int l,const string& w) | 47 | utility_no_suffix(const string& fi,const string& fu,int l,const string& w) |
54 | : utility_no_affix(fi,fu,l,w) { } | 48 | : utility_no_affix(fi,fu,l,w) { } |
55 | }; | 49 | }; |
56 | 50 | ||
57 | /** | 51 | /** |
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. | 52 | * The file lock object. Released at the object destruction. |
69 | */ | 53 | */ |
70 | class file_lock { | 54 | class file_lock { |
71 | public: | 55 | public: |
72 | /** | 56 | /** |
73 | * The file descriptor. | 57 | * The file descriptor. |
74 | */ | 58 | */ |
75 | int fd; | 59 | int fd; |
76 | 60 | ||
77 | file_lock() | 61 | file_lock() |
78 | : fd(-1) { } | 62 | : fd(-1) { } |
79 | /** | 63 | /** |
80 | * @param f file name. | 64 | * @param f file name. |
81 | */ | 65 | */ |
82 | file_lock(const string& f) | 66 | file_lock(const string& f) |
83 | : fd(-1) { lock(f); } | 67 | : fd(-1) { lock(f); } |
84 | ~file_lock() { unlock(); } | 68 | ~file_lock() { unlock(); } |
85 | 69 | ||
86 | /** | 70 | /** |
87 | * Do lock. | 71 | * Do lock. |
88 | * @param f file name. | 72 | * @param f file name. |
89 | */ | 73 | */ |
90 | void lock(const string& f); | 74 | void lock(const string& f); |
91 | /** | 75 | /** |
@@ -194,129 +178,64 @@ namespace sitecing { | |||
194 | /** | 178 | /** |
195 | * @param s reference to the semaphore. | 179 | * @param s reference to the semaphore. |
196 | * @param l lock at creation? | 180 | * @param l lock at creation? |
197 | */ | 181 | */ |
198 | semaphore_lock(semaphore& s,bool l=true) | 182 | semaphore_lock(semaphore& s,bool l=true) |
199 | : sem(&s), locked(false) { | 183 | : sem(&s), locked(false) { |
200 | if(l) lock(); | 184 | if(l) lock(); |
201 | } | 185 | } |
202 | 186 | ||
203 | ~semaphore_lock() { | 187 | ~semaphore_lock() { |
204 | unlock(); | 188 | unlock(); |
205 | } | 189 | } |
206 | 190 | ||
207 | /** | 191 | /** |
208 | * Lock it. | 192 | * Lock it. |
209 | */ | 193 | */ |
210 | void lock(); | 194 | void lock(); |
211 | /** | 195 | /** |
212 | * Unlock it. | 196 | * Unlock it. |
213 | */ | 197 | */ |
214 | void unlock(); | 198 | void unlock(); |
215 | }; | 199 | }; |
216 | 200 | ||
217 | /** | 201 | /** |
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. | 202 | * Strip prefix from the string. |
261 | * @param str the string. | 203 | * @param str the string. |
262 | * @param prefix prefix to strip. | 204 | * @param prefix prefix to strip. |
263 | * @return the string without prefix. | 205 | * @return the string without prefix. |
264 | * @todo TODO: document exceptions. | 206 | * @todo TODO: document exceptions. |
265 | */ | 207 | */ |
266 | string strip_prefix(const string& str,const string& prefix); | 208 | string strip_prefix(const string& str,const string& prefix); |
267 | /** | 209 | /** |
268 | * Strip suffix from the string. | 210 | * Strip suffix from the string. |
269 | * @param str the string. | 211 | * @param str the string. |
270 | * @param suffix suffix to strip. | 212 | * @param suffix suffix to strip. |
271 | * @return the string without suffix. | 213 | * @return the string without suffix. |
272 | * @todo TODO: document exceptions. | 214 | * @todo TODO: document exceptions. |
273 | */ | 215 | */ |
274 | string strip_suffix(const string& str,const string& suffix); | 216 | string strip_suffix(const string& str,const string& suffix); |
275 | /** | 217 | |
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 | /** | 218 | /** |
300 | * Change to the directory and pop back at object's destruction (e.g. when | 219 | * Change to the directory and pop back at object's destruction (e.g. when |
301 | * the object goes out of scope). | 220 | * the object goes out of scope). |
302 | */ | 221 | */ |
303 | class auto_chdir { | 222 | class auto_chdir { |
304 | public: | 223 | public: |
305 | /** | 224 | /** |
306 | * Saved working directory. | 225 | * Saved working directory. |
307 | */ | 226 | */ |
308 | string saved_pwd; | 227 | string saved_pwd; |
309 | /** | 228 | /** |
310 | * Whether we want to change back automatically. | 229 | * Whether we want to change back automatically. |
311 | */ | 230 | */ |
312 | bool autopop; | 231 | bool autopop; |
313 | 232 | ||
314 | auto_chdir() | 233 | auto_chdir() |
315 | : autopop(false) { } | 234 | : autopop(false) { } |
316 | /** | 235 | /** |
317 | * @param td destination path. | 236 | * @param td destination path. |
318 | * @param ap automatically come back? | 237 | * @param ap automatically come back? |
319 | */ | 238 | */ |
320 | auto_chdir(const string& td,bool ap=true) | 239 | auto_chdir(const string& td,bool ap=true) |
321 | : autopop(false) { pushdir(td,ap); } | 240 | : autopop(false) { pushdir(td,ap); } |
322 | ~auto_chdir() { | 241 | ~auto_chdir() { |