1 | #ifndef _theplu_svndigest_utility_ |
---|
2 | #define _theplu_svndigest_utility_ |
---|
3 | |
---|
4 | // $Id: utility.h 1675 2023-08-26 17:13:59Z 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, 2011, 2012 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 "Functor.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <functional> |
---|
31 | #include <iosfwd> |
---|
32 | #include <limits> |
---|
33 | #include <sstream> |
---|
34 | #include <stdexcept> |
---|
35 | #include <string> |
---|
36 | #include <utility> |
---|
37 | #include <vector> |
---|
38 | |
---|
39 | #include <sys/stat.h> |
---|
40 | |
---|
41 | namespace theplu{ |
---|
42 | namespace svndigest{ |
---|
43 | |
---|
44 | /** |
---|
45 | if path is absolute return path otherwise return |
---|
46 | concatenate_path(pwd(), path) |
---|
47 | */ |
---|
48 | std::string absolute_path(const std::string& path); |
---|
49 | |
---|
50 | /// |
---|
51 | /// @brief Check if access permissions match \a mode. \a mode must |
---|
52 | /// be given as r, w, x, or combinations of these letters. |
---|
53 | /// |
---|
54 | /// @return On success (all requested permissions granted), zero |
---|
55 | /// is returned. On error (at least one bit in mode asked for a |
---|
56 | /// permission that is denied, or some other error occurred), -1 |
---|
57 | /// is returned, and errno is set appropriately. |
---|
58 | /// |
---|
59 | /// @throw An std::runtime_error is thrown when checking for write |
---|
60 | /// permissions for a file/direcotry that does not exist. |
---|
61 | /// |
---|
62 | /// @see access(2) |
---|
63 | /// |
---|
64 | int access_rights(const std::string& path,const std::string& bits); |
---|
65 | |
---|
66 | /** |
---|
67 | \return dir+base if dir ends with '/', else dir+'/'+base |
---|
68 | */ |
---|
69 | std::string concatenate_path(std::string dir, std::string base); |
---|
70 | |
---|
71 | /// |
---|
72 | /// @brief environment variable @a var |
---|
73 | /// |
---|
74 | std::string getenv(const std::string& var); |
---|
75 | |
---|
76 | /// |
---|
77 | /// If @a width is set, size is forced to length @a width. This |
---|
78 | /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1", |
---|
79 | /// respectively. |
---|
80 | /// |
---|
81 | /// @return x in hexadecimal base |
---|
82 | /// |
---|
83 | std::string hex(int x, unsigned int width=0); |
---|
84 | |
---|
85 | /// |
---|
86 | /// @brief remove trailing whitespaces |
---|
87 | /// |
---|
88 | std::string htrim(std::string str); |
---|
89 | |
---|
90 | /** |
---|
91 | \return true if path has finite size and first charcter is '/' |
---|
92 | */ |
---|
93 | bool is_absolute_path(const std::string& path); |
---|
94 | |
---|
95 | /** |
---|
96 | same as lstat(2) but throws an errno_error if error is detected |
---|
97 | */ |
---|
98 | void lstat(const std::string path, struct stat*); |
---|
99 | |
---|
100 | /// |
---|
101 | /// @brief remove leading whitespaces |
---|
102 | /// |
---|
103 | std::string ltrim(std::string str); |
---|
104 | |
---|
105 | inline bool match_begin(std::string::const_iterator first, |
---|
106 | std::string::const_iterator last, |
---|
107 | const std::string& str) |
---|
108 | { return (std::distance(first, last)>=static_cast<int>(str.size()) && |
---|
109 | std::equal(str.begin(), str.end(), first)); |
---|
110 | } |
---|
111 | |
---|
112 | inline bool match_end(std::string::const_reverse_iterator first, |
---|
113 | std::string::const_reverse_iterator last, |
---|
114 | const std::string& str) |
---|
115 | { return (std::distance(first,last)>=static_cast<int>(str.size()) && |
---|
116 | std::equal(str.rbegin(), str.rend(), first)); |
---|
117 | } |
---|
118 | |
---|
119 | /// |
---|
120 | /// @brief Check whether \a path already exists or not. |
---|
121 | /// |
---|
122 | /// @return True if \a path exists, false otherwise. |
---|
123 | /// |
---|
124 | bool node_exist(const std::string& path); |
---|
125 | |
---|
126 | /** |
---|
127 | @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$ |
---|
128 | */ |
---|
129 | int percent(int a, int b); |
---|
130 | |
---|
131 | /// |
---|
132 | /// @return the current working directory. |
---|
133 | /// |
---|
134 | std::string pwd(void); |
---|
135 | |
---|
136 | /** |
---|
137 | \return true if \a str matches \a pattern |
---|
138 | |
---|
139 | \a pattern may contain wildcards '*', '?' and \a vec will contain |
---|
140 | the matching string in \a str. If it's not a match, \a vec is |
---|
141 | undefined. The algorithm is greedy, i.e., wildcards '*' will |
---|
142 | consume as many charcters as possible. |
---|
143 | |
---|
144 | \note \a vec is supposed to be empty |
---|
145 | */ |
---|
146 | bool regexp(const std::string& pattern, const std::string& str, |
---|
147 | std::vector<std::string>& vec); |
---|
148 | |
---|
149 | /** |
---|
150 | \return \a abs_path relative from \a dir, i.e., abs_path = |
---|
151 | dir/<returned value> |
---|
152 | */ |
---|
153 | std::string relative_path(const std::string& abs_path,const std::string& dir); |
---|
154 | |
---|
155 | /// |
---|
156 | /// Search finds a subsecuence in [first, last) being identical to @ str |
---|
157 | /// |
---|
158 | /// @return iterator pointing to first character in found subsequence |
---|
159 | /// |
---|
160 | /// @see std::search |
---|
161 | /// |
---|
162 | inline std::string::iterator search(std::string::iterator first, |
---|
163 | std::string::iterator last, |
---|
164 | std::string str) |
---|
165 | { return std::search(first, last, str.begin(), str.end()); } |
---|
166 | |
---|
167 | /** |
---|
168 | same as sum(3) but using binary(result, *first) rather than |
---|
169 | result += *first |
---|
170 | */ |
---|
171 | template<typename InputIterator, typename T, typename BinaryOperation> |
---|
172 | void sum(InputIterator first, InputIterator last, T& result, |
---|
173 | BinaryOperation binary) |
---|
174 | { |
---|
175 | for (; first!=last; ++first) |
---|
176 | binary(result, *first); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | Add all values of [first, last) to result |
---|
181 | */ |
---|
182 | template<typename InputIterator, typename T> |
---|
183 | void sum(InputIterator first, InputIterator last, T& result) |
---|
184 | { |
---|
185 | typedef typename std::iterator_traits<InputIterator>::const_reference ref; |
---|
186 | typedef PlusAssign<T&, ref> binary; |
---|
187 | return sum(first, last, result, binary()); |
---|
188 | } |
---|
189 | |
---|
190 | /// |
---|
191 | /// If file does not exist create empty file. |
---|
192 | /// |
---|
193 | void touch(std::string); |
---|
194 | |
---|
195 | /// |
---|
196 | /// remove leading and trailing whitespaces |
---|
197 | /// |
---|
198 | inline std::string trim(std::string str) { return htrim(ltrim(str)); } |
---|
199 | |
---|
200 | |
---|
201 | template <typename T> |
---|
202 | std::string match(std::string::const_iterator& first, |
---|
203 | const std::string::const_iterator& last, |
---|
204 | const T& func) |
---|
205 | { |
---|
206 | std::string res; |
---|
207 | for (;first!=last && func(first); ++first) |
---|
208 | res.append(1,*first); |
---|
209 | return res; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | std::string match(std::string::const_iterator& first, |
---|
214 | const std::string::const_iterator& last, |
---|
215 | std::string); |
---|
216 | |
---|
217 | }} // end of namespace svndigest end of namespace theplu |
---|
218 | #endif |
---|