1 | // $Id: utility.h 100 2006-06-19 09:53:16Z peter $ |
---|
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 <functional> |
---|
29 | #include <map> |
---|
30 | #include <string> |
---|
31 | #include <utility> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | namespace theplu{ |
---|
35 | namespace svnstat{ |
---|
36 | |
---|
37 | /// |
---|
38 | /// @return 0 if ok |
---|
39 | /// |
---|
40 | int blame(const std::string&); |
---|
41 | |
---|
42 | /// |
---|
43 | /// Create directory \a dir. The call can fail in many ways, cf. 'man |
---|
44 | /// mkdir'. If the \a dir exists, setting \a force to true will |
---|
45 | /// trigger a recursive removal of the target \a dir. |
---|
46 | /// |
---|
47 | int createdir(const std::string& dir, bool force=false); |
---|
48 | |
---|
49 | /// |
---|
50 | /// @return everything after last '/' |
---|
51 | /// |
---|
52 | std::string file_name(const std::string&); |
---|
53 | |
---|
54 | /// |
---|
55 | /// Extracts information from 'svn info <node>' |
---|
56 | /// |
---|
57 | /// @note <node> must be in subversion control. |
---|
58 | /// |
---|
59 | std::map<std::string, std::string> info(const std::string&); |
---|
60 | |
---|
61 | // int log(const std::string&); |
---|
62 | |
---|
63 | /// |
---|
64 | /// Calculating sum of two vectors. |
---|
65 | /// |
---|
66 | /// @return resulting vector |
---|
67 | /// |
---|
68 | template <typename T > |
---|
69 | struct VectorPlus : |
---|
70 | public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> > |
---|
71 | { |
---|
72 | std::vector<T> operator()(const std::vector<T>& u, |
---|
73 | const std::vector<T>& v) const |
---|
74 | { |
---|
75 | if ( u.size() > v.size() ){ |
---|
76 | std::vector<T> res(u.size()); |
---|
77 | transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>()); |
---|
78 | copy(u.begin()+v.size(), u.end(), res.begin()+v.size()); |
---|
79 | return res; |
---|
80 | } |
---|
81 | |
---|
82 | std::vector<T> res(v.size()); |
---|
83 | transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>()); |
---|
84 | if ( v.size() > u.size() ) |
---|
85 | copy(v.begin()+u.size(), v.end(), res.begin()+u.size()); |
---|
86 | return res; |
---|
87 | } |
---|
88 | |
---|
89 | }; |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return resulting vector |
---|
93 | /// |
---|
94 | template <typename Key, typename T> |
---|
95 | struct PairValuePlus : |
---|
96 | public std::binary_function<std::vector<T>, |
---|
97 | std::pair<const Key, std::vector<T> >, |
---|
98 | std::vector<T> > |
---|
99 | { |
---|
100 | std::vector<T> operator()(const std::vector<T>& sum, |
---|
101 | const std::pair<const Key,std::vector<T> >& p) |
---|
102 | { |
---|
103 | return VectorPlus<T>()(sum, p.second); |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | /// |
---|
108 | /// Jari, is this obsolete? And thus erasable? |
---|
109 | /// |
---|
110 | struct CodingMore : |
---|
111 | public std::binary_function<std::pair<std::string,std::vector<u_int> >, |
---|
112 | std::pair<std::string,std::vector<u_int> >, |
---|
113 | bool > |
---|
114 | { |
---|
115 | inline bool operator() |
---|
116 | (const std::pair<std::string,std::vector<u_int> >& a, |
---|
117 | const std::pair<std::string,std::vector<u_int> >& b) |
---|
118 | { |
---|
119 | if (a.second.back() > b.second.back()) |
---|
120 | return true; |
---|
121 | else if (a.second.back() < b.second.back()) |
---|
122 | return false; |
---|
123 | return a.first < b.first; |
---|
124 | } |
---|
125 | }; |
---|
126 | |
---|
127 | }} |
---|
128 | |
---|
129 | // end of namespace svnstat end of namespace theplu |
---|
130 | #endif |
---|