-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolder.cpp
More file actions
executable file
·220 lines (201 loc) · 5.89 KB
/
Copy pathFolder.cpp
File metadata and controls
executable file
·220 lines (201 loc) · 5.89 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Folder.cpp
*
* Created on: 13 áàôø× 2018
* Author: Omer
*/
#include "Folder.h"
Folder& Folder::operator =(const Folder& otherFolder) {
files = otherFolder.files;
folders = otherFolder.folders;
name = otherFolder.name;
path = otherFolder.path;
return *this;
}
Folder::~Folder() {
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
delete *fileIt;
for(vector<Folder*>::iterator folderIt = folders.begin(); folderIt!=folders.end(); ++folderIt)
delete *folderIt;
}
/*
* Recursive function get path to folder, search the folder in the folder system
* and return a pointer to it if found, else throws exception.
*/
Folder* Folder::findFolder(string folderName) throw(PathIsNotExistsException) {
folderName = eraseCurrantFolderFromPath(folderName);
if(folderName=="/") //tnai atsira
return this;
size_t pos = folderName.find("/");
if(pos!=string::npos) //if "/" found - folderName contains path.
{
string currentFolder = folderName.substr(0, pos);
string otherPath;
if(pos == folderName.length()-1)
otherPath = "/";
else
otherPath = folderName.substr(pos+1, folderName.length());
for(vector<Folder*>::iterator it = folders.begin();it!=folders.end();it++)
{
if((*it)->name==currentFolder) //folder found -> begin recursive search
return (*it)->findFolder(otherPath);
}
}
else //if "/" not found - no path and need to search folder in this folder.
{
for(vector<Folder*>::iterator it = folders.begin();it!=folders.end();it++)
if((*it)->name==folderName)
return *it;
}
throw PathIsNotExistsException();
return NULL;
}
/*
* Function make a new direction if not exist in the path given. else throw exception.
*/
void Folder::mkdir(string folderName) throw(FolderAlradyExistsExcaption) {
folderName = eraseCurrantFolderFromPath(folderName);
string tmpFolderName = folderName;
char c= tmpFolderName[folderName.length()-1];
if(c!='/')
tmpFolderName.append("/"); //add "/" so findFolder can work.
try{
findFolder(tmpFolderName);
}
catch (PathIsNotExistsException& e) { //happen if folder not found.
folders.push_back(new Folder(folderName,path+this->name+"/"));
return;
}
throw FolderAlradyExistsExcaption();
}
/*
* Function get path and change currant working folder.
*/
Folder* Folder::chdir(string path) throw(PathIsNotExistsException){
Folder* f = this->findFolder(path);
return f;
}
/*
* Function get folder name and remove it from folder system if found. else throw exception.
*/
bool Folder::rmdir(string folderName) throw (PathIsNotExistsException) {
for(vector<Folder*>::iterator it = folders.begin();it!=folders.end();it++)
if((*it)->name==folderName)
{
delete *it; //delete the folder
folders.erase(it); //erase folder from folder system.
return true;
}
throw PathIsNotExistsException();
return false;
}
/*
* Function display all folders and files in this folder.
*/
void Folder::ls() {
for(vector<Folder*>::iterator it = folders.begin();it!=folders.end();it++)
cout << *(**it) <<" ";
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
cout << *(**fileIt) <<" ";
cout << endl;
}
/*
* Function call to recursive print for display hierarchy of folders and files.
*/
void Folder::lproot() {
cout << name << endl;
string str="\t";
recTreePrint(str);
}
/*
* Function search for file in the file system of this folder and return
* a pointer to it if found, else throw exception.
*/
File* Folder::findFile(string fileName) throw(FileNotFoundExcaption) {
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
if(*(**fileIt)==fileName)
return *fileIt;
throw FileNotFoundExcaption();
return NULL;
}
/*
* Function create a new file by file name and add it to thee file system of this folder.
*/
File& Folder::createFile(string fileName) {
fstream* io = new fstream(fileName,ios::out);
io->close();
File* nwFile = new File(fileName,*io);
files.push_back(nwFile);
return *nwFile;
}
/*
* Function get path and delete from start the folder name if
* i'm the folder at the start of the path.
*/
string Folder::eraseCurrantFolderFromPath(string path) const {
string nwPath;
size_t pos = path.find("/");
if(path.substr(0, pos)==name)
{
nwPath = path.substr(pos+1,path.length());
if(nwPath.length()==0)
return "/";
return path.substr(pos+1,path.length());
}
return path;
}
/*
* Function print recursivly the content of the folders.
*/
void Folder::recTreePrint(string str) {
for(vector<Folder*>::iterator it = folders.begin();it!=folders.end();it++)
{
cout <<str<< *(**it) << endl;
(*it)->recTreePrint(str+"\t");
}
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
{
cout << str << *(**fileIt) << " -> " <<(*fileIt)->getRefCount() << endl;
}
}
/*
* Function print all folders in this folder's folder system.
*/
bool Folder::printFolders() {
if(!folders.empty())
{
for(vector<Folder*>::iterator folderIt = folders.begin(); folderIt!=folders.end(); ++folderIt)
cout << (*folderIt)->getFolderName() << " ";
return true;
}
return false;
}
/*
* Function print all files in this folder's file system.
*/
bool Folder::printFiles() {
if(!files.empty())
{
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
cout << *(**fileIt) << " ";
return true;
}
return false;
}
/*
* Function get file name and remve the file from file system if found.
*/
void Folder::eraseFile(string fileName) {
for(vector<File*>::iterator fileIt = files.begin(); fileIt!=files.end(); ++fileIt)
if(*(**fileIt)==fileName)
{
files.erase(fileIt);
return;
}
}
/*
* Function insert file to the file system.
*/
void Folder::addFile(File* file) {
files.push_back(file);
}