1 | // $Id: utility.cc 1186 2010-08-27 22:13:37Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2010 Peter Johansson |
---|
6 | |
---|
7 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
8 | |
---|
9 | svndigest is free software; you can redistribute it and/or modify it |
---|
10 | under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 3 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | svndigest is distributed in the hope that it will be useful, but |
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "utility.h" |
---|
24 | |
---|
25 | #include "subversion_info.h" |
---|
26 | #include "config.h" |
---|
27 | |
---|
28 | #include "yat/Exception.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | #include <cerrno> |
---|
32 | #include <cstdio> |
---|
33 | #include <cstdlib> |
---|
34 | #include <cstring> |
---|
35 | #include <fnmatch.h> |
---|
36 | #include <fstream> |
---|
37 | #include <sstream> |
---|
38 | #include <stdexcept> |
---|
39 | #include <string> |
---|
40 | #include <sys/param.h> |
---|
41 | #include <sys/stat.h> |
---|
42 | #include <unistd.h> |
---|
43 | |
---|
44 | #include <iostream> |
---|
45 | |
---|
46 | namespace theplu{ |
---|
47 | namespace svndigest{ |
---|
48 | |
---|
49 | int access_rights(const std::string& path, const std::string& bits) |
---|
50 | { |
---|
51 | if (access(path.c_str(),F_OK)) { |
---|
52 | throw std::runtime_error(std::string("access_rights: ") + path + |
---|
53 | "' does not exist."); |
---|
54 | } |
---|
55 | int mode=0; |
---|
56 | for (unsigned int i=0; i<bits.length(); ++i) |
---|
57 | switch (bits[i]) { |
---|
58 | case 'r': |
---|
59 | mode|=R_OK; |
---|
60 | break; |
---|
61 | case 'w': |
---|
62 | mode|=W_OK; |
---|
63 | break; |
---|
64 | case 'x': |
---|
65 | mode|=X_OK; |
---|
66 | break; |
---|
67 | } |
---|
68 | return access(path.c_str(),mode); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void chdir(const std::string& dir) |
---|
73 | { |
---|
74 | if (::chdir(dir.c_str()) ) |
---|
75 | throw yat::utility::errno_error("chdir: " + dir + ":"); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | std::string concatenate_path(std::string dir, std::string base) |
---|
80 | { |
---|
81 | if (dir.empty() || dir==".") |
---|
82 | return base; |
---|
83 | if (dir[dir.size()-1]!='/') |
---|
84 | dir.append("/"); |
---|
85 | return dir+base; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void copy_file(const std::string& source, const std::string& target) |
---|
90 | { |
---|
91 | std::ofstream o(target.c_str()); |
---|
92 | std::ifstream i(source.c_str()); |
---|
93 | while (i.good()) { |
---|
94 | char ch=i.get(); |
---|
95 | if (i.good()) |
---|
96 | o.put(ch); |
---|
97 | if (!o.good()) |
---|
98 | throw std::runtime_error(std::string("copy_file: ") + |
---|
99 | "writing target file failed '" + target + "'"); |
---|
100 | } |
---|
101 | if (!i.eof() && (i.bad() || i.fail())) // fail on everything except eof |
---|
102 | throw std::runtime_error(std::string("copy_file: ") + |
---|
103 | "error reading source file '" + source + "'"); |
---|
104 | i.close(); o.close(); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | std::string directory_name(std::string path) |
---|
109 | { |
---|
110 | size_t pos = path.find_last_of("/"); |
---|
111 | if (pos==std::string::npos) |
---|
112 | return "."; |
---|
113 | if (pos==path.size()-1) |
---|
114 | return directory_name(path.substr(0,path.size()-2)); |
---|
115 | return path.substr(0,pos+1); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | std::string file_name(const std::string& full_path) |
---|
120 | { |
---|
121 | std::stringstream ss(full_path); |
---|
122 | std::string name; |
---|
123 | while (getline(ss,name,'/')) {} |
---|
124 | return name; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | std::string getenv(const std::string& var) |
---|
129 | { |
---|
130 | char* buffer=std::getenv(var.c_str()); |
---|
131 | if (!buffer) |
---|
132 | throw std::runtime_error("Environment variable "+var+" is not set"); |
---|
133 | return std::string(buffer); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | std::string hex(int x, unsigned int width) |
---|
138 | { |
---|
139 | std::stringstream ss; |
---|
140 | ss << std::hex << x; |
---|
141 | if (!width) |
---|
142 | return ss.str(); |
---|
143 | if (ss.str().size()<width) |
---|
144 | return std::string(width-ss.str().size(), '0') + ss.str(); |
---|
145 | return ss.str().substr(0, width); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | std::string htrim(std::string str) |
---|
150 | { |
---|
151 | size_t length=str.size(); |
---|
152 | while(length && isspace(str[length-1])) |
---|
153 | --length; |
---|
154 | return str.substr(0,length); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | bool fnmatch(const std::string& pattern, const std::string& str) |
---|
159 | { |
---|
160 | int res = ::fnmatch(pattern.c_str(), str.c_str(), 0); |
---|
161 | if (res==0) |
---|
162 | return true; |
---|
163 | if (res!=FNM_NOMATCH) { |
---|
164 | std::stringstream ss; |
---|
165 | ss << "fnmatch with args: " << pattern << ", " << str; |
---|
166 | throw std::runtime_error(ss.str()); |
---|
167 | } |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | void lstat(const std::string path, struct stat* nodestat) |
---|
173 | { |
---|
174 | int res = ::lstat(path.c_str(), nodestat); |
---|
175 | if (res) { |
---|
176 | throw yat::utility::errno_error("lstat: "); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | std::string ltrim(std::string str) |
---|
182 | { |
---|
183 | size_t i = 0; |
---|
184 | while(i<str.size() && isspace(str[i])) |
---|
185 | ++i; |
---|
186 | return str.substr(i); |
---|
187 | } |
---|
188 | |
---|
189 | void mkdir(const std::string& dir) |
---|
190 | { |
---|
191 | int code = ::mkdir(dir.c_str(),0777); |
---|
192 | if (code){ |
---|
193 | std::stringstream ss; |
---|
194 | ss << "mkdir: `" << dir << "': "; |
---|
195 | throw yat::utility::errno_error(ss.str()); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | void mkdir_p(const std::string& dir) |
---|
201 | { |
---|
202 | if (node_exist(dir)) |
---|
203 | return; |
---|
204 | std::string mother = directory_name(dir); |
---|
205 | mkdir_p(mother); |
---|
206 | mkdir(dir); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | bool node_exist(const std::string& path) |
---|
211 | { |
---|
212 | struct stat buf; |
---|
213 | return !stat(path.c_str(),&buf); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | int percent(int a, int b) |
---|
218 | { |
---|
219 | if (b) |
---|
220 | return (100*a)/b; |
---|
221 | return 0; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | std::string pwd(void) |
---|
226 | { |
---|
227 | char buffer[MAXPATHLEN]; |
---|
228 | if (!getcwd(buffer, MAXPATHLEN)) |
---|
229 | throw std::runtime_error("Failed to get current working directory"); |
---|
230 | return std::string(buffer); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | bool regexp(const std::string& pattern, const std::string& str, |
---|
235 | std::vector<std::string>& vec) |
---|
236 | { |
---|
237 | bool regexp__(std::string::const_iterator first1, |
---|
238 | std::string::const_iterator last1, |
---|
239 | std::string::const_iterator first2, |
---|
240 | std::string::const_iterator last2, |
---|
241 | std::vector<std::string>::iterator item); |
---|
242 | |
---|
243 | // find number of special chars |
---|
244 | size_t count=0; |
---|
245 | for (std::string::const_iterator i=pattern.begin(); i!=pattern.end(); ++i) |
---|
246 | if (*i=='*' || *i=='?' || *i=='[') |
---|
247 | ++count; |
---|
248 | vec.resize(count); |
---|
249 | return regexp__(pattern.begin(), pattern.end(), str.begin(), str.end(), |
---|
250 | vec.begin()); |
---|
251 | } |
---|
252 | |
---|
253 | bool regexp__(std::string::const_iterator first1, |
---|
254 | std::string::const_iterator last1, |
---|
255 | std::string::const_iterator first2, |
---|
256 | std::string::const_iterator last2, |
---|
257 | std::vector<std::string>::iterator item) |
---|
258 | { |
---|
259 | if (first1==last1) { |
---|
260 | return first2==last2; |
---|
261 | } |
---|
262 | if (*first1 == '*') { |
---|
263 | if (first2<last2) { |
---|
264 | item->push_back(*first2); |
---|
265 | if (regexp__(first1, last1, first2+1, last2, item)) |
---|
266 | return true; |
---|
267 | item->resize(item->size()-1); |
---|
268 | } |
---|
269 | return regexp__(first1+1, last1, first2, last2, item+1); |
---|
270 | } |
---|
271 | if (*first1 == '?') { |
---|
272 | if (first2==last2) |
---|
273 | return false; |
---|
274 | *item = *first2; |
---|
275 | return regexp__(first1+1, last1, first2+1, last2, item+1); |
---|
276 | } |
---|
277 | if (*first1 == '[') { |
---|
278 | if (first2==last2) |
---|
279 | return false; |
---|
280 | bool found = false; |
---|
281 | while (*first1 != ']') { |
---|
282 | if (*first1 == *first2) { |
---|
283 | found = true; |
---|
284 | *item = *first2; |
---|
285 | } |
---|
286 | ++first1; |
---|
287 | assert(first1!=last1); |
---|
288 | } |
---|
289 | return regexp__(first1+1, last1, first2+1, last2, item+1); |
---|
290 | } |
---|
291 | |
---|
292 | if (first2==last2) |
---|
293 | return false; |
---|
294 | if (*first1 != *first2) |
---|
295 | return false; |
---|
296 | return regexp__(first1+1, last1, first2+1, last2, item); |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | void rename(const std::string& from, const std::string to) |
---|
301 | { |
---|
302 | int code = ::rename(from.c_str(), to.c_str()); |
---|
303 | if (code){ |
---|
304 | std::stringstream ss; |
---|
305 | ss << "rename" << from << " to " << to << ": "; |
---|
306 | throw yat::utility::errno_error(ss.str()); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | void replace(std::string& str, std::string old_str, std::string new_str) |
---|
312 | { |
---|
313 | std::string::iterator iter(str.begin()); |
---|
314 | while ((iter=search(iter, str.end(), old_str)) != str.end()) { |
---|
315 | size_t i = iter-str.begin(); |
---|
316 | str = std::string(str.begin(), iter) + new_str + |
---|
317 | std::string(iter+old_str.size(), str.end()); |
---|
318 | // pointing to char after substr we just inserted |
---|
319 | iter = str.begin() + (i+new_str.size()); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | void touch(std::string str) |
---|
325 | { |
---|
326 | if (!node_exist(str)) { |
---|
327 | std::ofstream os(str.c_str()); |
---|
328 | os.close(); |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | time_t str2time(const std::string& str) |
---|
333 | { |
---|
334 | // str in format 2006-09-09T10:55:52.132733Z |
---|
335 | std::stringstream sstream(str); |
---|
336 | time_t rawtime; |
---|
337 | struct tm * timeinfo; |
---|
338 | time ( &rawtime ); |
---|
339 | timeinfo = gmtime ( &rawtime ); |
---|
340 | |
---|
341 | unsigned int year, month, day, hour, minute, second; |
---|
342 | std::string tmp; |
---|
343 | getline(sstream,tmp,'-'); |
---|
344 | year=atoi(tmp.c_str()); |
---|
345 | timeinfo->tm_year = year - 1900; |
---|
346 | |
---|
347 | getline(sstream,tmp,'-'); |
---|
348 | month=atoi(tmp.c_str()); |
---|
349 | timeinfo->tm_mon = month - 1; |
---|
350 | |
---|
351 | getline(sstream,tmp,'T'); |
---|
352 | day=atoi(tmp.c_str()); |
---|
353 | timeinfo->tm_mday = day; |
---|
354 | |
---|
355 | getline(sstream,tmp,':'); |
---|
356 | hour=atoi(tmp.c_str()); |
---|
357 | timeinfo->tm_hour = hour; |
---|
358 | |
---|
359 | getline(sstream,tmp,':'); |
---|
360 | minute=atoi(tmp.c_str()); |
---|
361 | timeinfo->tm_min = minute; |
---|
362 | |
---|
363 | getline(sstream,tmp,'.'); |
---|
364 | second=atoi(tmp.c_str()); |
---|
365 | timeinfo->tm_sec = second; |
---|
366 | |
---|
367 | return mktime(timeinfo); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | std::string match(std::string::const_iterator& first, |
---|
372 | const std::string::const_iterator& last, |
---|
373 | std::string str) |
---|
374 | { |
---|
375 | if (match_begin(first, last, str)){ |
---|
376 | first+=str.size(); |
---|
377 | return str; |
---|
378 | } |
---|
379 | return std::string(); |
---|
380 | } |
---|
381 | |
---|
382 | }} // end of namespace svndigest and namespace theplu |
---|