source: trunk/lib/File.cc @ 198

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

refs #36 optionally a copyright information, based on Stats, can be displayed using --copyright flag. This will flag will be added to usage output when this feature is mature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1// $Id: File.cc 198 2006-09-09 10:04:27Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest 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  svndigest 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#include "File.h"
25#include "html_utility.h"
26#include "Stats.h"
27
28#include <fstream>
29#include <iostream>
30#include <string>
31
32namespace theplu{
33namespace svndigest{
34
35
36  std::string File::href(void) const
37  { 
38    return name()+".html"; 
39  }
40
41
42  const std::string File::node_type(void) const
43  {
44    return std::string("file");
45  }
46
47
48  const Stats& File::parse(const bool verbose)
49  {
50    if (verbose)
51      std::cout << "Parsing " << path_ << std::endl; 
52    stats_.reset();
53    stats_.parse(path_);
54    return stats_;
55  }
56
57
58  void File::print(const bool verbose) const 
59  {
60    // no output page for binary files
61    if (ignore())
62      return;
63    std::string output(output_name() + ".html");
64    if (verbose)
65      std::cout << "Printing output for " << path_ << std::endl;
66    std::ofstream os(output.c_str());
67    print_header(os);
68    os << "<p align=center>\n<img src='" 
69       << file_name(stats_.plot(output_name()+".png",output_name()))
70       << "' alt='[plot]' border=0>\n</p>";
71
72    os << "<table class=\"listings\">\n";
73    os << "<thead>";
74    os << "<tr>\n";
75    os << "<th>Author</th>\n";
76    os << "<th>Lines</th>\n";
77    os << "<th>Code</th>\n";
78    os << "<th>Comments</th>\n";
79    os << "</tr>\n</thead>\n";
80    os << "<tbody>";
81
82    bool dark=false;
83    os << "<tr class=\"light\">\n";
84    os << "<td class=\"directory\" colspan=\"5\">";
85    anchor(os, "index.html", "../");
86    os << "</td>\n</tr>\n";
87    dark=!dark;
88   
89    // print authors
90    for (std::set<std::string>::const_iterator i=stats_.authors().begin();
91         i!=stats_.authors().end(); ++i){
92      if (dark)
93        os << "<tr class=\"dark\"><td>" << *i
94           << "</td><td>" << stats_.lines(*i)
95           << "</td><td>" << stats_.code(*i)
96           << "</td><td>" << stats_.comments(*i)
97           << "</td></tr>\n";
98      else
99        os << "<tr class=\"light\"><td>" << *i
100           << "</td><td>" << stats_.lines(*i)
101           << "</td><td>" << stats_.code(*i)
102           << "</td><td>" << stats_.comments(*i)
103           << "</td></tr>\n";
104      dark=!dark;
105    }
106    if (dark)
107      os << "<tr class=\"dark\">\n";
108    else
109      os << "<tr class=\"light\">\n";
110    os << "<td>Total</td>\n";
111    os << "<td>" << stats_.lines() << "</td>\n";
112    os << "<td>" << stats_.code() << "</td>\n";
113    os << "<td>" << stats_.comments() << "</td>\n";
114    os << "</tr>\n";
115    os << "</table>\n";
116    os << "</p>\n";
117
118    print_footer(os);
119    os.close(); 
120  }
121
122
123  void File::print_copyright(const std::vector<std::string>& dates) const
124  {
125    if (ignore())
126      return;
127    std::cout << "\nCOPYRIGHT " << output_name() << std::endl;
128    // last rev and 4-chars string for each year
129    std::vector<std::pair<size_t,std::string> > years;
130    for (size_t i=1; i<dates.size(); ++i) // ignoring rev 0
131      if (years.empty() || years.back().second != dates[i].substr(0,4)){
132        years.push_back(std::pair<size_t,std::string>(i,dates[i].substr(0,4)));
133      } 
134      else {
135        years.back() = std::pair<size_t, std::string>(i, dates[i].substr(0,4));
136      }
137
138    // copyrigt[i] is set of contributors for year 'i'
139    std::vector<std::set<std::string> > copyright;
140    copyright.resize(years.size());
141    for (std::set<std::string>::const_iterator au=stats_.authors().begin();
142         au!=stats_.authors().end(); ++au){
143      std::vector<u_int> lines = stats_.total(*au);
144      assert(!lines.empty());
145      // insert author for 'year zero'
146      if (lines[years[0].first]>0 ){
147        copyright[0].insert(*au);
148      }
149      // insert authors for remaining years
150      for (size_t y=1; y<years.size(); ++y)
151        if (lines[years[y].first]!=lines[years[y-1].first] ){
152          copyright[y].insert(*au);
153        }
154     
155    }
156    // looping over years
157    for (size_t i = 0; i<copyright.size();++i){
158      if (copyright[i].empty()){
159        continue;
160      }
161
162      std::cout << "Copyright (C) " << years[i].second;
163      for (size_t j=i+1; j<copyright.size(); ++j){
164        if (copyright[j].empty())
165          continue;
166        else if (copyright[j]==copyright[i]){
167          std::cout << ", " << years[j].second;
168          i=j;
169        }
170        else
171          break;
172      }
173      std::cout << " ";
174     
175      for (std::set<std::string>::const_iterator iter=copyright[i].begin();
176           iter!=copyright[i].end(); ++iter){
177        if (iter!=copyright[i].begin())
178          std::cout << ", ";
179        std::cout << *iter;
180       
181      }
182      std::cout << std::endl;
183    }
184  }
185}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.