source: trunk/lib/utility.h @ 815

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

added a function mkdir_p(1)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 796 2009-07-01 00:55:02Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2009 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     \return dir+base if dir ends with '/', else dir+'/'+base
60   */
61  std::string concatenate_path(std::string dir, std::string base);
62
63  /**
64     @brief Copy file \a source to \a target.
65
66     @throw std::runtime_error when read error of \a source or write
67     error for \a target is encountered.
68  */
69  void copy_file(const std::string& source, const std::string& target);
70
71  /**
72     Parsing out the directory of path that is everything prior the
73     last '/' with the eception if \a path ends with '/' in which case
74     everything prior the second last '/' is returned.
75
76     \return directory of path.
77   */
78  std::string directory_name(std::string path);
79
80  /**
81     Function to compare strings. Strings may contain '*'.
82
83     \return true if \a a and \a b are identical.
84   */
85  bool equal(std::string::const_iterator first1, 
86             std::string::const_iterator end1,
87             std::string::const_iterator first2,
88             std::string::const_iterator end2);
89
90  ///
91  /// @return everything after last '/'
92  ///
93  std::string file_name(const std::string&);
94
95  ///
96  /// @brief environment variable @a var
97  ///
98  std::string getenv(const std::string& var); 
99
100  ///
101  /// If @a width is set, size is forced to length @a width. This
102  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
103  /// respectively.
104  ///
105  /// @return x in hexadecimal base
106  ///
107  std::string hex(int x, unsigned int width=0);
108
109  ///
110  /// @brief remove trailing whitespaces
111  ///
112  std::string htrim(std::string str);
113
114  ///
115  /// @brief remove leading whitespaces
116  ///
117  std::string ltrim(std::string str);
118
119  inline bool match_begin(std::string::const_iterator first, 
120                          std::string::const_iterator last, 
121                          const std::string& str)
122  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
123            std::equal(str.begin(), str.end(), first)); 
124  }
125
126  inline bool match_end(std::string::const_reverse_iterator first, 
127                        std::string::const_reverse_iterator last, 
128                        const std::string& str)
129  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
130            std::equal(str.rbegin(), str.rend(), first)); 
131  }
132
133  ///
134  /// Create directory \a dir. The call can fail in many ways, cf. 'man
135  /// mkdir'.
136  ///
137  void mkdir(const std::string& dir);
138
139  ///
140  /// Create directory \a dir and parents directories if needed. No
141  /// error if \a dir already exists.
142  ///
143  void mkdir_p(const std::string& dir);
144
145  ///
146  /// @brief Check whether \a path already exists or not.
147  ///
148  /// @return True if \a path exists, false otherwise.
149  ///
150  bool node_exist(const std::string& path);
151
152  /**
153     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
154  */
155  int percent(int a, int b);
156
157  ///
158  /// @return the current working directory.
159  ///
160  std::string pwd(void);
161
162  /**
163     \return true if first range match second range
164
165     Second range may contain wildcards (such as '*' and '?'), in
166     which case vector \a vec is filled with the corresponding strings
167     in first range. The algorithm is greedy, i.e., wildcard * is will
168     consume as many charcters as possible.
169
170     \note \a vec is supposed to be empty
171   */
172  bool regexp(std::string::const_iterator first1, 
173              std::string::const_iterator end1,
174              std::string::const_iterator first2,
175              std::string::const_iterator end2,
176              std::vector<std::string>& vec);
177
178  /**
179     In \a full_str  replace every sub-string \a old_str with
180     \a new_str;
181   */
182  void replace(std::string& full_str, std::string old_str, std::string new_str);
183
184  ///
185  /// Search finds a subsecuence in [first, last) being identical to @ str
186  ///
187  /// @return iterator pointing to first character in found subsequence
188  ///
189  /// @see std::search
190  ///
191  inline std::string::iterator search(std::string::iterator first, 
192                                      std::string::iterator last, 
193                                      std::string str)
194  { return std::search(first, last, str.begin(), str.end()); }
195
196  ///
197  ///
198  ///
199  time_t str2time(const std::string&);
200
201  ///
202  /// If file does not exist create empty file.
203  ///
204  void touch(std::string);
205
206  ///
207  /// remove leading and trailing whitespaces
208  ///
209  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
210
211
212  template <typename T>
213  std::string match(std::string::const_iterator& first,
214                    const std::string::const_iterator& last,
215                    const T& func)
216  {
217    std::string res;
218    for (;first!=last && func(first); ++first) 
219      res.append(1,*first);
220    return res;
221  }
222
223
224  std::string match(std::string::const_iterator& first,
225                    const std::string::const_iterator& last,
226                    std::string);
227
228}} // end of namespace svndigest end of namespace theplu
229
230#endif
Note: See TracBrowser for help on using the repository browser.