source: trunk/lib/File.cc @ 343

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

refs #169

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: File.cc 343 2007-05-19 13:58:19Z 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_dir(void) const
66  {
67    return std::string();
68  }
69
70
71  std::string File::output_path(void) const
72  {
73    return local_path_+".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_core(const std::string& user, const std::string& line_type,
88                        const SVNlog& log) const 
89  {
90    std::string outpath = user+"/"+line_type+"/"+local_path();
91    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
92    std::string html_name(outpath + ".html");
93    std::ofstream os(html_name.c_str());
94    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
95    path_anchor(os);
96    os << "<p align=center>\n<img src='"; 
97    for (size_t i=0; i<level_; ++i)
98      os << "../";
99    os << "../../";
100    if (user=="all")
101      os << stats_.plot(imagefile,line_type);
102    else
103      os << imagefile;
104    os << "' alt='[plot]' border=0>\n</p>";
105
106    print_author_summary(os, line_type, log);
107    os << "</p>\n";
108
109    print_footer(os);
110    os.close(); 
111  }
112
113
114  void File::print_core(const bool verbose) const 
115  {
116  }
117
118
119  void File::print_blame(std::ofstream& os, const std::string line_type) const
120  {
121    os << "<table class=\"blame\">\n";
122
123    os << "</table>\n";
124  }
125
126
127  void File::print_copyright(std::map<std::string, Alias>& alias) const 
128  {
129    if (ignore())
130      return;
131    using namespace std;
132
133    SVNlog log(path());
134
135    map<int, set<Alias> > year_authors;
136
137    assert(log.author().size()==log.date().size());
138    vector<string>::const_iterator author=log.author().begin();
139    for (vector<string>::const_iterator date=log.date().begin();
140         date!=log.date().end(); ++date, ++author) {
141      time_t sec = str2time(*date);
142      tm* timeinfo = gmtime(&sec);
143
144      // find username in map of aliases
145      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
146
147      // if alias exist insert alias
148      if (name != alias.end() && name->first==*author)
149        year_authors[timeinfo->tm_year].insert(name->second);
150      else {
151        // else insert user name
152        Alias a(*author,alias.size());
153        year_authors[timeinfo->tm_year].insert(a);
154        std::cerr << "svndigest: warning: no copyright alias found for `" 
155                  << *author << "`\n";
156        // insert alias to avoid multiple warnings.
157        alias.insert(name, std::make_pair(*author, a));
158      }
159    }
160
161
162    // Code copied from Gnuplot -r70
163    char tmpname[]="/tmp/svndigestXXXXXX";
164    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
165    if (fd == -1)
166      throw std::runtime_error(std::string("Failed to get unique filename: ") +
167                               tmpname);
168    // Jari would like to do something like 'std::ofstream tmp(fd);'
169    // but has to settle for (which is stupid since the file is
170    // already open for writing.
171    std::ofstream tmp(tmpname);
172
173    ifstream is(path().c_str());
174    assert(is.good());
175    string line;
176    bool found_copyright = false;
177    bool after_copyright = false;
178    string prefix;
179    while(getline(is, line)){
180      if (after_copyright) 
181        tmp << line << "\n";
182      else if (found_copyright){
183        // check if line is end of copyright statement, i.e. contains
184        // no alphanumerical character
185        after_copyright = true;
186        for (size_t i=0; i<line.size()&&after_copyright; ++i)
187          if (isalnum(line[i]))
188            after_copyright = false;
189        if (after_copyright)
190          tmp << line << "\n";
191         
192      }
193      else {
194        // check whether copyright starts on this line
195        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
196        if (i==line.end()) {
197          tmp << line << "\n";
198        }     
199        else {
200          prefix = line.substr(0, distance(line.begin(), i));
201          found_copyright = true;
202          // Printing copyright statement
203          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
204               i!=year_authors.end();) {
205          tmp << prefix << "Copyright (C) "
206              << 1900+i->first;
207          map<int, set<Alias> >::const_iterator j = i;
208          assert(i!=year_authors.end());
209          while (++j!=year_authors.end() && 
210                 i->second == j->second){
211            tmp << ", " << 1900+(j->first);
212          }
213          // printing authors
214          std::vector<Alias> vec_alias;
215          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
216          std::copy(i->second.begin(), i->second.end(), ii);
217          // sort with respect to id
218          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
219          for (std::vector<Alias>::iterator a=vec_alias.begin();
220               a!=vec_alias.end(); ++a){
221            if (a!=vec_alias.begin())
222              tmp << ",";
223            tmp << " " << a->name();
224          }
225          tmp << "\n";
226          i = j;
227          }
228        }
229      }
230    }
231    is.close();
232    tmp.close();
233    close(fd);
234    // finally move printed temporary file to original file
235    rename(tmpname, path().c_str());
236   
237  }
238}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.