-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost_ls.cpp
More file actions
81 lines (64 loc) · 2.28 KB
/
boost_ls.cpp
File metadata and controls
81 lines (64 loc) · 2.28 KB
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
/*
* C++ program to list all non-directory files in provided path
* Usage: ./boost_ls path
* e.g. ./boost_ls A/
*/
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main (int argc, char* argv[])
{
// Exit the program in case of missing argument
if ( argc < 2 )
{
cout << "Usage: boost_ls path\n" ;
return 1;
}
path p ( argv[1] );
// Exit the program if argument is not a valid directory path
if ( ! exists(p) )
{
cout << p.string() << " directory does not exist" << endl;
return 1;
}
// Exit the program if argument is not a directory
if ( ! is_directory(p) )
{
cout << p.string() << " is not a directory" << endl;
return 1;
}
cout << "Entered path is " << p.string() << endl;
directory_iterator end_iter;
// Stack to maintain a list of directories to be traversed
stack<directory_iterator> directoriesStack;
// directory_iterator object initialized for current directory
directory_iterator dir_iter(p);
// Push present directory onto stack
directoriesStack.push(dir_iter);
cout << "All files in this path are " << endl;
// Traverse directories in stack until stack is empty
while ( ! directoriesStack.empty() ){
// Get and remove the directory on top of the stack
directory_iterator curr_dir_iter = directoriesStack.top();
directoriesStack.pop();
// Traverse all files and directories in present directory
for ( ; curr_dir_iter != end_iter; ++curr_dir_iter )
{
// If present directory contains a directory push it on the stack to be traveresed later
if ( is_directory(curr_dir_iter->status()) )
{
//cout << "Directory " << curr_dir_iter->path().filename() << endl;
directory_iterator dir_iter( curr_dir_iter->path() );
directoriesStack.push( dir_iter );
}
// If present directory contains a file print it out
else if ( is_regular_file( curr_dir_iter->status() ) )
{
cout << curr_dir_iter->path().relative_path().string() << endl;
}
}
}
}