-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManager.cpp
178 lines (147 loc) · 4.29 KB
/
Manager.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
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <regex>
#include <string>
#include "Manager.h"
#include "VirtualHD.h"
using namespace std;
void Manager::loadHD(){
fstream fs;
VirtualHD temp;
fs.open("HardDriveManager.txt", fstream::in);
while(fs >> temp.HDname >> temp.sizeB >> temp.qtdB)
HDS[temp.HDname] = temp;
fs.close();
qtdHDS = HDS.size();
}
void Manager::help(){
fstream fs;
string f, ds;
fs.open("HelpHD.txt", fstream::in);
fs.seekg (0, fs.end);
int length = fs.tellg();
fs.seekg (0, fs.beg);
char* buffer = new char [length];
fs.read(buffer, length);
cout << buffer << endl;
fs.close();
delete[] buffer;
}
void Manager::unloadHD() {
fstream fs;
VirtualHD temp;
fs.open("HardDriveManager.txt", fstream::out);
for (it = HDS.begin(); it != HDS.end(); ++it)
fs << it->second.HDname << " " << it->second.sizeB << " " << it->second.qtdB << endl;
fs.close();
}
void Manager::removeHD() {
string name;
cin >> name;
if(searchHD(name)){
remove((name + ".txt").c_str());
HDS.erase(HDS.find(name));
unloadHD();
} else {
cout << "HD nao localizado!" << endl;
}
}
void Manager::formatHD(string name) {
if(searchHD(name)){
fstream fs;
fs.open(name+".txt", fstream::out| fstream::trunc);
HDS[name].initialize(fs);
fs.close();
}else
cout << "Nao foi possivel formatar o HD, pois ele nao existe" << endl;
}
void Manager::printHDS(){
cout << setw(20) << "HD_NAME" << setw(20) << "TAM_BLOCK" << setw(20) << "QTD_BLOCK" << endl;
for (it = HDS.begin(); it != HDS.end(); ++it)
cout << setw(20) << it->second.HDname <<
setw(20) << it->second.sizeB <<
setw(20) << it->second.qtdB << endl;
}
void Manager::writeHD(){
fstream fs;
VirtualHD newhd;
newhd.setParameters();
if(validateHD(newhd)){
HDS[newhd.getHDname()] = newhd;
newhd.createHD();
fs.open("HardDriveManager.txt", fstream::out);
for (it = HDS.begin(); it != HDS.end(); ++it)
fs << it->second.HDname << " " << it->second.sizeB << " " << it->second.qtdB << endl;
fs.close();
qtdHDS = HDS.size();
cout << qtdHDS << " HDs writed with success" << endl;
}
}
bool Manager::validateHD(VirtualHD temp){
if(temp.getHDname().find(":") != string::npos){
cout << "Nome invalido, tente outro nome" << endl;
return false;
}
if(searchHD(temp.getHDname())){
cout << "Ja existe um HD com este nome, tente outro" << endl;
return false;
}
if(temp.getSizeB() > 128 || temp.getQtdB() > 10000 ){
cout << "HD muito grande, diminua os parametros" << endl;
return false;
}
if(temp.getSizeB() < 16 || temp.getQtdB() < 10){
cout << "HD muito pequeno, aumente os parametros" << endl;
return false;
}
return true;
}
bool Manager::searchHD(string hdSearch){
auto exist = HDS.find(hdSearch);
if(exist == HDS.end())
return false;
return true;
}
void Manager::statusHD(){
string name;
cin >> name;
if(searchHD(name)){
fstream in;
in.open(name + ".txt", fstream::in);
HDS[name].loadBlocks(in);
HDS[name].sizeHD();
in.close();
} else {
cout << "HD nao localizado!" << endl;
}
}
void Manager::setPath(string path){
this->path = path;
}
string Manager::getPath(){
return this->path;
}
void Manager::addPath(string path){
this->path += path + ">";
}
void Manager::resetPath(){
setPath("#");
}
void Manager::removePath(){
int countChr = count(path.begin(), path.end(), '>');
if (countChr < 2)
cout << "Voce ja esta na raiz do hd, para sair digite exit" << endl;
else{
int pos = path.size()-2;
while(path[pos] != '>'){
pos--;
}
setPath(path.substr(0, pos + 1));
}
}
string Manager::regexFunction(string target, string replacement, string value){
auto target_ = regex(target);
return regex_replace(value, target_, replacement);
}