source: trunk/lib/File.cc @ 211

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

fixes #94

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