I am trying to create a switch statement using the parameters that can be passed into my File classes constructor in the mode parameter. However, as mode can be up to 2 chars ('r', 'r+', 'w', or 'w+'), I do not know how to create a switch statement that will get this done for me.
Does anyone have any suggestions on how to implement this? Currently with what I have below, I am getting errors for the 'r+' and 'w+' cases, as those are not chars.
File::File(const char *name, const char *mode) {
int fileMode = 0; //Initializing mode to 0
fileName = name; //Setting the file name of this File object to the name we pass in.
switch(*mode){
case 'r':
fileMode = O_RDONLY | O_CREAT;
canWeRead = true;
canWeWrite = false;
break;
// Open a text file for update (that is, for both reading and writing)
case 'r+':
fileMode = O_RDWR | O_CREAT;
canWeRead = true;
canWeWrite = true;
break;
case 'w':
fileMode = O_WRONLY | O_CREAT;
canWeRead = false;
canWeWrite = true;
break;
// Open a text file for update (reading and writing),
// first truncating the file to zero length if it exists
// or creating the file if it does not exist.
case 'w+':
fileMode = O_RDWR | O_CREAT | O_TRUNC;
canWeRead = true;
canWeWrite = true;
break;
default: //We should never reach the default case, so I assert(0)
assert(0);
}
fileBuffer = (char*)malloc(bufsiz); //Create buffer with malloc
//Free is called in the File class destructor.
assert(fileBuffer != NULL); //If fileBuffer == NULL, we did not allocate a buffer.
fileDescriptor = open(name, fileMode);
/*On success, open() returns the new file descriptor (a nonnegative integer).
On error, -1 is returned and errno is set to indicate the error.*/
assert(fileDescriptor >= 0); //If we dont get a positive nonnegative int, we have a problem.
}
File::~File() {
free(fileBuffer); //Free the memory we have for the buffer
int rc = close(this->fileDescriptor); //We want to close the FileDescriptor of the current File object.
assert(rc == 0); //close returns 0 on success. So if we dont get 0, we have a problem.
}