Commit 8e1e5c9c authored by rswindell's avatar rswindell
Browse files

Created strListDelete() and strListReplace().

Changed some return types to more logical/useful values.
parent e0368e71
......@@ -64,7 +64,7 @@ size_t strListCount(const str_list_t list)
return(i);
}
static str_list_t str_list_append(str_list_t* list, char* str, size_t index)
static char* str_list_append(str_list_t* list, char* str, size_t index)
{
str_list_t lp;
......@@ -75,10 +75,10 @@ static str_list_t str_list_append(str_list_t* list, char* str, size_t index)
lp[index++]=str;
lp[index]=NULL; /* terminate list */
return(lp);
return(str);
}
static str_list_t str_list_insert(str_list_t* list, char* str, size_t index)
static char* str_list_insert(str_list_t* list, char* str, size_t index)
{
size_t i;
size_t count;
......@@ -97,16 +97,21 @@ static str_list_t str_list_insert(str_list_t* list, char* str, size_t index)
lp[i]=lp[i-1];
lp[index]=str;
return(lp);
return(str);
}
str_list_t strListRemove(str_list_t* list, size_t index)
char* strListRemove(str_list_t* list, size_t index)
{
char* str;
size_t i;
size_t count;
str_list_t lp;
count = strListCount(*list);
if(index==STR_LIST_LAST_INDEX && count)
index = count-1;
if(index >= count) /* invalid index, do nothing */
return(NULL);
......@@ -115,14 +120,49 @@ str_list_t strListRemove(str_list_t* list, size_t index)
return(NULL);
*list=lp;
str=lp[index];
for(i=index; i<count; i++)
lp[i]=lp[i+1];
lp[count]=NULL;
return(lp);
return(str);
}
BOOL strListDelete(str_list_t* list, size_t index)
{
char* str;
if((str=strListRemove(list, index))==NULL)
return(FALSE);
free(str);
return(TRUE);
}
char* strListReplace(const str_list_t list, size_t index, const char* str)
{
char* buf;
size_t count;
count = strListCount(list);
if(index==STR_LIST_LAST_INDEX && count)
index = count-1;
if(index >= count) /* invalid index, do nothing */
return(NULL);
if((buf=(char*)realloc(list[index],strlen(str)+1))==NULL)
return(NULL);
list[index]=buf;
strcpy(buf,str);
return(buf);
}
str_list_t strListAppend(str_list_t* list, const char* str, size_t index)
char* strListAppend(str_list_t* list, const char* str, size_t index)
{
char* buf;
......@@ -131,13 +171,13 @@ str_list_t strListAppend(str_list_t* list, const char* str, size_t index)
strcpy(buf,str);
if(index==STR_LIST_APPEND)
if(index==STR_LIST_LAST_INDEX)
index=strListCount(*list);
return(str_list_append(list,buf,index));
}
str_list_t strListAppendList(str_list_t* list, const str_list_t add_list)
size_t strListAppendList(str_list_t* list, const str_list_t add_list)
{
size_t i;
size_t count;
......@@ -146,10 +186,10 @@ str_list_t strListAppendList(str_list_t* list, const str_list_t add_list)
for(i=0; add_list[i]!=NULL; i++)
strListAppend(list,add_list[i],count++);
return(*list);
return(count);
}
str_list_t strListInsert(str_list_t* list, const char* str, size_t index)
char* strListInsert(str_list_t* list, const char* str, size_t index)
{
char* buf;
......@@ -161,29 +201,37 @@ str_list_t strListInsert(str_list_t* list, const char* str, size_t index)
return(str_list_insert(list,buf,index));
}
str_list_t strListInsertList(str_list_t* list, const str_list_t add_list, size_t index)
size_t strListInsertList(str_list_t* list, const str_list_t add_list, size_t index)
{
size_t i;
for(i=0; add_list[i]!=NULL; i++)
strListInsert(list,add_list[i],index++);
if(strListInsert(list,add_list[i],index++)==NULL)
break;
return(*list);
return(i);
}
str_list_t strListSplit(str_list_t* list, char* str, const char* delimit)
str_list_t strListSplit(str_list_t* lp, char* str, const char* delimit)
{
size_t count;
char* token;
str_list_t list;
if(list==NULL) {
if((*list = strListInit())==NULL)
return(NULL);
}
if(lp==NULL) {
if((list = strListInit())==NULL)
return(0);
lp=&list;
count=0;
} else
count=strListCount(*lp);
for(token = strtok(str, delimit); token!=NULL; token=strtok(NULL, delimit))
strListAppend(list, token, STR_LIST_APPEND);
if(strListAppend(lp, token, count++)==NULL)
break;
return(*list);
return(*lp);
}
str_list_t strListSplitCopy(str_list_t* list, const char* str, const char* delimit)
......@@ -195,22 +243,23 @@ str_list_t strListSplitCopy(str_list_t* list, const char* str, const char* delim
strcpy(buf,str);
*list = strListSplit(list,buf,delimit);
*list=strListSplit(list,buf,delimit);
free(buf);
return(*list);
}
str_list_t strListMerge(str_list_t* list, str_list_t add_list)
size_t strListMerge(str_list_t* list, str_list_t add_list)
{
size_t i,j;
size_t i;
size_t count;
j=strListCount(*list);
count=strListCount(*list);
for(i=0; add_list[i]!=NULL; i++)
str_list_append(list,add_list[i],j++);
str_list_append(list,add_list[i],count++);
return(*list);
return(i);
}
static int strListCompareAlpha(const void *arg1, const void *arg2)
......@@ -272,20 +321,22 @@ void strListFree(str_list_t* list)
}
}
str_list_t strListReadFile(FILE* fp, str_list_t* list, size_t max_line_len, BOOL pad)
str_list_t strListReadFile(FILE* fp, str_list_t* lp, size_t max_line_len, BOOL pad)
{
char* buf=NULL;
size_t count;
str_list_t list;
if(max_line_len<1)
max_line_len=2048;
if(list==NULL) {
if((*list = strListInit())==NULL)
if(lp==NULL) {
if((list = strListInit())==NULL)
return(NULL);
lp=&list;
}
count = strListCount(*list);
count = strListCount(*lp);
while(!feof(fp)) {
if(buf==NULL && (buf=malloc(max_line_len+1))==NULL)
return(NULL);
......@@ -293,16 +344,16 @@ str_list_t strListReadFile(FILE* fp, str_list_t* list, size_t max_line_len, BOOL
if(fgets(buf,max_line_len+1,fp)==NULL)
break;
if(pad) {
str_list_append(list, buf, count++);
str_list_append(lp, buf, count++);
buf=NULL;
} else
strListAppend(list, buf, count++);
strListAppend(lp, buf, count++);
}
if(buf)
if(buf!=NULL)
free(buf);
return(*list);
return(*lp);
}
size_t strListWriteFile(FILE* fp, const str_list_t list, const char* separator)
......
......@@ -45,7 +45,7 @@
extern "C" {
#endif
#define STR_LIST_APPEND (~0)
#define STR_LIST_LAST_INDEX (~0)
typedef char** str_list_t;
......@@ -60,19 +60,25 @@ void strListFreeStrings(str_list_t list);
/* Pass a pointer to a string list, the string to add (append) */
/* Returns the updated list or NULL on error */
str_list_t strListAppend(str_list_t* list, const char* str, size_t index);
char* strListAppend(str_list_t* list, const char* str, size_t index);
/* Append a string list onto another string list */
str_list_t strListAppendList(str_list_t* list, const str_list_t append_list);
size_t strListAppendList(str_list_t* list, const str_list_t append_list);
/* Inserts a string into the list at a specific index */
str_list_t strListInsert(str_list_t* list, const char* str, size_t index);
char* strListInsert(str_list_t* list, const char* str, size_t index);
/* Insert a string list into another string list */
str_list_t strListInsertList(str_list_t* list, const str_list_t append_list, size_t index);
size_t strListInsertList(str_list_t* list, const str_list_t append_list, size_t index);
/* Remove a string at a specific index */
str_list_t strListRemove(str_list_t* list, size_t index);
char* strListRemove(str_list_t* list, size_t index);
/* Remove and free a string at a specific index */
BOOL strListDelete(str_list_t* list, size_t index);
/* Replace a string at a specific index */
char* strListReplace(const str_list_t list, size_t index, const char* str);
/* Add to an exiting or new string list by splitting specified string (str) */
/* into multiple strings, separated by one of the delimit characters */
......@@ -82,7 +88,7 @@ str_list_t strListSplit(str_list_t* list, char* str, const char* delimit);
str_list_t strListSplitCopy(str_list_t* list, const char* str, const char* delimit);
/* Merge 2 string lists (no copying of string data) */
str_list_t strListMerge(str_list_t* list, str_list_t append_list);
size_t strListMerge(str_list_t* list, str_list_t append_list);
/* Count the number of strings in the list and returns the count */
size_t strListCount(const str_list_t list);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment