source: trunk/lib/utility.h @ 1203

Last change on this file since 1203 was 1203, checked in by Peter Johansson, 12 years ago

refs #475. prefer operator+= and remove operator+ for Vector class to ensure we use +=

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