source: trunk/lib/utility.h

Last change on this file was 1547, checked in by Peter Johansson, 9 years ago

refs #518. Implement SVNcat using 'svn_ra_get_file' which does not expand svn:keywords.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 1547 2012-10-20 08:31:38Z 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
41namespace theplu{
42namespace 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   */
68  void chmod(const std::string& filename, mode_t mode);
69
70  /**
71     wrapper around GNU C Library function chdir
72
73     \throw if underlying chdir call does not return 0
74   */
75  void chdir(const std::string& dir);
76
77  /**
78     \return dir+base if dir ends with '/', else dir+'/'+base
79   */
80  std::string concatenate_path(std::string dir, std::string base);
81
82  /**
83     @brief Copy file \a source to \a target.
84
85     @throw std::runtime_error when read error of \a source or write
86     error for \a target is encountered.
87  */
88  void copy_file(const std::string& source, const std::string& target);
89
90  /**
91     Parsing out the directory of path that is everything prior the
92     last '/' with the eception if \a path ends with '/' in which case
93     everything prior the second last '/' is returned.
94
95     \return directory of path.
96   */
97  std::string directory_name(const std::string& path);
98
99  ///
100  /// @return everything after last '/'
101  ///
102  std::string file_name(const std::string&);
103
104  /**
105     \return true if \a str matches \a pattern
106
107     \see fnmatch(3)
108   */
109  bool fnmatch(const std::string& pattern, const std::string& str);
110
111  ///
112  /// @brief environment variable @a var
113  ///
114  std::string getenv(const std::string& var);
115
116  ///
117  /// If @a width is set, size is forced to length @a width. This
118  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
119  /// respectively.
120  ///
121  /// @return x in hexadecimal base
122  ///
123  std::string hex(int x, unsigned int width=0);
124
125  ///
126  /// @brief remove trailing whitespaces
127  ///
128  std::string htrim(std::string str);
129
130  /**
131     \return true if path has finite size and first charcter is '/'
132   */
133  bool is_absolute_path(const std::string& path);
134
135  /**
136     same as lstat(2) but throws an errno_error if error is detected
137   */
138  void lstat(const std::string path, struct stat*);
139
140  ///
141  /// @brief remove leading whitespaces
142  ///
143  std::string ltrim(std::string str);
144
145  inline bool match_begin(std::string::const_iterator first,
146                          std::string::const_iterator last,
147                          const std::string& str)
148  { return (std::distance(first, last)>=static_cast<int>(str.size()) &&
149            std::equal(str.begin(), str.end(), first));
150  }
151
152  inline bool match_end(std::string::const_reverse_iterator first,
153                        std::string::const_reverse_iterator last,
154                        const std::string& str)
155  { return (std::distance(first,last)>=static_cast<int>(str.size()) &&
156            std::equal(str.rbegin(), str.rend(), first));
157  }
158
159  ///
160  /// Create directory \a dir. The call can fail in many ways, cf. 'man
161  /// mkdir'.
162  ///
163  void mkdir(const std::string& dir);
164
165  ///
166  /// Create directory \a dir and parents directories if needed. No
167  /// error if \a dir already exists.
168  ///
169  void mkdir_p(const std::string& dir);
170
171  ///
172  /// @brief Check whether \a path already exists or not.
173  ///
174  /// @return True if \a path exists, false otherwise.
175  ///
176  bool node_exist(const std::string& path);
177
178  /**
179     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
180  */
181  int percent(int a, int b);
182
183  ///
184  /// @return the current working directory.
185  ///
186  std::string pwd(void);
187
188  /**
189     \return true if \a str matches \a pattern
190
191     \a pattern may contain wildcards '*', '?' and \a vec will contain
192     the matching string in \a str. If it's not a match, \a vec is
193     undefined. The algorithm is greedy, i.e., wildcards '*' will
194     consume as many charcters as possible.
195
196     \note \a vec is supposed to be empty
197   */
198  bool regexp(const std::string& pattern, const std::string& str,
199              std::vector<std::string>& vec);
200
201  /**
202     \return \a abs_path relative from \a dir, i.e., abs_path =
203     dir/<returned value>
204   */
205  std::string relative_path(const std::string& abs_path,const std::string& dir);
206
207  /**
208     same as C function remove but throws errno_error at failure
209
210     \see man remove
211   */
212  void remove(const std::string& fn);
213
214  /**
215     same as rename(2) but throw errno if error is encountered
216
217     \see man rename
218   */
219  void rename(const std::string& from, const std::string to);
220
221  /**
222     In \a full_str  replace every sub-string \a old_str with
223     \a new_str;
224   */
225  void replace(std::string& full_str, std::string old_str, std::string new_str);
226
227  ///
228  /// Search finds a subsecuence in [first, last) being identical to @ str
229  ///
230  /// @return iterator pointing to first character in found subsequence
231  ///
232  /// @see std::search
233  ///
234  inline std::string::iterator search(std::string::iterator first,
235                                      std::string::iterator last,
236                                      std::string str)
237  { return std::search(first, last, str.begin(), str.end()); }
238
239  /**
240     same as sum(3) but using binary(result, *first) rather than
241     result += *first
242   */
243  template<typename InputIterator, typename T, typename BinaryOperation>
244  void sum(InputIterator first, InputIterator last, T& result,
245           BinaryOperation binary)
246  {
247    for (; first!=last; ++first)
248      binary(result, *first);
249  }
250
251  /**
252     Add all values of [first, last) to result
253   */
254  template<typename InputIterator, typename T>
255  void sum(InputIterator first, InputIterator last, T& result)
256  {
257    typedef typename std::iterator_traits<InputIterator>::const_reference ref;
258    typedef PlusAssign<T&, ref> binary;
259    return sum(first, last, result, binary());
260  }
261
262  ///
263  /// If file does not exist create empty file.
264  ///
265  void touch(std::string);
266
267  ///
268  /// remove leading and trailing whitespaces
269  ///
270  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
271
272
273  template <typename T>
274  std::string match(std::string::const_iterator& first,
275                    const std::string::const_iterator& last,
276                    const T& func)
277  {
278    std::string res;
279    for (;first!=last && func(first); ++first)
280      res.append(1,*first);
281    return res;
282  }
283
284
285  std::string match(std::string::const_iterator& first,
286                    const std::string::const_iterator& last,
287                    std::string);
288
289}} // end of namespace svndigest end of namespace theplu
290#endif
Note: See TracBrowser for help on using the repository browser.