source: branches/0.6-stable/lib/utility.h @ 731

Last change on this file since 731 was 731, checked in by Peter Johansson, 13 years ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 731 2008-12-15 19:03:04Z peter $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
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 2 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 this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include <algorithm>
29#include <functional>
30#include <iosfwd>
31#include <string>
32#include <utility>
33#include <vector>
34
35#include <sys/stat.h>
36
37namespace theplu{
38namespace svndigest{
39
40  ///
41  /// @brief Check if access permissions match \a mode. \a mode must
42  /// be given as r, w, x, or combinations of these letters.
43  ///
44  /// @return On success (all requested permissions granted), zero
45  /// is returned. On error (at least one bit in mode asked for a
46  /// permission that is denied, or some other error occurred), -1
47  /// is returned, and errno is set appropriately.
48  ///
49  /// @throw An std::runtime_error is thrown when checking for write
50  /// permissions for a file/direcotry that does not exist.
51  ///
52  /// @see access(2)
53  ///
54  int access_rights(const std::string& path,const std::string& bits);
55
56  /**
57     @brief Copy file \a source to \a target.
58
59     @throw std::runtime_error when read error of \a source or write
60     error for \a target is encountered.
61  */
62  void copy_file(const std::string& source, const std::string& target);
63
64  ///
65  /// @return everything after last '/'
66  ///
67  std::string file_name(const std::string&);
68
69  ///
70  /// @brief environment variable @a var
71  ///
72  std::string getenv(const std::string& var); 
73
74  ///
75  /// If @a width is set, size is forced to length @a width. This
76  /// implies, e.g., that hex(15,2) and hex(17,1) return "0F" and "1",
77  /// respectively.
78  ///
79  /// @return x in hexadecimal base
80  ///
81  std::string hex(int x, unsigned int width=0);
82
83  ///
84  /// @brief remove trailing whitespaces
85  ///
86  std::string htrim(std::string str);
87
88  ///
89  /// @return true if \a str is an integer
90  ///
91  bool is_int(std::string str);
92
93  ///
94  /// @brief remove leading whitespaces
95  ///
96  std::string ltrim(std::string str);
97
98  inline bool match_begin(std::string::const_iterator first, 
99                          std::string::const_iterator last, 
100                          const std::string& str)
101  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
102            std::equal(str.begin(), str.end(), first)); 
103  }
104
105  inline bool match_end(std::string::const_reverse_iterator first, 
106                        std::string::const_reverse_iterator last, 
107                        const std::string& str)
108  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
109            std::equal(str.rbegin(), str.rend(), first)); 
110  }
111
112  ///
113  /// Create directory \a dir. The call can fail in many ways, cf. 'man
114  /// mkdir'.
115  ///
116  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
117
118  ///
119  /// @brief Check whether \a path already exists or not.
120  ///
121  /// @return True if \a path exists, false otherwise.
122  ///
123  bool node_exist(const std::string& path);
124
125  /**
126     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
127  */
128  int percent(int a, int b);
129
130  ///
131  /// @return the current working directory.
132  ///
133  std::string pwd(void);
134
135  ///
136  /// Search finds a subsecuence in [first, last) being identical to @ str
137  ///
138  /// @return iterator pointing to first character in found subsequence
139  ///
140  /// @see std::search
141  ///
142  inline std::string::iterator search(std::string::iterator first, 
143                                      std::string::iterator last, 
144                                      std::string str)
145  { return std::search(first, last, str.begin(), str.end()); }
146
147  ///
148  ///
149  ///
150  time_t str2time(const std::string&);
151
152  ///
153  /// If file does not exist create empty file.
154  ///
155  void touch(std::string);
156
157  ///
158  /// remove leading and trailing whitespaces
159  ///
160  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
161
162
163  template <typename T>
164  std::string match(std::string::const_iterator& first,
165                    const std::string::const_iterator& last,
166                    const T& func)
167  {
168    std::string res;
169    for (;first!=last && func(first); ++first) 
170      res.append(1,*first);
171    return res;
172  }
173
174
175  std::string match(std::string::const_iterator& first,
176                    const std::string::const_iterator& last,
177                    std::string);
178
179  struct AlNum
180  {
181    inline bool operator()(std::string::const_iterator i) const 
182    { return isalnum(*i); }
183  };
184
185
186  struct Digit
187  {
188    inline bool operator()(std::string::const_iterator i) const 
189    { return isdigit(*i); }
190  };
191
192
193  class notChar
194  {
195  public:
196    notChar(char);
197    inline bool operator()(std::string::const_iterator i) const 
198    { return *i!=char_; }
199  private:
200    char char_;
201  };
202
203
204  class not2Char
205  {
206  public:
207    not2Char(char, char);
208    inline bool operator()(std::string::const_iterator i) const 
209    { return *i!=char1_ && *i!=char2_; }
210  private:
211    const char char1_;
212    const char char2_;
213  };
214
215  class not2Str
216  {
217  public:
218    not2Str(std::string, std::string);
219    inline bool operator()(std::string::const_iterator i) const 
220    { 
221      return !(std::equal(str1_.begin(), str1_.end(), i) ||
222              std::equal(str2_.begin(), str2_.end(), i));
223    } 
224
225  private:
226    const std::string str1_;
227    const std::string str2_;
228  };
229
230  ///
231  /// Functor to be used on contaioners and works as standard less,
232  /// but on the reversed container.
233  ///
234  /// Requirements on T is that has rend and rbegin. T::value_type
235  /// must be comparable (i.e. have operator<)
236  ///
237  template <typename T>
238  struct LessReversed
239  {
240    ///
241    /// using std::lexicographical_compare on the reversed container
242    ///
243    inline bool operator()(const T& x, const T& y) const
244    { return std::lexicographical_compare(x.rbegin(),x.rend(),
245                                          y.rbegin(),y.rend()); }
246  };
247
248
249  ///
250  /// @brief Functor comparing pairs using second.
251  ///
252  /// STL provides operator< for the pair.first element, but none for
253  /// pair.second. This template provides this and can be used as the
254  /// comparison object in generic functions such as the STL sort.
255  ///
256  template <typename T1, typename T2>
257  struct pair_value_compare
258  {
259    ///
260    /// @return true if x.second<y.second or (x.second==y.second and
261    /// x.first<y.first)
262    ///
263    inline bool operator()(const std::pair<T1,T2>& x,
264                           const std::pair<T1,T2>& y) {
265      return ((x.second<y.second) ||
266              (!(y.second<x.second) && (x.first<y.first))); 
267    }
268  };
269
270 
271  ///
272  /// Functor working on pair.second, using a user passed functor.
273  ///
274  template <typename T1, typename T2, typename T3>
275  struct PairSecondCompare
276  {
277
278    ///
279    /// @brief Constructor
280    ///
281    explicit PairSecondCompare(const T3& comp)
282      : compare_(comp) {}
283
284    ///
285    /// @return compare(x.second, y.second) where compare is a
286    /// internal comparison functor.
287    ///
288    inline bool operator()(const std::pair<T1,T2>& x,
289                           const std::pair<T1,T2>& y) const
290    { return compare_(x.second,y.second); }
291
292  private:
293    T3 compare_;
294
295  };
296 
297  ///
298  /// Calculating sum of two vectors.
299  ///
300  /// @return resulting vector
301  ///
302  template <typename T >
303  struct VectorPlus : 
304    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
305  {
306    std::vector<T> operator()(const std::vector<T>& u,
307                              const std::vector<T>& v) const 
308    {
309      if ( u.size() > v.size() ){
310        std::vector<T> res(u.size());
311        std::transform(v.begin(), v.end(), u.begin(), res.begin(),
312                       std::plus<T>());
313        std::copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
314        return res;
315      }
316      std::vector<T> res(v.size());
317      std::transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
318      std::copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
319      return res;
320    }
321  };
322
323  ///
324  /// @return resulting vector
325  ///
326  template <typename Key, typename T>
327  struct PairValuePlus :
328    public std::binary_function<std::vector<T>,
329                                std::pair<const Key, std::vector<T> >, 
330                                std::vector<T> >
331  {
332    std::vector<T> operator()(const std::vector<T>& sum, 
333                              const std::pair<const Key,std::vector<T> >& p)
334    {
335      return VectorPlus<T>()(sum, p.second);
336    }
337  };
338
339}} // end of namespace svndigest end of namespace theplu
340
341#endif
Note: See TracBrowser for help on using the repository browser.