source: trunk/lib/utility.h @ 201

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

fixes #76 and refs #87 new output file structure and draft to new main page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 201 2006-09-09 17:36:34Z 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  ///
97  ///
98  time_t str2time(const std::string&);
99
100  ///
101  /// Calculating sum of two vectors.
102  ///
103  /// @return resulting vector
104  ///
105  template <typename T >
106  struct VectorPlus : 
107    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
108  {
109    std::vector<T> operator()(const std::vector<T>& u,
110                              const std::vector<T>& v) const 
111    {
112      if ( u.size() > v.size() ){
113        std::vector<T> res(u.size());
114        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
115        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
116        return res;
117      }
118 
119      std::vector<T> res(v.size());
120      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
121      if ( v.size() > u.size() )
122        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
123      return res;
124    }
125
126  };
127
128  ///
129  /// @return resulting vector
130  ///
131  template <typename Key, typename T>
132  struct PairValuePlus :
133    public std::binary_function<std::vector<T>,
134                                std::pair<const Key, std::vector<T> >, 
135                                std::vector<T> >
136  {
137    std::vector<T> operator()(const std::vector<T>& sum, 
138                              const std::pair<const Key,std::vector<T> >& p)
139    {
140      return VectorPlus<T>()(sum, p.second);
141    }
142  };
143
144}} // end of namespace svndigest end of namespace theplu
145
146#endif
Note: See TracBrowser for help on using the repository browser.