-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (168 loc) · 4.59 KB
/
Copy pathmain.cpp
File metadata and controls
199 lines (168 loc) · 4.59 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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "GNode.hpp"
#include "Tree.hpp"
#include "LDisk.hpp"
void useStatement(){
std::cout<<"./driver -f input_file -d input_dir -s disk_size -b block_size"<<std::endl;
}
int main(int argc, char* argv[]){
//PARSE ARGS
std::string fileList = "";
std::string dirList = "";
int diskSize = -1;
int blockSize = -1;
char flag;
while((flag = getopt(argc, argv, "f:d:s:b:")) != -1)
{
switch(flag)
{
case 'f':
fileList = optarg;
break;
case 'd':
dirList = optarg;
break;
case 's':
diskSize = atoi(optarg);
break;
case 'b':
blockSize = atoi(optarg);
break;
case '?':
useStatement();
exit(1);
default:
useStatement();
exit(1);
}
}
// ERROR CHECKING
if(diskSize < 0 || blockSize < 0 || fileList.length() < 1 || dirList.length() < 1)
{
useStatement();
exit(1);
}
//READING STREAMS
std::ifstream fileStream(fileList);
std::ifstream directoryStream(dirList);
std::string line;
size_t index=0;
if (!directoryStream){
std::cerr<< "Cannot open file " <<std::endl;
exit(1);
}
int numBlocks= diskSize/blockSize;
//CREATE THE DIRECTORY
Tree directory= Tree(blockSize,numBlocks);
//READ THE DIRECTORIES
while(std::getline(directoryStream, line)){
//ADD EACH DIRECTORY AS A NODE
directory.addNode(-1,line);
}
directoryStream.close();
if (!fileStream){
std::cerr<< "Cannot open file" <<std::endl;
}
//READ THE FILES
while (std::getline(fileStream,line)){
//PARSE THE LINE INTO NAME AND SIZE
//MAY VARY
std::string name;
int size=0;
std::string buffer;
std::stringstream stream(line);
int ctr=0;
while (stream >> buffer){
if (ctr==6){
size= atoi(buffer.c_str());
}
if (ctr == 10){
name= buffer;
}
ctr++;
}
// ADD EACH TO THE DIRECTORY
directory.addNode(size,name);
}
fileStream.close();
//HANDLE USER INPUT
while(1){
std::cout << "FileSystem/Shell: ";
std::string command;
std::string arg1;
std::string arg2;
getline(std::cin,command);
std::stringstream stream(command);
stream >> command;
stream >> arg1;
stream >> arg2;
if (!command.compare("cd")){
directory.cd(arg1);
}
else if(!command.compare("cd..")){
directory.cdOut();
}
else if(!command.compare("ls")){
directory.ls();
}
else if(!command.compare("mkdir")){
if (arg1.compare("")){
directory.mkdir(arg1);
}
else{
std::cout << "Need more arguments" <<std::endl;
}
}
else if(!command.compare("create")){
if (arg1.compare("")){
directory.create(arg1);
}
else{
std::cout << "Need more arguments" <<std::endl;
}
}
else if(!command.compare("append")){
if (arg1.compare("") && arg2.compare("")){
directory.append(arg1,stoi(arg2));
}
else{
std::cout << "Need more arguments" <<std::endl;
}
}
else if(!command.compare("remove")){
if (arg1.compare("") && arg2.compare("")){
directory.remove(arg1,stoi(arg2));
}
else{
std::cout << "Need more arguments" <<std::endl;
}
}
else if(!command.compare("delete")){
directory.deleteNode(arg1);
}
else if(!command.compare("exit")){
std::cout << "Bye" << std::endl;
break;
}
else if(!command.compare("dir")){
directory.dir();
}
else if(!command.compare("prfiles")){
directory.printFiles();
}
else if(!command.compare("prdisk")){
directory.printDisk();
}
else{
std::cout << "Command does not exist" <<std::endl;
}
}
}