source: trunk/lib/Directory.cc @ 71

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

class goes to typename, hi hi

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: Directory.cc 71 2006-02-23 09:25:41Z 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    // Peter, use for_each
89    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++){
90      (*i)->print_link(os);
91      os << "<br/>" << std::endl;
92    }
93
94    print_footer(os);
95    os.close(); 
96
97    // Peter, use for_each
98    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++) 
99      (*i)->print(verbose);
100  }
101
102  void Directory::purge(const bool verbose) 
103  {
104    if (verbose)
105      std::cout << "Purging " << path_ << std::endl;
106    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
107      if (!((*i)->subversion_controlled())) {
108        delete *i;
109        i=daughters_.erase(i);
110      }
111      else {
112        (*i)->purge(verbose);
113        i++;
114      }
115  }
116
117}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.