source: trunk/lib/Directory.cc @ 209

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

fixes #44 parallel page with code, comment, or empty lines.

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