diff --git a/source/compiler/sc.h b/source/compiler/sc.h index e256a18..b770ebe 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -264,8 +264,8 @@ enum { }; typedef struct s_stringlist { - struct s_stringlist *next; - char *line; + char **strings; + int size; } stringlist; typedef struct s_stringpair { diff --git a/source/compiler/sclist.c b/source/compiler/sclist.c index 139de90..4ca4ddc 100644 --- a/source/compiler/sclist.c +++ b/source/compiler/sclist.c @@ -135,71 +135,69 @@ static int delete_stringpair(stringpair *root,stringpair *item) } /* ----- string list functions ----------------------------------- */ -static stringlist *insert_string(stringlist *root,char *string) +static stringlist *insert_string(stringlist *list,char *string) { - stringlist *cur; + char **newstrings; + int newsize; + int i; assert(string!=NULL); - if ((cur=(stringlist*)malloc(sizeof(stringlist)))==NULL) - error(103); /* insufficient memory (fatal error) */ - if ((cur->line=duplicatestring(string))==NULL) - error(103); /* insufficient memory (fatal error) */ + if (list->strings==NULL) { + /* list is used for the first time */ + newsize=10; + if ((newstrings=calloc(newsize,sizeof(char*)))==NULL) + error(103); /* insufficient memory (fatal error) */ + list->strings=newstrings; + list->size=newsize; + } /* if */ + for (i=0; isize; i++) + if (list->strings[i]==NULL) + break; + if (i==list->size) { + /* double the size of the list */ + newsize=list->size*2; + if ((newstrings=realloc(list->strings,newsize*sizeof(char*)))==NULL) + error(103); /* insufficient memory (fatal error) */ + memset(newstrings+list->size,0,list->size*sizeof(char*)); + list->strings=newstrings; + list->size=newsize; + } /* if */ + if ((list->strings[i]=duplicatestring(string))==NULL) + error(103); /* insufficient memory (fatal error) */ /* insert as "last" */ - assert(root!=NULL); - while (root->next!=NULL) - root=root->next; - cur->next=root->next; - root->next=cur; - return cur; + return list; } -static char *get_string(stringlist *root,int index) +static char *get_string(stringlist *list,int index) { - stringlist *cur; - - assert(root!=NULL); - cur=root->next; - while (cur!=NULL && index-->0) - cur=cur->next; - if (cur!=NULL) { - assert(cur->line!=NULL); - return cur->line; - } /* if */ + assert(list!=NULL); + if (index>=0 && indexsize) + return list->strings[index]; return NULL; } -static int delete_string(stringlist *root,int index) +static int delete_string(stringlist *list,int index) { - stringlist *cur,*item; - - assert(root!=NULL); - for (cur=root; cur->next!=NULL && index>0; cur=cur->next,index--) - /* nothing */; - if (cur->next!=NULL) { - item=cur->next; - cur->next=item->next; /* unlink from list */ - assert(item->line!=NULL); - free(item->line); - free(item); - return TRUE; + assert(list!=NULL); + if (index>=0 && indexsize) { + if (list->strings[index]!=NULL) { + free(list->strings[index]); + list->strings[index]=NULL; + return TRUE; + } /* if */ } /* if */ return FALSE; } -SC_FUNC void delete_stringtable(stringlist *root) +SC_FUNC void delete_stringtable(stringlist *list) { - stringlist *cur,*next; - - assert(root!=NULL); - cur=root->next; - while (cur!=NULL) { - next=cur->next; - assert(cur->line!=NULL); - free(cur->line); - free(cur); - cur=next; - } /* while */ - memset(root,0,sizeof(stringlist)); + int i; + + assert(list!=NULL); + for (i=0; isize; i++) + free(list->strings[i]); + free(list->strings); + memset(list,0,sizeof(stringlist)); } @@ -235,7 +233,7 @@ SC_FUNC void delete_aliastable(void) } /* ----- include paths list -------------------------------------- */ -static stringlist includepaths = {NULL, NULL}; /* directory list for include files */ +static stringlist includepaths = {NULL, 0}; /* directory list for include files */ SC_FUNC stringlist *insert_path(char *path) { @@ -250,7 +248,7 @@ SC_FUNC char *get_path(int index) SC_FUNC void delete_pathtable(void) { delete_stringtable(&includepaths); - assert(includepaths.next==NULL); + assert(includepaths.strings==NULL); } @@ -323,7 +321,7 @@ SC_FUNC void delete_substtable(void) /* ----- input file list ----------------------------------------- */ -static stringlist sourcefiles = {NULL, NULL}; +static stringlist sourcefiles = {NULL, 0}; SC_FUNC stringlist *insert_sourcefile(char *string) { @@ -338,13 +336,13 @@ SC_FUNC char *get_sourcefile(int index) SC_FUNC void delete_sourcefiletable(void) { delete_stringtable(&sourcefiles); - assert(sourcefiles.next==NULL); + assert(sourcefiles.strings==NULL); } /* ----- documentation tags -------------------------------------- */ #if !defined SC_LIGHT -static stringlist docstrings = {NULL, NULL}; +static stringlist docstrings = {NULL, 0}; SC_FUNC stringlist *insert_docstring(char *string) { @@ -364,13 +362,13 @@ SC_FUNC void delete_docstring(int index) SC_FUNC void delete_docstringtable(void) { delete_stringtable(&docstrings); - assert(docstrings.next==NULL); + assert(docstrings.strings==NULL); } #endif /* !defined SC_LIGHT */ /* ----- autolisting --------------------------------------------- */ -static stringlist autolist = {NULL, NULL}; +static stringlist autolist = {NULL, 0}; SC_FUNC stringlist *insert_autolist(char *string) { @@ -385,7 +383,7 @@ SC_FUNC char *get_autolist(int index) SC_FUNC void delete_autolisttable(void) { delete_stringtable(&autolist); - assert(autolist.next==NULL); + assert(autolist.strings==NULL); } @@ -462,7 +460,7 @@ SC_FUNC void delete_heaplisttable(void) #define PRIxC "x" #endif -static stringlist dbgstrings = {NULL, NULL}; +static stringlist dbgstrings = {NULL, 0}; SC_FUNC stringlist *insert_dbgfile(const char *filename) { @@ -530,5 +528,5 @@ SC_FUNC char *get_dbgstring(int index) SC_FUNC void delete_dbgstringtable(void) { delete_stringtable(&dbgstrings); - assert(dbgstrings.next==NULL); + assert(dbgstrings.strings==NULL); }