source: trunk/lib/Directory.cc @ 210

Last change on this file since 210 was 210, checked in by Jari Häkkinen, 15 years ago

Added another #include <cassert>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1// $Id: Directory.cc 210 2006-09-12 12:04:50Z jari $
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    // print daughter nodes, i.e., this function is recursive
132    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
133      (*i)->print(verbose);
134  }
135
136
137  void Directory::print(const std::string& user, 
138                        const std::string& line_type) const
139  {
140    std::string outdir = user+"/"+line_type+"/"+local_path_;
141    if (local_path_=="")
142      outdir = user+"/"+line_type;
143
144    mkdir(outdir);
145    std::string html_name = outdir+"/index.html";
146    std::ofstream os(html_name.c_str());
147    assert(os.good());
148    print_header(os, name(), level_+2);
149    path_anchor(os);
150    os << "<p align=center>\n<img src='" 
151       << file_name(stats_.plot(outdir+"/index.png", local_path(),line_type))
152       << "' alt='[plot]' border=0><br>\n";
153    os << "<table class=\"listings\">\n";
154    os << "<thead>";
155    os << "<tr>\n";
156    os << "<th>Node</th>\n";
157    os << "<th>Lines</th>\n";
158    os << "<th>Code</th>\n";
159    os << "<th>Comments</th>\n";
160    os << "<th>Empty</th>\n";
161    os << "<th>Revision</th>\n";
162    os << "<th>Author</th>\n";
163    os << "</tr>\n</thead>\n";
164    os << "<tbody>";
165
166    bool dark=false;
167    if (level_){
168      os << "<tr class=\"light\">\n";
169      os << "<td class=\"directory\" colspan=\"6\">";
170      anchor(os, "../index.html", "../");
171      os << "</td>\n</tr>\n";
172      dark=!dark;
173    }
174
175    // print html links to daughter nodes
176    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
177      if (dark)
178        (*d)->html_tablerow(os,"dark");
179      else
180        (*d)->html_tablerow(os,"light");
181      dark = !dark;
182    }
183    if (dark)
184      os << "<tr class=\"dark\">\n";
185    else
186      os << "<tr class=\"light\">\n";
187    os << "<td>Total</td>\n";
188    os << "<td>" << stats_.lines() << "</td>\n";
189    os << "<td>" << stats_.code() << "</td>\n";
190    os << "<td>" << stats_.comments() << "</td>\n";
191    os << "<td>" << stats_.empty() << "</td>\n";
192    os << "<td>" << stats_.last_changed_rev() << "</td>\n";
193    os << "<td>" << author() << "</td>\n";
194    os << "</tr>\n";
195    os << "</table>\n";
196    os << "</p>\n";
197    print_footer(os);
198    os.close(); 
199
200
201
202  }
203
204
205  void Directory::print_copyright(const std::vector<std::string>& dates) const
206  {
207    if (!ignore()){
208      // print daughter nodes, i.e, this function is recursive
209      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
210        (*i)->print_copyright(dates);
211    }
212  }
213
214}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.