source: trunk/lib/utility.h @ 308

Last change on this file since 308 was 308, checked in by Peter Johansson, 15 years ago

Refs #189. Added doc to private classes in Trac class. Re-ordered functions. Changed the behaviour: revisions does not need to be an integer can for example be head

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 308 2007-05-12 10:36:23Z 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://lev.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  /// @brief remove trailing whitespaces
68  ///
69  std::string htrim(std::string str);
70
71  ///
72  /// @return true if \a str is an integer
73  ///
74  bool is_int(std::string str);
75
76  ///
77  /// @brief remove leading whitespaces
78  ///
79  std::string ltrim(std::string str);
80
81  inline bool match_begin(std::string::const_iterator first, 
82                          std::string::const_iterator last, 
83                          const std::string& str)
84  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
85            std::equal(str.begin(), str.end(), first)); 
86  }
87
88  inline bool match_end(std::string::const_reverse_iterator first, 
89                        std::string::const_reverse_iterator last, 
90                        const std::string& str)
91  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
92            std::equal(str.rbegin(), str.rend(), first)); 
93  }
94
95  ///
96  /// Create directory \a dir. The call can fail in many ways, cf. 'man
97  /// mkdir'.
98  ///
99  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
100
101  ///
102  /// @brief Check whether \a path already exists or not.
103  ///
104  /// @return True if \a path exists, false otherwise.
105  ///
106  bool node_exist(const std::string& path);
107
108  ///
109  /// @return the current working directory.
110  ///
111  std::string pwd(void);
112
113  ///
114  /// Search finds a subsecuence in [first, last) being identical to @ str
115  ///
116  /// @return iterator pointing to first character in found subsequence
117  ///
118  /// @see std::search
119  ///
120  inline std::string::iterator search(std::string::iterator first, 
121                                      std::string::iterator last, 
122                                      std::string str)
123  { return std::search(first, last, str.begin(), str.end()); }
124
125  ///
126  ///
127  ///
128  time_t str2time(const std::string&);
129
130  ///
131  /// If file does not exist create empty file.
132  ///
133  void touch(std::string);
134
135  ///
136  /// remove leading and trailing whitespaces
137  ///
138  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
139
140
141  template <class T>
142  std::string match(std::string::const_iterator& first,
143                    const std::string::const_iterator& last,
144                    const T& func)
145  {
146    std::string res;
147    for (;first!=last && func(first); ++first) 
148      res.append(1,*first);
149    return res;
150  }
151
152  struct AlNum
153  {
154    inline bool operator()(std::string::const_iterator i) const 
155    { return isalnum(*i); }
156  };
157
158
159  struct Digit
160  {
161    inline bool operator()(std::string::const_iterator i) const 
162    { return isdigit(*i); }
163  };
164
165
166  class notChar
167  {
168  public:
169    notChar(char);
170    inline bool operator()(std::string::const_iterator i) const 
171    { return *i!=char_; }
172  private:
173    char char_;
174  };
175
176  class not2Char
177  {
178  public:
179    not2Char(char, char);
180    inline bool operator()(std::string::const_iterator i) const 
181    { return *i!=char1_ && *i!=char2_; }
182  private:
183    const char char1_;
184    const char char2_;
185  };
186
187  class Str
188  {
189  public:
190    Str(std::string);
191    inline bool operator()(std::string::const_iterator i) const 
192    { return (std::equal(str_.begin(), str_.end(), i)); } 
193
194  private:
195    const std::string str_;
196  };
197
198  class not2Str
199  {
200  public:
201    not2Str(std::string, std::string);
202    inline bool operator()(std::string::const_iterator i) const 
203    { 
204      return !(std::equal(str1_.begin(), str1_.end(), i) ||
205              std::equal(str2_.begin(), str2_.end(), i));
206    } 
207
208  private:
209    const std::string str1_;
210    const std::string str2_;
211  };
212
213  ///
214  /// @brief Functor comparing pairs using second.
215  ///
216  /// STL provides operator< for the pair.first element, but none for
217  /// pair.second. This template provides this and can be used as the
218  /// comparison object in generic functions such as the STL sort.
219  ///
220  template <class T1,class T2>
221  struct pair_value_compare
222  {
223    ///
224    /// @return true if x.second<y.second or (x.second==y.second and
225    /// x.first<y.first)
226    ///
227    inline bool operator()(const std::pair<T1,T2>& x,
228                           const std::pair<T1,T2>& y) {
229      return ((x.second<y.second) ||
230              (!(y.second<x.second) && (x.first<y.first))); 
231    }
232  };
233
234  template <class T1,class T2, class T3>
235  struct PairSecondCompare
236  {
237
238    ///
239    /// @brief Constructor
240    ///
241    explicit PairSecondCompare(const T3& comp)
242      : compare_(comp) {}
243
244    ///
245    /// @return true if x.second<y.second or (x.second==y.second and
246    /// x.first<y.first)
247    ///
248    inline bool operator()(const std::pair<T1,T2>& x,
249                           const std::pair<T1,T2>& y) const
250    { return compare_(x.second,y.second); }
251
252  private:
253    T3 compare_;
254
255  };
256
257  ///
258  /// Calculating sum of two vectors.
259  ///
260  /// @return resulting vector
261  ///
262  template <typename T >
263  struct VectorPlus : 
264    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
265  {
266    std::vector<T> operator()(const std::vector<T>& u,
267                              const std::vector<T>& v) const 
268    {
269      if ( u.size() > v.size() ){
270        std::vector<T> res(u.size());
271        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
272        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
273        return res;
274      }
275 
276      std::vector<T> res(v.size());
277      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
278      if ( v.size() > u.size() )
279        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
280      return res;
281    }
282
283  };
284
285  ///
286  /// @return resulting vector
287  ///
288  template <typename Key, typename T>
289  struct PairValuePlus :
290    public std::binary_function<std::vector<T>,
291                                std::pair<const Key, std::vector<T> >, 
292                                std::vector<T> >
293  {
294    std::vector<T> operator()(const std::vector<T>& sum, 
295                              const std::pair<const Key,std::vector<T> >& p)
296    {
297      return VectorPlus<T>()(sum, p.second);
298    }
299  };
300
301}} // end of namespace svndigest end of namespace theplu
302
303#endif
Note: See TracBrowser for help on using the repository browser.