source: trunk/lib/File.cc @ 356

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

fixes #203. Authors statistics link broken.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: File.cc 356 2007-05-25 16:59:46Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.thep.lu.se/trac/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 "html_utility.h"
29#include "Stats.h"
30#include "SVNlog.h"
31
32#include <cassert>
33#include <ctime>
34#include <fstream>
35#include <iostream>
36#include <map>
37#include <string>
38
39namespace theplu{
40namespace svndigest{
41
42
43  File::File(const u_int level, const std::string& path, 
44             const std::string& output) 
45    : Node(level,path,output) 
46  {
47    output_dir_=output;
48    if (!output_dir_.empty())
49      output_dir_+='/';
50  }
51
52
53  std::string File::href(void) const
54  { 
55    return name()+".html"; 
56  }
57
58
59  std::string File::node_type(void) const
60  {
61    return std::string("file");
62  }
63
64
65  std::string File::output_path(void) const
66  {
67    return output_dir()+name()+".html";
68  }
69
70
71  const Stats& File::parse(const bool verbose)
72  {
73    if (verbose)
74      std::cout << "Parsing " << path_ << std::endl; 
75    stats_.reset();
76    stats_.parse(path_);
77    return stats_;
78  }
79
80
81  void File::print_core(const std::string& user, const std::string& line_type,
82                        const SVNlog& log) const 
83  {
84    std::string outpath = user+"/"+line_type+"/"+local_path();
85    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
86    std::string html_name(outpath + ".html");
87    std::ofstream os(html_name.c_str());
88    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
89    path_anchor(os);
90    os << "<p align=center>\n<img src='"; 
91    for (size_t i=0; i<level_; ++i)
92      os << "../";
93    os << "../../";
94    if (user=="all")
95      os << stats_.plot(imagefile,line_type);
96    else
97      os << imagefile;
98    os << "' alt='[plot]' border=0>\n</p>";
99
100    print_author_summary(os, line_type, log);
101    os << "</p>\n";
102
103    print_footer(os);
104    os.close(); 
105  }
106
107
108  void File::print_core(const bool verbose) const 
109  {
110  }
111
112
113  void File::print_blame(std::ofstream& os, const std::string line_type) const
114  {
115    os << "<table class=\"blame\">\n";
116
117    os << "</table>\n";
118  }
119
120
121  void File::print_copyright(std::map<std::string, Alias>& alias) const 
122  {
123    if (ignore())
124      return;
125    using namespace std;
126
127    SVNlog log(path());
128
129    map<int, set<Alias> > year_authors;
130
131    assert(log.author().size()==log.date().size());
132    vector<string>::const_iterator author=log.author().begin();
133    for (vector<string>::const_iterator date=log.date().begin();
134         date!=log.date().end(); ++date, ++author) {
135      time_t sec = str2time(*date);
136      tm* timeinfo = gmtime(&sec);
137
138      // find username in map of aliases
139      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
140
141      // if alias exist insert alias
142      if (name != alias.end() && name->first==*author)
143        year_authors[timeinfo->tm_year].insert(name->second);
144      else {
145        // else insert user name
146        Alias a(*author,alias.size());
147        year_authors[timeinfo->tm_year].insert(a);
148        std::cerr << "svndigest: warning: no copyright alias found for `" 
149                  << *author << "`\n";
150        // insert alias to avoid multiple warnings.
151        alias.insert(name, std::make_pair(*author, a));
152      }
153    }
154
155
156    // Code copied from Gnuplot -r70
157    char tmpname[]="/tmp/svndigestXXXXXX";
158    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
159    if (fd == -1)
160      throw std::runtime_error(std::string("Failed to get unique filename: ") +
161                               tmpname);
162    // Jari would like to do something like 'std::ofstream tmp(fd);'
163    // but has to settle for (which is stupid since the file is
164    // already open for writing.
165    std::ofstream tmp(tmpname);
166
167    ifstream is(path().c_str());
168    assert(is.good());
169    string line;
170    bool found_copyright = false;
171    bool after_copyright = false;
172    string prefix;
173    while(getline(is, line)){
174      if (after_copyright) 
175        tmp << line << "\n";
176      else if (found_copyright){
177        // check if line is end of copyright statement, i.e. contains
178        // no alphanumerical character
179        after_copyright = true;
180        for (size_t i=0; i<line.size()&&after_copyright; ++i)
181          if (isalnum(line[i]))
182            after_copyright = false;
183        if (after_copyright)
184          tmp << line << "\n";
185         
186      }
187      else {
188        // check whether copyright starts on this line
189        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
190        if (i==line.end()) {
191          tmp << line << "\n";
192        }     
193        else {
194          prefix = line.substr(0, distance(line.begin(), i));
195          found_copyright = true;
196          // Printing copyright statement
197          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
198               i!=year_authors.end();) {
199          tmp << prefix << "Copyright (C) "
200              << 1900+i->first;
201          map<int, set<Alias> >::const_iterator j = i;
202          assert(i!=year_authors.end());
203          while (++j!=year_authors.end() && 
204                 i->second == j->second){
205            tmp << ", " << 1900+(j->first);
206          }
207          // printing authors
208          std::vector<Alias> vec_alias;
209          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
210          std::copy(i->second.begin(), i->second.end(), ii);
211          // sort with respect to id
212          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
213          for (std::vector<Alias>::iterator a=vec_alias.begin();
214               a!=vec_alias.end(); ++a){
215            if (a!=vec_alias.begin())
216              tmp << ",";
217            tmp << " " << a->name();
218          }
219          tmp << "\n";
220          i = j;
221          }
222        }
223      }
224    }
225    is.close();
226    tmp.close();
227    close(fd);
228    // finally move printed temporary file to original file
229    rename(tmpname, path().c_str());
230   
231  }
232}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.