summaryrefslogtreecommitdiffabout
path: root/include/sitecing/sitecing_util.h
blob: f642c74a3f37595fdec0f9d4777c3e47507d5a62 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef __SITECING_SITECING_UTIL_H
#define __SITECING_SITECING_UTIL_H

#include <sys/types.h>
#include <string>
#include <konforka/exception.h>
#include <konforka/util.h>

/**
 * @file
 * @brief utility classes and functions.
 */

namespace sitecing {
    using namespace std;

    /**
     * Base class for utility exceptions.
     */
    class utility_error : public konforka::exception {
	public:
	    utility_error(const string& fi,const string& fu,int l,const string& w)
		: konforka::exception(fi,fu,l,w) { }
    };
    
    /**
     * No prefix or suffix found to strip out.
     */
    class utility_no_affix : public utility_error {
	public:
	    utility_no_affix(const string& fi,const string& fu,int l,const string& w)
		: utility_error(fi,fu,l,w) { }
    };
    /**
     * No prefix to strip found.
     */
    class utility_no_prefix : public utility_no_affix {
	public:
	    utility_no_prefix(const string& fi,const string& fu,int l,const string& w)
		: utility_no_affix(fi,fu,l,w) { }
    };
    /**
     * No suffix to strip found.
     */
    class utility_no_suffix : public utility_no_affix {
	public:
	    utility_no_suffix(const string& fi,const string& fu,int l,const string& w)
		: utility_no_affix(fi,fu,l,w) { }
    };

    /**
     * The file lock object. Released at the object destruction.
     */
    class file_lock {
	public:
	    /**
	     * The file descriptor.
	     */
	    int fd;

	    file_lock()
		: fd(-1) { }
	    /**
	     * @param f file name.
	     */
	    file_lock(const string& f)
		: fd(-1) { lock(f); }
	    ~file_lock() { unlock(); }

	    /**
	     * Do lock.
	     * @param f file name.
	     */
	    void lock(const string& f);
	    /**
	     * @todo TODO: wish I could remember the details -- document me.
	     */
	    void lock();
	    /**
	     * Release the lock obtained.
	     */
	    void unlock();
    };

    /**
     * The pid file. Removed at object destruction.
     */
    class pid_file {
	public:
	    /**
	     * The file name.
	     */
	    string file_name;
	    /**
	     * Do we unlink the file after we're done?
	     */
	    bool unlink_pid;

	    pid_file()
		: unlink_pid(false) { }
	    ~pid_file() { unlink(); }

	    /**
	     * @param f file name.
	     * @param u whether we want to unlink the file.
	     */
	    void set(const string& f,bool u=true);
	    /**
	     * Unlink the file if we wanted to in the first place.
	     */
	    void unlink();
    };

    /**
     * The semaphore object.
     */
    class semaphore {
	public:
	    /**
	     * The semaphore id.
	     */
	    int semid;

	    semaphore()
		: semid(-1) { }
	    /**
	     * @param sid semaphore id.
	     */
	    semaphore(int sid)
		: semid(sid) { }
	    ~semaphore() {
		deinit();
	    }

	    /**
	     * Init semaphore.
	     */
	    void init();
	    /**
	     * Undo the init.
	     */
	    void deinit();

	    /**
	     * Semaphore on.
	     */
	    void on();
	    /**
	     * Semaphore off.
	     */
	    void off();
    };

    /**
     * The semaphor lock object, released at object destruction.
     */
    class semaphore_lock {
	public:
	    /**
	     * Pointer to the semaphore we're operating on.
	     */
	    semaphore* sem;
	    /**
	     * Whether it is locked.
	     */
	    bool locked;

	    semaphore_lock()
		: sem(NULL), locked(false) {}
	    /**
	     * @param s pointer to the semaphore.
	     * @param l lock at creation?
	     */
	    semaphore_lock(semaphore* s,bool l=true)
		: sem(s), locked(false) {
		    if(l) lock();
		}
	    /**
	     * @param s reference to the semaphore.
	     * @param l lock at creation?
	     */
	    semaphore_lock(semaphore& s,bool l=true)
		: sem(&s), locked(false) {
		    if(l) lock();
		}

	    ~semaphore_lock() {
		unlock();
	    }

	    /**
	     * Lock it.
	     */
	    void lock();
	    /**
	     * Unlock it.
	     */
	    void unlock();
    };

    /**
     * Strip prefix from the string.
     * @param str the string.
     * @param prefix prefix to strip.
     * @return the string without prefix.
     * @todo TODO: document exceptions.
     */
    string strip_prefix(const string& str,const string& prefix);
    /**
     * Strip suffix from the string.
     * @param str the string.
     * @param suffix suffix to strip.
     * @return the string without suffix.
     * @todo TODO: document exceptions.
     */
    string strip_suffix(const string& str,const string& suffix);
  
    /**
     * Change to the directory and pop back at object's destruction (e.g. when
     * the object goes out of scope).
     */
    class auto_chdir {
	public:
	    /**
	     * Saved working directory.
	     */
	    string saved_pwd;
	    /**
	     * Whether we want to change back automatically.
	     */
	    bool autopop;

	    auto_chdir()
		: autopop(false) { }
	    /**
	     * @param td destination path.
	     * @param ap automatically come back?
	     */
	    auto_chdir(const string& td,bool ap=true)
		: autopop(false) { pushdir(td,ap); }
	    ~auto_chdir() {
		if(autopop)
		    popdir();
	    }

	    /**
	     * Change into directory.
	     * @param td the directory.
	     * @param ap automaticall pop back?
	     */
	    void pushdir(const string& td,bool ap=true);
	    /**
	     * Change to the saved directory.
	     */
	    void popdir();
    };

}

#endif /* __SITECING_SITECING_UTIL_H */