source: trunk/lib/Directory.cc @ 73

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

Divided Gnuplot class into a basic gnuplot communication class
and a front end class specialized for svnstat.
Added option to plot stats against time or revision.
Removed gnuplot usage of temporary files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: Directory.cc 73 2006-03-04 18:10:07Z 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 << std::endl;
82    stats_.print(os,output_name()+".png");
83    os << std::endl;
84
85    // print html links to daughter nodes
86    transform(daughters_.begin(), daughters_.end(), 
87              std::ostream_iterator<std::string>(os,"\n"), 
88              std::mem_fun(&Node::html_link));
89
90    print_footer(os);
91    os.close(); 
92
93    // print daughter nodes, i.e, this function is recursive
94    for (std::list<Node*>::const_iterator i=daughters_.begin();
95         i!=daughters_.end(); i++)
96      (*i)->print(verbose);
97  }
98
99  void Directory::purge(const bool verbose) 
100  {
101    if (verbose)
102      std::cout << "Purging " << path_ << std::endl;
103    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
104      if (!((*i)->subversion_controlled())) {
105        delete *i;
106        i=daughters_.erase(i);
107      }
108      else {
109        (*i)->purge(verbose);
110        i++;
111      }
112  }
113
114}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.