source: trunk/lib/File.cc @ 258

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

moving some printing code to base class

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