source: trunk/lib/Directory.cc @ 176

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

fixes #12 binary files are now printed with note without anchor. Binary files and directories all counted as zero lines.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1// $Id: Directory.cc 176 2006-09-02 04:17:28Z 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 "Node.h"
27#include "SVN.h"
28#include "utility.h"
29
30#include <algorithm>
31#include <fstream>
32#include <functional>
33#include <iostream>
34#include <iterator>
35#include <list>
36
37#include <cerrno> // Needed to check error state below.
38#include <dirent.h>
39#include <sys/stat.h>
40
41namespace theplu{
42namespace svndigest{
43
44
45  Directory::Directory(const u_int level, const std::string& path, 
46                       const std::string& output)
47    : Node(level,path,output)
48  {
49    using namespace std;
50    DIR* directory=opendir(path.c_str());    // C API from dirent.h
51    if (!directory)
52      throw NodeException("ERROR: opendir() failed; " + path +
53                          " is not a directory");
54    list<string> entries;
55    struct dirent* entry;
56    errno=0;  // Global variable used by C to track errors, from errno.h
57    while ((entry=readdir(directory)))       // C API from dirent.h
58      entries.push_back(string(entry->d_name));
59    if (errno)
60      throw NodeException("ERROR: readdir() failed on " + path);
61    closedir(directory);
62
63    SVN* svn=SVN::instance();
64    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
65      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
66        string fullpath(path_+'/'+(*i));
67        switch (svn->version_controlled(fullpath)) {
68        case SVN::uptodate:
69          struct stat nodestat;                // C api from sys/stat.h
70          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
71          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
72            daughters_.push_back(new Directory(level_+1,fullpath, 
73                                               output_name()+"/"));
74          else
75            daughters_.push_back(new File(level_,fullpath,output_name()+"/"));
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::binary(void) const
93  {
94    return false;
95  }
96
97  bool Directory::dir(void) const
98  {
99    return true;
100  }
101
102  std::string Directory::href(void) const
103  { 
104    return name() + "/index.html";
105  }
106
107  const Stats& Directory::parse(const bool verbose)
108  {
109    stats_.reset();
110
111    // Directories give no contribution to statistics.
112    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
113      stats_ += (*i)->parse(verbose);
114    return stats_;
115  }
116
117
118  void Directory::print(const bool verbose) const
119  {
120    mkdir(output_name());
121    std::string output(output_name() + "/index.html");
122    if (verbose)
123      std::cout << "Printing output for " << path_ << std::endl;
124    std::ofstream os(output.c_str());
125    print_header(os);
126    os << "<p align=center>\n<img src='" 
127       << file_name(stats_.plot(output_name()+"/index.png", output_name()))
128       << "' alt='[plot]' border=0><br>\n";
129    os << "<table class=\"listings\">\n";
130    os << "<thead>";
131    os << "<tr>\n";
132    os << "<th>Node</th>\n";
133    os << "<th>Lines</th>\n";
134    os << "<th>Code</th>\n";
135    os << "<th>Comments</th>\n";
136    os << "<th>Revision</th>\n";
137    os << "<th>Author</th>\n";
138    os << "</tr>\n</thead>\n";
139    os << "<tbody>";
140
141    bool dark=false;
142    if (level_){
143      os << "<tr class=\"light\">\n";
144      os << "<td colspan=\"6\"><a href=\"../index.html\">../</a></td>\n";
145      os << "</tr>\n";
146      dark=!dark;
147    }
148
149    // print html links to daughter nodes
150    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
151      if (dark)
152        (*d)->html_tablerow(os,"dark");
153      else
154        (*d)->html_tablerow(os,"light");
155      dark = !dark;
156    }
157    if (dark)
158      os << "<tr class=\"dark\">\n";
159    else
160      os << "<tr class=\"light\">\n";
161    os << "<td>Total</td>\n";
162    os << "<td>" << stats_.lines() << "</td>\n";
163    os << "<td>" << stats_.code() << "</td>\n";
164    os << "<td>" << stats_.comments() << "</td>\n";
165    os << "<td>" << stats_.last_changed_rev() << "</td>\n";
166    os << "<td>" << author() << "</td>\n";
167    os << "</tr>\n";
168    os << "</table>\n";
169    os << "</p>\n";
170    print_footer(os);
171    os.close(); 
172
173    // print daughter nodes, i.e, this function is recursive
174    std::for_each(daughters_.begin(), daughters_.end(),
175                  std::bind2nd(std::mem_fun(&Node::print),verbose));
176  }
177
178}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.