source: trunk/lib/Directory.cc @ 234

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

fixes #87 and #95 and #103 and #104 and #80 and #108, and refs #69

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