source: trunk/lib/Directory.cc @ 19

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

added include algorithm

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