source: trunk/lib/Directory.cc @ 126

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

Changed createdir to mkdir, removed the ugly force option from the function.

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