This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
255 lines (239 loc) · 6.85 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
/* Name : Dumitru
** Class : CS 330 - 001
** Date : Jan/Feb 2007
**
** Description: Phase I of S.O.S Project
Implements the overall management of the file systems.
It can install a new simulated disk, format the disk to create a file system,
load the information about the file system into the S.O.S (mount), and display
information about the mounted file systems and the contents of a particular
file system.
Instructions are loaded from a (ascii) text file and the output is logged in
a (ascii) log file. These files are passed to the program as arguments.
******************************************************************************/
#include "SimpleOS.h" // SimpleOS class
#include "ProcessCmd.h" // ProcessCmds class
#include <time.h> // for ctime()
/******************************************************************************
** void isPrintable(ProcessCmd & cmds, SimpleOS & sos, fstream & output );
Checks which command has to be processed according to cmds, checks for the
correct number of parameters for the specified command and calls the
corresponding function of the sos.
******************************************************************************/
void Run(ProcessCmd&,SimpleOS&,fstream&);
/*################################# main ####################################*/
int main(int argc, char * argv[])
{
// check for valid parameters
// no need to create instances if the arguments are invalid
if(argc != 3)
{
cout << "usage: program [inputFileName] [outputFileName]" << endl;
return -1;
}
ProcessCmd cmds; // used to process each command
SimpleOS sos; // simple operating system, performs commands
fstream output, input; // for file input/output
string cmd; // temp string used to store command from file
time_t theTime; // used to record the current time
// open input/output files
output.open(argv[2],ios_base::out);
input.open(argv[1],ios_base::in);
if(!input) // input file could not be opened
{
cout << argv[1] << " could not be opened." << endl
<< "Program terminated." << endl;
return 1; // exit program
}
if(!output) // log file could not be created
{
cout << argv[2] << " could not be opened." << endl
<< "Programm terminated." << endl;
return 1; // exit program
}
// initiate current time and record it
time(&theTime);
output << "SOS started on " << ctime(&theTime) << endl;
// process input file
while(getline(input,cmd))
{
if(!cmd.empty()) // skip empty lines
{
output << cmd << endl;
cmds.NewCommand(cmd);
if(!cmds.IsComment())
Run(cmds,sos,output);
}
}
// record the time
output << endl << "SOS stopped on " << ctime(&theTime) << endl;
// close input/output streams
input.clear(); input.close();
output.clear(); output.close();
return 0;
} // END main()
void Run(ProcessCmd & cmds,SimpleOS & sos,fstream & output)
/******************************************************************************
Checks which command has to be processed according to cmds, checks for the
correct number of parameters for the specified command and calls the
corresponding function of the sos.
******************************************************************************/
{
switch(cmds.GetCommandNo())
{
case 0: // install: Install Disk
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 2)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// get the integer value of the string
int diskSize = isValidNum(cmds.GetParam(1));
// SimpleOS 'system call'
sos.InstallDisk(cmds.GetParam(0),diskSize,output); break;
}
case 1: // format: Format Disk
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 4)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// get the integer values
int fsSize = isValidNum(cmds.GetParam(2));
int maxFiles = isValidNum(cmds.GetParam(3));
// SimpleOS 'system call'
sos.FormatDisk(cmds.GetParam(0),cmds.GetParam(1),
fsSize,maxFiles,output);
break;
}
case 2: // mount: Mount File System
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 2)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// SimpleOS 'system call'
sos.MountDisk(cmds.GetParam(0),cmds.GetParam(1),output); break;
}
case 3: // showmounts: Show Mounted File Systems
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 0)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// SimpleOS 'system call'
sos.ShowMounts(output); break;
}
case 4: // cfs: Change File System
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 1)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// SimpleOS 'system call'
sos.ChangeFs(cmds.GetParam(0),output); break;
}
case 5: // dumpfs: Dump File System Contents
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 2)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// get integer values of parameters
int startByte = isValidNum(cmds.GetParam(0));
int numBytes = isValidNum(cmds.GetParam(1));
// SimpleOS 'system call'
sos.DumpFs(startByte,numBytes,output); break;
}
case 6: // showfs: Show Current File System Info
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 0)
{ // Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// SimpleOS 'system call'
sos.ShowFs(output); break;
}
case 7: // unmount: Unmount A File System
{
// check for valid no. of parameters
if(cmds.GetNumParams() != 1)
{
// Error: Invalid Instruction
output << ERROR[2] << endl; break;
}
// SimpleOS 'system call'
sos.UnmountDisk(cmds.GetParam(0),output); break;
}
case 8:
{
if(cmds.GetNumParams() != 1)
{
output << ERROR[2] << endl; break;
}
sos.ImportFile(cmds.GetParam(0),output); break;
}
case 9:
{
if(cmds.GetNumParams() != 2)
{
output << ERROR[2] << endl; break;
}
sos.CpFile(cmds.GetParam(0),cmds.GetParam(1),output);
break;
}
case 10:
{
if(cmds.GetNumParams() != 2)
{
output << ERROR[2] << endl; break;
}
sos.RenameFile(cmds.GetParam(0),cmds.GetParam(1),output);
break;
}
case 11:
{
if(cmds.GetNumParams() != 0)
{
output << ERROR[2] << endl; break;
}
sos.ListFiles(output); break;
}
case 12:
{
if(cmds.GetNumParams() != 2)
{
output << ERROR[2] << endl; break;
}
int newOwner = isValidNum(cmds.GetParam(1));
sos.ChgOwner(cmds.GetParam(0),newOwner,output); break;
}
case 13:
{
if(cmds.GetNumParams() != 2)
{
output << ERROR[2] << endl; break;
}
sos.ChangePerm(cmds.GetParam(0),cmds.GetParam(1),output);
break;
}
case 14:
{
if(cmds.GetNumParams() != 1)
{
output << ERROR[2] << endl; break;
}
sos.ExportFile(cmds.GetParam(0),output); break;
}
// if it got here, the command is invalid
// Error: Invalid Instruction
default: output << ERROR[2] << endl;
}
}