source: trunk/lib/utility.h @ 126

Last change on this file since 126 was 126, checked in by Jari Häkkinen, 17 years ago

Changed createdir to mkdir, removed the ugly force option from the function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1// $Id: utility.h 126 2006-08-01 23:05:52Z jari $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svnstat is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#ifndef _theplu_svnstat_utility_
25#define _theplu_svnstat_utility_
26
27#include <algorithm>
28#include <fstream>
29#include <functional>
30#include <map>
31#include <string>
32#include <utility>
33#include <vector>
34
35#include <sys/stat.h>
36
37namespace theplu{
38namespace svnstat{
39
40  ///
41  /// @return 0 if ok
42  ///
43  int blame(const std::string&);
44
45  ///
46  /// Create directory \a dir. The call can fail in many ways, cf. 'man
47  /// mkdir'.
48  ///
49  inline int mkdir(const std::string& dir) { return ::mkdir(dir.c_str(),0777); }
50
51  ///
52  /// @return everything after last '/'
53  ///
54  std::string file_name(const std::string&);
55
56  ///
57  /// Extracts information from 'svn info <node>'
58  ///
59  /// @note <node> must be in subversion control.
60  ///
61  std::map<std::string, std::string> info(const std::string&);
62
63  ///
64  /// stupid function needed for systems calls to be correct for
65  /// filenames containing spaces.
66  /// @note this function will be removed.
67  ///
68  /// @return passed string with ' ' changed to '\ '
69  ///
70  std::string mod_str(const std::string&);
71
72  ///
73  /// @return the current working directory.
74  ///
75  std::string pwd(void);
76
77  ///
78  /// @printing cascading style sheet to stream @a s.
79  ///
80  void print_css(std::ostream& s);
81
82  inline bool match_begin(std::string::iterator first, 
83                          std::string::iterator last, 
84                          const std::string& str)
85  { return (std::distance(first, last)>=static_cast<int>(str.size()) && 
86            std::equal(str.begin(), str.end(), first)); 
87  }
88
89  inline bool match_end(std::string::iterator first, 
90                        std::string::iterator last, 
91                        const std::string& str)
92  { return (std::distance(first,last)>=static_cast<int>(str.size()) && 
93            std::equal(str.rbegin(), str.rend(), first)); 
94  }
95
96  inline std::string::iterator search(std::string::iterator& first, 
97                                      std::string::iterator& last, 
98                                      const std::string& str)
99  { return std::search(first, last, str.begin(), str.end()); }
100
101  ///
102  /// Calculating sum of two vectors.
103  ///
104  /// @return resulting vector
105  ///
106  template <typename T >
107  struct VectorPlus : 
108    public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
109  {
110    std::vector<T> operator()(const std::vector<T>& u,
111                              const std::vector<T>& v) const 
112    {
113      if ( u.size() > v.size() ){
114        std::vector<T> res(u.size());
115        transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
116        copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
117        return res;
118      }
119 
120      std::vector<T> res(v.size());
121      transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
122      if ( v.size() > u.size() )
123        copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
124      return res;
125    }
126
127  };
128
129  ///
130  /// @return resulting vector
131  ///
132  template <typename Key, typename T>
133  struct PairValuePlus :
134    public std::binary_function<std::vector<T>,
135                                std::pair<const Key, std::vector<T> >, 
136                                std::vector<T> >
137  {
138    std::vector<T> operator()(const std::vector<T>& sum, 
139                              const std::pair<const Key,std::vector<T> >& p)
140    {
141      return VectorPlus<T>()(sum, p.second);
142    }
143  };
144
145  ///
146  /// Jari, is this obsolete? And thus erasable?
147  ///
148  struct CodingMore :
149    public std::binary_function<std::pair<std::string,std::vector<u_int> >,
150                                std::pair<std::string,std::vector<u_int> >,
151                                bool >
152  {
153    inline bool operator()
154      (const std::pair<std::string,std::vector<u_int> >& a, 
155       const std::pair<std::string,std::vector<u_int> >& b)
156    {
157      if (a.second.back() > b.second.back())
158        return true;
159      else if (a.second.back() < b.second.back())
160        return false;
161      return a.first < b.first;
162    }
163  };
164
165}} // end of namespace svnstat end of namespace theplu
166
167#endif
Note: See TracBrowser for help on using the repository browser.