/
Directory.cpp
117 lines (94 loc) · 3.24 KB
/
Directory.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
#include "Directory.h"
#include "File.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
namespace kilicaslanFiles
{
Directory :: Directory() : File()
{
//Body intentionally empty
}
Directory :: Directory(const string& theName, const string& theOwner,
const int theSize, const string& theTime,
const string& thePermission, const string& theCurrentPath)
: File(theName, theOwner, theSize, theTime, thePermission, theCurrentPath)
{
//Body intentionally empty
}
bool Directory :: cp( File& other)
{
for(int i=0; i<files.size(); i++)
if(files[i]->getName() == other.getName())//check if the same file name already exist
return false;
files.push_back(&other);
other.addToPath((*this).getName());
return true;
}
void Directory :: ls(const string listType)
{
if(files.size() < 1)//if the directory is empty don't go to loop
return;
for(int i=0; i<files.size(); i++)
{
if(listType == " ")//just name of the file
files[i]->output();
else if(listType == "l")//long form of the file output
files[i]->outputLong();
else if(listType == "R") //recursive function
{
if(files[i]->isDir())
{
files[i]->output(); //just name
files[i]->ls("R");
}
else
files[i]->output();
}
else if(listType == "Rl" || listType == "lR") //recursive function
{
if(files[i]->isDir())
{
files[i]->outputLong(); //long output because of "l" in "lR"
files[i]->ls("Rl");
}
else
files[i]->outputLong();
}
else
{
cout << "Error!... Wrong usage of ls parameters " << endl;
return;
}
}
}
File& Directory :: cd( File& other )
{
//if this is not a directory
if(other.isDir() == false)
cerr << "This is not a directory" << endl;
//change the directory
else
{
Directory * temp;
temp = dynamic_cast<Directory *> (&other);
setName(other.getName());
setOwner(other.getOwner());
setSize(other.getSize());
setTime(other.getTime());
files = temp->files;
}
return *this;
}
void Directory :: output() const
{
cout << getName() << endl;
}
//print permission, name, owner, size and last modification time
void Directory :: outputLong() const
{
cout << getPermission() + " " + getName() + " " +
getOwner() + " " << getSize() << " " + getTime() << endl;
}
}