source: trunk/lib/Directory.cc @ 23

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

added print functionality

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Directory.cc 23 2006-01-02 08:14:57Z 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)
21    : Node(path)
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));
50        else
51          daughters_.push_back(new File(fullpath));
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    // Peter, creation of dir should be done without system call
76    std::string system_call("mkdir " + path + name()+">& /dev/null");
77    system(system_call.c_str());
78    std::string output(path + name() + "/index.html");
79    std::ofstream os(output.c_str());
80    print_header(os);
81    os << std::endl;
82    stats_.print(os);
83    os << std::endl;
84
85    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++){
86      (*i)->print_link(os);
87      os << "<br/>" << std::endl;
88    }
89
90    print_footer(os);
91    os.close(); 
92
93    for (NodeConstIter_ i=daughters_.begin();i!=daughters_.end();i++) 
94      (*i)->print(path+name()+"/");
95  }
96
97  void Directory::print_link(std::ostream& os) const
98  {
99    os << "<a href=\"" << name() << "/index.html\">" << name() << "</a>";
100  }
101
102
103  void Directory::purge(void) {
104    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
105      if (!((*i)->subversion_controlled())) {
106        delete *i;
107        i=daughters_.erase(i);
108      }
109      else {
110        (*i)->purge();
111        i++;
112      }
113  }
114
115}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.