165 |
24 Aug 06 |
jari |
1 |
#ifndef _theplu_svndigest_utility_ |
165 |
24 Aug 06 |
jari |
2 |
#define _theplu_svndigest_utility_ |
165 |
24 Aug 06 |
jari |
3 |
|
84 |
13 Mar 06 |
jari |
// $Id$ |
4 |
29 Dec 05 |
peter |
5 |
|
84 |
13 Mar 06 |
jari |
6 |
/* |
768 |
31 Jan 09 |
peter |
Copyright (C) 2005 Peter Johansson |
978 |
12 Dec 09 |
peter |
Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
1515 |
26 Sep 12 |
peter |
Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson |
84 |
13 Mar 06 |
jari |
10 |
|
687 |
04 Aug 08 |
peter |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
84 |
13 Mar 06 |
jari |
12 |
|
149 |
12 Aug 06 |
jari |
svndigest is free software; you can redistribute it and/or modify it |
84 |
13 Mar 06 |
jari |
under the terms of the GNU General Public License as published by |
693 |
11 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
84 |
13 Mar 06 |
jari |
(at your option) any later version. |
84 |
13 Mar 06 |
jari |
17 |
|
149 |
12 Aug 06 |
jari |
svndigest is distributed in the hope that it will be useful, but |
84 |
13 Mar 06 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
126 |
01 Aug 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
84 |
13 Mar 06 |
jari |
General Public License for more details. |
84 |
13 Mar 06 |
jari |
22 |
|
84 |
13 Mar 06 |
jari |
You should have received a copy of the GNU General Public License |
693 |
11 Sep 08 |
jari |
along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
84 |
13 Mar 06 |
jari |
25 |
*/ |
84 |
13 Mar 06 |
jari |
26 |
|
1203 |
05 Oct 10 |
peter |
27 |
#include "Functor.h" |
1203 |
05 Oct 10 |
peter |
28 |
|
4 |
29 Dec 05 |
peter |
29 |
#include <algorithm> |
4 |
29 Dec 05 |
peter |
30 |
#include <functional> |
129 |
02 Aug 06 |
jari |
31 |
#include <iosfwd> |
727 |
11 Dec 08 |
jari |
32 |
#include <limits> |
705 |
25 Nov 08 |
peter |
33 |
#include <sstream> |
727 |
11 Dec 08 |
jari |
34 |
#include <stdexcept> |
23 |
02 Jan 06 |
peter |
35 |
#include <string> |
4 |
29 Dec 05 |
peter |
36 |
#include <utility> |
4 |
29 Dec 05 |
peter |
37 |
#include <vector> |
4 |
29 Dec 05 |
peter |
38 |
|
126 |
01 Aug 06 |
jari |
39 |
#include <sys/stat.h> |
126 |
01 Aug 06 |
jari |
40 |
|
4 |
29 Dec 05 |
peter |
41 |
namespace theplu{ |
149 |
12 Aug 06 |
jari |
42 |
namespace svndigest{ |
4 |
29 Dec 05 |
peter |
43 |
|
1547 |
20 Oct 12 |
peter |
44 |
/** |
1547 |
20 Oct 12 |
peter |
if path is absolute return path otherwise return |
1547 |
20 Oct 12 |
peter |
concatenate_path(pwd(), path) |
1547 |
20 Oct 12 |
peter |
47 |
*/ |
1547 |
20 Oct 12 |
peter |
48 |
std::string absolute_path(const std::string& path); |
1547 |
20 Oct 12 |
peter |
49 |
|
23 |
02 Jan 06 |
peter |
50 |
/// |
151 |
13 Aug 06 |
jari |
/// @brief Check if access permissions match \a mode. \a mode must |
151 |
13 Aug 06 |
jari |
/// be given as r, w, x, or combinations of these letters. |
151 |
13 Aug 06 |
jari |
53 |
/// |
151 |
13 Aug 06 |
jari |
/// @return On success (all requested permissions granted), zero |
151 |
13 Aug 06 |
jari |
/// is returned. On error (at least one bit in mode asked for a |
151 |
13 Aug 06 |
jari |
/// permission that is denied, or some other error occurred), -1 |
151 |
13 Aug 06 |
jari |
/// is returned, and errno is set appropriately. |
151 |
13 Aug 06 |
jari |
58 |
/// |
151 |
13 Aug 06 |
jari |
/// @throw An std::runtime_error is thrown when checking for write |
151 |
13 Aug 06 |
jari |
/// permissions for a file/direcotry that does not exist. |
151 |
13 Aug 06 |
jari |
61 |
/// |
151 |
13 Aug 06 |
jari |
/// @see access(2) |
151 |
13 Aug 06 |
jari |
63 |
/// |
151 |
13 Aug 06 |
jari |
64 |
int access_rights(const std::string& path,const std::string& bits); |
151 |
13 Aug 06 |
jari |
65 |
|
462 |
21 Aug 07 |
jari |
66 |
/** |
1392 |
12 Jul 11 |
peter |
67 |
*/ |
1392 |
12 Jul 11 |
peter |
68 |
void chmod(const std::string& filename, mode_t mode); |
1392 |
12 Jul 11 |
peter |
69 |
|
1392 |
12 Jul 11 |
peter |
70 |
/** |
1085 |
12 Jun 10 |
peter |
wrapper around GNU C Library function chdir |
1085 |
12 Jun 10 |
peter |
72 |
|
1085 |
12 Jun 10 |
peter |
\throw if underlying chdir call does not return 0 |
1085 |
12 Jun 10 |
peter |
74 |
*/ |
1085 |
12 Jun 10 |
peter |
75 |
void chdir(const std::string& dir); |
1085 |
12 Jun 10 |
peter |
76 |
|
1085 |
12 Jun 10 |
peter |
77 |
/** |
705 |
25 Nov 08 |
peter |
\return dir+base if dir ends with '/', else dir+'/'+base |
705 |
25 Nov 08 |
peter |
79 |
*/ |
705 |
25 Nov 08 |
peter |
80 |
std::string concatenate_path(std::string dir, std::string base); |
705 |
25 Nov 08 |
peter |
81 |
|
705 |
25 Nov 08 |
peter |
82 |
/** |
462 |
21 Aug 07 |
jari |
@brief Copy file \a source to \a target. |
462 |
21 Aug 07 |
jari |
84 |
|
462 |
21 Aug 07 |
jari |
@throw std::runtime_error when read error of \a source or write |
462 |
21 Aug 07 |
jari |
error for \a target is encountered. |
462 |
21 Aug 07 |
jari |
87 |
*/ |
462 |
21 Aug 07 |
jari |
88 |
void copy_file(const std::string& source, const std::string& target); |
462 |
21 Aug 07 |
jari |
89 |
|
482 |
13 Oct 07 |
peter |
90 |
/** |
482 |
13 Oct 07 |
peter |
Parsing out the directory of path that is everything prior the |
482 |
13 Oct 07 |
peter |
last '/' with the eception if \a path ends with '/' in which case |
482 |
13 Oct 07 |
peter |
everything prior the second last '/' is returned. |
482 |
13 Oct 07 |
peter |
94 |
|
482 |
13 Oct 07 |
peter |
\return directory of path. |
482 |
13 Oct 07 |
peter |
96 |
*/ |
1432 |
18 Dec 11 |
peter |
97 |
std::string directory_name(const std::string& path); |
482 |
13 Oct 07 |
peter |
98 |
|
151 |
13 Aug 06 |
jari |
99 |
/// |
100 |
19 Jun 06 |
peter |
/// @return everything after last '/' |
100 |
19 Jun 06 |
peter |
101 |
/// |
100 |
19 Jun 06 |
peter |
102 |
std::string file_name(const std::string&); |
100 |
19 Jun 06 |
peter |
103 |
|
1052 |
18 Apr 10 |
peter |
104 |
/** |
1052 |
18 Apr 10 |
peter |
\return true if \a str matches \a pattern |
1052 |
18 Apr 10 |
peter |
106 |
|
1052 |
18 Apr 10 |
peter |
\see fnmatch(3) |
1052 |
18 Apr 10 |
peter |
108 |
*/ |
1052 |
18 Apr 10 |
peter |
109 |
bool fnmatch(const std::string& pattern, const std::string& str); |
1052 |
18 Apr 10 |
peter |
110 |
|
198 |
09 Sep 06 |
peter |
111 |
/// |
198 |
09 Sep 06 |
peter |
/// @brief environment variable @a var |
198 |
09 Sep 06 |
peter |
113 |
/// |
1513 |
23 Sep 12 |
peter |
114 |
std::string getenv(const std::string& var); |
198 |
09 Sep 06 |
peter |
115 |
|
226 |
11 Mar 07 |
peter |
116 |
/// |
373 |
19 Jun 07 |
peter |
/// If @a width is set, size is forced to length @a width. This |
373 |
19 Jun 07 |
peter |
/// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1", |
373 |
19 Jun 07 |
peter |
/// respectively. |
373 |
19 Jun 07 |
peter |
120 |
/// |
373 |
19 Jun 07 |
peter |
/// @return x in hexadecimal base |
373 |
19 Jun 07 |
peter |
122 |
/// |
589 |
12 Apr 08 |
jari |
123 |
std::string hex(int x, unsigned int width=0); |
373 |
19 Jun 07 |
peter |
124 |
|
373 |
19 Jun 07 |
peter |
125 |
/// |
226 |
11 Mar 07 |
peter |
/// @brief remove trailing whitespaces |
226 |
11 Mar 07 |
peter |
127 |
/// |
226 |
11 Mar 07 |
peter |
128 |
std::string htrim(std::string str); |
226 |
11 Mar 07 |
peter |
129 |
|
1186 |
27 Aug 10 |
peter |
130 |
/** |
1547 |
20 Oct 12 |
peter |
\return true if path has finite size and first charcter is '/' |
1547 |
20 Oct 12 |
peter |
132 |
*/ |
1547 |
20 Oct 12 |
peter |
133 |
bool is_absolute_path(const std::string& path); |
1547 |
20 Oct 12 |
peter |
134 |
|
1547 |
20 Oct 12 |
peter |
135 |
/** |
1186 |
27 Aug 10 |
peter |
same as lstat(2) but throws an errno_error if error is detected |
1186 |
27 Aug 10 |
peter |
137 |
*/ |
1186 |
27 Aug 10 |
peter |
138 |
void lstat(const std::string path, struct stat*); |
1186 |
27 Aug 10 |
peter |
139 |
|
226 |
11 Mar 07 |
peter |
140 |
/// |
226 |
11 Mar 07 |
peter |
/// @brief remove leading whitespaces |
226 |
11 Mar 07 |
peter |
142 |
/// |
226 |
11 Mar 07 |
peter |
143 |
std::string ltrim(std::string str); |
226 |
11 Mar 07 |
peter |
144 |
|
1513 |
23 Sep 12 |
peter |
145 |
inline bool match_begin(std::string::const_iterator first, |
1513 |
23 Sep 12 |
peter |
146 |
std::string::const_iterator last, |
118 |
03 Jul 06 |
peter |
147 |
const std::string& str) |
1513 |
23 Sep 12 |
peter |
148 |
{ return (std::distance(first, last)>=static_cast<int>(str.size()) && |
1513 |
23 Sep 12 |
peter |
149 |
std::equal(str.begin(), str.end(), first)); |
118 |
03 Jul 06 |
peter |
150 |
} |
118 |
03 Jul 06 |
peter |
151 |
|
1513 |
23 Sep 12 |
peter |
152 |
inline bool match_end(std::string::const_reverse_iterator first, |
1513 |
23 Sep 12 |
peter |
153 |
std::string::const_reverse_iterator last, |
118 |
03 Jul 06 |
peter |
154 |
const std::string& str) |
1513 |
23 Sep 12 |
peter |
155 |
{ return (std::distance(first,last)>=static_cast<int>(str.size()) && |
1513 |
23 Sep 12 |
peter |
156 |
std::equal(str.rbegin(), str.rend(), first)); |
118 |
03 Jul 06 |
peter |
157 |
} |
118 |
03 Jul 06 |
peter |
158 |
|
152 |
13 Aug 06 |
jari |
159 |
/// |
152 |
13 Aug 06 |
jari |
/// Create directory \a dir. The call can fail in many ways, cf. 'man |
152 |
13 Aug 06 |
jari |
/// mkdir'. |
152 |
13 Aug 06 |
jari |
162 |
/// |
482 |
13 Oct 07 |
peter |
163 |
void mkdir(const std::string& dir); |
152 |
13 Aug 06 |
jari |
164 |
|
152 |
13 Aug 06 |
jari |
165 |
/// |
796 |
01 Jul 09 |
peter |
/// Create directory \a dir and parents directories if needed. No |
796 |
01 Jul 09 |
peter |
/// error if \a dir already exists. |
796 |
01 Jul 09 |
peter |
168 |
/// |
796 |
01 Jul 09 |
peter |
169 |
void mkdir_p(const std::string& dir); |
796 |
01 Jul 09 |
peter |
170 |
|
796 |
01 Jul 09 |
peter |
171 |
/// |
226 |
11 Mar 07 |
peter |
/// @brief Check whether \a path already exists or not. |
226 |
11 Mar 07 |
peter |
173 |
/// |
226 |
11 Mar 07 |
peter |
/// @return True if \a path exists, false otherwise. |
226 |
11 Mar 07 |
peter |
175 |
/// |
226 |
11 Mar 07 |
peter |
176 |
bool node_exist(const std::string& path); |
226 |
11 Mar 07 |
peter |
177 |
|
313 |
17 May 07 |
peter |
178 |
/** |
1513 |
23 Sep 12 |
peter |
@return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$ |
313 |
17 May 07 |
peter |
180 |
*/ |
313 |
17 May 07 |
peter |
181 |
int percent(int a, int b); |
313 |
17 May 07 |
peter |
182 |
|
226 |
11 Mar 07 |
peter |
183 |
/// |
152 |
13 Aug 06 |
jari |
/// @return the current working directory. |
152 |
13 Aug 06 |
jari |
185 |
/// |
152 |
13 Aug 06 |
jari |
186 |
std::string pwd(void); |
152 |
13 Aug 06 |
jari |
187 |
|
514 |
09 Dec 07 |
peter |
188 |
/** |
1098 |
13 Jun 10 |
peter |
\return true if \a str matches \a pattern |
516 |
09 Dec 07 |
peter |
190 |
|
1098 |
13 Jun 10 |
peter |
\a pattern may contain wildcards '*', '?' and \a vec will contain |
1098 |
13 Jun 10 |
peter |
the matching string in \a str. If it's not a match, \a vec is |
1098 |
13 Jun 10 |
peter |
undefined. The algorithm is greedy, i.e., wildcards '*' will |
516 |
09 Dec 07 |
peter |
consume as many charcters as possible. |
516 |
09 Dec 07 |
peter |
195 |
|
516 |
09 Dec 07 |
peter |
\note \a vec is supposed to be empty |
516 |
09 Dec 07 |
peter |
197 |
*/ |
1098 |
13 Jun 10 |
peter |
198 |
bool regexp(const std::string& pattern, const std::string& str, |
516 |
09 Dec 07 |
peter |
199 |
std::vector<std::string>& vec); |
516 |
09 Dec 07 |
peter |
200 |
|
516 |
09 Dec 07 |
peter |
201 |
/** |
1547 |
20 Oct 12 |
peter |
\return \a abs_path relative from \a dir, i.e., abs_path = |
1547 |
20 Oct 12 |
peter |
dir/<returned value> |
1547 |
20 Oct 12 |
peter |
204 |
*/ |
1547 |
20 Oct 12 |
peter |
205 |
std::string relative_path(const std::string& abs_path,const std::string& dir); |
1547 |
20 Oct 12 |
peter |
206 |
|
1547 |
20 Oct 12 |
peter |
207 |
/** |
1423 |
16 Dec 11 |
peter |
same as C function remove but throws errno_error at failure |
1423 |
16 Dec 11 |
peter |
209 |
|
1423 |
16 Dec 11 |
peter |
\see man remove |
1423 |
16 Dec 11 |
peter |
211 |
*/ |
1423 |
16 Dec 11 |
peter |
212 |
void remove(const std::string& fn); |
1423 |
16 Dec 11 |
peter |
213 |
|
1423 |
16 Dec 11 |
peter |
214 |
/** |
1132 |
16 Jul 10 |
peter |
same as rename(2) but throw errno if error is encountered |
1132 |
16 Jul 10 |
peter |
216 |
|
1132 |
16 Jul 10 |
peter |
\see man rename |
1132 |
16 Jul 10 |
peter |
218 |
*/ |
1132 |
16 Jul 10 |
peter |
219 |
void rename(const std::string& from, const std::string to); |
1132 |
16 Jul 10 |
peter |
220 |
|
1132 |
16 Jul 10 |
peter |
221 |
/** |
514 |
09 Dec 07 |
peter |
In \a full_str replace every sub-string \a old_str with |
514 |
09 Dec 07 |
peter |
\a new_str; |
514 |
09 Dec 07 |
peter |
224 |
*/ |
514 |
09 Dec 07 |
peter |
225 |
void replace(std::string& full_str, std::string old_str, std::string new_str); |
514 |
09 Dec 07 |
peter |
226 |
|
227 |
12 Mar 07 |
peter |
227 |
/// |
227 |
12 Mar 07 |
peter |
/// Search finds a subsecuence in [first, last) being identical to @ str |
227 |
12 Mar 07 |
peter |
229 |
/// |
227 |
12 Mar 07 |
peter |
/// @return iterator pointing to first character in found subsequence |
227 |
12 Mar 07 |
peter |
231 |
/// |
227 |
12 Mar 07 |
peter |
/// @see std::search |
227 |
12 Mar 07 |
peter |
233 |
/// |
1513 |
23 Sep 12 |
peter |
234 |
inline std::string::iterator search(std::string::iterator first, |
1513 |
23 Sep 12 |
peter |
235 |
std::string::iterator last, |
227 |
12 Mar 07 |
peter |
236 |
std::string str) |
118 |
03 Jul 06 |
peter |
237 |
{ return std::search(first, last, str.begin(), str.end()); } |
118 |
03 Jul 06 |
peter |
238 |
|
1203 |
05 Oct 10 |
peter |
239 |
/** |
1203 |
05 Oct 10 |
peter |
same as sum(3) but using binary(result, *first) rather than |
1203 |
05 Oct 10 |
peter |
result += *first |
1203 |
05 Oct 10 |
peter |
242 |
*/ |
1203 |
05 Oct 10 |
peter |
243 |
template<typename InputIterator, typename T, typename BinaryOperation> |
1513 |
23 Sep 12 |
peter |
244 |
void sum(InputIterator first, InputIterator last, T& result, |
1203 |
05 Oct 10 |
peter |
245 |
BinaryOperation binary) |
1203 |
05 Oct 10 |
peter |
246 |
{ |
1203 |
05 Oct 10 |
peter |
247 |
for (; first!=last; ++first) |
1203 |
05 Oct 10 |
peter |
248 |
binary(result, *first); |
1203 |
05 Oct 10 |
peter |
249 |
} |
1203 |
05 Oct 10 |
peter |
250 |
|
1203 |
05 Oct 10 |
peter |
251 |
/** |
1203 |
05 Oct 10 |
peter |
Add all values of [first, last) to result |
1203 |
05 Oct 10 |
peter |
253 |
*/ |
1203 |
05 Oct 10 |
peter |
254 |
template<typename InputIterator, typename T> |
1203 |
05 Oct 10 |
peter |
255 |
void sum(InputIterator first, InputIterator last, T& result) |
1203 |
05 Oct 10 |
peter |
256 |
{ |
1203 |
05 Oct 10 |
peter |
257 |
typedef typename std::iterator_traits<InputIterator>::const_reference ref; |
1203 |
05 Oct 10 |
peter |
258 |
typedef PlusAssign<T&, ref> binary; |
1203 |
05 Oct 10 |
peter |
259 |
return sum(first, last, result, binary()); |
1203 |
05 Oct 10 |
peter |
260 |
} |
1203 |
05 Oct 10 |
peter |
261 |
|
201 |
09 Sep 06 |
peter |
262 |
/// |
297 |
08 May 07 |
peter |
/// If file does not exist create empty file. |
297 |
08 May 07 |
peter |
264 |
/// |
297 |
08 May 07 |
peter |
265 |
void touch(std::string); |
297 |
08 May 07 |
peter |
266 |
|
297 |
08 May 07 |
peter |
267 |
/// |
226 |
11 Mar 07 |
peter |
/// remove leading and trailing whitespaces |
226 |
11 Mar 07 |
peter |
269 |
/// |
226 |
11 Mar 07 |
peter |
270 |
inline std::string trim(std::string str) { return htrim(ltrim(str)); } |
226 |
11 Mar 07 |
peter |
271 |
|
289 |
08 May 07 |
peter |
272 |
|
371 |
19 Jun 07 |
peter |
273 |
template <typename T> |
289 |
08 May 07 |
peter |
274 |
std::string match(std::string::const_iterator& first, |
289 |
08 May 07 |
peter |
275 |
const std::string::const_iterator& last, |
289 |
08 May 07 |
peter |
276 |
const T& func) |
289 |
08 May 07 |
peter |
277 |
{ |
289 |
08 May 07 |
peter |
278 |
std::string res; |
1513 |
23 Sep 12 |
peter |
279 |
for (;first!=last && func(first); ++first) |
289 |
08 May 07 |
peter |
280 |
res.append(1,*first); |
289 |
08 May 07 |
peter |
281 |
return res; |
289 |
08 May 07 |
peter |
282 |
} |
289 |
08 May 07 |
peter |
283 |
|
309 |
12 May 07 |
peter |
284 |
|
309 |
12 May 07 |
peter |
285 |
std::string match(std::string::const_iterator& first, |
309 |
12 May 07 |
peter |
286 |
const std::string::const_iterator& last, |
309 |
12 May 07 |
peter |
287 |
std::string); |
309 |
12 May 07 |
peter |
288 |
|
149 |
12 Aug 06 |
jari |
289 |
}} // end of namespace svndigest end of namespace theplu |
1423 |
16 Dec 11 |
peter |
290 |
#endif |