source: trunk/lib/utility.h @ 175

Last change on this file since 175 was 175, checked in by Peter Johansson, 16 years ago

fixes #53

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 175 2006-09-02 03:22:52Z 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 send anchor to stream @a os
41  ///
42  /// @param address to link to
43  /// @param name text visible on page
44  /// @param level '../' is added @a level times before @a href
45  /// @param title title of anchor
46  ///
47  void anchor(std::ostream& os, const std::string& href, 
48              const std::string& name, u_int level=0, 
49              const std::string& title="");
50
51  ///
52  /// @brief Check if access permissions match \a mode. \a mode must
53  /// be given as r, w, x, or combinations of these letters.
54  ///
55  /// @return On success (all requested permissions granted), zero
56  /// is returned. On error (at least one bit in mode asked for a
57  /// permission that is denied, or some other error occurred), -1
58  /// is returned, and errno is set appropriately.
59  ///
60  /// @throw An std::runtime_error is thrown when checking for write
61  /// permissions for a file/direcotry that does not exist.
62  ///
63  /// @see access(2)
64  ///
65  int access_rights(const std::string& path,const std::string& bits);
66
67  ///
68  /// @return everything after last '/'
69  ///
70  std::string file_name(const std::string&);
71
72  inline bool match_begin(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.begin(), str.end(), first)); 
77  }
78
79  inline bool match_end(std::string::iterator first, 
80                        std::string::iterator last, 
81                        const std::string& str)
82  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
83            std::equal(str.rbegin(), str.rend(), first)); 
84  }
85
86  ///
87  /// Create directory \a dir. The call can fail in many ways, cf. 'man
88  /// mkdir'.
89  ///
90  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
91
92  ///
93  /// @printing cascading style sheet to stream @a s.
94  ///
95  void print_css(std::ostream& s);
96
97  ///
98  /// @return the current working directory.
99  ///
100  std::string pwd(void);
101
102  inline std::string::iterator search(std::string::iterator& first, 
103                                      std::string::iterator& last, 
104                                      const std::string& str)
105  { return std::search(first, last, str.begin(), str.end()); }
106
107  ///
108  /// Calculating sum of two vectors.
109  ///
110  /// @return resulting vector
111  ///
112  template <typename T >
113  struct VectorPlus : 
114    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
115  {
116    std::vector<T> operator()(const std::vector<T>& u,
117                              const std::vector<T>& v) const 
118    {
119      if ( u.size() > v.size() ){
120        std::vector<T> res(u.size());
121        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
122        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
123        return res;
124      }
125 
126      std::vector<T> res(v.size());
127      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
128      if ( v.size() > u.size() )
129        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
130      return res;
131    }
132
133  };
134
135  ///
136  /// @return resulting vector
137  ///
138  template <typename Key, typename T>
139  struct PairValuePlus :
140    public std::binary_function<std::vector<T>,
141                                std::pair<const Key, std::vector<T> >, 
142                                std::vector<T> >
143  {
144    std::vector<T> operator()(const std::vector<T>& sum, 
145                              const std::pair<const Key,std::vector<T> >& p)
146    {
147      return VectorPlus<T>()(sum, p.second);
148    }
149  };
150
151}} // end of namespace svndigest end of namespace theplu
152
153#endif
Note: See TracBrowser for help on using the repository browser.