source: trunk/lib/utility.h @ 1186

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

using errno_error. closes #410

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 1186 2010-08-27 22:13:37Z 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     same as lstat(2) but throws an errno_error if error is detected
120   */
121  void lstat(const std::string path, struct stat*);
122
123  ///
124  /// @brief remove leading whitespaces
125  ///
126  std::string ltrim(std::string str);
127
128  inline bool match_begin(std::string::const_iterator first, 
129                          std::string::const_iterator last, 
130                          const std::string& str)
131  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
132            std::equal(str.begin(), str.end(), first)); 
133  }
134
135  inline bool match_end(std::string::const_reverse_iterator first, 
136                        std::string::const_reverse_iterator last, 
137                        const std::string& str)
138  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
139            std::equal(str.rbegin(), str.rend(), first)); 
140  }
141
142  ///
143  /// Create directory \a dir. The call can fail in many ways, cf. 'man
144  /// mkdir'.
145  ///
146  void mkdir(const std::string& dir);
147
148  ///
149  /// Create directory \a dir and parents directories if needed. No
150  /// error if \a dir already exists.
151  ///
152  void mkdir_p(const std::string& dir);
153
154  ///
155  /// @brief Check whether \a path already exists or not.
156  ///
157  /// @return True if \a path exists, false otherwise.
158  ///
159  bool node_exist(const std::string& path);
160
161  /**
162     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
163  */
164  int percent(int a, int b);
165
166  ///
167  /// @return the current working directory.
168  ///
169  std::string pwd(void);
170
171  /**
172     \return true if \a str matches \a pattern
173
174     \a pattern may contain wildcards '*', '?' and \a vec will contain
175     the matching string in \a str. If it's not a match, \a vec is
176     undefined. The algorithm is greedy, i.e., wildcards '*' will
177     consume as many charcters as possible.
178
179     \note \a vec is supposed to be empty
180   */
181  bool regexp(const std::string& pattern, const std::string& str,
182              std::vector<std::string>& vec);
183
184  /**
185     same as rename(2) but throw errno if error is encountered
186
187     \see man rename
188   */
189  void rename(const std::string& from, const std::string to);
190
191  /**
192     In \a full_str  replace every sub-string \a old_str with
193     \a new_str;
194   */
195  void replace(std::string& full_str, std::string old_str, std::string new_str);
196
197  ///
198  /// Search finds a subsecuence in [first, last) being identical to @ str
199  ///
200  /// @return iterator pointing to first character in found subsequence
201  ///
202  /// @see std::search
203  ///
204  inline std::string::iterator search(std::string::iterator first, 
205                                      std::string::iterator last, 
206                                      std::string str)
207  { return std::search(first, last, str.begin(), str.end()); }
208
209  ///
210  ///
211  ///
212  time_t str2time(const std::string&);
213
214  ///
215  /// If file does not exist create empty file.
216  ///
217  void touch(std::string);
218
219  ///
220  /// remove leading and trailing whitespaces
221  ///
222  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
223
224
225  template <typename T>
226  std::string match(std::string::const_iterator& first,
227                    const std::string::const_iterator& last,
228                    const T& func)
229  {
230    std::string res;
231    for (;first!=last && func(first); ++first) 
232      res.append(1,*first);
233    return res;
234  }
235
236
237  std::string match(std::string::const_iterator& first,
238                    const std::string::const_iterator& last,
239                    std::string);
240
241}} // end of namespace svndigest end of namespace theplu
242
243#endif
Note: See TracBrowser for help on using the repository browser.