1 | // $Id: Directory.cc 10 2005-12-30 11:57:57Z jari $ |
---|
2 | |
---|
3 | #include "Directory.h" |
---|
4 | #include "File.h" |
---|
5 | #include "Node.h" |
---|
6 | |
---|
7 | #include <iostream> |
---|
8 | #include <iterator> |
---|
9 | |
---|
10 | #include <errno.h> // Needed to check error state below. |
---|
11 | #include <dirent.h> |
---|
12 | |
---|
13 | namespace theplu{ |
---|
14 | namespace svnstat{ |
---|
15 | |
---|
16 | Directory::Directory(const std::string& path) |
---|
17 | : Node(path) |
---|
18 | { |
---|
19 | using namespace std; |
---|
20 | DIR* directory=opendir(path.c_str()); |
---|
21 | if (!directory) { |
---|
22 | // Jari, throw exception |
---|
23 | cerr << "ERROR: opendir() failed; terminating" << endl; |
---|
24 | exit(1); |
---|
25 | } |
---|
26 | vector<string> entries; |
---|
27 | struct dirent* entry; |
---|
28 | errno=0; // Global variable used by C to track errors. |
---|
29 | while ((entry=readdir(directory))) |
---|
30 | entries.push_back(string(entry->d_name)); // Jari, push backing |
---|
31 | if (errno) { |
---|
32 | // Jari, throw exception |
---|
33 | cerr << "ERROR: readdir() failure; terminating" << endl; |
---|
34 | exit(1); |
---|
35 | } |
---|
36 | closedir(directory); |
---|
37 | |
---|
38 | // Jari, change this to some STL algo? |
---|
39 | for (vector<string>::iterator i=entries.begin(); i!=entries.end(); i++) |
---|
40 | if ((*i)!=string(".") && (*i)!=string("..")) { |
---|
41 | /// Jari, should test if directory in some other way |
---|
42 | DIR* dirtest=opendir(i->c_str()); |
---|
43 | if (!dirtest) |
---|
44 | daughters_.push_back(new File(*i)); |
---|
45 | else |
---|
46 | daughters_.push_back(new Directory(*i)); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | Directory::~Directory(void) |
---|
52 | { |
---|
53 | // Jari, change this to some STL algo? |
---|
54 | for (std::vector<Node*>::iterator i=daughters_.begin(); |
---|
55 | i!=daughters_.end(); i++) |
---|
56 | delete *i; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | bool Directory::parse(void) |
---|
61 | { |
---|
62 | // Jari, where is the include for for_each? |
---|
63 | for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::parse)); |
---|
64 | return true; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | void Directory::print(void) { |
---|
69 | // Jari, temporary using this to print directory tree |
---|
70 | std::cout << "-------------- Directory '" << path_ << "'" << std::endl; |
---|
71 | for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::print)); |
---|
72 | } |
---|
73 | |
---|
74 | }} // end of namespace svnstat and namespace theplu |
---|