source: trunk/lib/File.cc @ 232

Last change on this file since 232 was 232, checked in by Peter Johansson, 16 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1// $Id: File.cc 232 2007-03-25 22:00:13Z 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 "SVN.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, 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    os << "<table class=\"listings\">\n";
75    os << "<thead>";
76    os << "<tr>\n";
77    os << "<th>Author</th>\n";
78    os << "<th>Lines</th>\n";
79    os << "<th>Code</th>\n";
80    os << "<th>Comments</th>\n";
81    os << "</tr>\n</thead>\n";
82    os << "<tbody>";
83
84    bool dark=false;
85    os << "<tr class=\"light\">\n";
86    os << "<td class=\"directory\" colspan=\"5\">";
87    anchor(os, "index.html", "../");
88    os << "</td>\n</tr>\n";
89    dark=!dark;
90   
91    // print authors
92    for (std::set<std::string>::const_iterator i=stats_.authors().begin();
93         i!=stats_.authors().end(); ++i){
94      if (dark)
95        os << "<tr class=\"dark\"><td>" << *i
96           << "</td><td>" << stats_.lines(*i)
97           << "</td><td>" << stats_.code(*i)
98           << "</td><td>" << stats_.comments(*i)
99           << "</td></tr>\n";
100      else
101        os << "<tr class=\"light\"><td>" << *i
102           << "</td><td>" << stats_.lines(*i)
103           << "</td><td>" << stats_.code(*i)
104           << "</td><td>" << stats_.comments(*i)
105           << "</td></tr>\n";
106      dark=!dark;
107    }
108    if (dark)
109      os << "<tr class=\"dark\">\n";
110    else
111      os << "<tr class=\"light\">\n";
112    os << "<td>Total</td>\n";
113    os << "<td>" << stats_.lines() << "</td>\n";
114    os << "<td>" << stats_.code() << "</td>\n";
115    os << "<td>" << stats_.comments() << "</td>\n";
116    os << "</tr>\n";
117    os << "</table>\n";
118    os << "</p>\n";
119
120    print_footer(os);
121    os.close(); 
122  }
123
124
125  void File::print(const bool verbose) const 
126  {
127    // no output page for binary files
128    if (ignore())
129      return;
130    if (verbose)
131      std::cout << "Printing output for " << path_ << std::endl;
132    print("all", "total");
133    print("all", "code");
134    print("all", "comments");
135    print("all", "empty");
136
137    for (std::set<std::string>::const_iterator i = stats_.authors().begin();
138         i!=stats_.authors().end(); ++i) {
139      print(*i, "total");
140      print(*i, "code");
141      print(*i, "comments");
142      print(*i, "empty");
143    }
144
145  }
146
147
148  void File::print_blame(std::ofstream& os, const std::string line_type) const
149  {
150    os << "<table class=\"blame\">\n";
151
152    os << "</table>\n";
153  }
154
155
156  void File::print_copyright
157  (const std::vector<std::string>& dates,
158   const std::vector<std::string>& authors,
159   std::map<std::string, std::string>& alias) const
160  {
161    if (ignore())
162      return;
163    assert(dates.size()==authors.size());
164    using namespace std;
165    SVN* svn=SVN::instance();
166    vector<size_t> rev=svn->revisions(path());
167
168    typedef map<int, set<string> > Container;
169    Container copyright;
170    for (vector<size_t>::iterator i=rev.begin(); i!=rev.end(); ++i) {
171      assert(*i<dates.size());
172      time_t sec = str2time(dates[*i]);
173      tm* timeinfo = gmtime(&sec);
174
175      // find username in map of aliases
176      map<string, string>::iterator name = alias.lower_bound(authors[*i]);
177      // if alias exist insert alias
178      if (name != alias.end() && name->first==authors[*i])
179        copyright[timeinfo->tm_year].insert(name->second);
180      else {
181        // else insert user name
182        copyright[timeinfo->tm_year].insert(authors[*i]);
183        std::cerr << "Warning: no alias found for `" << authors[*i] << "`\n";
184        // insert alias to avoid multiple warnings.
185        alias.insert(name, std::make_pair(authors[*i], authors[*i]));
186      }
187    }
188   
189    // Code copied from Gnuplot -r70
190    char tmpname[]="/tmp/svndigestXXXXXX";
191    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
192    if (fd == -1)
193      throw std::runtime_error(std::string("Failed to get unique filename: ") +
194                               tmpname);
195    // Jari would like to do something like 'std::ofstream tmp(fd);'
196    // but has to settle for (which is stupid since the file is
197    // already open for writing.
198    std::ofstream tmp(tmpname);
199     
200
201
202
203    ifstream is(path().c_str());
204    string line;
205    bool found_copyright = false;
206    bool after_copyright = false;
207    string prefix;
208    while(getline(is, line)){
209      if (after_copyright) 
210        tmp << line << "\n";
211      else if (found_copyright){
212        // check if line is end of copyright statement, i.e. contains
213        // no alphanumerical character
214        after_copyright = true;
215        for (size_t i=0; i<line.size()&&after_copyright; ++i)
216          if (isalnum(line[i]))
217            after_copyright = false;
218        if (after_copyright)
219          tmp << line << "\n";
220         
221      }
222      else {
223        // check whether copyright starts on this line
224        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
225       
226        if (i==line.end())
227          tmp << line << "\n";
228        else {
229          prefix = line.substr(0, distance(line.begin(), i));
230          found_copyright = true;
231          if (!found_copyright) 
232            tmp << line << "\n";
233          else {
234            // Printing copyright statement
235            for (Container::const_iterator i=copyright.begin();
236                 i!=copyright.end();) {
237              tmp << prefix << "Copyright (C) "
238                   << 1900+i->first;
239              Container::const_iterator j = i;
240              while (++j!=copyright.end() && i->second == j->second){
241                tmp << ", " << 1900+j->first;
242              }
243              // printing authors
244              for (set<string>::iterator a=i->second.begin();
245                   a!=i->second.end(); ++a){
246                if (a!=i->second.begin())
247                  tmp << ",";
248                tmp << " " << *a;
249              }
250              tmp << "\n";
251              i = j;
252            }
253          }
254        }
255      }
256    }
257    is.close();
258    tmp.close();
259    close(fd);
260    // finally move printed temporary file to original file
261    rename(tmpname, path().c_str());
262  }
263}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.