source: trunk/lib/Directory.cc @ 447

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

refs #185

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1// $Id: Directory.cc 447 2007-08-16 16:22:07Z 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/trac/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 u_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    daughters_.sort(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(void) const
113  {
114    // not yet implemented
115    assert(0);
116  }
117
118  std::string Directory::node_type(void) const
119  {
120    return std::string("directory");
121  }
122
123
124  std::string Directory::output_path(void) const
125  {
126    return output_dir()+"index.html";
127  }
128
129  const Stats& Directory::parse(const bool verbose)
130  {
131    stats_.reset();
132    // Directories themselved give no contribution to statistics.
133    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
134      if (!(*i)->ignore())
135        stats_ += (*i)->parse(verbose);
136    return stats_;
137  }
138
139
140  void Directory::print_core(const bool verbose) const
141  {
142    // print daughter nodes, i.e., this function is recursive
143    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
144      (*i)->print(verbose);
145  }
146
147
148  void Directory::print_core(const std::string& user, 
149                             const std::string& line_type,
150                             const SVNlog& log) const
151  {
152
153    std::string outdir = user+"/"+line_type+"/"+local_path_;
154    if (local_path_=="")
155      outdir = user+"/"+line_type;
156
157    mkdir(outdir);
158    std::string imagedir = "images/"+line_type+"/"+local_path_;
159    if (user=="all")
160      mkdir(imagedir);
161
162    std::string html_name = outdir+"/index.html";
163    std::ofstream os(html_name.c_str());
164    assert(os.good());
165    if (local_path().empty())
166      print_header(os, name(), level_+2, user, line_type, "index.html");
167    else
168      print_header(os, name(), level_+2, user, line_type, 
169                   local_path()+"/index.html");
170    path_anchor(os);
171    os << "<p class=\"plot\">\n<img src='"; 
172    for (size_t i=0; i<level_; ++i)
173      os << "../";
174    os << "../../";
175    if (user=="all")
176      os << stats_.plot(imagedir+"/index.png", line_type);
177    else
178      os << imagedir << "/index.png";
179    os << "' alt='[plot]' /></p>\n";
180    os << "<h3>File Summary";
181    if (user!="all")
182      os << " for " << user;
183    os << "</h3>";     
184    os << "<table class=\"listings\">\n";
185    os << "<thead>\n";
186    os << "<tr>\n";
187    os << "<th>Node</th>\n";
188    os << "<th>Lines</th>\n";
189    os << "<th>Code</th>\n";
190    os << "<th>Comments</th>\n";
191    os << "<th>Other</th>\n";
192    os << "<th>Revision</th>\n";
193    os << "<th>Author</th>\n";
194    os << "</tr>\n</thead>\n";
195    os << "<tbody>\n";
196
197    std::string color("light");
198    if (level_){
199      os << "<tr class=\"light\">\n";
200      os << "<td class=\"directory\" colspan=\"7\">";
201      os << anchor("../index.html", "../");
202      os << "</td>\n</tr>\n";
203      color = "dark";
204    }
205
206    // print html links to daughter nodes
207    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
208      (*d)->html_tablerow(os,color, user);
209      if (color=="dark")
210        color = "light";
211      else
212        color = "dark";
213    }
214    os << "<tr class=\"" << color << "\">\n";
215    os << "<td>Total</td>\n";
216    if (user=="all"){
217      os << "<td>" << stats_.lines() << "</td>\n";
218      os << "<td>" << stats_.code() << "</td>\n";
219      os << "<td>" << stats_.comments() << "</td>\n";
220      os << "<td>" << stats_.empty() << "</td>\n";
221    }
222    else {
223      os << "<td>" << stats_.lines(user); 
224      if (stats_.lines(user)) 
225        os << " (" << percent(stats_.lines(user),stats_.lines()) << "%)"; 
226      os << "</td>\n";
227      os << "<td>" << stats_.code(user); 
228      if (stats_.code(user)) 
229        os << " (" << percent(stats_.code(user),stats_.code()) << "%)"; 
230      os << "</td>\n";
231      os << "<td>" << stats_.comments(user); 
232      if (stats_.comments(user)) 
233        os << " (" << percent(stats_.comments(user),stats_.comments()) << "%)"; 
234      os << "</td>\n";
235      os << "<td>" << stats_.empty(user); 
236      if (stats_.empty(user)) 
237        os << " (" << percent(stats_.empty(user),stats_.empty()) << "%)"; 
238      os << "</td>\n";
239    }
240    os << "<td>" << trac_revision(stats_.last_changed_rev()) << "</td>\n";
241    os << "<td>" << author() << "</td>\n";
242    os << "</tr>\n";
243    os << "</tbody>\n";
244    os << "</table>\n";
245    print_author_summary(os, line_type, log);
246    os << "\n";
247    print_footer(os);
248    os.close(); 
249  }
250
251
252  void Directory::print_copyright(std::map<std::string, Alias>& alias, 
253                                  bool verbose) const {
254    if (!ignore()){
255      // print daughter nodes, i.e, this function is recursive
256      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
257        (*i)->print_copyright(alias, verbose);
258    }
259  }
260
261}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.