#include #include #include #include #include "strlist.h" static str_node *strlist_head = NULL ; static str_node *strlist_last = NULL ; /************************************************************* Print message and abort. */ static void crash_and_burn(char * mesg) { fprintf (stderr, "Error: %s\n", mesg) ; abort() ; } /************************************************************* Initialize a linked list of strings with dummy header. */ void strlist_init() { str_node *tptr ; if (! (tptr = malloc(sizeof(str_node)))) crash_and_burn("malloc() out of memory!!") ; tptr->number = -1 ; tptr->str = NULL ; tptr->next = NULL ; strlist_head = tptr ; strlist_last = tptr ; } /************************************************************* Add str_node at the end. */ void strlist_append(int number, char *str) { str_node *tptr ; if (! (tptr = malloc(sizeof(str_node)))) crash_and_burn("malloc() out of memory!!") ; tptr->number = number ; tptr->str = strdup(str) ; tptr->next = NULL ; assert(strlist_last != NULL) ; strlist_last->next = tptr ; strlist_last = tptr ; } /************************************************************* Deallocate list */ void strlist_destroy(char *str) { str_node *ptr1, *fptr; ptr1 = strlist_head ; strlist_head = NULL ; strlist_last = NULL ; while(ptr1 != NULL) { fptr = ptr1 ; ptr1 = ptr1->next ; free(fptr->str) ; /* OK to free NULL pointer */ free(fptr) ; } } /************************************************************* Print out string constants */ void strlist_output(FILE *of) { str_node *ptr ; fprintf(of, "\n; String constants\n") ; assert(strlist_head != NULL) ; ptr = strlist_head->next ; while(ptr != NULL) { fprintf(of, "%s%d:\t", STR_LABEL_PREFIX, ptr->number) ; fprintf(of, "db\t\"%s\", 0\n", ptr->str) ; ptr = ptr->next ; } fprintf(of, "\n") ; }