source: trunk/lib/File.cc @ 307

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

Never implement ctor in header file (latin proverb)

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