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

Last change on this file since 430 was 430, checked in by Peter Johansson, 16 years ago

changing lev.thep.lu.se to trac.thep.lu.se

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