source: trunk/lib/utility.h @ 226

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

added reading of config file in Parameter closes ##106 and #98. Move function check_target from svndigest.cc to utility.cc and changed name to node_exist. added support for aliases in Copyright update refs #36

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1#ifndef _theplu_svndigest_utility_
2#define _theplu_svndigest_utility_
3
4// $Id: utility.h 226 2007-03-11 19:32:41Z 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  ///
66  /// @brief remove trailing whitespaces
67  ///
68  std::string htrim(std::string str);
69
70  ///
71  /// @brief remove leading whitespaces
72  ///
73  std::string ltrim(std::string str);
74
75  inline bool match_begin(std::string::const_iterator first, 
76                          std::string::const_iterator last, 
77                          const std::string& str)
78  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
79            std::equal(str.begin(), str.end(), first)); 
80  }
81
82  inline bool match_end(std::string::const_reverse_iterator first, 
83                        std::string::const_reverse_iterator last, 
84                        const std::string& str)
85  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
86            std::equal(str.rbegin(), str.rend(), first)); 
87  }
88
89  ///
90  /// Create directory \a dir. The call can fail in many ways, cf. 'man
91  /// mkdir'.
92  ///
93  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
94
95  ///
96  /// @brief Check whether \a path already exists or not.
97  ///
98  /// @return True if \a path exists, false otherwise.
99  ///
100  bool node_exist(const std::string& path);
101
102  ///
103  /// @return the current working directory.
104  ///
105  std::string pwd(void);
106
107  inline std::string::iterator search(std::string::iterator& first, 
108                                      std::string::iterator& last, 
109                                      const std::string& str)
110  { return std::search(first, last, str.begin(), str.end()); }
111
112  ///
113  ///
114  ///
115  time_t str2time(const std::string&);
116
117  ///
118  /// remove leading and trailing whitespaces
119  ///
120  inline std::string trim(std::string str) { return htrim(ltrim(str)); }
121
122  ///
123  /// Calculating sum of two vectors.
124  ///
125  /// @return resulting vector
126  ///
127  template <typename T >
128  struct VectorPlus : 
129    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
130  {
131    std::vector<T> operator()(const std::vector<T>& u,
132                              const std::vector<T>& v) const 
133    {
134      if ( u.size() > v.size() ){
135        std::vector<T> res(u.size());
136        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
137        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
138        return res;
139      }
140 
141      std::vector<T> res(v.size());
142      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
143      if ( v.size() > u.size() )
144        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
145      return res;
146    }
147
148  };
149
150  ///
151  /// @return resulting vector
152  ///
153  template <typename Key, typename T>
154  struct PairValuePlus :
155    public std::binary_function<std::vector<T>,
156                                std::pair<const Key, std::vector<T> >, 
157                                std::vector<T> >
158  {
159    std::vector<T> operator()(const std::vector<T>& sum, 
160                              const std::pair<const Key,std::vector<T> >& p)
161    {
162      return VectorPlus<T>()(sum, p.second);
163    }
164  };
165
166}} // end of namespace svndigest end of namespace theplu
167
168#endif
Note: See TracBrowser for help on using the repository browser.