-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.cpp
More file actions
310 lines (278 loc) · 6.07 KB
/
Q3.cpp
File metadata and controls
310 lines (278 loc) · 6.07 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
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
#include<iostream>
#include<fstream>
#include<string>
#include<list>
using namespace std;
// Max number of blocks
#define LIMIT 1000
// File pointer for writing
ofstream ofptr;
// File pointer for reading
ifstream ifptr;
// Indexes of blocks that are occupied
bool disk_space[LIMIT] = { 0 };
// Number of blocks allocated
int parts_allocated = 0;
// Relation handling between file name
// and blocks
list < pair<string, list<int>>> dir;
int finddir(string);
int dfdir(string);
int pfdir(string);
int lsdir(string);
int rfdir(string, string);
// Function for command "mf"
// make file
int mfdir(string fn, string fc) {
if (finddir(fn) == 1){
cout<<"File already exists"<<endl;
return 1;
}
// For maintaining the blocks
list <int> listNodes;
int i, counter = 0;
string temp = "";
// Checking whether the file contents
// are of length divisible by 4
int rem = (fc.length() % 4);
// If not, then padding with
// dummy character
if (rem != 0) {
fc += string(4 - rem, '\0');
}
// For maintaing the string slice of
// length 4
counter = 0;
// For loop for slicing the string into
// parts of 4 and writing it into
// block files
for (i = 0; i < fc.length(); i++) {
temp += fc[i];
counter += 1;
// If counter is of size 4
// then write it into a block file
// of number upto which blocks are
// occupied
if (counter == 4) {
// Creating file of name block number
ofptr.open((to_string(parts_allocated) + ".txt").c_str());
// Maintaining which block is associated with the
// given file name
listNodes.push_back(parts_allocated);
// The next block to write
parts_allocated += 1;
// Resetting the slice size to 0
counter = 0;
ofptr << temp;
// Resetting the slice value to ""
temp = "";
ofptr.close();
}
}
// Maintaing the number of blocks relation with the
// file name
dir.push_back(make_pair(fn, listNodes));
cout << "\t\t" << fn << " named file generated\n";
return 0;
}
// Function for command "df"
// delete file
int dfdir(string fn) {
auto file_entry = dir.begin();
while (file_entry != dir.end())
{
if (file_entry->first == fn)
{
auto blocks = file_entry->second.begin();
while (blocks != file_entry->second.end())//find file block
{
auto block_loc = (to_string(*blocks) + ".txt");
remove(block_loc.c_str()); //deleting file
disk_space[*blocks++] = 0;
}
dir.remove(*file_entry);//remove file directory
return 0;
}
file_entry++;
}
cout << "File not found" << endl;
return 1;
}
// Function pre existing
// file
int finddir(string fn) {
auto file_entry = dir.begin();
while (file_entry != dir.end())
{
if (file_entry->first == fn)
{
return 1;
}
file_entry++;
}
return 0;
}
// Function for command "rf"
// rename file
int rfdir(string fn1, string fn2) {
auto file_entry = dir.begin();
if (finddir(fn2) == 1)
{
cout << "Can't rename to existing file name" << endl;
return 0;
}
while (file_entry != dir.end())
{
if (file_entry->first == fn1)
{
file_entry->first = fn2;
return 1;
}
file_entry++;
}
cout << "File not found" << endl;
return 1;
}
// Function for command "pf"
// printing file
int pfdir(string fn) {
auto file_entry = dir.begin();
while (file_entry != dir.end())
{
if (file_entry->first == fn)
{
auto blocks = file_entry->second.begin();
string fc = "";
while (blocks != file_entry->second.end())
{
auto block_loc = (to_string(*blocks) + ".txt");
ifptr.open(block_loc);
string line;
getline(ifptr, line);
fc += line;
ifptr.close();
blocks++;
}
cout << file_entry->first << ": " << fc << endl;
return 0;
}
file_entry++;
}
cout << "File not found" << endl;
return 1;
}
// Function for command "ls"
// List of directories
int lsdir() {
auto file_entry = dir.begin();
while (file_entry != dir.end())
{
auto blocks = file_entry->second.begin();
string fb = "";
while (blocks != file_entry->second.end())
{
fb += to_string(*blocks) + ",";
blocks++;
}
fb.pop_back();
cout << file_entry->first << ": " << fb << endl;
file_entry++;
}
return 0;
}
int main() {
// User given command
string command = "";
// Command type
string fcmd = "";
// File name
string fname = "";
// Contents of the file
string fcontents = "";
string temp = "";
int i, j = 0;
char ch = 'Y';
// While loop for user driven
// loop
while (ch == 'Y') {
temp = "";
fcmd = "";
// File name
fname = "";
// Contents of the file
fcontents = "";
cout << ">>: ";
getline(cin, command);
for (i = 3; i < command.length(); i++) {
if (command[i] == ' ') {
fname = temp;
j = i;
temp = "";
break;
}
temp += command[i];
}
if (temp != "")
{
fname = temp;
}
for (i = j + 1; i < command.length(); i++) {
if (command[i] == '"')
{
continue;
}
temp += command[i];
}
fcontents = temp;
fcmd = fcmd + command[0] + command[1];
// If command is mf,
// then make file
if (fcmd == "mf") {
fcmd = "";
mfdir(fname, fcontents);
}
// If command is rf,
// then rename file
else if (fcmd == "rf") {
fcmd = "";
rfdir(fname, fcontents);
}
// If command is df,
// then delete file
else if (fcmd == "df") {
fcmd = "";
dfdir(fname);
}
// If command is pf,
// then print file
else if (fcmd == "pf") {
fcmd = "";
pfdir(fname);
}
// If command is ls,
// then list all directories
else if (fcmd == "ls") {
fcmd = "";
lsdir();
}
// If command is es,
// then exiting the system
else if (fcmd == "es") {
fcmd = "";
exit(0);
}
// If no command is valid
else {
fcmd = "";
cout << "Invalid command: " << fcmd << endl;
cout << "Existing commands: \n";
cout << "1. mf: Make file.\nSyntax:> mf <filename> <file contents>\n";
cout << "2. rf: Rename file.\nSyntax:> rf <old filename> <new filename>\n";
cout << "3. df: Delete file.\nSyntax:> df <filename>\n";
cout << "4. pf: Print file.\nSyntax:> pf <filename>\n";
cout << "5. ls: List all files.\nSyntax:> ls\n";
cout << "6. es: Escape program.\nSyntax: es\n";
}
}
return 1;
}