比较多个字符而不使用字符串比较函数
Compare multiple chars without using string compare functions
我正在尝试使用可以在 mode
参数中传递到我的 File 类 构造函数中的参数来创建一个 switch 语句。但是,由于模式最多可以有 2 个字符('r'、'r+'、'w' 或 'w+'),我不知道如何创建一个 switch 语句来获取这是为我做的。
有人对如何实现这个有什么建议吗?目前我在下面看到的是 'r+' 和 'w+' 情况下的错误,因为它们不是字符。
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.
}
您不能使用多个值以防万一。
1 - 您可以使用多个开关:
switch(mode[0]){
case 'r':
switch(mode[1]){
case '+':
switch(mode[2]){
case 0:
fileMode = O_RDWR | O_CREAT;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
case 0:
fileMode = O_RDONLY | O_CREAT;
canWeRead = true;
canWeWrite = false;
break;
}
break;
case 'w':
switch(mode[1]){
case '+':
switch(mode[2]){
case 0:
fileMode = O_RDWR | O_CREAT | O_TRUNC;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
case 0:
fileMode = O_WRONLY | O_CREAT;
canWeRead = false;
canWeWrite = true;
break;
}
break;
default:
assert(0);
}
2 - 您可以创建 mode
的类型 char[4] 并进行丑陋的函数调用。喜欢File("filename", {'r', '+'})
(这种方式会受到字节序差异的影响)
switch(*(uint32_t *)mode){
// r
case 0x72000000:
fileMode = O_RDONLY | O_CREAT;
canWeRead = true;
canWeWrite = false;
break;
// r+
case 0x722b0000:
fileMode = O_RDWR | O_CREAT;
canWeRead = true;
canWeWrite = true;
break;
// w
case 0x77000000:
fileMode = O_WRONLY | O_CREAT;
canWeRead = false;
canWeWrite = true;
break;
// w+
case 0x772b0000:
fileMode = O_RDWR | O_CREAT | O_TRUNC;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
无论如何,对于简单的事情来说,这些方法实在是太矫枉过正了。系统调用比字符串比较要多吃数千倍的时间。
我正在尝试使用可以在 mode
参数中传递到我的 File 类 构造函数中的参数来创建一个 switch 语句。但是,由于模式最多可以有 2 个字符('r'、'r+'、'w' 或 'w+'),我不知道如何创建一个 switch 语句来获取这是为我做的。
有人对如何实现这个有什么建议吗?目前我在下面看到的是 'r+' 和 'w+' 情况下的错误,因为它们不是字符。
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.
}
您不能使用多个值以防万一。
1 - 您可以使用多个开关:
switch(mode[0]){
case 'r':
switch(mode[1]){
case '+':
switch(mode[2]){
case 0:
fileMode = O_RDWR | O_CREAT;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
case 0:
fileMode = O_RDONLY | O_CREAT;
canWeRead = true;
canWeWrite = false;
break;
}
break;
case 'w':
switch(mode[1]){
case '+':
switch(mode[2]){
case 0:
fileMode = O_RDWR | O_CREAT | O_TRUNC;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
case 0:
fileMode = O_WRONLY | O_CREAT;
canWeRead = false;
canWeWrite = true;
break;
}
break;
default:
assert(0);
}
2 - 您可以创建 mode
的类型 char[4] 并进行丑陋的函数调用。喜欢File("filename", {'r', '+'})
(这种方式会受到字节序差异的影响)
switch(*(uint32_t *)mode){
// r
case 0x72000000:
fileMode = O_RDONLY | O_CREAT;
canWeRead = true;
canWeWrite = false;
break;
// r+
case 0x722b0000:
fileMode = O_RDWR | O_CREAT;
canWeRead = true;
canWeWrite = true;
break;
// w
case 0x77000000:
fileMode = O_WRONLY | O_CREAT;
canWeRead = false;
canWeWrite = true;
break;
// w+
case 0x772b0000:
fileMode = O_RDWR | O_CREAT | O_TRUNC;
canWeRead = true;
canWeWrite = true;
break;
default:
assert(0);
}
无论如何,对于简单的事情来说,这些方法实在是太矫枉过正了。系统调用比字符串比较要多吃数千倍的时间。