source: trunk/lib/Directory.cc @ 225

Last change on this file since 225 was 225, checked in by Peter Johansson, 15 years ago

copyright based on log is printed to cout refs #36

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