source: trunk/lib/Stats.h @ 589

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

Replace u_int with unsigned int.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1#ifndef _theplu_svndigest_stats_
2#define _theplu_svndigest_stats_
3
4// $Id: Stats.h 589 2008-04-12 16:53:33Z jari $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2007 Peter Johansson
10
11  This file is part of svndigest, http://trac.thep.lu.se/svndigest
12
13  svndigest is free software; you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License, or
16  (at your option) any later version.
17
18  svndigest is distributed in the hope that it will be useful, but
19  WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29#include "LineTypeParser.h"
30
31#include <subversion-1/svn_types.h>
32
33#include <map>
34#include <istream>
35#include <set>
36#include <string>
37#include <vector>
38
39namespace theplu{
40namespace svndigest{
41
42  ///
43  /// Class taking care of statistics from svn.
44  ///
45  class Stats
46  {
47  public:
48    ///
49    /// @brief Default Constructor
50    ///
51    explicit Stats(const std::string& path);
52
53    /**
54       \brief Destructor
55    */
56    virtual ~Stats(void);
57
58    ///
59    /// @brief adding \a n line(s) to \a user from \a revision to the stats
60    ///
61    void add(const std::string& user, const unsigned int& revision,
62             const LineTypeParser::line_type&, unsigned int n=1); 
63
64    ///
65    /// @return set of authors
66    ///
67    const std::set<std::string>& authors(void) const;
68
69    ///
70    ///
71    ///
72    unsigned int code(const std::string& user="all") const; 
73
74
75    ///
76    ///
77    ///
78    unsigned int comments(const std::string& user="all") const; 
79
80    ///
81    ///
82    ///
83    unsigned int empty(const std::string& user="all") const; 
84
85    ///
86    ///
87    ///
88    svn_revnum_t last_changed_rev(void) const;
89
90    ///
91    ///
92    ///
93    unsigned int lines(const std::string& user="all") const; 
94
95    /**
96       Load object from a stream.
97       
98       \return true if successful
99     */
100    bool load_cache(std::istream&);
101
102    /**
103       Do the parsing
104    */
105    void parse(const std::string&);
106
107    ///
108    /// Create statistics graph.
109    ///
110    std::string plot(const std::string&, const std::string&) const;
111
112    ///
113    /// Plotting code, comment, other, and total in same plot (for
114    /// 'all' not individual authors).
115    ///
116    void plot_summary(const std::string& output) const;
117
118    /**
119       Send Stats to a stream.
120     */
121    void print(std::ostream&) const;
122
123    ///
124    /// @brief Clear all statistics
125    ///
126    void reset(void); 
127
128    ///
129    /// \return latest revision for whole project
130    ///
131    svn_revnum_t revision(void) const { return revision_; }
132
133    /**
134       \return resulting Stats
135    */
136    Stats& operator+=(const Stats&);
137
138    /**
139       \return number of lines for \a author and \a linetype from
140       revision \a rev.
141
142       \throw if \a author does not exist
143     */
144    size_t operator()(int linetype, std::string author, size_t rev) const;
145
146  protected:
147    typedef std::map<std::string, std::vector<unsigned int> > Author2Vector;
148    typedef Author2Vector::iterator A2VIter;
149    typedef Author2Vector::const_iterator A2VConstIter;
150
151    void accumulate_stats(void);
152    void add_author(std::string);
153    void add_authors(std::set<std::string>::const_iterator, 
154                     std::set<std::string>::const_iterator);
155
156    // references to data
157    inline Author2Vector& code_stats(void) 
158    { return stats_[LineTypeParser::code]; }
159    inline Author2Vector& comment_stats(void) 
160    { return stats_[LineTypeParser::comment]; }
161    inline Author2Vector& copyright_stats(void) 
162    { return stats_[LineTypeParser::copyright]; }
163    inline Author2Vector& other_stats(void) 
164    { return stats_[LineTypeParser::other]; }
165    inline Author2Vector& comment_or_copy_stats(void) 
166    { return stats_[LineTypeParser::comment_or_copy]; }
167    inline Author2Vector& total_stats(void) 
168    { return stats_[LineTypeParser::total]; }
169
170    // const references to data
171    inline const Author2Vector& code_stats(void) const
172    { return stats_[LineTypeParser::code]; }
173    inline const Author2Vector& comment_stats(void) const
174    { return stats_[LineTypeParser::comment]; }
175    inline const Author2Vector& copyright_stats(void) const
176    { return stats_[LineTypeParser::copyright]; }
177    inline const Author2Vector& other_stats(void) const
178    { return stats_[LineTypeParser::other]; }
179    inline const Author2Vector& comment_or_copy_stats(void) const
180    { return stats_[LineTypeParser::comment_or_copy]; }
181    inline const Author2Vector& total_stats(void) const
182    { return stats_[LineTypeParser::total]; }
183
184    /**
185       add range [\a first, \a last) to \a map
186     */
187    void map_add(Author2Vector::const_iterator first, 
188                 Author2Vector::const_iterator last,
189                 Author2Vector& map);
190
191    ///
192    /// Init statistics graph.
193    ///
194    void plot_init(const std::string& output) const;
195
196    std::set<std::string> authors_;
197
198    const std::vector<unsigned int>& get_vector(const Author2Vector&, 
199                                         std::string user) const;
200  private:
201    void accumulate(std::vector<unsigned int>& vec) const;
202    virtual void do_parse(const std::string&)=0;
203   
204    // Change this string if cache format is changed in such a way
205    // that all old cache files are obsolete.
206    inline std::string cache_check_str(void) const 
207    {return "CACHE FILE VERSION 5";} 
208
209    void calc_all(void);
210    void calc_comment_or_copy(void);
211    void calc_total(void);
212    unsigned int get_back(const Author2Vector&, std::string user) const;
213    void load(std::istream& is, Author2Vector& m);
214    void print(std::ostream& os, const Author2Vector& m) const;
215
216    svn_revnum_t revision_; // Should be the latest revision for whole project
217    svn_revnum_t last_changed_rev_; // Should be the latest revision for file
218
219    std::vector<Author2Vector> stats_; // from linetype to a2v
220
221    // using compiler generated copy constructor
222    //Stats(const Stats&);
223    // no assignment
224    Stats& operator=(const Stats&);
225
226  };
227}} // end of namespace svndigest end of namespace theplu
228
229#endif
Note: See TracBrowser for help on using the repository browser.