source: trunk/lib/utility.h @ 177

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

moved html related utility to its own 'html_utility'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 177 2006-09-02 04:30:57Z 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  inline bool match_begin(std::string::iterator first, 
61                          std::string::iterator last, 
62                          const std::string& str)
63  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
64            std::equal(str.begin(), str.end(), first)); 
65  }
66
67  inline bool match_end(std::string::iterator first, 
68                        std::string::iterator last, 
69                        const std::string& str)
70  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
71            std::equal(str.rbegin(), str.rend(), first)); 
72  }
73
74  ///
75  /// Create directory \a dir. The call can fail in many ways, cf. 'man
76  /// mkdir'.
77  ///
78  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
79
80  ///
81  /// @return the current working directory.
82  ///
83  std::string pwd(void);
84
85  inline std::string::iterator search(std::string::iterator& first, 
86                                      std::string::iterator& last, 
87                                      const std::string& str)
88  { return std::search(first, last, str.begin(), str.end()); }
89
90  ///
91  /// Calculating sum of two vectors.
92  ///
93  /// @return resulting vector
94  ///
95  template <typename T >
96  struct VectorPlus : 
97    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
98  {
99    std::vector<T> operator()(const std::vector<T>& u,
100                              const std::vector<T>& v) const 
101    {
102      if ( u.size() > v.size() ){
103        std::vector<T> res(u.size());
104        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
105        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
106        return res;
107      }
108 
109      std::vector<T> res(v.size());
110      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
111      if ( v.size() > u.size() )
112        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
113      return res;
114    }
115
116  };
117
118  ///
119  /// @return resulting vector
120  ///
121  template <typename Key, typename T>
122  struct PairValuePlus :
123    public std::binary_function<std::vector<T>,
124                                std::pair<const Key, std::vector<T> >, 
125                                std::vector<T> >
126  {
127    std::vector<T> operator()(const std::vector<T>& sum, 
128                              const std::pair<const Key,std::vector<T> >& p)
129    {
130      return VectorPlus<T>()(sum, p.second);
131    }
132  };
133
134}} // end of namespace svndigest end of namespace theplu
135
136#endif
Note: See TracBrowser for help on using the repository browser.