source: tags/0.6.8/lib/File.cc @ 848

Last change on this file since 848 was 731, checked in by Peter Johansson, 13 years ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1// $Id: File.cc 731 2008-12-15 19:03:04Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "File.h"
26
27#include "Alias.h"
28#include "Date.h"
29#include "GnuplotFE.h"
30#include "html_utility.h"
31#include "HtmlStream.h"
32#include "Stats.h"
33#include "SVNblame.h"
34#include "SVNlog.h"
35
36#include <algorithm>
37#include <cassert>
38#include <cstdio>
39#include <ctime>
40#include <fstream>
41#include <iostream>
42#include <map>
43#include <string>
44
45namespace theplu{
46namespace svndigest{
47
48
49  File::File(const u_int level, const std::string& path, 
50             const std::string& output) 
51    : Node(level,path,output) 
52  {
53    output_dir_=output;
54    if (!output_dir_.empty())
55      output_dir_+='/';
56  }
57
58
59  std::string File::href(void) const
60  { 
61    return name()+".html"; 
62  }
63
64
65  std::string File::node_type(void) const
66  {
67    return std::string("file");
68  }
69
70
71  std::string File::output_path(void) const
72  {
73    return output_dir()+name()+".html";
74  }
75
76
77  const Stats& File::parse(const bool verbose)
78  {
79    if (verbose)
80      std::cout << "Parsing " << path_ << std::endl; 
81    stats_.reset();
82    stats_.parse(path_);
83    return stats_;
84  }
85
86
87  void File::print_blame(std::ofstream& os) const
88  {
89    os << "<br /><h3>Blame Information</h3>";
90    os << "<table class=\"blame\">\n";
91    os << "<thead>\n";
92    os << "<tr>\n";
93    os << "<th class=\"rev\">Rev</th>\n";
94    os << "<th class=\"date\">Date</th>\n";
95    os << "<th class=\"author\">Author</th>\n";
96    os << "<th class=\"line\">Line</th>\n";
97    os << "<th></th>\n";
98    os << "</tr>\n</thead>\n";
99    os << "<tbody>\n";
100    HtmlStream hs(os);
101    SVNblame blame(path_);
102    Parser parser(path_);
103    std::vector<Parser::line_type>::const_iterator
104      line_type(parser.type().begin());
105    int last=0;
106    int first=0;
107    bool using_dates=true;
108    if (GnuplotFE::instance()->dates().empty()){
109      using_dates=false;
110      last = stats_.revision();
111    }
112    else {
113      last = Date(GnuplotFE::instance()->dates().back()).seconds();
114      // earliest date corresponds either to revision 0 or revision 1
115      first = std::min(Date(GnuplotFE::instance()->dates()[0]).seconds(),
116                       Date(GnuplotFE::instance()->dates()[1]).seconds());
117    }
118    // color is calculated linearly on time, c = kt + m
119    // brightest color (for oldest rev in log) is set to 192.
120    double k = 192.0/(first-last);
121    double m = -last*k; 
122    while (blame.valid()) {
123      std::string color;
124      if (using_dates)
125        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
126      else
127        color = hex(static_cast<int>(k*blame.revision()+m),2);
128      os << "<tr>\n<td class=\"rev\"><font color=\"#" << color
129         << color << color << "\">" << blame.revision()
130         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
131         << color << color << "\">" ;
132      hs << Date(blame.date())("%d %b %y");
133      os << "</font></td>\n<td class=\"author\">";
134      hs << blame.author();
135      os << "</td>\n<td class=\"";
136      assert(line_type!=parser.type().end());
137      if (*line_type==Parser::empty)
138        os << "line-other";
139      else if (*line_type==Parser::comment)
140        os << "line-comment";
141      else
142        os << "line-code";
143      os << "\">" << blame.line_no()+1
144         << "</td>\n<td>";
145      hs << blame.line();
146      os << "</td>\n</tr>\n";
147      blame.next_line();
148      ++line_type;
149    }
150    os << "</tbody>\n";
151    os << "</table>\n";
152  }
153
154
155  void File::print_copyright(std::map<std::string, Alias>& alias) const 
156  {
157    if (ignore())
158      return;
159    using namespace std;
160
161    SVNlog log(path());
162
163    map<int, set<Alias> > year_authors;
164
165    assert(log.author().size()==log.date().size());
166    vector<string>::const_iterator author=log.author().begin();
167    for (vector<string>::const_iterator date=log.date().begin();
168         date!=log.date().end(); ++date, ++author) {
169      time_t sec = str2time(*date);
170      tm* timeinfo = gmtime(&sec);
171
172      // find username in map of aliases
173      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
174
175      // if alias exist insert alias
176      if (name != alias.end() && name->first==*author)
177        year_authors[timeinfo->tm_year].insert(name->second);
178      else {
179        // else insert user name
180        Alias a(*author,alias.size());
181        year_authors[timeinfo->tm_year].insert(a);
182        std::cerr << "svndigest: warning: no copyright alias found for `" 
183                  << *author << "`\n";
184        // insert alias to avoid multiple warnings.
185        alias.insert(name, std::make_pair(*author, a));
186      }
187    }
188
189    // Code copied from Gnuplot -r70
190    char tmpname[]="/tmp/svndigestXXXXXX";
191    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
192    if (fd == -1)
193      throw std::runtime_error(std::string("Failed to get unique filename: ") +
194                               tmpname);
195    // Jari would like to do something like 'std::ofstream tmp(fd);'
196    // but has to settle for (which is stupid since the file is
197    // already open for writing.
198    std::ofstream tmp(tmpname);
199
200    ifstream is(path().c_str());
201    assert(is.good());
202    string line;
203    bool found_copyright = false;
204    bool after_copyright = false;
205    string prefix;
206    while(getline(is, line)){
207      if (after_copyright) {
208        tmp << line;
209        if (is.good()) // not end of file
210          tmp << "\n";
211        else { // check if file ends with newline character
212          is.unget();
213          char c = is.get();
214          if (c=='\n') 
215            tmp << "\n";
216        }
217      }
218      else if (found_copyright){
219        // check if line is end of copyright statement, i.e. contains
220        // no alphanumerical character
221        after_copyright = true;
222        for (size_t i=0; i<line.size()&&after_copyright; ++i)
223          if (isalnum(line[i]))
224            after_copyright = false;
225        if (after_copyright)
226          tmp << line << "\n";
227         
228      }
229      else {
230        // check whether copyright starts on this line
231        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
232        if (i==line.end()) {
233          tmp << line << "\n";
234        }     
235        else {
236          prefix = line.substr(0, distance(line.begin(), i));
237          found_copyright = true;
238          // Printing copyright statement
239          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
240               i!=year_authors.end();) {
241          tmp << prefix << "Copyright (C) "
242              << 1900+i->first;
243          map<int, set<Alias> >::const_iterator j = i;
244          assert(i!=year_authors.end());
245          while (++j!=year_authors.end() && 
246                 i->second == j->second){
247            tmp << ", " << 1900+(j->first);
248          }
249          // printing authors
250          std::vector<Alias> vec_alias;
251          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
252          std::copy(i->second.begin(), i->second.end(), ii);
253          // sort with respect to id
254          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
255          for (std::vector<Alias>::iterator a=vec_alias.begin();
256               a!=vec_alias.end(); ++a){
257            if (a!=vec_alias.begin())
258              tmp << ",";
259            tmp << " " << a->name();
260          }
261          tmp << "\n";
262          i = j;
263          }
264        }
265      }
266    }
267    is.close();
268    tmp.close();
269    close(fd);
270    // finally copy temporary file to replace original file, and
271    // remove the temporary file
272    try {
273      copy_file(tmpname, path());
274    }
275    catch (std::runtime_error e) {
276      // catch exception, cleanup, and rethrow
277      std::cerr << "File::print_copyright: Exception caught, "
278                << "removing temporary file " << tmpname << std::endl;
279      if (unlink(tmpname))
280        throw runtime_error(std::string("File::print_copyright: ") +
281                            "failed to unlink temporary file" + tmpname);
282      throw;
283    }
284    if (unlink(tmpname))
285      throw runtime_error(std::string("File::print_copyright: ") +
286                          "failed to unlink temporary file" + tmpname);
287  }
288
289
290  void File::print_core(const bool verbose) const 
291  {
292  }
293
294
295  void File::print_core(const std::string& user, const std::string& line_type,
296                        const SVNlog& log) const 
297  {
298    std::string outpath = user+"/"+line_type+"/"+local_path();
299    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
300    std::string html_name(outpath + ".html");
301    std::ofstream os(html_name.c_str());
302    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
303    path_anchor(os);
304
305    os << "<p class=\"plot\">\n<img src='"; 
306    for (size_t i=0; i<level_; ++i)
307      os << "../";
308    os << "../../";
309    if (user=="all")
310      os << stats_.plot(imagefile,line_type);
311    else
312      os << imagefile;
313    os << "' alt='[plot]' />\n</p>";
314
315    print_author_summary(os, line_type, log);
316    os << "\n";
317
318    print_blame(os);
319
320    print_footer(os);
321    os.close(); 
322  }
323
324}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.