-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
167 lines (133 loc) · 4.25 KB
/
File.cpp
File metadata and controls
167 lines (133 loc) · 4.25 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
#include"File.h"
#include"ExeFile.h"
//konstruktori
File::File(string& text, TreeNode* parent) :TreeNode(text, parent)
{
}
File::File(string name, TreeNode* parent, const string& content, const string& extension):TreeNode(name,parent),content_(content),extension_(extension)
{
}
File::~File()
{
extension_.clear();
content_.clear();
}
//Prijavljuje gresku u slucaju da neko pozove ovu metodu nad fajlom jer je fajl list
vector<TreeNode*> File::getChildren()
{
throw("File has no children");
}
//Prijavljuje gresku u slucaju da neko pozove ovu metodu nad fajlom jer je fajl list
void File::copyChildren(vector<TreeNode*>children)
{
throw("File has no children");
}
void File::deleteChildren()
{
throw("File has no children");
}
//getteri
string File::getNameWithExtension() const
{
return name_ + "." + extension_;
}
string File::getExtension() const
{
return extension_;
}
string File::getContent()const
{
return content_;
}
//Proverava da li je ime u stringu name_with_extension ime ovog fajla sa ekstenzijom
//Ako nije znamo da smo na kraju stabla i da trazeni fajl/folder nije nadjen nigde u stablu
TreeNode* File::findTreeNode(const string& name_with_extension)
{
if ((name_ + "." + extension_) == name_with_extension)return this;
else return nullptr;
}
//U slucaju da neko pokusa da nadje dete fajla javljamo gresku jer je fajl list
int File::findChild(const string& name_with_extension)const
{
throw(SearchError("File has no children"));
}
//Prijavljuje gresku u slucaju da neko pozove ovu metodu nad fajlom jer je fajl list
TreeNode* File::getChild(int i)const
{
throw(SearchError("File have no children"));
}
//Prijavljuje gresku u slucaju pokusaja dodavanja deteta u fajl
void File::addTreeNode(string text)
{
throw InsertError("Cannot add document into file");
}
//Ispisuje ime fajla sa ekstenzijom u izlazni fajl
void File::output(const string& path, fstream& output_file)const
{
output_file << path + "\\" + name_ + "." + extension_;
}
//Ispisuje ime fajla sa ekstenzijom i sadrzajem na standardni izlaz
void File::output(string path)const
{
cout << path + "\\" + name_ + "." + extension_+" "+content_;
}
//nema decu tako da samo prekida rekurziju u funkcijama za ispis
void File::outputChildren(bool root_call, string path, fstream& output_file)const
{
}
//Javlja gresku ako neko pokusa da obrise decu fajla jer je fajl list
void File::eraseChild(int i)
{
throw SearchError("File has no children");
}
//Fajl jeste list u stablu
bool File::isLeaf()const
{
return true;
}
//Cita ekstenziju koja se nalazi u tekstu
string File::readExtension(string& text)
{
string extension = "";
int start = text.find("."),i=start+1;//nalazi tacku
//Ako ne postoji tacka onda nema ni ekstenzije
if (text.empty()||start==-1)throw ExtensionError("Extension not found");
//Ekstenzija je sve izmedju tacke i razmaka
while (i < text.length() && text[i] != ' ') {
//Proverava ispravnost imena ekstenzije
if (!isalpha(text[i]) && !isdigit(text[i]) && !(text[i] == '_'))
throw(ExtensionError("Invalid name of extension"));
else //ako je validan ubacuje ga u ime
extension.push_back(text[i++]);
}
//Brise iz teksta procitanu ekstenziju
text = text.erase(start, i-start);
return extension;
}
//Bira koji konstruktor treba pozvati
File* File::chooseFile(string& text, TreeNode* parent)
{
string extension = readExtension(text);
if (extension=="exe")return new ExeFile(text, parent);
if (extension=="txt")return new TxtFile(text, parent);
throw(ExtensionError("Extension not found"));
}
File* File::chooseFile(const string& name, TreeNode* parent, const string& content,const string& extension)
{
if (extension == "exe")return new ExeFile(name, parent, content);
if (extension == "txt")return new TxtFile(name, parent, content);
throw(ExtensionError("Extension not found"));
}
//Odbija executovanje bilo kog fajla koji nije exe
void File::execute()
{
throw("Cannot execute ordinary file");
}
TxtFile::TxtFile(string& text, TreeNode* parent) :File(text, parent)
{
content_ = text;
extension_ = "txt";
}
TxtFile::TxtFile(const string& name, TreeNode* parent, const string& content):File(name,parent,content,"txt")
{
}