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
/
constants.h
182 lines (173 loc) · 5.65 KB
/
constants.h
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
/* Name : Dumitru
** Class : CS 330 - 001
** Date : Jan/Feb 2007
**
** Description: constants.h
Contains all preprocessor directories, as well as constants and global
functions needed.
******************************************************************************/
#ifndef CONSTANTS_H // Avoid multiple definitions
#define CONSTANTS_H
/*############################### INCLUDES ##################################*/
#include <iostream> // I/O, "right" flag
#include <iomanip> // setw()
#include <fstream> // file I/O
#include <vector> // vector container
#include <string> // string class
using namespace std;// standard namespace
/*############################### CONSTANTS #################################*/
// allowed file system types
const string FS_TYPE[] =
{
"SFS-1.0" // 0
};
// allowed S.O.S commands
const string CMDS[] =
{
"install", // 0
"format", // 1
"mount", // 2
"showmounts", // 3
"cfs", // 4
"dumpfs", // 5
"showfs", // 6
"unmount", // 7
"import", // 8
"copy", // 9
"rename", // 10
"dir", // 11
"owner", // 12
"permission", // 13
"export" // 14
};
// available error messages
const string ERROR[] = {
"", // empty to keep index no = to error no
"*ERROR 1: System error",
"*ERROR 2: Invalid instruction",
"*ERROR 3: Invalid parameter",
"*ERROR 4: Cannot create diskfile",
"*ERROR 5: Unsupported filesystem type",
"*ERROR 6: Invalid filesystem name",
"*ERROR 7: Cannot format disk",
"*ERROR 8: Insufficient room in filesystem",
"*ERROR 9: Maximum files in filesystem",
"*ERROR 10: Cannot update filesystem",
"*ERROR 11: Cannot read filesystem",
"*ERROR 12: Invalid filesystem",
"*ERROR 13: Mount table is full",
"*ERROR 14: Filesystem is not mounted",
"*ERROR 15: No filesystems are mounted",
"*ERROR 16: No filesystem has been specified",
"*ERROR 17: Filesystem is already mounted",
"*ERROR 18: Filesystem is in use",
"*ERROR 19: Disk is already installed",
"*ERROR 20: No disks installed",
"*ERROR 21: Disk not installed",
"*ERROR 22: File cannot be read",
"*ERROR 23: File cannot be created",
"*ERROR 24: File cannot be written",
"*ERROR 25: Specified minifile does not exist",
"*ERROR 26: Specified minifile already exists",
"*ERROR 27: Specified minifile cannot be written",
"*ERROR 28: Cannot read disk"
};
// next 3 lines: constants for the number of entries in each array
#define SIZE_ERROR_ARRAY (sizeof(ERROR)/sizeof(string))
#define SIZE_CMDS_ARRAY (sizeof(CMDS)/sizeof(string))
#define SIZE_FS_TYPE_ARRAY (sizeof(FS_TYPE)/sizeof(string))
#define MAX_DISK_BYTE_SIZE 100000 // max no. bytes for any disk
#define MAX_NUM_FS 10 // max no. file systems in S.O.S
#define MAX_NUM_FILES 500 // max no. of files on disk
/*############################### FUNCTIONS #################################*/
/******************************************************************************
** bool isPrintable(string str,int maxLength, bool isFsName);
Returns:
1. true if a) str is a valid disk name (isFsName must be false)
b) str is a valid filesystem name (isFsName must be true)
2. false otherwise
******************************************************************************/
inline bool isValidName(string str,int maxLength, bool isFsName)
{
// to hold no. of '.' or ':' characters
int numPeriods = 0, numColons = 0;
// make sure the length is:
// -- greater than 0 and
// -- less than maxLength
if((int)str.length() > maxLength || (int)str.length() == 0)
return false;
// make sure the first character is a letter
if(!isalpha(str.at(0)))
return false;
// check the rest of the string
for(int i = 0;i < (int)str.length(); i++)
{
// check for periods, at most 1 period allowed
if(str.at(i) == '.')
{
if(numPeriods > 0)
return false;
else numPeriods++;
}
// if it's a file system name it can have at most 1 ':'
else if(str.at(i) == ':' && isFsName)
{
if(numColons > 0)
return false;
else numColons++;
}
// if it's not ".", it must be alphanumeric(a,A-z,Z ; 0-9)
else if(!isalnum(str.at(i)))
return false;
}
// if it is a file system name the last char must be a ':'
if(isFsName)
{
if(str.at((int)str.length()-1)!= ':')
return false;
}
// if it got to this point, we got a valid name
return true;
}
/******************************************************************************
** int isValidNum(string str);
Returns:
1. -1 if str is not made up of all digits
2. the integer value of str
******************************************************************************/
inline int isValidNum(string str)
{
if(str.empty()) // theStr == ""
return -1;
// each char in the string MUST be a digit, otherwise return -1
for(int i = 0; i < (int)str.length(); i++)
if(!isdigit(str.at(i)))
return -1;
// integer value of the string
return atoi(str.c_str());
}
/******************************************************************************
** bool isFsType(string str);
Returns:
1. true if str is == to any of the allowed file system types
2. false otherwise
******************************************************************************/
inline bool isFsType(string str)
{
// if it's in the array, then it's valid
for(int i = 0; i < SIZE_FS_TYPE_ARRAY; i++)
if(str.compare(FS_TYPE[i]))
return true;
return false; // not in array
}
/******************************************************************************
** bool isPrintable(char ch);
Returns:
1. true if ch is one of the allowed printable characters
2. false otherwise
******************************************************************************/
inline bool isPrintable(char ch)
{
return (ch >= '!' && ch <= '~') ? true : false;
}
#endif // CONSTANTS_H