source: branches/0.8-stable/lib/utility.h @ 1199

Last change on this file since 1199 was 1098, checked in by Peter Johansson, 11 years ago

closes #450. extend filename matching tp allow [xyz] and ?. Also fixed a bug when having asterisk (*) in end of pattern string

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