-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
344 lines (269 loc) · 6.25 KB
/
main.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// filesys-new.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "string"
#include "iomanip"
#include "fstream"
using namespace std;
/*Main class for the directory or file*/
class node
{
public:
string name;
node *child;
node *peer;
node *parent;
enum type {file, directory};
type type1;
int fno;
node() //constructor
{
name = "";
child = NULL;
peer = NULL;
parent = NULL;
type1 = directory;
fno = 0;
}
~node(){};
};
/*Class for the file system which contains functions to operate on file or directory */
class fs
{
string fsname;
node *root_dir;
node *cwd;
string prompt;
static int fileno;
public:
fs()
{
fsname = "S:";
root_dir = new node;
cwd = new node;
prompt = fsname;
}
void mkdir(string);
void mkfile(string);
int changedir(string);
void dir();
int del(string);
int counter();
void showprompt();
void out();
void root();
};
int fs::fileno = 0;
/* Create Directory, dir name as argument */
void fs::mkdir(string dname)
{
/*Allocate a new node*/
node *pointer = new node;
pointer->name = dname;
/* Insert the new node into the directory list */
pointer->peer = cwd->child;
cwd->child = pointer;
pointer->parent = cwd;
/*Change the type as Dir*/
pointer->type1 = pointer->directory;
/*Assign the fileno */
pointer->fno = counter();
/*Save it to disk
char dname_array[10];
strcpy(dname_array, dname);
ofstream outfile(dname_array, ios::binary);
outfile.write(reinterpret_cast<char*>(pointer), sizeof(*pointer));
outfile.close();
ifstream infile(dname, ios::binary);
infile.read(reinterpret_cast<char*>(pointer), sizeof(*pointer));
cout<<pointer->name<<endl;*/
}
void fs::mkfile(string fname)
{
/*Allocate a new node*/
node *pointer = new node;
pointer->name = fname;
/*Insert the new node into the directory list */
pointer->peer = cwd->child;
cwd->child = pointer;
pointer->parent = cwd;
/*Change the type as File */
pointer->type1 = pointer->file;
/*Assign the fileno*/
pointer->fno = counter();
}
int fs::changedir(string dir_to_change)
{
/* Create a new node pointer and assign the cwd's child to it*/
node *pointer = cwd->child;
/*Now, traverse through its peers for matching the dname */
while (pointer != NULL)
{
if (pointer->name == dir_to_change)
{
pointer->parent = cwd;
cwd = pointer;
/*To display the proper prompt */
prompt = prompt + "\\" + cwd->name ;
//cout<<"parent of "<<pointer->name<<" is "<<cwd->name;
return 0;
}
pointer = pointer->peer;
}
cout<<endl<<"Invalid Directory name";
return 0;
}
void fs::dir()
{
/* Create a new node pointer and assign the cwd's child to it*/
node *pointer = cwd->child;
/*Now, traverse through its peers and print the names */
while (pointer != NULL)
{
string dir_or_file;
dir_or_file = ((pointer->type1) == 1)? "<DIR>" : "<File>";
cout<<dir_or_file<<setw(20)<<pointer->name<<endl;
pointer = pointer->peer;
}
}
int fs::del(string name_to_delete)
{
/* Create a new node pointer and assign the cwd's child to it*/
node *pointer = cwd->child;
node *previous = cwd->child;
/*Now, traverse through its peers for matching the name */
while (pointer != NULL)
{
if (pointer->name == name_to_delete)
{
if (previous->name == pointer->name) //If its the first dir under cwd
cwd->child = pointer->peer;
else
previous->peer = pointer->peer;
delete pointer; /* Delete the node using delete()*/
return 0;
}
previous = pointer;
pointer = pointer->peer;
}
cout<<endl<<"Invalid name";
return 0;
}
void fs::out()
{
node *pointer = cwd->parent;
if (cwd->parent == NULL)
{
return;
}
int namelength = (cwd->name).length();
int n = prompt.find(cwd->name);
prompt.erase(n-1, namelength+1);
cwd = pointer;
}
void fs::root()
{
cwd = root_dir;
prompt = fsname;
}
int fs::counter()
{
fileno++;
//increment the fileno each time an object is created and return
return fileno;
}
void fs::showprompt()
{
cout<<endl<<prompt<<">";
}
/* Command */
#define MKDIR 0
#define MKFILE 1
#define DIR 2
#define CD 3
#define DEL 4
#define EXIT 5
#define OUT 6
#define ROOT 7
#define INVALID -1
int parse_command(string *cmd)
{
string command[8]= {"mkdir", "mkfile", "dir", "cd", "del", "exit", "cd..", "cd\\"};
int c = 0;
while (command[c] != *cmd)
{
c++;
if(c>7) return -1;
}
return c;
}
int _tmain(int argc, _TCHAR* argv[])
{
int commandno; //To parse through the function
string command; //The whole command line
//string prompt;
string *only_command = new string; //Separated from name
char comd_array[12]; //For storing the command separately
string input_name; //Required name from the command line
int wlen, pos_of_space; //Word length and the position of space
fs filesys;
/*Print the opening prompt*/
filesys.showprompt();
/* Getline commands and parse it through the parse function */
/* Access commands for operating on the directory and files */
while (1)
{
/* Get a command */
getline (cin, command, '\n');
wlen = command.length(); //count the command length
pos_of_space = command.find(' '); //Find the position of the space
command.copy(comd_array, pos_of_space, 0); //Copy it to the char array
if (pos_of_space != string::npos)
{
comd_array[pos_of_space]=0; //terminate with '\0'
input_name = command.substr(pos_of_space+1, wlen);
}
else //If it is a single command without name
comd_array[wlen] = '\0';
*only_command = comd_array; //Convert the char array to string object
commandno = parse_command(only_command); //Call the parse function
/* Print prompt with directory*/
switch (commandno)
{
case MKDIR:
filesys.mkdir(input_name);
break;
case MKFILE:
filesys.mkfile(input_name);
break;
case DIR:
filesys.dir();
break;
case CD:
filesys.changedir(input_name);
break;
case DEL:
filesys.del(input_name);
break;
case EXIT:
return 0;
break;
//case FS:
case INVALID:
cout<<"\nInvalid command\n";
break;
case OUT: //for cd..
filesys.out();
break;
case ROOT: /*for cd\ */
filesys.root();
break;
default:
cout<<"Invalid command\n";
break;
}
filesys.showprompt();
}
return 0;
}