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

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

Refs #251.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.5 KB
RevLine 
[165]1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
[84]4// $Id: utility.h 456 2007-08-21 07:56:06Z jari $
[4]5
[84]6/*
7  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
[234]8  Copyright (C) 2007 Peter Johansson
[84]9
[430]10  This file is part of svndigest, http://trac.thep.lu.se/trac/svndigest
[84]11
[149]12  svndigest is free software; you can redistribute it and/or modify it
[84]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
[149]17  svndigest is distributed in the hope that it will be useful, but
[84]18  WITHOUT ANY WARRANTY; without even the implied warranty of
[126]19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[84]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
[4]28#include <algorithm>
29#include <functional>
[129]30#include <iosfwd>
[23]31#include <string>
[4]32#include <utility>
33#include <vector>
34
[126]35#include <sys/stat.h>
36
[4]37namespace theplu{
[149]38namespace svndigest{
[4]39
[23]40  ///
[151]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
[456]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
[151]64  ///
[100]65  /// @return everything after last '/'
66  ///
67  std::string file_name(const std::string&);
68
[198]69  ///
70  /// @brief environment variable @a var
71  ///
72  std::string getenv(const std::string& var); 
73
[226]74  ///
[373]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, u_int width=0);
82
83  ///
[226]84  /// @brief remove trailing whitespaces
85  ///
86  std::string htrim(std::string str);
87
88  ///
[274]89  /// @return true if \a str is an integer
90  ///
91  bool is_int(std::string str);
92
93  ///
[226]94  /// @brief remove leading whitespaces
95  ///
96  std::string ltrim(std::string str);
97
[214]98  inline bool match_begin(std::string::const_iterator first, 
99                          std::string::const_iterator last, 
[118]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
[217]105  inline bool match_end(std::string::const_reverse_iterator first, 
106                        std::string::const_reverse_iterator last, 
[118]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
[152]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  ///
[226]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
[313]125  /**
126     @return 0 if \a b = 0 otherwise \f$ \frac{100*a}{b} \f$
127  */
128  int percent(int a, int b);
129
[226]130  ///
[152]131  /// @return the current working directory.
132  ///
133  std::string pwd(void);
134
[227]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)
[118]145  { return std::search(first, last, str.begin(), str.end()); }
146
[60]147  ///
[201]148  ///
149  ///
150  time_t str2time(const std::string&);
151
152  ///
[297]153  /// If file does not exist create empty file.
154  ///
155  void touch(std::string);
156
157  ///
[226]158  /// remove leading and trailing whitespaces
159  ///
160  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
161
[289]162
[371]163  template <typename T>
[289]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;
[294]169    for (;first!=last && func(first); ++first) 
[289]170      res.append(1,*first);
171    return res;
172  }
173
[309]174
175  std::string match(std::string::const_iterator& first,
176                    const std::string::const_iterator& last,
177                    std::string);
178
[308]179  struct AlNum
180  {
181    inline bool operator()(std::string::const_iterator i) const 
182    { return isalnum(*i); }
183  };
184
185
[289]186  struct Digit
187  {
[294]188    inline bool operator()(std::string::const_iterator i) const 
189    { return isdigit(*i); }
[289]190  };
191
[308]192
[289]193  class notChar
194  {
195  public:
196    notChar(char);
[294]197    inline bool operator()(std::string::const_iterator i) const 
198    { return *i!=char_; }
[289]199  private:
200    char char_;
201  };
202
[371]203
[289]204  class not2Char
205  {
206  public:
207    not2Char(char, char);
[294]208    inline bool operator()(std::string::const_iterator i) const 
209    { return *i!=char1_ && *i!=char2_; }
[289]210  private:
211    const char char1_;
212    const char char2_;
213  };
214
[294]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
[226]230  ///
[371]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  ///
[234]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  ///
[371]256  template <typename T1, typename T2>
[234]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
[371]270 
271  ///
272  /// Functor working on pair.second, using a user passed functor.
273  ///
274  template <typename T1, typename T2, typename T3>
[303]275  struct PairSecondCompare
276  {
277
278    ///
279    /// @brief Constructor
280    ///
281    explicit PairSecondCompare(const T3& comp)
282      : compare_(comp) {}
283
284    ///
[371]285    /// @return compare(x.second, y.second) where compare is a
286    /// internal comparison functor.
[303]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  };
[371]296 
[234]297  ///
[23]298  /// Calculating sum of two vectors.
299  ///
300  /// @return resulting vector
301  ///
[71]302  template <typename T >
[23]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        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
312        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
313        return res;
314      }
[14]315 
[23]316      std::vector<T> res(v.size());
317      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
318      if ( v.size() > u.size() )
319        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
320      return res;
321    }
[4]322
[23]323  };
[14]324
[126]325  ///
326  /// @return resulting vector
327  ///
328  template <typename Key, typename T>
[23]329  struct PairValuePlus :
330    public std::binary_function<std::vector<T>,
331                                std::pair<const Key, std::vector<T> >, 
332                                std::vector<T> >
333  {
334    std::vector<T> operator()(const std::vector<T>& sum, 
335                              const std::pair<const Key,std::vector<T> >& p)
336    {
337      return VectorPlus<T>()(sum, p.second);
338    }
339  };
340
[149]341}} // end of namespace svndigest end of namespace theplu
[4]342
[23]343#endif
Note: See TracBrowser for help on using the repository browser.