source: trunk/lib/Directory.cc @ 343

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

refs #169

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1// $Id: Directory.cc 343 2007-05-19 13:58:19Z 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 "Directory.h"
26
27#include "Alias.h"
28#include "File.h"
29#include "html_utility.h"
30#include "Node.h"
31#include "SVN.h"
32#include "SVNlog.h"
33#include "utility.h"
34
35#include <algorithm>
36#include <cassert>
37#include <fstream>
38#include <functional>
39#include <iostream>
40#include <iterator>
41#include <list>
42#include <map>
43
44#include <cerrno> // Needed to check error state below.
45#include <dirent.h>
46#include <sys/stat.h>
47
48namespace theplu{
49namespace svndigest{
50
51
52  Directory::Directory(const u_int level, const std::string& path, 
53                       const std::string& output)
54    : Node(level,path,output)
55  {
56    output_dir_=local_path();
57    if (!output_dir_.empty())
58      output_dir_+="/";
59
60    using namespace std;
61    DIR* directory=opendir(path.c_str());    // C API from dirent.h
62    if (!directory)
63      throw NodeException("ERROR: opendir() failed; " + path +
64                          " is not a directory");
65    list<string> entries;
66    struct dirent* entry;
67    errno=0;  // Global variable used by C to track errors, from errno.h
68    while ((entry=readdir(directory)))       // C API from dirent.h
69      entries.push_back(string(entry->d_name));
70    if (errno)
71      throw NodeException("ERROR: readdir() failed on " + path);
72    closedir(directory);
73
74    SVN* svn=SVN::instance();
75    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
76      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
77        string fullpath(path_+'/'+(*i));
78        switch (svn->version_controlled(fullpath)) {
79        case SVN::uptodate:
80          struct stat nodestat;                // C api from sys/stat.h
81          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
82          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
83            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
84          else
85            daughters_.push_back(new File(level_,fullpath,local_path()));
86          break;
87        case SVN::unresolved:
88          throw NodeException(fullpath+" is not up to date");
89        case SVN::unversioned: ; // do nothing
90        }
91      }
92    daughters_.sort(NodePtrLess());
93  }
94
95
96  Directory::~Directory(void)
97  {
98    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
99      delete *i;
100  }
101
102  bool Directory::dir(void) const
103  {
104    return true;
105  }
106
107  std::string Directory::href(void) const
108  { 
109    return name() + "/index.html";
110  }
111
112
113  std::string Directory::node_type(void) const
114  {
115    return std::string("file");
116  }
117
118
119  std::string Directory::output_path(void) const
120  {
121    return output_dir()+"index.html";
122  }
123
124  const Stats& Directory::parse(const bool verbose)
125  {
126    stats_.reset();
127    // Directories themselved give no contribution to statistics.
128    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
129      if (!(*i)->ignore())
130        stats_ += (*i)->parse(verbose);
131    return stats_;
132  }
133
134
135  void Directory::print_core(const bool verbose) const
136  {
137    // print daughter nodes, i.e., this function is recursive
138    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
139      (*i)->print(verbose);
140  }
141
142
143  void Directory::print_core(const std::string& user, 
144                             const std::string& line_type,
145                             const SVNlog& log) const
146  {
147
148    std::string outdir = user+"/"+line_type+"/"+local_path_;
149    if (local_path_=="")
150      outdir = user+"/"+line_type;
151
152    mkdir(outdir);
153    std::string imagedir = "images/"+line_type+"/"+local_path_;
154    if (user=="all")
155      mkdir(imagedir);
156
157    std::string html_name = outdir+"/index.html";
158    std::ofstream os(html_name.c_str());
159    assert(os.good());
160    if (local_path().empty())
161      print_header(os, name(), level_+2, user, line_type, "index.html");
162    else
163      print_header(os, name(), level_+2, user, line_type, 
164                   local_path()+"/index.html");
165    path_anchor(os);
166    os << "<p align=center>\n<img src='"; 
167    for (size_t i=0; i<level_; ++i)
168      os << "../";
169    os << "../../";
170    if (user=="all")
171      os << stats_.plot(imagedir+"/index.png", line_type);
172    else
173      os << imagedir << "/index.png";
174    os << "' alt='[plot]' border=0><br>\n";
175    os << "<h3>File Summary";
176    if (user!="all")
177      os << " for " << user;
178    os << "</h3>";     
179    os << "<table class=\"listings\">\n";
180    os << "<thead>";
181    os << "<tr>\n";
182    os << "<th>Node</th>\n";
183    os << "<th>Lines</th>\n";
184    os << "<th>Code</th>\n";
185    os << "<th>Comments</th>\n";
186    os << "<th>Other</th>\n";
187    os << "<th>Revision</th>\n";
188    os << "<th>Author</th>\n";
189    os << "</tr>\n</thead>\n";
190    os << "<tbody>";
191
192    std::string color("light");
193    if (level_){
194      os << "<tr class=\"light\">\n";
195      os << "<td class=\"directory\" colspan=\"6\">";
196      os << anchor("../index.html", "../");
197      os << "</td>\n</tr>\n";
198      color = "dark";
199    }
200
201    // print html links to daughter nodes
202    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
203      (*d)->html_tablerow(os,color, user);
204      if (color=="dark")
205        color = "light";
206      else
207        color = "dark";
208    }
209    os << "<tr class=\"" << color << "\">\n";
210    os << "<td>Total</td>\n";
211    if (user=="all"){
212      os << "<td>" << stats_.lines() << "</td>\n";
213      os << "<td>" << stats_.code() << "</td>\n";
214      os << "<td>" << stats_.comments() << "</td>\n";
215      os << "<td>" << stats_.empty() << "</td>\n";
216    }
217    else {
218      os << "<td>" << stats_.lines(user); 
219      if (stats_.lines(user)) 
220        os << " (" << percent(stats_.lines(user),stats_.lines()) << "%)"; 
221      os << "</td>\n";
222      os << "<td>" << stats_.code(user); 
223      if (stats_.code(user)) 
224        os << " (" << percent(stats_.code(user),stats_.code()) << "%)"; 
225      os << "</td>\n";
226      os << "<td>" << stats_.comments(user); 
227      if (stats_.comments(user)) 
228        os << " (" << percent(stats_.comments(user),stats_.comments()) << "%)"; 
229      os << "</td>\n";
230      os << "<td>" << stats_.empty(user); 
231      if (stats_.empty(user)) 
232        os << " (" << percent(stats_.empty(user),stats_.empty()) << "%)"; 
233      os << "</td>\n";
234    }
235    os << "<td>" << trac_revision(stats_.last_changed_rev()) << "</td>\n";
236    os << "<td>" << author() << "</td>\n";
237    os << "</tr>\n";
238    os << "</table>\n";
239    print_author_summary(os, line_type, log);
240    os << "</p>\n";
241    print_footer(os);
242    os.close(); 
243  }
244
245
246  void Directory::print_copyright(std::map<std::string, Alias>& alias) const {
247    if (!ignore()){
248      // print daughter nodes, i.e, this function is recursive
249      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
250        (*i)->print_copyright(alias);
251    }
252  }
253
254}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.