/* * jims_utils.c * * Various C utility functions * * Jim Mahoney | cs.marlboro.edu | March 2011 | GPL */ #include "jims_utils.h" #define DEBUG 0 // Print error_message and exit. char error_message[80]; void die(){ printf("*** in jims_utils *** %s\n", error_message); exit(EXIT_FAILURE); } // versions of C functions that exit with error if they fail. void* malloc_safe(size_t size){ void* buffer = malloc(size); if (buffer == NULL){ sprintf(error_message, "malloc failed to get %lu bytes", size); die(); } return buffer; } void* calloc_safe(size_t count, size_t element_size){ void* buffer = calloc(count, element_size); if (buffer == NULL){ sprintf(error_message, "calloc failed to get %lu bytes", count * element_size); die(); } return buffer; } FILE* fopen_safe(const char* filename, const char* mode){ FILE* file = fopen(filename, mode); if (file == NULL){ sprintf(error_message, "fopen failed to open file '%s' with mode %s", filename, mode); die(); } return file; } // Read an entire file into memory and return a pointer to a buffer // containing its contents. The size of the buffer will be one byte // larger than the file, with a trailing 0 byte. If anything goes // wrong (e.g. there isn't enough memory or the file can't be found), // print an error and quit the program with status=1 (Based in part on // code from http://www.mrx.net/c/program.html.) char* slurpfile(char* filename){ // open the file FILE* file = fopen_safe(filename, "r"); char* buffer; long filelength, bytesread; // find its length fseek(file, 0L, SEEK_END); filelength = ftell(file); if (DEBUG) printf(" debug: slurp file length = %lu \n", filelength); rewind(file); // allocate the buffer for its contents buffer = calloc_safe(filelength, sizeof(char)); // read in 1 thing of length filelength bytesread = fread(buffer, filelength, 1, file); if (bytesread != filelength){ sprintf(error_message, "slurp expected %lu bytes; got %lu", filelength, bytesread); } close(file); return buffer; } // Return 1 if character is letter, 0 otherwise. int isletter(char character){ // ascii for 'a' to 'z', 'A' to 'Z' are 97 to 122 and 65 to 90 if (DEBUG) printf(" isletter: %c \n", character); return ((character >= 97 && character <= 122) || (character >= 65 && character <= 90)); } int getnextword(char* text, int* offset, char* word, int maxsize){ int wordstart; if (DEBUG){ printf(" getnextword: text = %p ; text[0] = %c \n", text, text[0]); printf(" getnextword: word = %p ; offset = %i \n", word, *offset); } while (text[*offset] && !isletter(text[*offset])) (*offset)++; // skip to word if (DEBUG) printf(" getnextword: found word char; *offset=%i, text[*offset] = %c \n", *offset, text[*offset]); wordstart = *offset; while (isletter(text[*offset])) (*offset)++; // continue while word chars if ((wordstart == *offset) || ((*offset)-wordstart+1 > maxsize)) return 0; if (DEBUG) { printf(" getnextword: at end of word; *offset=%i \n", *offset); printf(" will memcopy from %p to %p , %i chars \n", &text[wordstart], word, (*offset)-wordstart); } memcpy(word, &text[wordstart], (*offset)-wordstart); word[(*offset)-wordstart] = 0; // null terminate word string return 1; } // int to alpha char* itoa(int i){ char* string = calloc(16, sizeof(char)); sprintf(string, "%i", i); return string; }