source: trunk/lib/File.cc @ 379

Last change on this file since 379 was 379, checked in by Jari Häkkinen, 14 years ago

Addresses #62.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1// $Id: File.cc 379 2007-06-20 21:35:02Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2007 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
26#include "Alias.h"
27#include "html_utility.h"
28#include "Stats.h"
29#include "SVNlog.h"
30
31#include <cassert>
32#include <ctime>
33#include <fstream>
34#include <iostream>
35#include <map>
36#include <string>
37
38namespace theplu{
39namespace svndigest{
40
41
42  File::File(const u_int level, const std::string& path, 
43             const std::string& output) 
44    : Node(level,path,output) 
45  {
46    output_dir_=output;
47    if (!output_dir_.empty())
48      output_dir_+='/';
49  }
50
51
52  std::string File::href(void) const
53  { 
54    return name()+".html"; 
55  }
56
57
58  std::string File::node_type(void) const
59  {
60    return std::string("file");
61  }
62
63
64  std::string File::output_path(void) const
65  {
66    return output_dir()+name()+".html";
67  }
68
69
70  const Stats& File::parse(const bool verbose)
71  {
72    if (verbose)
73      std::cout << "Parsing " << path_ << std::endl; 
74    stats_.reset();
75    stats_.parse(path_);
76    return stats_;
77  }
78
79  /*
80  void File::blame_anchor(std::ostream& os) const
81  {
82    os << "<h2 class=\"path\">\n";
83    std::vector<std::string> words;
84    words.reserve(level_+1);
85    std::string word;
86    words.push_back(Node::project_);
87    std::stringstream ss(local_path());
88    while(getline(ss,word,'/'))
89      if (!word.empty()) // ignore double slash in path
90        words.push_back(word);
91    if (words.size()==1)
92      os << anchor("index.html", Node::project_,0, "View " + Node::project_);
93    else {
94      for (size_t i=0; i<words.size()-1; ++i){
95        os << anchor("index.html", words[i], level_-i, "View " + words[i]);
96        os << "<span class=\"sep\">/</span>\n";
97      }
98      os << anchor(href(), words.back(), level_+2-words.size(),
99             "View " + words.back());
100    }
101    os << "\n</h2>\n";
102  }
103  */
104
105  void File::print_blame(std::ofstream& os, const std::string line_type) const
106  {
107    os << "<h3>Blame information</h3>";
108    os << "<table class=\"blame\">\n";
109    os << "<thead>\n";
110    os << "<tr>\n";
111    os << "<th>Rev</th>\n";
112    os << "<th>Author</th>\n";
113    os << "<th>Line</th>\n";
114    os << "</tr>\n</thead>\n";
115    os << "<tbody>\n";
116
117    os << "</tbody>\n";
118    os << "</table>\n";
119  }
120
121
122  void File::print_copyright(std::map<std::string, Alias>& alias) const 
123  {
124    if (ignore())
125      return;
126    using namespace std;
127
128    SVNlog log(path());
129
130    map<int, set<Alias> > year_authors;
131
132    assert(log.author().size()==log.date().size());
133    vector<string>::const_iterator author=log.author().begin();
134    for (vector<string>::const_iterator date=log.date().begin();
135         date!=log.date().end(); ++date, ++author) {
136      time_t sec = str2time(*date);
137      tm* timeinfo = gmtime(&sec);
138
139      // find username in map of aliases
140      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
141
142      // if alias exist insert alias
143      if (name != alias.end() && name->first==*author)
144        year_authors[timeinfo->tm_year].insert(name->second);
145      else {
146        // else insert user name
147        Alias a(*author,alias.size());
148        year_authors[timeinfo->tm_year].insert(a);
149        std::cerr << "svndigest: warning: no copyright alias found for `" 
150                  << *author << "`\n";
151        // insert alias to avoid multiple warnings.
152        alias.insert(name, std::make_pair(*author, a));
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
233  void File::print_core(const bool verbose) const 
234  {
235  }
236
237
238  void File::print_core(const std::string& user, const std::string& line_type,
239                        const SVNlog& log) const 
240  {
241    std::string outpath = user+"/"+line_type+"/"+local_path();
242    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
243    std::string html_name(outpath + ".html");
244    std::ofstream os(html_name.c_str());
245    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
246    path_anchor(os);
247
248    os << "<p align=center>\n<img src='"; 
249    for (size_t i=0; i<level_; ++i)
250      os << "../";
251    os << "../../";
252    if (user=="all")
253      os << stats_.plot(imagefile,line_type);
254    else
255      os << imagefile;
256    os << "' alt='[plot]' border=0>\n</p>";
257
258    print_author_summary(os, line_type, log);
259    os << "</p>\n";
260
261    print_blame(os,line_type);
262
263    print_footer(os);
264    os.close(); 
265  }
266
267}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.