source: trunk/lib/utility.h @ 1432

Last change on this file since 1432 was 1432, checked in by Peter Johansson, 10 years ago

implement file_name and directory_name. fixes #511

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