source: trunk/lib/utility.h @ 693

Last change on this file since 693 was 693, checked in by Jari Häkkinen, 14 years ago

Fixes #339. Change to GPLv3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 693 2008-09-11 20:42:56Z jari $
5
6/*
7  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Jari Häkkinen
9
10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <algorithm>
27#include <functional>
28#include <iosfwd>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <sys/stat.h>
34
35namespace theplu{
36namespace svndigest{
37
38  ///
39  /// @brief Check if access permissions match \a mode. \a mode must
40  /// be given as r, w, x, or combinations of these letters.
41  ///
42  /// @return On success (all requested permissions granted), zero
43  /// is returned. On error (at least one bit in mode asked for a
44  /// permission that is denied, or some other error occurred), -1
45  /// is returned, and errno is set appropriately.
46  ///
47  /// @throw An std::runtime_error is thrown when checking for write
48  /// permissions for a file/direcotry that does not exist.
49  ///
50  /// @see access(2)
51  ///
52  int access_rights(const std::string& path,const std::string& bits);
53
54  /**
55     @brief Copy file \a source to \a target.
56
57     @throw std::runtime_error when read error of \a source or write
58     error for \a target is encountered.
59  */
60  void copy_file(const std::string& source, const std::string& target);
61
62  /**
63     Parsing out the directory of path that is everything prior the
64     last '/' with the eception if \a path ends with '/' in which case
65     everything prior the second last '/' is returned.
66
67     \return directory of path.
68   */
69  std::string directory_name(std::string path);
70
71  /**
72     Function to compare strings. Strings may contain '*'.
73
74     \return true if \a a and \a b are identical.
75   */
76  bool equal(std::string::const_iterator first1, 
77             std::string::const_iterator end1,
78             std::string::const_iterator first2,
79             std::string::const_iterator end2);
80
81  ///
82  /// @return everything after last '/'
83  ///
84  std::string file_name(const std::string&);
85
86  ///
87  /// @brief environment variable @a var
88  ///
89  std::string getenv(const std::string& var); 
90
91  ///
92  /// If @a width is set, size is forced to length @a width. This
93  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
94  /// respectively.
95  ///
96  /// @return x in hexadecimal base
97  ///
98  std::string hex(int x, unsigned int width=0);
99
100  ///
101  /// @brief remove trailing whitespaces
102  ///
103  std::string htrim(std::string str);
104
105  ///
106  /// @return true if \a str is an integer
107  ///
108  bool is_int(std::string str);
109
110  ///
111  /// @brief remove leading whitespaces
112  ///
113  std::string ltrim(std::string str);
114
115  inline bool match_begin(std::string::const_iterator first, 
116                          std::string::const_iterator last, 
117                          const std::string& str)
118  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
119            std::equal(str.begin(), str.end(), first)); 
120  }
121
122  inline bool match_end(std::string::const_reverse_iterator first, 
123                        std::string::const_reverse_iterator last, 
124                        const std::string& str)
125  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
126            std::equal(str.rbegin(), str.rend(), first)); 
127  }
128
129  ///
130  /// Create directory \a dir. The call can fail in many ways, cf. 'man
131  /// mkdir'.
132  ///
133  void mkdir(const std::string& dir);
134
135  ///
136  /// @brief Check whether \a path already exists or not.
137  ///
138  /// @return True if \a path exists, false otherwise.
139  ///
140  bool node_exist(const std::string& path);
141
142  /**
143     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
144  */
145  int percent(int a, int b);
146
147  ///
148  /// @return the current working directory.
149  ///
150  std::string pwd(void);
151
152  /**
153     \return true if first range match second range
154
155     Second range may contain wildcards (such as '*' and '?'), in
156     which case vector \a vec is filled with the corresponding strings
157     in first range. The algorithm is greedy, i.e., wildcard * is will
158     consume as many charcters as possible.
159
160     \note \a vec is supposed to be empty
161   */
162  bool regexp(std::string::const_iterator first1, 
163              std::string::const_iterator end1,
164              std::string::const_iterator first2,
165              std::string::const_iterator end2,
166              std::vector<std::string>& vec);
167
168  /**
169     In \a full_str  replace every sub-string \a old_str with
170     \a new_str;
171   */
172  void replace(std::string& full_str, std::string old_str, std::string new_str);
173
174  ///
175  /// Search finds a subsecuence in [first, last) being identical to @ str
176  ///
177  /// @return iterator pointing to first character in found subsequence
178  ///
179  /// @see std::search
180  ///
181  inline std::string::iterator search(std::string::iterator first, 
182                                      std::string::iterator last, 
183                                      std::string str)
184  { return std::search(first, last, str.begin(), str.end()); }
185
186  ///
187  ///
188  ///
189  time_t str2time(const std::string&);
190
191  ///
192  /// If file does not exist create empty file.
193  ///
194  void touch(std::string);
195
196  ///
197  /// remove leading and trailing whitespaces
198  ///
199  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
200
201  ///
202  /// \return string with version info
203  ///
204  std::string version_string(void);
205 
206
207  template <typename T>
208  std::string match(std::string::const_iterator& first,
209                    const std::string::const_iterator& last,
210                    const T& func)
211  {
212    std::string res;
213    for (;first!=last && func(first); ++first) 
214      res.append(1,*first);
215    return res;
216  }
217
218
219  std::string match(std::string::const_iterator& first,
220                    const std::string::const_iterator& last,
221                    std::string);
222
223}} // end of namespace svndigest end of namespace theplu
224
225#endif
Note: See TracBrowser for help on using the repository browser.