source: trunk/lib/Directory.cc @ 91

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

Fixes #4, #19, #32, #34: Started to implement subversion API usage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: Directory.cc 91 2006-03-23 22:56:17Z 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
29#include <algorithm>
30#include <fstream>
31#include <functional>
32#include <iostream>
33#include <iterator>
34#include <list>
35
36#include <cerrno> // Needed to check error state below.
37#include <dirent.h>
38#include <sys/stat.h>
39
40namespace theplu{
41namespace svnstat{
42
43
44  Directory::Directory(const std::string& path, const std::string& output)
45    : Node(path,output)
46  {
47    using namespace std;
48    DIR* directory=opendir(path.c_str());    // C API from dirent.h
49    if (!directory)
50      throw NodeException("ERROR: opendir() failed; " + path +
51                          " is not a directory");
52    list<string> entries;
53    struct dirent* entry;
54    errno=0;  // Global variable used by C to track errors, from errno.h
55    while ((entry=readdir(directory)))       // C API from dirent.h
56      entries.push_back(string(entry->d_name));
57    if (errno)
58      throw NodeException("ERROR: readdir() failed on " + path);
59    closedir(directory);
60
61    SVN* svn=SVN::instance();
62    for (list<string>::iterator i=entries.begin(); i!=entries.end(); i++)
63      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
64        string fullpath(path_+'/'+(*i));
65        switch (svn->version_controlled(fullpath)) {
66        case SVN::uptodate:
67          struct stat nodestat;                // C api from sys/stat.h
68          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
69          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
70            daughters_.push_back(new Directory(fullpath, output_name()+"_"));
71          else
72            daughters_.push_back(new File(fullpath,output_name()+"_"));
73          break;
74        case SVN::unresolved:
75          throw NodeException(fullpath+" is not up to date");
76        case SVN::unversioned: ; // do nothing
77        }
78      }
79  }
80
81
82  Directory::~Directory(void)
83  {
84    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
85      delete *i;
86  }
87
88
89  const Stats& Directory::parse(const bool verbose)
90  {
91    stats_.reset();
92
93    // Directories give no contribution to statistics.
94    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
95      stats_ += (*i)->parse(verbose);
96    return stats_;
97  }
98
99
100  void Directory::print(const bool verbose) const
101  {
102    std::string output(output_name() + ".html");
103    if (verbose)
104      std::cout << "Printing output for " << path_ << std::endl;
105    std::ofstream os(output.c_str());
106    print_header(os);
107    os << "<p align=center>\n<img src='" << stats_.plot(output_name()+".png")
108       << "' alt='[plot]' border=0><br>\n";
109    os << "<table>\n";
110    os << "<tr><td><strong>Node</strong></td>\n";
111    os << "<td><strong>Count</strong></td></tr>\n";
112    os << "<tr><td>Total</td>\n";
113    os << "<td align=right>" << stats_.rows() << "</td></tr>\n";
114    // print html links to daughter nodes
115    transform(daughters_.begin(), daughters_.end(), 
116              std::ostream_iterator<std::string>(os," "), 
117              std::mem_fun(&Node::html_tablerow));
118    os << "</table>\n";
119    os << "</p>";
120    print_footer(os);
121    os.close(); 
122
123    // print daughter nodes, i.e, this function is recursive
124    std::for_each(daughters_.begin(), daughters_.end(),
125                  std::bind2nd(std::mem_fun(&Node::print),verbose));
126  }
127
128}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.