62 |
19 Jan 06 |
peter |
// $Id$ |
62 |
19 Jan 06 |
peter |
2 |
|
84 |
13 Mar 06 |
jari |
3 |
/* |
978 |
12 Dec 09 |
peter |
Copyright (C) 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson |
1515 |
26 Sep 12 |
peter |
Copyright (C) 2010, 2011, 2012 Peter Johansson |
84 |
13 Mar 06 |
jari |
6 |
|
687 |
04 Aug 08 |
peter |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
84 |
13 Mar 06 |
jari |
8 |
|
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 |
13 |
|
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 |
149 |
12 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 |
18 |
|
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 |
21 |
*/ |
84 |
13 Mar 06 |
jari |
22 |
|
1253 |
31 Oct 10 |
peter |
23 |
#include <config.h> |
1253 |
31 Oct 10 |
peter |
24 |
|
62 |
19 Jan 06 |
peter |
25 |
#include "utility.h" |
62 |
19 Jan 06 |
peter |
26 |
|
1675 |
26 Aug 23 |
peter |
27 |
#include <yat/utility/FileUtil.h> |
1673 |
26 Aug 23 |
peter |
28 |
#include <yat/utility/Exception.h> |
1675 |
26 Aug 23 |
peter |
29 |
#include <yat/utility/utility.h> |
1085 |
12 Jun 10 |
peter |
30 |
|
516 |
09 Dec 07 |
peter |
31 |
#include <cassert> |
755 |
15 Jan 09 |
jari |
32 |
#include <cstdio> |
198 |
09 Sep 06 |
peter |
33 |
#include <cstdlib> |
755 |
15 Jan 09 |
jari |
34 |
#include <cstring> |
62 |
19 Jan 06 |
peter |
35 |
#include <fstream> |
62 |
19 Jan 06 |
peter |
36 |
#include <sstream> |
151 |
13 Aug 06 |
jari |
37 |
#include <stdexcept> |
62 |
19 Jan 06 |
peter |
38 |
#include <string> |
705 |
25 Nov 08 |
peter |
39 |
#include <sys/stat.h> |
115 |
30 Jun 06 |
peter |
40 |
#include <unistd.h> |
62 |
19 Jan 06 |
peter |
41 |
|
201 |
09 Sep 06 |
peter |
42 |
#include <iostream> |
201 |
09 Sep 06 |
peter |
43 |
|
1425 |
16 Dec 11 |
peter |
44 |
#include <libgen.h> |
1425 |
16 Dec 11 |
peter |
45 |
|
62 |
19 Jan 06 |
peter |
46 |
namespace theplu{ |
149 |
12 Aug 06 |
jari |
47 |
namespace svndigest{ |
62 |
19 Jan 06 |
peter |
48 |
|
1547 |
20 Oct 12 |
peter |
49 |
std::string absolute_path(const std::string& path) |
1547 |
20 Oct 12 |
peter |
50 |
{ |
1547 |
20 Oct 12 |
peter |
51 |
if (is_absolute_path(path)) |
1547 |
20 Oct 12 |
peter |
52 |
return path; |
1547 |
20 Oct 12 |
peter |
53 |
return concatenate_path(pwd(), path); |
1547 |
20 Oct 12 |
peter |
54 |
} |
1547 |
20 Oct 12 |
peter |
55 |
|
151 |
13 Aug 06 |
jari |
56 |
int access_rights(const std::string& path, const std::string& bits) |
151 |
13 Aug 06 |
jari |
57 |
{ |
1675 |
26 Aug 23 |
peter |
58 |
return yat::utility::FileUtil(path).permissions(bits); |
151 |
13 Aug 06 |
jari |
59 |
} |
151 |
13 Aug 06 |
jari |
60 |
|
151 |
13 Aug 06 |
jari |
61 |
|
705 |
25 Nov 08 |
peter |
62 |
std::string concatenate_path(std::string dir, std::string base) |
705 |
25 Nov 08 |
peter |
63 |
{ |
709 |
26 Nov 08 |
peter |
64 |
if (dir.empty() || dir==".") |
705 |
25 Nov 08 |
peter |
65 |
return base; |
705 |
25 Nov 08 |
peter |
66 |
if (dir[dir.size()-1]!='/') |
705 |
25 Nov 08 |
peter |
67 |
dir.append("/"); |
705 |
25 Nov 08 |
peter |
68 |
return dir+base; |
705 |
25 Nov 08 |
peter |
69 |
} |
705 |
25 Nov 08 |
peter |
70 |
|
705 |
25 Nov 08 |
peter |
71 |
|
198 |
09 Sep 06 |
peter |
72 |
std::string getenv(const std::string& var) |
198 |
09 Sep 06 |
peter |
73 |
{ |
198 |
09 Sep 06 |
peter |
74 |
char* buffer=std::getenv(var.c_str()); |
198 |
09 Sep 06 |
peter |
75 |
if (!buffer) |
198 |
09 Sep 06 |
peter |
76 |
throw std::runtime_error("Environment variable "+var+" is not set"); |
198 |
09 Sep 06 |
peter |
77 |
return std::string(buffer); |
198 |
09 Sep 06 |
peter |
78 |
} |
198 |
09 Sep 06 |
peter |
79 |
|
198 |
09 Sep 06 |
peter |
80 |
|
589 |
12 Apr 08 |
jari |
81 |
std::string hex(int x, unsigned int width) |
373 |
19 Jun 07 |
peter |
82 |
{ |
373 |
19 Jun 07 |
peter |
83 |
std::stringstream ss; |
373 |
19 Jun 07 |
peter |
84 |
ss << std::hex << x; |
373 |
19 Jun 07 |
peter |
85 |
if (!width) |
373 |
19 Jun 07 |
peter |
86 |
return ss.str(); |
1439 |
20 Dec 11 |
peter |
87 |
if (ss.str().size()<width) |
373 |
19 Jun 07 |
peter |
88 |
return std::string(width-ss.str().size(), '0') + ss.str(); |
373 |
19 Jun 07 |
peter |
89 |
return ss.str().substr(0, width); |
373 |
19 Jun 07 |
peter |
90 |
} |
373 |
19 Jun 07 |
peter |
91 |
|
373 |
19 Jun 07 |
peter |
92 |
|
226 |
11 Mar 07 |
peter |
93 |
std::string htrim(std::string str) |
226 |
11 Mar 07 |
peter |
94 |
{ |
226 |
11 Mar 07 |
peter |
95 |
size_t length=str.size(); |
226 |
11 Mar 07 |
peter |
96 |
while(length && isspace(str[length-1])) |
226 |
11 Mar 07 |
peter |
97 |
--length; |
226 |
11 Mar 07 |
peter |
98 |
return str.substr(0,length); |
226 |
11 Mar 07 |
peter |
99 |
} |
226 |
11 Mar 07 |
peter |
100 |
|
226 |
11 Mar 07 |
peter |
101 |
|
1547 |
20 Oct 12 |
peter |
102 |
bool is_absolute_path(const std::string& path) |
1547 |
20 Oct 12 |
peter |
103 |
{ |
1547 |
20 Oct 12 |
peter |
104 |
return path.size() && path[0]=='/'; |
1547 |
20 Oct 12 |
peter |
105 |
} |
1547 |
20 Oct 12 |
peter |
106 |
|
1547 |
20 Oct 12 |
peter |
107 |
|
1186 |
27 Aug 10 |
peter |
108 |
void lstat(const std::string path, struct stat* nodestat) |
1186 |
27 Aug 10 |
peter |
109 |
{ |
1186 |
27 Aug 10 |
peter |
110 |
int res = ::lstat(path.c_str(), nodestat); |
1186 |
27 Aug 10 |
peter |
111 |
if (res) { |
1186 |
27 Aug 10 |
peter |
112 |
throw yat::utility::errno_error("lstat: "); |
1186 |
27 Aug 10 |
peter |
113 |
} |
1186 |
27 Aug 10 |
peter |
114 |
} |
1186 |
27 Aug 10 |
peter |
115 |
|
1186 |
27 Aug 10 |
peter |
116 |
|
226 |
11 Mar 07 |
peter |
117 |
std::string ltrim(std::string str) |
226 |
11 Mar 07 |
peter |
118 |
{ |
226 |
11 Mar 07 |
peter |
119 |
size_t i = 0; |
226 |
11 Mar 07 |
peter |
120 |
while(i<str.size() && isspace(str[i])) |
226 |
11 Mar 07 |
peter |
121 |
++i; |
226 |
11 Mar 07 |
peter |
122 |
return str.substr(i); |
226 |
11 Mar 07 |
peter |
123 |
} |
226 |
11 Mar 07 |
peter |
124 |
|
482 |
13 Oct 07 |
peter |
125 |
void mkdir(const std::string& dir) |
1439 |
20 Dec 11 |
peter |
126 |
{ |
1675 |
26 Aug 23 |
peter |
127 |
yat::utility::mkdir(dir); |
313 |
17 May 07 |
peter |
128 |
} |
313 |
17 May 07 |
peter |
129 |
|
313 |
17 May 07 |
peter |
130 |
|
796 |
01 Jul 09 |
peter |
131 |
void mkdir_p(const std::string& dir) |
1439 |
20 Dec 11 |
peter |
132 |
{ |
1675 |
26 Aug 23 |
peter |
133 |
yat::utility::mkdir_p(dir); |
796 |
01 Jul 09 |
peter |
134 |
} |
796 |
01 Jul 09 |
peter |
135 |
|
796 |
01 Jul 09 |
peter |
136 |
|
226 |
11 Mar 07 |
peter |
137 |
bool node_exist(const std::string& path) |
226 |
11 Mar 07 |
peter |
138 |
{ |
1675 |
26 Aug 23 |
peter |
139 |
return yat::utility::FileUtil(path).exists(); |
226 |
11 Mar 07 |
peter |
140 |
} |
226 |
11 Mar 07 |
peter |
141 |
|
226 |
11 Mar 07 |
peter |
142 |
|
482 |
13 Oct 07 |
peter |
143 |
int percent(int a, int b) |
482 |
13 Oct 07 |
peter |
144 |
{ |
482 |
13 Oct 07 |
peter |
145 |
if (b) |
482 |
13 Oct 07 |
peter |
146 |
return (100*a)/b; |
482 |
13 Oct 07 |
peter |
147 |
return 0; |
482 |
13 Oct 07 |
peter |
148 |
} |
482 |
13 Oct 07 |
peter |
149 |
|
482 |
13 Oct 07 |
peter |
150 |
|
115 |
30 Jun 06 |
peter |
151 |
std::string pwd(void) |
115 |
30 Jun 06 |
peter |
152 |
{ |
1322 |
30 Nov 10 |
peter |
153 |
char buffer[FILENAME_MAX]; |
1322 |
30 Nov 10 |
peter |
154 |
if (!getcwd(buffer, FILENAME_MAX)) |
1245 |
25 Oct 10 |
peter |
155 |
throw yat::utility::errno_error("pwd: "); |
115 |
30 Jun 06 |
peter |
156 |
return std::string(buffer); |
115 |
30 Jun 06 |
peter |
157 |
} |
115 |
30 Jun 06 |
peter |
158 |
|
297 |
08 May 07 |
peter |
159 |
|
1098 |
13 Jun 10 |
peter |
160 |
bool regexp(const std::string& pattern, const std::string& str, |
516 |
09 Dec 07 |
peter |
161 |
std::vector<std::string>& vec) |
516 |
09 Dec 07 |
peter |
162 |
{ |
1439 |
20 Dec 11 |
peter |
163 |
bool regexp__(std::string::const_iterator first1, |
1098 |
13 Jun 10 |
peter |
164 |
std::string::const_iterator last1, |
1098 |
13 Jun 10 |
peter |
165 |
std::string::const_iterator first2, |
1098 |
13 Jun 10 |
peter |
166 |
std::string::const_iterator last2, |
1098 |
13 Jun 10 |
peter |
167 |
std::vector<std::string>::iterator item); |
1439 |
20 Dec 11 |
peter |
168 |
|
1098 |
13 Jun 10 |
peter |
// find number of special chars |
1098 |
13 Jun 10 |
peter |
170 |
size_t count=0; |
1098 |
13 Jun 10 |
peter |
171 |
for (std::string::const_iterator i=pattern.begin(); i!=pattern.end(); ++i) |
1098 |
13 Jun 10 |
peter |
172 |
if (*i=='*' || *i=='?' || *i=='[') |
1098 |
13 Jun 10 |
peter |
173 |
++count; |
1098 |
13 Jun 10 |
peter |
174 |
vec.resize(count); |
1098 |
13 Jun 10 |
peter |
175 |
return regexp__(pattern.begin(), pattern.end(), str.begin(), str.end(), |
1098 |
13 Jun 10 |
peter |
176 |
vec.begin()); |
1098 |
13 Jun 10 |
peter |
177 |
} |
516 |
09 Dec 07 |
peter |
178 |
|
1439 |
20 Dec 11 |
peter |
179 |
bool regexp__(std::string::const_iterator first1, |
1098 |
13 Jun 10 |
peter |
180 |
std::string::const_iterator last1, |
1098 |
13 Jun 10 |
peter |
181 |
std::string::const_iterator first2, |
1098 |
13 Jun 10 |
peter |
182 |
std::string::const_iterator last2, |
1098 |
13 Jun 10 |
peter |
183 |
std::vector<std::string>::iterator item) |
1098 |
13 Jun 10 |
peter |
184 |
{ |
1098 |
13 Jun 10 |
peter |
185 |
if (first1==last1) { |
1098 |
13 Jun 10 |
peter |
186 |
return first2==last2; |
516 |
09 Dec 07 |
peter |
187 |
} |
1098 |
13 Jun 10 |
peter |
188 |
if (*first1 == '*') { |
1098 |
13 Jun 10 |
peter |
189 |
if (first2<last2) { |
1098 |
13 Jun 10 |
peter |
190 |
item->push_back(*first2); |
1098 |
13 Jun 10 |
peter |
191 |
if (regexp__(first1, last1, first2+1, last2, item)) |
1098 |
13 Jun 10 |
peter |
192 |
return true; |
1098 |
13 Jun 10 |
peter |
193 |
item->resize(item->size()-1); |
1098 |
13 Jun 10 |
peter |
194 |
} |
1098 |
13 Jun 10 |
peter |
195 |
return regexp__(first1+1, last1, first2, last2, item+1); |
1098 |
13 Jun 10 |
peter |
196 |
} |
1098 |
13 Jun 10 |
peter |
197 |
if (*first1 == '?') { |
1098 |
13 Jun 10 |
peter |
198 |
if (first2==last2) |
1098 |
13 Jun 10 |
peter |
199 |
return false; |
1098 |
13 Jun 10 |
peter |
200 |
*item = *first2; |
1098 |
13 Jun 10 |
peter |
201 |
return regexp__(first1+1, last1, first2+1, last2, item+1); |
1098 |
13 Jun 10 |
peter |
202 |
} |
1098 |
13 Jun 10 |
peter |
203 |
if (*first1 == '[') { |
1098 |
13 Jun 10 |
peter |
204 |
if (first2==last2) |
1098 |
13 Jun 10 |
peter |
205 |
return false; |
1098 |
13 Jun 10 |
peter |
206 |
while (*first1 != ']') { |
1098 |
13 Jun 10 |
peter |
207 |
if (*first1 == *first2) { |
1098 |
13 Jun 10 |
peter |
208 |
*item = *first2; |
1098 |
13 Jun 10 |
peter |
209 |
} |
1098 |
13 Jun 10 |
peter |
210 |
++first1; |
1098 |
13 Jun 10 |
peter |
211 |
assert(first1!=last1); |
1098 |
13 Jun 10 |
peter |
212 |
} |
1098 |
13 Jun 10 |
peter |
213 |
return regexp__(first1+1, last1, first2+1, last2, item+1); |
1098 |
13 Jun 10 |
peter |
214 |
} |
1098 |
13 Jun 10 |
peter |
215 |
|
1098 |
13 Jun 10 |
peter |
216 |
if (first2==last2) |
1098 |
13 Jun 10 |
peter |
217 |
return false; |
1098 |
13 Jun 10 |
peter |
218 |
if (*first1 != *first2) |
1098 |
13 Jun 10 |
peter |
219 |
return false; |
1098 |
13 Jun 10 |
peter |
220 |
return regexp__(first1+1, last1, first2+1, last2, item); |
516 |
09 Dec 07 |
peter |
221 |
} |
516 |
09 Dec 07 |
peter |
222 |
|
516 |
09 Dec 07 |
peter |
223 |
|
1547 |
20 Oct 12 |
peter |
224 |
std::string relative_path(const std::string& abs_path,const std::string& dir) |
1547 |
20 Oct 12 |
peter |
225 |
{ |
1547 |
20 Oct 12 |
peter |
226 |
if (abs_path == dir) |
1547 |
20 Oct 12 |
peter |
227 |
return "."; |
1547 |
20 Oct 12 |
peter |
228 |
|
1547 |
20 Oct 12 |
peter |
229 |
if (abs_path.substr(0,dir.size())==dir) { |
1547 |
20 Oct 12 |
peter |
230 |
size_t pos = dir.size(); |
1547 |
20 Oct 12 |
peter |
231 |
if (abs_path[pos] == '/') { |
1547 |
20 Oct 12 |
peter |
232 |
++pos; |
1547 |
20 Oct 12 |
peter |
233 |
if (pos==abs_path.size()) |
1547 |
20 Oct 12 |
peter |
234 |
return "."; |
1547 |
20 Oct 12 |
peter |
235 |
} |
1547 |
20 Oct 12 |
peter |
236 |
return abs_path.substr(pos); |
1547 |
20 Oct 12 |
peter |
237 |
} |
1547 |
20 Oct 12 |
peter |
238 |
assert(0 && "this case is not implemented"); |
1547 |
20 Oct 12 |
peter |
239 |
return ""; |
1547 |
20 Oct 12 |
peter |
240 |
} |
1547 |
20 Oct 12 |
peter |
241 |
|
1547 |
20 Oct 12 |
peter |
242 |
|
297 |
08 May 07 |
peter |
243 |
void touch(std::string str) |
297 |
08 May 07 |
peter |
244 |
{ |
297 |
08 May 07 |
peter |
245 |
if (!node_exist(str)) { |
297 |
08 May 07 |
peter |
246 |
std::ofstream os(str.c_str()); |
297 |
08 May 07 |
peter |
247 |
os.close(); |
297 |
08 May 07 |
peter |
248 |
} |
297 |
08 May 07 |
peter |
249 |
} |
297 |
08 May 07 |
peter |
250 |
|
201 |
09 Sep 06 |
peter |
251 |
|
309 |
12 May 07 |
peter |
252 |
std::string match(std::string::const_iterator& first, |
309 |
12 May 07 |
peter |
253 |
const std::string::const_iterator& last, |
309 |
12 May 07 |
peter |
254 |
std::string str) |
309 |
12 May 07 |
peter |
255 |
{ |
309 |
12 May 07 |
peter |
256 |
if (match_begin(first, last, str)){ |
309 |
12 May 07 |
peter |
257 |
first+=str.size(); |
309 |
12 May 07 |
peter |
258 |
return str; |
309 |
12 May 07 |
peter |
259 |
} |
309 |
12 May 07 |
peter |
260 |
return std::string(); |
309 |
12 May 07 |
peter |
261 |
} |
309 |
12 May 07 |
peter |
262 |
|
149 |
12 Aug 06 |
jari |
263 |
}} // end of namespace svndigest and namespace theplu |