source: trunk/lib/Directory.cc @ 213

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

fixes #41 and refs #87 added output for individual developers. Links should be added from the main page.

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