source: trunk/lib/Directory.cc @ 151

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

Changed svnstat to svndigest.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1// $Id: Directory.cc 149 2006-08-12 09:11:46Z 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 "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::dir(void) const
93  {
94    return true;
95  }
96
97  std::string Directory::html_link(void) const
98  { 
99    return "<a href=\"" + name() + "/index.html\">" + name() + "</a>"; 
100  }
101
102  const Stats& Directory::parse(const bool verbose)
103  {
104    stats_.reset();
105
106    // Directories give no contribution to statistics.
107    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
108      stats_ += (*i)->parse(verbose);
109    return stats_;
110  }
111
112
113  void Directory::print(const bool verbose) const
114  {
115    mkdir(output_name());
116    std::string output(output_name() + "/index.html");
117    if (verbose)
118      std::cout << "Printing output for " << path_ << std::endl;
119    std::ofstream os(output.c_str());
120    print_header(os);
121    os << "<p align=center>\n<img src='" 
122       << file_name(stats_.plot(output_name()+"/index.png", output_name()))
123       << "' alt='[plot]' border=0><br>\n";
124    os << "<table class=\"listings\">\n";
125    os << "<thead>";
126    os << "<tr>\n";
127    os << "<th>Node</th>\n";
128    os << "<th>Lines</th>\n";
129    os << "<th>Code</th>\n";
130    os << "<th>Comments</th>\n";
131    os << "<th>Revision</th>\n";
132    os << "<th>Author</th>\n";
133    os << "</tr>\n</thead>\n";
134    os << "<tbody>";
135
136    bool dark=false;
137    if (level_){
138      os << "<tr class=\"light\">\n";
139      os << "<td colspan=\"6\"><a href=\"../index.html\">../</a></td>\n";
140      os << "</tr>\n";
141      dark=!dark;
142    }
143
144    // print html links to daughter nodes
145    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); d++){
146      if (dark)
147        os << (*d)->html_tablerow("dark");
148      else
149        os << (*d)->html_tablerow("light");
150      dark = !dark;
151    }
152    if (dark)
153      os << "<tr class=\"dark\">\n";
154    else
155      os << "<tr class=\"light\">\n";
156    os << "<td>Total</td>\n";
157    os << "<td>" << stats_.lines() << "</td>\n";
158    os << "<td>" << stats_.code() << "</td>\n";
159    os << "<td>" << stats_.comments() << "</td>\n";
160    os << "<td>" << stats_.last_changed_rev() << "</td>\n";
161    os << "<td>" << author() << "</td>\n";
162    os << "</tr>\n";
163    os << "</table>\n";
164    os << "</p>\n";
165    print_footer(os);
166    os.close(); 
167
168    // print daughter nodes, i.e, this function is recursive
169    std::for_each(daughters_.begin(), daughters_.end(),
170                  std::bind2nd(std::mem_fun(&Node::print),verbose));
171  }
172
173}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.