source: trunk/lib/Directory.cc @ 658

Last change on this file since 658 was 658, checked in by Peter Johansson, 13 years ago

using vector<Node*> to store daughters in Directory. For historical reasons a link was used before.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1// $Id: Directory.cc 658 2008-06-10 22:54:42Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Directory.h"
25
26#include "Alias.h"
27#include "File.h"
28#include "html_utility.h"
29#include "Node.h"
30#include "SVN.h"
31#include "SVNlog.h"
32#include "utility.h"
33
34#include <algorithm>
35#include <cassert>
36#include <fstream>
37#include <functional>
38#include <iostream>
39#include <iterator>
40#include <list>
41#include <map>
42
43#include <cerrno> // Needed to check error state below.
44#include <dirent.h>
45#include <sys/stat.h>
46
47namespace theplu{
48namespace svndigest{
49
50
51  Directory::Directory(const unsigned int level, const std::string& path, 
52                       const std::string& output)
53    : Node(level,path,output)
54  {
55    output_dir_=local_path();
56    if (!output_dir_.empty())
57      output_dir_+='/';
58
59    using namespace std;
60    DIR* directory=opendir(path.c_str());    // C API from dirent.h
61    if (!directory)
62      throw NodeException("ERROR: opendir() failed; " + path +
63                          " is not a directory");
64    list<string> entries;
65    struct dirent* entry;
66    errno=0;  // Global variable used by C to track errors, from errno.h
67    while ((entry=readdir(directory)))       // C API from dirent.h
68      entries.push_back(string(entry->d_name));
69    if (errno)
70      throw NodeException("ERROR: readdir() failed on " + path);
71    closedir(directory);
72
73    SVN* svn=SVN::instance();
74    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
75      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
76        string fullpath(path_+'/'+(*i));
77        switch (svn->version_controlled(fullpath)) {
78        case SVN::uptodate:
79          struct stat nodestat;                // C api from sys/stat.h
80          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
81          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
82            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
83          else
84            daughters_.push_back(new File(level_,fullpath,local_path()));
85          break;
86        case SVN::unresolved:
87          throw NodeException(fullpath+" is not up to date");
88        case SVN::unversioned: ; // do nothing
89        }
90      }
91    std::sort(daughters_.begin(), daughters_.end(), NodePtrLess());
92  }
93
94
95  Directory::~Directory(void)
96  {
97    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
98      delete *i;
99  }
100
101  bool Directory::dir(void) const
102  {
103    return true;
104  }
105
106  std::string Directory::href(void) const
107  { 
108    return name() + "/index.html";
109  }
110
111
112  SVNlog Directory::log_core(void) const
113  {
114    SVNlog log(path());
115    for (NodeConstIterator i(daughters_.begin()); i != daughters_.end(); ++i)
116      log += (*i)->log();
117    return log;
118  }
119
120  std::string Directory::node_type(void) const
121  {
122    return std::string("directory");
123  }
124
125
126  std::string Directory::output_path(void) const
127  {
128    return output_dir()+"index.html";
129  }
130
131  const StatsCollection& Directory::parse(bool verbose, bool ignore)
132  {
133    stats_.reset();
134    // Directories themselved give no contribution to statistics.
135    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
136      if (!(*i)->ignore())
137        stats_ += (*i)->parse(verbose, ignore);
138    return stats_;
139  }
140
141
142  void Directory::print_core(const bool verbose) const
143  {
144    // print daughter nodes, i.e., this function is recursive
145    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
146      (*i)->print(verbose);
147  }
148
149
150  void Directory::print_core(const std::string& stats_type,
151                             const std::string& user, 
152                             const std::string& line_type,
153                             const SVNlog& log) const
154  {
155
156    const Stats& stats = stats_[stats_type];
157    std::string outdir = stats_type+"/"+user+"/"+line_type+"/"+local_path_;
158    if (local_path_=="")
159      outdir = stats_type+"/"+user+"/"+line_type;
160
161    mkdir(outdir);
162    std::string imagedir = stats_type+"/"+"images/"+line_type+"/"+local_path_;
163    if (user=="all")
164      mkdir(imagedir);
165    std::string html_name = outdir+"/index.html";
166    std::ofstream os(html_name.c_str());
167    assert(os.good());
168    if (local_path().empty())
169      print_header(os, name(), level_+3, user, line_type, "index.html", 
170                   stats_type);
171    else
172      print_header(os, name(), level_+3, user, line_type, 
173                   local_path()+"/index.html", stats_type);
174    path_anchor(os);
175    os << "<p class=\"plot\">\n<img src='"; 
176    for (size_t i=0; i<level_; ++i)
177      os << "../";
178    os << "../../../";
179    if (user=="all")
180      os << stats.plot(imagedir+"/index.png", line_type);
181    else
182      os << imagedir << "/index.png";
183    os << "' alt='[plot]' /></p>\n";
184    os << "<h3>File Summary";
185    if (user!="all")
186      os << " for " << user;
187    os << "</h3>";     
188    os << "<table class=\"listings\">\n";
189    os << "<thead>\n";
190    os << "<tr>\n";
191    os << "<th>Node</th>\n";
192    os << "<th>Lines</th>\n";
193    os << "<th>Code</th>\n";
194    os << "<th>Comments</th>\n";
195    os << "<th>Other</th>\n";
196    os << "<th>Revision</th>\n";
197    os << "<th>Author</th>\n";
198    os << "</tr>\n</thead>\n";
199    os << "<tbody>\n";
200
201    std::string color("light");
202    if (level_){
203      os << "<tr class=\"light\">\n";
204      os << "<td class=\"directory\" colspan=\"7\">";
205      os << anchor("../index.html", "../");
206      os << "</td>\n</tr>\n";
207      color = "dark";
208    }
209
210    // print html links to daughter nodes
211    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
212      (*d)->html_tablerow(os,stats_type, color, user);
213      if (color=="dark")
214        color = "light";
215      else
216        color = "dark";
217    }
218    os << "<tr class=\"" << color << "\">\n";
219    os << "<td>Total</td>\n";
220    if (user=="all"){
221      os << "<td>" << stats.lines() << "</td>\n";
222      os << "<td>" << stats.code() << "</td>\n";
223      os << "<td>" << stats.comments() << "</td>\n";
224      os << "<td>" << stats.empty() << "</td>\n";
225    }
226    else {
227      os << "<td>" << stats.lines(user); 
228      if (stats.lines(user)) 
229        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
230      os << "</td>\n";
231      os << "<td>" << stats.code(user); 
232      if (stats.code(user)) 
233        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
234      os << "</td>\n";
235      os << "<td>" << stats.comments(user); 
236      if (stats.comments(user)) 
237        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
238      os << "</td>\n";
239      os << "<td>" << stats.empty(user); 
240      if (stats.empty(user)) 
241        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
242      os << "</td>\n";
243    }
244    os << "<td>" << trac_revision(stats.last_changed_rev()) << "</td>\n";
245    os << "<td>" << author() << "</td>\n";
246    os << "</tr>\n";
247    os << "</tbody>\n";
248    os << "</table>\n";
249    print_author_summary(os, stats, line_type, log);
250    os << "\n";
251    print_footer(os);
252    os.close(); 
253  }
254
255
256  void Directory::print_copyright(std::map<std::string, Alias>& alias, 
257                                  bool verbose,
258                                  const std::map<int,svn_revnum_t>& y2r) const {
259    if (!ignore()){
260      // print daughter nodes, i.e, this function is recursive
261      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
262        (*i)->print_copyright(alias, verbose, y2r);
263    }
264  }
265
266}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.