source: trunk/lib/Directory.cc @ 74

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

Improved web presentation. Y axises normalized, ticket ticket:15 resolved.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: Directory.cc 74 2006-03-07 15:46:59Z jari $
2
3#include "Directory.h"
4#include "File.h"
5#include "Node.h"
6
7#include <algorithm>
8#include <fstream>
9#include <functional>
10#include <iostream>
11#include <iterator>
12#include <list>
13
14#include <cerrno> // Needed to check error state below.
15#include <dirent.h>
16#include <sys/stat.h>
17
18namespace theplu{
19namespace svnstat{
20
21  Directory::Directory(const std::string& path, const std::string& output)
22    : Node(path,output)
23  {
24    using namespace std;
25    DIR* directory=opendir(path.c_str());    // C API from dirent.h
26    if (!directory) {
27      // Jari, throw exception
28      cerr << "ERROR: opendir() failed; terminating" << endl;
29      exit(1);
30    }
31    list<string> entries;
32    struct dirent* entry;
33    errno=0;  // Global variable used by C to track errors, from errno.h
34    while ((entry=readdir(directory)))       // C API from dirent.h
35      entries.push_back(string(entry->d_name));
36    if (errno) {
37      // Jari, throw exception
38      cerr << "ERROR: readdir() failure; terminating" << endl;
39      exit(1);
40    }
41    closedir(directory);
42
43    // Jari, change this to some STL algo?
44    for (list<string>::iterator i=entries.begin(); i!=entries.end(); i++)
45      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
46        string fullpath(path_+'/'+(*i));
47        struct stat nodestat;                // C api from sys/stat.h
48        lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
49        if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
50          daughters_.push_back(new Directory(fullpath, output_name()+"_"));
51        else
52          daughters_.push_back(new File(fullpath,output_name()+"_"));
53      }
54  }
55
56
57  Directory::~Directory(void)
58  {
59    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
60      delete *i;
61  }
62
63
64  const Stats& Directory::parse(const bool verbose)
65  {
66    stats_.reset();
67
68    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
69      stats_ += (*i)->parse(verbose);
70    return stats_;
71  }
72
73
74  void Directory::print(const bool verbose) const
75  {
76    std::string output(output_name() + ".html");
77    if (verbose)
78      std::cout << "Printing output for " << path_ << std::endl;
79    std::ofstream os(output.c_str());
80    print_header(os);
81    os << "<p align=center>\n<img src='" << stats_.plot(output_name()+".png")
82       << "' alt='[plot]' border=0><br>\n";
83    os << "<table>\n";
84    os << "<tr><td><strong>Node</strong></td>\n";
85    os << "<td><strong>Count</strong></td></tr>\n";
86    os << "<tr><td>Total</td>\n";
87    os << "<td align=right>" << stats_.rows() << "</td></tr>\n";
88    // print html links to daughter nodes
89    transform(daughters_.begin(), daughters_.end(), 
90              std::ostream_iterator<std::string>(os," "), 
91              std::mem_fun(&Node::html_tablerow));
92    os << "</table>\n";
93    os << "</p>";
94    print_footer(os);
95    os.close(); 
96
97    // print daughter nodes, i.e, this function is recursive
98    for (std::list<Node*>::const_iterator i=daughters_.begin();
99         i!=daughters_.end(); i++)
100      (*i)->print(verbose);
101  }
102
103  void Directory::purge(const bool verbose) 
104  {
105    if (verbose)
106      std::cout << "Purging " << path_ << std::endl;
107    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
108      if (!((*i)->subversion_controlled())) {
109        delete *i;
110        i=daughters_.erase(i);
111      }
112      else {
113        (*i)->purge(verbose);
114        i++;
115      }
116  }
117
118}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.