source: trunk/lib/Directory.cc @ 34

Last change on this file since 34 was 29, checked in by Peter Johansson, 17 years ago

modified output file names

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: Directory.cc 29 2006-01-09 09:35:50Z 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 <errno.h>  // 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(void)
64  {
65    stats_.reset();
66
67    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
68      stats_ += (*i)->parse();
69    return stats_;
70  }
71
72
73  void Directory::print(const std::string& path) const
74  {
75    std::string output(path + output_name_ + ".html");
76    std::ofstream os(output.c_str());
77    print_header(os);
78    os << std::endl;
79    stats_.print(os);
80    os << std::endl;
81
82    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++){
83      (*i)->print_link(os);
84      os << "<br/>" << std::endl;
85    }
86
87    print_footer(os);
88    os.close(); 
89
90    // Peter, use STL algo.
91    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++) 
92      (*i)->print(path);
93  }
94
95  void Directory::purge(void) {
96    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
97      if (!((*i)->subversion_controlled())) {
98        delete *i;
99        i=daughters_.erase(i);
100      }
101      else {
102        (*i)->purge();
103        i++;
104      }
105  }
106
107}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.