Commit 6371a00f authored by rswindell's avatar rswindell
Browse files

Created strListAlloc() to allocate and pre-terminate a string list.

Created strListAddAt() to add a string at a known index in the string list.
parent b87d76b0
......@@ -147,17 +147,16 @@ char** iniGetStringList(FILE* fp, const char* section, const char* key
SAFECOPY(list,value);
if((lp=malloc(sizeof(char*)))==NULL)
if((lp=strListAlloc())==NULL)
return(NULL);
token=strtok(list,sep);
while(token!=NULL) {
truncsp(token);
if(strListAdd(&lp,token,items++)==NULL)
if(strListAddAt(&lp,token,items++)==NULL)
break;
token=strtok(NULL,sep);
}
return(lp);
}
......@@ -194,11 +193,9 @@ char** iniGetSectionList(FILE* fp, const char* prefix)
char str[INI_MAX_LINE_LEN];
ulong items=0;
if((lp=malloc(sizeof(char*)))==NULL)
if((lp=strListAlloc())==NULL)
return(NULL);
*lp=NULL;
if(fp==NULL)
return(lp);
......@@ -219,7 +216,7 @@ char** iniGetSectionList(FILE* fp, const char* prefix)
if(prefix!=NULL)
if(strnicmp(p,prefix,strlen(prefix))!=0)
continue;
if(strListAdd(&lp,p,items++)==NULL)
if(strListAddAt(&lp,p,items++)==NULL)
break;
}
......@@ -234,11 +231,9 @@ char** iniGetKeyList(FILE* fp, const char* section)
char str[INI_MAX_LINE_LEN];
ulong items=0;
if((lp=malloc(sizeof(char*)))==NULL)
if((lp=strListAlloc())==NULL)
return(NULL);
*lp=NULL;
if(fp==NULL)
return(lp);
......@@ -261,7 +256,7 @@ char** iniGetKeyList(FILE* fp, const char* section)
continue;
*tp=0;
truncsp(p);
if(strListAdd(&lp,p,items++)==NULL)
if(strListAddAt(&lp,p,items++)==NULL)
break;
}
......
......@@ -38,6 +38,17 @@
#include <stdlib.h> /* malloc */
#include "str_list.h"
char** strListAlloc()
{
char** list;
if((list=malloc(sizeof(char*)))==NULL)
return(NULL);
list[0]=NULL; /* terminated by default */
return(list);
}
size_t strListCount(char** list)
{
size_t i;
......@@ -51,13 +62,10 @@ size_t strListCount(char** list)
return(i);
}
char** strListAdd(char*** list, char* str, size_t count)
char** strListAddAt(char*** list, char* str, size_t count)
{
char** lp;
if(!count)
count=strListCount(*list);
if((lp=realloc(*list,sizeof(char*)*(count+2)))==NULL)
return(NULL);
......@@ -71,6 +79,12 @@ char** strListAdd(char*** list, char* str, size_t count)
return(lp);
}
char** strListAdd(char*** list, char* str)
{
return strListAddAt(list,str,strListCount(*list));
}
void strListFree(char*** list)
{
size_t i;
......
......@@ -44,17 +44,22 @@
extern "C" {
#endif
/* Pass a pointer to a string list, the string to add, and the current list count if known */
/* (or 0 if unknown) */
/* Returns an allocated and terminated string list */
char** strListAlloc(void);
/* Frees the strings in the list (and the list itself) */
void strListFree(char*** list);
/* Pass a pointer to a string list, the string to add */
/* Returns the updated list or NULL on error */
char** strListAdd(char*** list, char* str, size_t count);
char** strListAdd(char*** list, char* str);
/* Adds a string into the list at a specific index */
char** strListAddAt(char*** list, char* str, size_t index);
/* Count the number of strings in the list and returns the count */
size_t strListCount(char** list);
/* Frees the strings in the list (and the list itself) */
void strListFree(char*** list);
#if defined(__cplusplus)
}
#endif
......
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