source: branches/0.6-stable/lib/File.cc @ 451

Last change on this file since 451 was 430, checked in by Peter Johansson, 14 years ago

changing lev.thep.lu.se to trac.thep.lu.se

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1// $Id: File.cc 430 2007-07-06 23:33:11Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.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
26#include "Alias.h"
27#include "Date.h"
28#include "GnuplotFE.h"
29#include "html_utility.h"
30#include "HtmlStream.h"
31#include "Stats.h"
32#include "SVNblame.h"
33#include "SVNlog.h"
34
35#include <cassert>
36#include <ctime>
37#include <fstream>
38#include <iostream>
39#include <map>
40#include <string>
41
42namespace theplu{
43namespace svndigest{
44
45
46  File::File(const u_int level, const std::string& path, 
47             const std::string& output) 
48    : Node(level,path,output) 
49  {
50    output_dir_=output;
51    if (!output_dir_.empty())
52      output_dir_+='/';
53  }
54
55
56  std::string File::href(void) const
57  { 
58    return name()+".html"; 
59  }
60
61
62  std::string File::node_type(void) const
63  {
64    return std::string("file");
65  }
66
67
68  std::string File::output_path(void) const
69  {
70    return output_dir()+name()+".html";
71  }
72
73
74  const Stats& File::parse(const bool verbose)
75  {
76    if (verbose)
77      std::cout << "Parsing " << path_ << std::endl; 
78    stats_.reset();
79    stats_.parse(path_);
80    return stats_;
81  }
82
83
84  void File::print_blame(std::ofstream& os) const
85  {
86    os << "<br /><h3>Blame Information</h3>";
87    os << "<table class=\"blame\">\n";
88    os << "<thead>\n";
89    os << "<tr>\n";
90    os << "<th class=\"rev\">Rev</th>\n";
91    os << "<th class=\"date\">Date</th>\n";
92    os << "<th class=\"author\">Author</th>\n";
93    os << "<th class=\"line\">Line</th>\n";
94    os << "<th></th>\n";
95    os << "</tr>\n</thead>\n";
96    os << "<tbody>\n";
97    HtmlStream hs(os);
98    SVNblame blame(path_);
99    Parser parser(path_);
100    std::vector<Parser::line_type>::const_iterator
101      line_type(parser.type().begin());
102    int last=0;
103    int first=0;
104    bool using_dates=true;
105    if (GnuplotFE::instance()->dates().empty()){
106      using_dates=false;
107      last = stats_.revision();
108    }
109    else {
110      last = Date(GnuplotFE::instance()->dates().back()).seconds();
111      first = Date(GnuplotFE::instance()->dates()[0]).seconds();
112    }
113    // color is calculated linearly on time, c = kt + m
114    // brightest color (for oldest rev in log) is set to 192.
115    double k = 192.0/(first-last);
116    double m = -last*k; 
117    while (blame.valid()) {
118      std::string color;
119      if (using_dates)
120        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
121      else
122        color = hex(static_cast<int>(k*blame.revision()+m),2);
123      os << "<tr>\n<td class=\"rev\"><font color=\"#" << color
124         << color << color << "\">" << blame.revision()
125         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
126         << color << color << "\">" ;
127      hs << Date(blame.date())("%d %b %y");
128      os << "</font></td>\n<td class=\"author\">";
129      hs << blame.author();
130      os << "</td>\n<td class=\"";
131      assert(line_type!=parser.type().end());
132      if (*line_type==Parser::empty)
133        os << "line-other";
134      else if (*line_type==Parser::comment)
135        os << "line-comment";
136      else
137        os << "line-code";
138      os << "\">" << blame.line_no()+1
139         << "</td>\n<td>";
140      hs << blame.line();
141      os << "</td>\n</tr>\n";
142      blame.next_line();
143      ++line_type;
144    }
145    os << "</tbody>\n";
146    os << "</table>\n";
147  }
148
149
150  void File::print_copyright(std::map<std::string, Alias>& alias) const 
151  {
152    if (ignore())
153      return;
154    using namespace std;
155
156    SVNlog log(path());
157
158    map<int, set<Alias> > year_authors;
159
160    assert(log.author().size()==log.date().size());
161    vector<string>::const_iterator author=log.author().begin();
162    for (vector<string>::const_iterator date=log.date().begin();
163         date!=log.date().end(); ++date, ++author) {
164      time_t sec = str2time(*date);
165      tm* timeinfo = gmtime(&sec);
166
167      // find username in map of aliases
168      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
169
170      // if alias exist insert alias
171      if (name != alias.end() && name->first==*author)
172        year_authors[timeinfo->tm_year].insert(name->second);
173      else {
174        // else insert user name
175        Alias a(*author,alias.size());
176        year_authors[timeinfo->tm_year].insert(a);
177        std::cerr << "svndigest: warning: no copyright alias found for `" 
178                  << *author << "`\n";
179        // insert alias to avoid multiple warnings.
180        alias.insert(name, std::make_pair(*author, a));
181      }
182    }
183
184    // Code copied from Gnuplot -r70
185    char tmpname[]="/tmp/svndigestXXXXXX";
186    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
187    if (fd == -1)
188      throw std::runtime_error(std::string("Failed to get unique filename: ") +
189                               tmpname);
190    // Jari would like to do something like 'std::ofstream tmp(fd);'
191    // but has to settle for (which is stupid since the file is
192    // already open for writing.
193    std::ofstream tmp(tmpname);
194
195    ifstream is(path().c_str());
196    assert(is.good());
197    string line;
198    bool found_copyright = false;
199    bool after_copyright = false;
200    string prefix;
201    while(getline(is, line)){
202      if (after_copyright) 
203        tmp << line << "\n";
204      else if (found_copyright){
205        // check if line is end of copyright statement, i.e. contains
206        // no alphanumerical character
207        after_copyright = true;
208        for (size_t i=0; i<line.size()&&after_copyright; ++i)
209          if (isalnum(line[i]))
210            after_copyright = false;
211        if (after_copyright)
212          tmp << line << "\n";
213         
214      }
215      else {
216        // check whether copyright starts on this line
217        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
218        if (i==line.end()) {
219          tmp << line << "\n";
220        }     
221        else {
222          prefix = line.substr(0, distance(line.begin(), i));
223          found_copyright = true;
224          // Printing copyright statement
225          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
226               i!=year_authors.end();) {
227          tmp << prefix << "Copyright (C) "
228              << 1900+i->first;
229          map<int, set<Alias> >::const_iterator j = i;
230          assert(i!=year_authors.end());
231          while (++j!=year_authors.end() && 
232                 i->second == j->second){
233            tmp << ", " << 1900+(j->first);
234          }
235          // printing authors
236          std::vector<Alias> vec_alias;
237          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
238          std::copy(i->second.begin(), i->second.end(), ii);
239          // sort with respect to id
240          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
241          for (std::vector<Alias>::iterator a=vec_alias.begin();
242               a!=vec_alias.end(); ++a){
243            if (a!=vec_alias.begin())
244              tmp << ",";
245            tmp << " " << a->name();
246          }
247          tmp << "\n";
248          i = j;
249          }
250        }
251      }
252    }
253    is.close();
254    tmp.close();
255    close(fd);
256    // finally move printed temporary file to original file
257    rename(tmpname, path().c_str());
258  }
259
260
261  void File::print_core(const bool verbose) const 
262  {
263  }
264
265
266  void File::print_core(const std::string& user, const std::string& line_type,
267                        const SVNlog& log) const 
268  {
269    std::string outpath = user+"/"+line_type+"/"+local_path();
270    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
271    std::string html_name(outpath + ".html");
272    std::ofstream os(html_name.c_str());
273    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
274    path_anchor(os);
275
276    os << "<p class=\"plot\">\n<img src='"; 
277    for (size_t i=0; i<level_; ++i)
278      os << "../";
279    os << "../../";
280    if (user=="all")
281      os << stats_.plot(imagefile,line_type);
282    else
283      os << imagefile;
284    os << "' alt='[plot]' />\n</p>";
285
286    print_author_summary(os, line_type, log);
287    os << "\n";
288
289    print_blame(os);
290
291    print_footer(os);
292    os.close(); 
293  }
294
295}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.