source: trunk/lib/File.cc @ 199

Last change on this file since 199 was 199, checked in by Peter Johansson, 16 years ago

refs #87 moved print_header function to html_utility

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1// $Id: File.cc 199 2006-09-09 10:42:09Z 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, name(), level_);
68    path_anchor(os);
69    os << "<p align=center>\n<img src='" 
70       << file_name(stats_.plot(output_name()+".png",output_name()))
71       << "' alt='[plot]' border=0>\n</p>";
72
73    os << "<table class=\"listings\">\n";
74    os << "<thead>";
75    os << "<tr>\n";
76    os << "<th>Author</th>\n";
77    os << "<th>Lines</th>\n";
78    os << "<th>Code</th>\n";
79    os << "<th>Comments</th>\n";
80    os << "</tr>\n</thead>\n";
81    os << "<tbody>";
82
83    bool dark=false;
84    os << "<tr class=\"light\">\n";
85    os << "<td class=\"directory\" colspan=\"5\">";
86    anchor(os, "index.html", "../");
87    os << "</td>\n</tr>\n";
88    dark=!dark;
89   
90    // print authors
91    for (std::set<std::string>::const_iterator i=stats_.authors().begin();
92         i!=stats_.authors().end(); ++i){
93      if (dark)
94        os << "<tr class=\"dark\"><td>" << *i
95           << "</td><td>" << stats_.lines(*i)
96           << "</td><td>" << stats_.code(*i)
97           << "</td><td>" << stats_.comments(*i)
98           << "</td></tr>\n";
99      else
100        os << "<tr class=\"light\"><td>" << *i
101           << "</td><td>" << stats_.lines(*i)
102           << "</td><td>" << stats_.code(*i)
103           << "</td><td>" << stats_.comments(*i)
104           << "</td></tr>\n";
105      dark=!dark;
106    }
107    if (dark)
108      os << "<tr class=\"dark\">\n";
109    else
110      os << "<tr class=\"light\">\n";
111    os << "<td>Total</td>\n";
112    os << "<td>" << stats_.lines() << "</td>\n";
113    os << "<td>" << stats_.code() << "</td>\n";
114    os << "<td>" << stats_.comments() << "</td>\n";
115    os << "</tr>\n";
116    os << "</table>\n";
117    os << "</p>\n";
118
119    print_footer(os);
120    os.close(); 
121  }
122
123
124  void File::print_copyright(const std::vector<std::string>& dates) const
125  {
126    if (ignore())
127      return;
128    std::cout << "\nCOPYRIGHT " << output_name() << std::endl;
129    // last rev and 4-chars string for each year
130    std::vector<std::pair<size_t,std::string> > years;
131    for (size_t i=1; i<dates.size(); ++i) // ignoring rev 0
132      if (years.empty() || years.back().second != dates[i].substr(0,4)){
133        years.push_back(std::pair<size_t,std::string>(i,dates[i].substr(0,4)));
134      } 
135      else {
136        years.back() = std::pair<size_t, std::string>(i, dates[i].substr(0,4));
137      }
138
139    // copyrigt[i] is set of contributors for year 'i'
140    std::vector<std::set<std::string> > copyright;
141    copyright.resize(years.size());
142    for (std::set<std::string>::const_iterator au=stats_.authors().begin();
143         au!=stats_.authors().end(); ++au){
144      std::vector<u_int> lines = stats_.total(*au);
145      assert(!lines.empty());
146      // insert author for 'year zero'
147      if (lines[years[0].first]>0 ){
148        copyright[0].insert(*au);
149      }
150      // insert authors for remaining years
151      for (size_t y=1; y<years.size(); ++y)
152        if (lines[years[y].first]!=lines[years[y-1].first] ){
153          copyright[y].insert(*au);
154        }
155     
156    }
157    // looping over years
158    for (size_t i = 0; i<copyright.size();++i){
159      if (copyright[i].empty()){
160        continue;
161      }
162
163      std::cout << "Copyright (C) " << years[i].second;
164      for (size_t j=i+1; j<copyright.size(); ++j){
165        if (copyright[j].empty())
166          continue;
167        else if (copyright[j]==copyright[i]){
168          std::cout << ", " << years[j].second;
169          i=j;
170        }
171        else
172          break;
173      }
174      std::cout << " ";
175     
176      for (std::set<std::string>::const_iterator iter=copyright[i].begin();
177           iter!=copyright[i].end(); ++iter){
178        if (iter!=copyright[i].begin())
179          std::cout << ", ";
180        std::cout << *iter;
181       
182      }
183      std::cout << std::endl;
184    }
185  }
186}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.