1 | #ifndef _theplu_svndigest_utility_ |
---|
2 | #define _theplu_svndigest_utility_ |
---|
3 | |
---|
4 | // $Id: utility.h 1132 2010-07-16 03:24:35Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Peter Johansson |
---|
8 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
9 | Copyright (C) 2009, 2010 Peter Johansson |
---|
10 | |
---|
11 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
12 | |
---|
13 | svndigest is free software; you can redistribute it and/or modify it |
---|
14 | under the terms of the GNU General Public License as published by |
---|
15 | the Free Software Foundation; either version 3 of the License, or |
---|
16 | (at your option) any later version. |
---|
17 | |
---|
18 | svndigest is distributed in the hope that it will be useful, but |
---|
19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <functional> |
---|
29 | #include <iosfwd> |
---|
30 | #include <limits> |
---|
31 | #include <sstream> |
---|
32 | #include <stdexcept> |
---|
33 | #include <string> |
---|
34 | #include <utility> |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | #include <sys/stat.h> |
---|
38 | |
---|
39 | namespace theplu{ |
---|
40 | namespace svndigest{ |
---|
41 | |
---|
42 | /// |
---|
43 | /// @brief Check if access permissions match \a mode. \a mode must |
---|
44 | /// be given as r, w, x, or combinations of these letters. |
---|
45 | /// |
---|
46 | /// @return On success (all requested permissions granted), zero |
---|
47 | /// is returned. On error (at least one bit in mode asked for a |
---|
48 | /// permission that is denied, or some other error occurred), -1 |
---|
49 | /// is returned, and errno is set appropriately. |
---|
50 | /// |
---|
51 | /// @throw An std::runtime_error is thrown when checking for write |
---|
52 | /// permissions for a file/direcotry that does not exist. |
---|
53 | /// |
---|
54 | /// @see access(2) |
---|
55 | /// |
---|
56 | int access_rights(const std::string& path,const std::string& bits); |
---|
57 | |
---|
58 | /** |
---|
59 | wrapper around GNU C Library function chdir |
---|
60 | |
---|
61 | \throw if underlying chdir call does not return 0 |
---|
62 | */ |
---|
63 | void chdir(const std::string& dir); |
---|
64 | |
---|
65 | /** |
---|
66 | \return dir+base if dir ends with '/', else dir+'/'+base |
---|
67 | */ |
---|
68 | std::string concatenate_path(std::string dir, std::string base); |
---|
69 | |
---|
70 | /** |
---|
71 | @brief Copy file \a source to \a target. |
---|
72 | |
---|
73 | @throw std::runtime_error when read error of \a source or write |
---|
74 | error for \a target is encountered. |
---|
75 | */ |
---|
76 | void copy_file(const std::string& source, const std::string& target); |
---|
77 | |
---|
78 | /** |
---|
79 | Parsing out the directory of path that is everything prior the |
---|
80 | last '/' with the eception if \a path ends with '/' in which case |
---|
81 | everything prior the second last '/' is returned. |
---|
82 | |
---|
83 | \return directory of path. |
---|
84 | */ |
---|
85 | std::string directory_name(std::string path); |
---|
86 | |
---|
87 | /// |
---|
88 | /// @return everything after last '/' |
---|
89 | /// |
---|
90 | std::string file_name(const std::string&); |
---|
91 | |
---|
92 | /** |
---|
93 | \return true if \a str matches \a pattern |
---|
94 | |
---|
95 | \see fnmatch(3) |
---|
96 | */ |
---|
97 | bool fnmatch(const std::string& pattern, const std::string& str); |
---|
98 | |
---|
99 | /// |
---|
100 | /// @brief environment variable @a var |
---|
101 | /// |
---|
102 | std::string getenv(const std::string& var); |
---|
103 | |
---|
104 | /// |
---|
105 | /// If @a width is set, size is forced to length @a width. This |
---|
106 | /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1", |
---|
107 | /// respectively. |
---|
108 | /// |
---|
109 | /// @return x in hexadecimal base |
---|
110 | /// |
---|
111 | std::string hex(int x, unsigned int width=0); |
---|
112 | |
---|
113 | /// |
---|
114 | /// @brief remove trailing whitespaces |
---|
115 | /// |
---|
116 | std::string htrim(std::string str); |
---|
117 | |
---|
118 | /// |
---|
119 | /// @brief remove leading whitespaces |
---|
120 | /// |
---|
121 | std::string ltrim(std::string str); |
---|
122 | |
---|
123 | inline bool match_begin(std::string::const_iterator first, |
---|
124 | std::string::const_iterator last, |
---|
125 | const std::string& str) |
---|
126 | { return (std::distance(first, last)>=static_cast<int>(str.size()) && |
---|
127 | std::equal(str.begin(), str.end(), first)); |
---|
128 | } |
---|
129 | |
---|
130 | inline bool match_end(std::string::const_reverse_iterator first, |
---|
131 | std::string::const_reverse_iterator last, |
---|
132 | const std::string& str) |
---|
133 | { return (std::distance(first,last)>=static_cast<int>(str.size()) && |
---|
134 | std::equal(str.rbegin(), str.rend(), first)); |
---|
135 | } |
---|
136 | |
---|
137 | /// |
---|
138 | /// Create directory \a dir. The call can fail in many ways, cf. 'man |
---|
139 | /// mkdir'. |
---|
140 | /// |
---|
141 | void mkdir(const std::string& dir); |
---|
142 | |
---|
143 | /// |
---|
144 | /// Create directory \a dir and parents directories if needed. No |
---|
145 | /// error if \a dir already exists. |
---|
146 | /// |
---|
147 | void mkdir_p(const std::string& dir); |
---|
148 | |
---|
149 | /// |
---|
150 | /// @brief Check whether \a path already exists or not. |
---|
151 | /// |
---|
152 | /// @return True if \a path exists, false otherwise. |
---|
153 | /// |
---|
154 | bool node_exist(const std::string& path); |
---|
155 | |
---|
156 | /** |
---|
157 | @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$ |
---|
158 | */ |
---|
159 | int percent(int a, int b); |
---|
160 | |
---|
161 | /// |
---|
162 | /// @return the current working directory. |
---|
163 | /// |
---|
164 | std::string pwd(void); |
---|
165 | |
---|
166 | /** |
---|
167 | \return true if \a str matches \a pattern |
---|
168 | |
---|
169 | \a pattern may contain wildcards '*', '?' and \a vec will contain |
---|
170 | the matching string in \a str. If it's not a match, \a vec is |
---|
171 | undefined. The algorithm is greedy, i.e., wildcards '*' will |
---|
172 | consume as many charcters as possible. |
---|
173 | |
---|
174 | \note \a vec is supposed to be empty |
---|
175 | */ |
---|
176 | bool regexp(const std::string& pattern, const std::string& str, |
---|
177 | std::vector<std::string>& vec); |
---|
178 | |
---|
179 | /** |
---|
180 | same as rename(2) but throw errno if error is encountered |
---|
181 | |
---|
182 | \see man rename |
---|
183 | */ |
---|
184 | void rename(const std::string& from, const std::string to); |
---|
185 | |
---|
186 | /** |
---|
187 | In \a full_str replace every sub-string \a old_str with |
---|
188 | \a new_str; |
---|
189 | */ |
---|
190 | void replace(std::string& full_str, std::string old_str, std::string new_str); |
---|
191 | |
---|
192 | /// |
---|
193 | /// Search finds a subsecuence in [first, last) being identical to @ str |
---|
194 | /// |
---|
195 | /// @return iterator pointing to first character in found subsequence |
---|
196 | /// |
---|
197 | /// @see std::search |
---|
198 | /// |
---|
199 | inline std::string::iterator search(std::string::iterator first, |
---|
200 | std::string::iterator last, |
---|
201 | std::string str) |
---|
202 | { return std::search(first, last, str.begin(), str.end()); } |
---|
203 | |
---|
204 | /// |
---|
205 | /// |
---|
206 | /// |
---|
207 | time_t str2time(const std::string&); |
---|
208 | |
---|
209 | /// |
---|
210 | /// If file does not exist create empty file. |
---|
211 | /// |
---|
212 | void touch(std::string); |
---|
213 | |
---|
214 | /// |
---|
215 | /// remove leading and trailing whitespaces |
---|
216 | /// |
---|
217 | inline std::string trim(std::string str) { return htrim(ltrim(str)); } |
---|
218 | |
---|
219 | |
---|
220 | template <typename T> |
---|
221 | std::string match(std::string::const_iterator& first, |
---|
222 | const std::string::const_iterator& last, |
---|
223 | const T& func) |
---|
224 | { |
---|
225 | std::string res; |
---|
226 | for (;first!=last && func(first); ++first) |
---|
227 | res.append(1,*first); |
---|
228 | return res; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | std::string match(std::string::const_iterator& first, |
---|
233 | const std::string::const_iterator& last, |
---|
234 | std::string); |
---|
235 | |
---|
236 | }} // end of namespace svndigest end of namespace theplu |
---|
237 | |
---|
238 | #endif |
---|