-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory.cpp
More file actions
128 lines (112 loc) · 2.63 KB
/
directory.cpp
File metadata and controls
128 lines (112 loc) · 2.63 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
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
#include "directory.hpp"
#include <fstream>
directory::directory() : path("\0")
{
}
directory::directory(string p) : path(p)
{
}
//prints contents of directory: returns 1 if directory is valid
bool directory::ls()
{
DIR *dir;
struct dirent *ent;
const char* path_c = path.c_str(); //convert path to const char*
if ((dir = opendir (path_c)) != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
//Hides System Directories (./../.DS_Store)
if(isFile(ent->d_name))
{
//print directory name
cout << "- " << ent->d_name << endl;
}
}
closedir (dir);
return true; //return true if successful
}
return false; //return false if path is invalid
}
//returns path of directory
string directory::getPath()
{
return path;
}
bool directory::cd(string dir)
{
//create temporary path to check validity
string tempPath = path + "/" + dir;
const char* path_c = tempPath.c_str();
//check for '..' operator (back directory)
if(dir=="..")
{
size_t find = path.rfind("/"); //find last '/' in string
path.erase(find,path.length()); //remove text from last '/' to end of string
return 1;
}
//check if path is valid
if ((opendir (path_c)) != NULL)
{
//update path if valid
path = tempPath;
return true;
}
return false; //return false if path is invalid */
}
bool directory::setPath(string newPath)
{
//create temporary path to check validity
string tempPath = newPath;
const char* path_c = tempPath.c_str();
//check if path is valid
if ((opendir (path_c)) != NULL)
{
//update path if valid
path = tempPath;
return true;
}
return false;
}
bool directory::cat(string filename)
{
//create temporary path to file
string file_path = path + "/" + filename;
//path to parent directory (const char*)
const char* path_c = path.c_str();
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path_c)) != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
//If "file" is found
if(ent->d_name==filename)
{
//cout << "- " << ent->d_name << endl;
printFile(file_path);
}
}
closedir (dir);
return true; //return true if successful
}
return false; //return false if path is invalid
}
//Returns false if s = '.', '..' or '.DS_Store'
bool isFile(string s)
{
if(s == "." || s == ".." || s == ".DS_Store")
{
return false;
}
return true;
}
void printFile(string path)
{
ifstream f;
f.open(path);
cout << endl << f.rdbuf();
f.close();
}