1 | // $Id: utility.h 118 2006-07-03 07:44:49Z 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 <fstream> |
---|
29 | #include <functional> |
---|
30 | #include <map> |
---|
31 | #include <string> |
---|
32 | #include <utility> |
---|
33 | #include <vector> |
---|
34 | |
---|
35 | namespace theplu{ |
---|
36 | namespace svnstat{ |
---|
37 | |
---|
38 | /// |
---|
39 | /// @return 0 if ok |
---|
40 | /// |
---|
41 | int blame(const std::string&); |
---|
42 | |
---|
43 | /// |
---|
44 | /// Create directory \a dir. The call can fail in many ways, cf. 'man |
---|
45 | /// mkdir'. If the \a dir exists, setting \a force to true will |
---|
46 | /// trigger a recursive removal of the target \a dir. |
---|
47 | /// |
---|
48 | int createdir(const std::string& dir, bool force=false); |
---|
49 | |
---|
50 | /// |
---|
51 | /// @return everything after last '/' |
---|
52 | /// |
---|
53 | std::string file_name(const std::string&); |
---|
54 | |
---|
55 | /// |
---|
56 | /// Extracts information from 'svn info <node>' |
---|
57 | /// |
---|
58 | /// @note <node> must be in subversion control. |
---|
59 | /// |
---|
60 | std::map<std::string, std::string> info(const std::string&); |
---|
61 | |
---|
62 | /// |
---|
63 | /// stupid function needed for systems calls to be correct for |
---|
64 | /// filenames containing spaces. |
---|
65 | /// @note this function will be removed. |
---|
66 | /// |
---|
67 | /// @return passed string with ' ' changed to '\ ' |
---|
68 | /// |
---|
69 | std::string mod_str(const std::string&); |
---|
70 | |
---|
71 | /// |
---|
72 | /// @return the current working directory. |
---|
73 | /// |
---|
74 | std::string pwd(void); |
---|
75 | |
---|
76 | /// |
---|
77 | /// @printing cascading style sheet to stream @a s. |
---|
78 | /// |
---|
79 | void print_css(std::ostream& s); |
---|
80 | |
---|
81 | inline bool match_begin(std::string::iterator first, |
---|
82 | std::string::iterator last, |
---|
83 | const std::string& str) |
---|
84 | { return (std::distance(first, last)>=static_cast<int>(str.size()) && |
---|
85 | std::equal(str.begin(), str.end(), first)); |
---|
86 | } |
---|
87 | |
---|
88 | inline bool match_end(std::string::iterator first, |
---|
89 | std::string::iterator last, |
---|
90 | const std::string& str) |
---|
91 | { return (std::distance(first,last)>=static_cast<int>(str.size()) && |
---|
92 | std::equal(str.rbegin(), str.rend(), first)); |
---|
93 | } |
---|
94 | |
---|
95 | inline std::string::iterator search(std::string::iterator& first, |
---|
96 | std::string::iterator& last, |
---|
97 | const std::string& str) |
---|
98 | { return std::search(first, last, str.begin(), str.end()); } |
---|
99 | |
---|
100 | /// |
---|
101 | /// Calculating sum of two vectors. |
---|
102 | /// |
---|
103 | /// @return resulting vector |
---|
104 | /// |
---|
105 | template <typename T > |
---|
106 | struct VectorPlus : |
---|
107 | public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> > |
---|
108 | { |
---|
109 | std::vector<T> operator()(const std::vector<T>& u, |
---|
110 | const std::vector<T>& v) const |
---|
111 | { |
---|
112 | if ( u.size() > v.size() ){ |
---|
113 | std::vector<T> res(u.size()); |
---|
114 | transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>()); |
---|
115 | copy(u.begin()+v.size(), u.end(), res.begin()+v.size()); |
---|
116 | return res; |
---|
117 | } |
---|
118 | |
---|
119 | std::vector<T> res(v.size()); |
---|
120 | transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>()); |
---|
121 | if ( v.size() > u.size() ) |
---|
122 | copy(v.begin()+u.size(), v.end(), res.begin()+u.size()); |
---|
123 | return res; |
---|
124 | } |
---|
125 | |
---|
126 | }; |
---|
127 | |
---|
128 | /// |
---|
129 | /// @return resulting vector |
---|
130 | /// |
---|
131 | template <typename Key, typename T> |
---|
132 | struct PairValuePlus : |
---|
133 | public std::binary_function<std::vector<T>, |
---|
134 | std::pair<const Key, std::vector<T> >, |
---|
135 | std::vector<T> > |
---|
136 | { |
---|
137 | std::vector<T> operator()(const std::vector<T>& sum, |
---|
138 | const std::pair<const Key,std::vector<T> >& p) |
---|
139 | { |
---|
140 | return VectorPlus<T>()(sum, p.second); |
---|
141 | } |
---|
142 | }; |
---|
143 | |
---|
144 | /// |
---|
145 | /// Jari, is this obsolete? And thus erasable? |
---|
146 | /// |
---|
147 | struct CodingMore : |
---|
148 | public std::binary_function<std::pair<std::string,std::vector<u_int> >, |
---|
149 | std::pair<std::string,std::vector<u_int> >, |
---|
150 | bool > |
---|
151 | { |
---|
152 | inline bool operator() |
---|
153 | (const std::pair<std::string,std::vector<u_int> >& a, |
---|
154 | const std::pair<std::string,std::vector<u_int> >& b) |
---|
155 | { |
---|
156 | if (a.second.back() > b.second.back()) |
---|
157 | return true; |
---|
158 | else if (a.second.back() < b.second.back()) |
---|
159 | return false; |
---|
160 | return a.first < b.first; |
---|
161 | } |
---|
162 | }; |
---|
163 | |
---|
164 | }} |
---|
165 | |
---|
166 | // end of namespace svnstat end of namespace theplu |
---|
167 | #endif |
---|