source: trunk/lib/Directory.cc @ 18

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

Directory tree created. Use purge() to remove non subversion controlled entries.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: Directory.cc 18 2005-12-30 17:41:33Z jari $
2
3#include "Directory.h"
4#include "File.h"
5#include "Node.h"
6
7#include <iostream>
8#include <iterator>
9#include <list>
10
11#include <errno.h>  // Needed to check error state below.
12#include <dirent.h>
13
14namespace theplu{
15namespace svnstat{
16
17  Directory::Directory(const std::string& path)
18    : Node(path)
19  {
20    using namespace std;
21    DIR* directory=opendir(path.c_str());
22    if (!directory) {
23      // Jari, throw exception
24      cerr << "ERROR: opendir() failed; terminating" << endl;
25      exit(1);
26    }
27    list<string> entries;
28    struct dirent* entry;
29    errno=0;  // Global variable used by C to track errors.
30    while ((entry=readdir(directory)))
31      entries.push_back(string(entry->d_name)); // Jari, push backing
32    if (errno) {
33      // Jari, throw exception
34      cerr << "ERROR: readdir() failure; terminating" << endl;
35      exit(1);
36    }
37    closedir(directory);
38
39    // Jari, change this to some STL algo?
40    for (list<string>::iterator i=entries.begin(); i!=entries.end(); i++)
41      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
42        /// Jari, should test if directory in some other way
43        string fullpath(path_+'/'+(*i));
44        DIR* dirtest=opendir(fullpath.c_str());
45        if (!dirtest)
46          daughters_.push_back(new File(fullpath));
47        else
48          daughters_.push_back(new Directory(fullpath));
49      }
50  }
51
52
53  Directory::~Directory(void)
54  {
55    // Jari, change this to some STL algo?
56    for (std::list<Node*>::iterator i=daughters_.begin();
57         i!=daughters_.end(); i++)
58      delete *i;
59  }
60
61
62  const Stats& Directory::parse(void)
63  {
64    stats_.reset();
65
66    for (std::list<Node*>::iterator i=daughters_.begin();
67         i!=daughters_.end(); i++)
68      stats_ += (*i)->parse();
69    return stats_;
70  }
71
72
73  void Directory::print(void) {
74    // Jari, temporary using this to print directory tree
75    for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::print));
76  }
77
78
79  void Directory::purge(void) {
80    for (std::list<Node*>::iterator i=daughters_.begin(); i!=daughters_.end(); )
81      if (!((*i)->subversion_controlled())) {
82        delete *i;
83        i=daughters_.erase(i);
84      }
85      else {
86        (*i)->purge();
87        i++;
88      }
89  }
90
91}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.