source: trunk/lib/Directory.cc @ 65

Last change on this file since 65 was 65, checked in by Peter Johansson, 18 years ago

corrected includes

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