intcharacter_is_symbol(charcharacter){// Returns TRUE / 1 if character is one of the characters in that string (array of characters), otherwise it returns FALSE / 0.
intcharacter_compare_array(charcharacter,char*character_array){// I didn't use name "string", but "character_array", to explicitly show the intention of argument.
intoffset;
for(offset=0;offset!=string_length(character_array);++offset){// We iterate through string (character array!) and return TRUE / 1 if we found it.
if(character==character_array[offset]){// If we don't find it in that string, we return FALSE / 0 since it's not there.
return(TRUE);// Note that we could do this without the variable 'offset', similar to string functions.
// - O_RDONLY: Open or create file as 'read only', prohibit writing to that file.
// - O_WRONLY: Open or create file as 'write only', so you have permission to modify it.
// - O_RDWR: Open or create file as 'read and write', so you can do whatever you want with it...
// - O_APPEND: Before each write system call, the file offset is positioned at the end of the file, as if with lseek system call. Like, continue writing...
// - O_CREAT: If path name doesn't exist, create a new file with the name you specified. The owner of the new file is set to the effective user ID of the process.
// - O_TRUNC: If the file already exists and is a regular file and the access mode allows writing (is O_RDWR or O_WRONLY) it will be truncated to length 0.
intdescriptor=-1;// Assume error value as default.
fatal_failure(name==NULL,"file_open: Failed to open file, name is null pointer.");// We must provide non-null address.
fatal_failure((descriptor=open(name,mode))==-1,"file_open: Failed to open file, function open returned invalid descriptor.");// We abort of 'open' error...
// We could write something like this too:
// descriptor = open (name, mode);
// fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
// Or align it to break two longer function arguments:
// fatal_failure ((descriptor = open (name, mode)) == -1,
// "file_open: Failed to open file, function open returned invalid descriptor.");
return(descriptor);// Return opened file descriptor.
}
intfile_close(intfile){
fatal_failure(file==-1,"file_close: Failed to close file, invalid file descriptor.");// If 'file' was already closed or corrupted, we abort.
fatal_failure(close(file)==-1,"file_close: Failed to close file, function close returned invalid code.");// Keep in mind that this isn't always safe.
return(-1);
}
voidfile_read(intfile,void*data,intsize){
fatal_failure(file==-1,"file_read: Failed to read from file, invalid descriptor.");// We'll comment this out once, since it's all similar with 'file_write'.
fatal_failure(data==NULL,"file_read: Failed to read from file, data is null pointer.");// This function is very similar to 'in', but it accepts a file descriptor.
fatal_failure(size==0,"file_read: Failed to read from file, size is zero.");// That means we handle the files, not standard input or output.
(void)read(file,data,(unsignedlongint)size);// If there was no errors, we read, and don't check for errors at all...
}
voidfile_write(intfile,void*data,intsize){
fatal_failure(file==-1,"file_write: Failed to write to file, invalid descriptor.");
fatal_failure(data==NULL,"file_write: Failed to write to file, data is null pointer.");
fatal_failure(size==0,"file_write: Failed to write to file, size is zero.");
(void)write(file,data,(unsignedlongint)size);
}
intfile_seek(intfile,intwhence){
fatal_failure(file==-1,"file_seek: Failed to seek in file, invalid descriptor.");// Make sure we have a valid file descriptor (it's also unsafe to assume it)...
return((int)lseek(file,0,whence));// Keep in mind that C isn't safe language. It's safe only if you use your brain.
}
intfile_size(char*name){
intsize=-1;// Lets just assume that everything is wrong, everything falls apart...
// Keep in mind that I'm intentionally being inconsistent, so you can see several ways to properly align your code, readability is the key to safety!
// Spacing between separate strings in array below is 10 characters, including comma and double quotes, and I "joined" curly braces too, it fits in 180 characters.
// You could break it up on curly braces, or put each string in it's own line if you wanted.