-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
210 lines (184 loc) · 8.35 KB
/
Copy pathProgram.cs
File metadata and controls
210 lines (184 loc) · 8.35 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
using System;
namespace NewProject
{
internal class Program
{
static void Main(string[] args)
{
FileSystem fs = new FileSystem();
Console.WriteLine("Enter the size of the blocks(preferably 512, 1024 or 2048 bytes): ");
fs._blockSize = int.Parse(Console.ReadLine() ?? "1024");
Console.WriteLine("Enter the number of the blocks between 4 and 20: ");
fs._totalBlocks = int.Parse(Console.ReadLine() ?? "5");
bool formatOnStart = true;
fs.InitializeFileSystem(fs.filePath, formatOnStart);
FileSystemBitmap bitmap = new FileSystemBitmap(fs._totalBlocks);
if (!formatOnStart)
{
bitmap.LoadBitmap(fs.filestream, fs._blockSize);
}
else
{
bitmap.OccupyBlock(0);
bitmap.OccupyBlock(1);
bitmap.OccupyBlock(2);
bitmap.WriteBitmap(fs.filestream, fs._blockSize);
}
Console.WriteLine("Commands: mkdir, rmdir, cd, ls, write, write+append, cat, rm, import, export, cp, paste, end");
string? command;
while ((command = Console.ReadLine()) != null)
{
if (command == "help")
{
Console.WriteLine("Commands: mkdir, rmdir, cd, ls, write, write+append, cat, rm, import, export, cp, paste, end");
}
else if (command == "mkdir")
{
Console.WriteLine("Enter directory name:");
string? name = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(name)) continue;
if (!fs.IsNameFree(name))
{
Console.WriteLine("Name already used.");
continue;
}
fs.CreateDir(name);
}
else if (command == "rmdir")
{
Console.WriteLine("Enter directory name:");
string? name = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(name)) continue;
fs.DeleteDir(name);
}
else if (command == "cd")
{
Console.WriteLine("Which directory you want to go to:");
string? dirName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(dirName)) continue;
fs.ChangeDir(dirName);
Console.WriteLine("Your current directory is: " + fs.currentDirectory);
}
else if (command == "ls")
{
fs.ListContent();
}
else if (command == "write")
{
Console.WriteLine("Write the name of the file you want to create:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
if (!fs.IsNameFree(fileName))
{
Console.WriteLine("There is already a file/dir with such name.");
continue;
}
Console.WriteLine("Write the content of the file:");
string? content = Console.ReadLine();
int freeBlock = bitmap.FindFirstFreeBlock();
if (freeBlock == -1 || freeBlock >= fs._totalBlocks)
{
Console.WriteLine("No free blocks available.");
continue;
}
bool ok = fs.CreateFile(fileName, content ?? "", freeBlock);
if (ok)
{
bitmap.OccupyBlock(freeBlock);
bitmap.WriteBitmap(fs.filestream, fs._blockSize);
Console.WriteLine("The content is added");
}
}
else if (command == "write+append")
{
Console.WriteLine("Write the name of the file you want to write in:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
Console.WriteLine("Write the content:");
string? content = Console.ReadLine();
fs.WriteToFile(content ?? "", fileName);
}
else if (command == "cat")
{
Console.WriteLine("Enter the name of the file you want to see:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
Console.WriteLine(fs.ShowFileContent(fileName));
}
else if (command == "rm")
{
Console.WriteLine("Enter the name of the file you want to delete:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
int block = fs.FindFileBlock(fileName);
if (block == -1)
{
Console.WriteLine("No such file in current directory.");
continue;
}
fs.DeleteFile(fileName);
bitmap.FreeBlock(block);
bitmap.WriteBitmap(fs.filestream, fs._blockSize);
}
else if (command == "export")
{
Console.WriteLine("Enter the name of the file you want to export:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
Console.WriteLine("Enter output file name (without .txt):");
string? outName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(outName)) continue;
fs.ExportFile(fileName, outName);
}
else if (command == "import")
{
Console.WriteLine("Enter the path of the external file you want to import:");
string? path = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(path)) continue;
Console.WriteLine("Enter the name of the internal file where you want to store this information:");
string? internalFile = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(internalFile)) continue;
fs.ImportFile(path, internalFile);
}
else if (command == "cp")
{
Console.WriteLine("Enter the name of the file you want to copy:");
string? fileName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(fileName)) continue;
fs.CopyFileToBuffer(fileName);
}
else if (command == "paste")
{
Console.WriteLine("Enter new file name to create from buffer:");
string? newName = Console.ReadLine();
if (TextUtils.IsNullOrWhiteSpace(newName)) continue;
if (!fs.IsNameFree(newName))
{
Console.WriteLine("Name already used.");
continue;
}
int freeBlock = bitmap.FindFirstFreeBlock();
if (freeBlock == -1 || freeBlock >= fs._totalBlocks)
{
Console.WriteLine("No free blocks available.");
continue;
}
bool ok = fs.PasteFromBuffer(newName, freeBlock);
if (ok)
{
bitmap.OccupyBlock(freeBlock);
bitmap.WriteBitmap(fs.filestream, fs._blockSize);
}
}
else if (command == "end")
{
break;
}
else
{
Console.WriteLine("Unknown command. Type help.");
}
}
}
}
}