source: trunk/lib/utility.h @ 198

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

refs #36 optionally a copyright information, based on Stats, can be displayed using --copyright flag. This will flag will be added to usage output when this feature is mature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 198 2006-09-09 10:04:27Z peter $
5
6/*
7  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
8
9  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
10
11  svndigest is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15
16  svndigest is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include <algorithm>
28#include <functional>
29#include <iosfwd>
30#include <string>
31#include <utility>
32#include <vector>
33
34#include <sys/stat.h>
35
36namespace theplu{
37namespace svndigest{
38
39  ///
40  /// @brief Check if access permissions match \a mode. \a mode must
41  /// be given as r, w, x, or combinations of these letters.
42  ///
43  /// @return On success (all requested permissions granted), zero
44  /// is returned. On error (at least one bit in mode asked for a
45  /// permission that is denied, or some other error occurred), -1
46  /// is returned, and errno is set appropriately.
47  ///
48  /// @throw An std::runtime_error is thrown when checking for write
49  /// permissions for a file/direcotry that does not exist.
50  ///
51  /// @see access(2)
52  ///
53  int access_rights(const std::string& path,const std::string& bits);
54
55  ///
56  /// @return everything after last '/'
57  ///
58  std::string file_name(const std::string&);
59
60  ///
61  /// @brief environment variable @a var
62  ///
63  std::string getenv(const std::string& var); 
64
65  inline bool match_begin(std::string::iterator first, 
66                          std::string::iterator last, 
67                          const std::string& str)
68  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
69            std::equal(str.begin(), str.end(), first)); 
70  }
71
72  inline bool match_end(std::string::iterator first, 
73                        std::string::iterator last, 
74                        const std::string& str)
75  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
76            std::equal(str.rbegin(), str.rend(), first)); 
77  }
78
79  ///
80  /// Create directory \a dir. The call can fail in many ways, cf. 'man
81  /// mkdir'.
82  ///
83  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
84
85  ///
86  /// @return the current working directory.
87  ///
88  std::string pwd(void);
89
90  inline std::string::iterator search(std::string::iterator& first, 
91                                      std::string::iterator& last, 
92                                      const std::string& str)
93  { return std::search(first, last, str.begin(), str.end()); }
94
95  ///
96  /// Calculating sum of two vectors.
97  ///
98  /// @return resulting vector
99  ///
100  template <typename T >
101  struct VectorPlus : 
102    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
103  {
104    std::vector<T> operator()(const std::vector<T>& u,
105                              const std::vector<T>& v) const 
106    {
107      if ( u.size() > v.size() ){
108        std::vector<T> res(u.size());
109        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
110        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
111        return res;
112      }
113 
114      std::vector<T> res(v.size());
115      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
116      if ( v.size() > u.size() )
117        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
118      return res;
119    }
120
121  };
122
123  ///
124  /// @return resulting vector
125  ///
126  template <typename Key, typename T>
127  struct PairValuePlus :
128    public std::binary_function<std::vector<T>,
129                                std::pair<const Key, std::vector<T> >, 
130                                std::vector<T> >
131  {
132    std::vector<T> operator()(const std::vector<T>& sum, 
133                              const std::pair<const Key,std::vector<T> >& p)
134    {
135      return VectorPlus<T>()(sum, p.second);
136    }
137  };
138
139}} // end of namespace svndigest end of namespace theplu
140
141#endif
Note: See TracBrowser for help on using the repository browser.