/
Nav.cpp
executable file
·162 lines (128 loc) · 4.51 KB
/
Nav.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/////////////////////////////////////////////////////////////////
// nav.cpp - recursively walk a directory tree starting at a //
// ver 1.6 specified path //
// //
// Language: Visual C++, ver 8.0 //
// Platform: Dell Precision T7400, Vista Ultimate, SP1 //
// Application: CSE775 Project #1, Spring 2009 //
// Author: Jim Fawcett, Syracuse University CST 4-187 //
// (315) 443-3948, jfawcett@twcny.rr.com //
/////////////////////////////////////////////////////////////////
#include <iostream> // standard output for test
#include <iomanip> // output formatting
#include <vector>
#include "fileInfo.h"
#include "nav.h"
#include "wintools.h"
#include "conio.h"
using namespace std;
using namespace Win32Tools;
//----< default dir name display function >--------------------
void defProc::dirsProc(const stdStr& dir)
{
stdOut << TEXT("\n ") << dir.c_str() << endl;
}
//----< default file data display function >-------------------
void defProc::
fileProc(const fileInfo& fi)
{
if(!fi.isDirectory())
fi.showData(stdOut);
}
//----< save user's working directory >------------------------
navig::navig(defProc &DP) : dp_(DP)
{
// save user's working directory
TCHAR buffer[PathBufferSize];
GetCurrentDirectory( PathBufferSize, buffer);
userDir_ = buffer;
}
//----< restore user's working directory >---------------------
navig::~navig() { SetCurrentDirectory(userDir_.c_str()); }
//----< walk directory tree rooted at dir >--------------------
void navig::walk(const stdStr& dir, const stdStr& fileMask) {
stdStr path = Path::getFullPath(dir);
dp_.dirsProc(path);
std::vector<stdStr> dirs;
std::vector<fileInfo> fis;
fis = Directory::GetFileInfos(path,fileMask);
for(size_t i=0; i<fis.size(); ++i)
{
dp_.fileProc(fis[i]);
}
dirs = Directory::GetDirectories(path,TEXT("*.*"));
for(size_t i=0; i<dirs.size(); ++i)
walk(dirs[i],fileMask);
}
//----< test stub >--------------------------------------------
//
// - Recursively walk directory subtree pointed to
// by the last command line argument.
// - use default navig processing each time a directory
// or file is encountered
// - replace default processing with user defined processing
// and repeat
//
#ifdef TEST_NAV
//----< wait for key press: requires conio >-------------------
void waitForKeyPress()
{
std::cout << "\n Press key to exit: ";
getch();
}
class userProc : public defProc {
public:
virtual void fileProc(const fileInfo &fi) {
fileInfo newfi = fi;
newfi.showDate(false);
newfi.showData(stdOut);
}
virtual void dirsProc(const stdStr &dir) {
stdOut << TEXT("\n ") << dir.c_str() << endl;
}
};
void main(int argc, char *argv[]) {
cout << "\n Testing Directory Navigator "
<< "\n =============================\n";
if(argc == 1) {
cout << "\n please enter starting path\n";
waitForKeyPress();
return;
}
try {
// create default processing object and start navigation
cout << "\n testing default dir/file processing: "
<< "\n --------------------------------------\n";
stdOut << TEXT("\n starting path = ") << fileInfo::getPath() << TEXT("\n");
defProc dp;
navig nav(dp);
nav.walk(Convert::ToStdStr(argv[argc-1]),TEXT("*.*"));
stdOut << TEXT("\n ending path = ") << fileInfo::getPath() << TEXT("\n\n");
// User defined processing definded here. Note that
// local classes can be defined as long as all members
// are inline. Otherwise, just declare and define above
// main.
//
// These do nothing but prepend each output with some
// "-" characters to show that user defined processing
// is actually being used. In a real application user
// defined processing will certainly be quite different
// from the default
//
// restart with user defined processing
cout << "\n testing modified dir/file processing: "
<< "\n ---------------------------------------\n";
stdOut << TEXT("\n starting path = ") << fileInfo::getPath() << TEXT("\n");
userProc udp;
navig newNav(udp);
newNav.walk(Convert::ToStdStr(argv[argc-1]),"*.*");
cout << "\n ending path = " << newNav.getPath();
cout << "\n\n";
}
catch(std::exception& ex)
{
cout << ex.what();
}
waitForKeyPress();
}
#endif