source: trunk/lib/File.cc @ 234

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

fixes #87 and #95 and #103 and #104 and #80 and #108, and refs #69

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1// $Id: File.cc 234 2007-04-09 12:08:26Z 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#include "html_utility.h"
27#include "Stats.h"
28#include "SVNlog.h"
29
30#include <cassert>
31#include <ctime>
32#include <fstream>
33#include <iostream>
34#include <map>
35#include <string>
36
37namespace theplu{
38namespace svndigest{
39
40
41  std::string File::href(void) const
42  { 
43    return name()+".html"; 
44  }
45
46
47  const std::string File::node_type(void) const
48  {
49    return std::string("file");
50  }
51
52
53  const Stats& File::parse(const bool verbose)
54  {
55    if (verbose)
56      std::cout << "Parsing " << path_ << std::endl; 
57    stats_.reset();
58    stats_.parse(path_);
59    return stats_;
60  }
61
62
63  void File::print(const std::string& user, const std::string& line_type) const 
64  {
65    std::string outpath = user+"/"+line_type+"/"+local_path();
66    std::string html_name(outpath + ".html");
67    std::ofstream os(html_name.c_str());
68    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
69    path_anchor(os);
70    os << "<p align=center>\n<img src='" 
71       << file_name(stats_.plot(outpath+".png",local_path(),line_type))
72       << "' alt='[plot]' border=0>\n</p>";
73
74    print_author_summary(os);
75    os << "</p>\n";
76
77    print_footer(os);
78    os.close(); 
79  }
80
81
82  void File::print(const bool verbose) const 
83  {
84    // no output page for binary files
85    if (ignore())
86      return;
87    if (verbose)
88      std::cout << "Printing output for " << path_ << std::endl;
89    print("all", "total");
90    print("all", "code");
91    print("all", "comments");
92    print("all", "empty");
93
94    for (std::set<std::string>::const_iterator i = stats_.authors().begin();
95         i!=stats_.authors().end(); ++i) {
96      print(*i, "total");
97      print(*i, "code");
98      print(*i, "comments");
99      print(*i, "empty");
100    }
101
102  }
103
104
105  void File::print_blame(std::ofstream& os, const std::string line_type) const
106  {
107    os << "<table class=\"blame\">\n";
108
109    os << "</table>\n";
110  }
111
112
113  void File::print_copyright (std::map<std::string,std::string>& alias) const{
114    if (ignore())
115      return;
116    using namespace std;
117
118    SVNlog log(path());
119
120    typedef map<int, set<string> > Container;
121    Container copyright;
122
123   
124    assert(log.author().size()==log.date().size());
125    vector<string>::const_iterator author=log.author().begin();
126    for (vector<string>::const_iterator date=log.date().begin();
127         date!=log.date().end(); ++date, ++author) {
128      time_t sec = str2time(*date);
129      tm* timeinfo = gmtime(&sec);
130
131      // find username in map of aliases
132      map<string, string>::iterator name = alias.lower_bound(*author);
133      // if alias exist insert alias
134      if (name != alias.end() && name->first==*author)
135        copyright[timeinfo->tm_year].insert(name->second);
136      else {
137        // else insert user name
138        copyright[timeinfo->tm_year].insert(*author);
139        std::cerr << "Warning: no alias found for `" << *author << "`\n";
140        // insert alias to avoid multiple warnings.
141        alias.insert(name, std::make_pair(*author, *author));
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 (Container::const_iterator i=copyright.begin();
188               i!=copyright.end();) {
189            tmp << prefix << "Copyright (C) "
190                << 1900+i->first;
191            Container::const_iterator j = i;
192            assert(i!=copyright.end());
193            while (++j!=copyright.end() && i->second == j->second){
194              tmp << ", " << 1900+(j->first);
195            }
196            // printing authors
197            for (set<string>::iterator a=i->second.begin();
198                 a!=i->second.end(); ++a){
199              if (a!=i->second.begin())
200                tmp << ",";
201              tmp << " " << *a;
202            }
203            tmp << "\n";
204            i = j;
205          }
206        }
207      }
208    }
209    is.close();
210    tmp.close();
211    close(fd);
212    // finally move printed temporary file to original file
213    rename(tmpname, path().c_str());
214  }
215}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.