Skip to content
Snippets Groups Projects
Commit 1e789698 authored by rswindell's avatar rswindell
Browse files

Created NULL-terminated string list utility module (xpdev/str_list), used by

xpdev/ini_file and future modules that need to deal with string lists.
parent 628fff37
No related branches found
No related tags found
No related merge requests found
...@@ -104,9 +104,10 @@ CON_OBJS = $(LIBODIR)/sbbscon.o $(LIBODIR)/conwrap.o \ ...@@ -104,9 +104,10 @@ CON_OBJS = $(LIBODIR)/sbbscon.o $(LIBODIR)/conwrap.o \
CON_LDFLAGS = -lftpsrvr -lwebsrvr -lmailsrvr -lservices CON_LDFLAGS = -lftpsrvr -lwebsrvr -lmailsrvr -lservices
FTP_OBJS = $(LIBODIR)/ftpsrvr.o FTP_OBJS = $(LIBODIR)/ftpsrvr.o
MAIL_OBJS = $(LIBODIR)/mailsrvr.o $(LIBODIR)/mxlookup.o \ MAIL_OBJS = $(LIBODIR)/mailsrvr.o $(LIBODIR)/mxlookup.o \
$(LIBODIR)/mime.o $(LIBODIR)/base64.o $(LIBODIR)/ini_file.o $(LIBODIR)/mime.o $(LIBODIR)/base64.o $(LIBODIR)/ini_file.o \
$(LIBODIR)/str_list.o
WEB_OBJS = $(LIBODIR)/websrvr.o $(LIBODIR)/sockwrap.o $(LIBODIR)/base64.o WEB_OBJS = $(LIBODIR)/websrvr.o $(LIBODIR)/sockwrap.o $(LIBODIR)/base64.o
SERVICE_OBJS = $(LIBODIR)/services.o $(LIBODIR)/ini_file.o SERVICE_OBJS = $(LIBODIR)/services.o $(LIBODIR)/ini_file.o $(LIBODIR)/str_list.o
MONO_OBJS = $(CON_OBJS) $(FTP_OBJS) $(WEB_OBJS) \ MONO_OBJS = $(CON_OBJS) $(FTP_OBJS) $(WEB_OBJS) \
$(MAIL_OBJS) $(SERVICE_OBJS) $(MAIL_OBJS) $(SERVICE_OBJS)
......
...@@ -93,6 +93,7 @@ OBJS = $(LIBODIR)$(SLASH)ansiterm.$(OFILE)\ ...@@ -93,6 +93,7 @@ OBJS = $(LIBODIR)$(SLASH)ansiterm.$(OFILE)\
$(LIBODIR)$(SLASH)sockopts.$(OFILE)\ $(LIBODIR)$(SLASH)sockopts.$(OFILE)\
$(LIBODIR)$(SLASH)sortdir.$(OFILE)\ $(LIBODIR)$(SLASH)sortdir.$(OFILE)\
$(LIBODIR)$(SLASH)str.$(OFILE)\ $(LIBODIR)$(SLASH)str.$(OFILE)\
$(LIBODIR)$(SLASH)str_list.$(OFILE)\
$(LIBODIR)$(SLASH)str_util.$(OFILE)\ $(LIBODIR)$(SLASH)str_util.$(OFILE)\
$(LIBODIR)$(SLASH)telgate.$(OFILE)\ $(LIBODIR)$(SLASH)telgate.$(OFILE)\
$(LIBODIR)$(SLASH)telnet.$(OFILE)\ $(LIBODIR)$(SLASH)telnet.$(OFILE)\
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* @format.tab-size 4 (Plain Text/Source Code File Header) * * @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* * * *
* Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html * * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
* * * *
* This library is free software; you can redistribute it and/or * * This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License * * modify it under the terms of the GNU Lesser General Public License *
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <ctype.h> /* isdigit */ #include <ctype.h> /* isdigit */
#include "sockwrap.h" /* inet_addr */ #include "sockwrap.h" /* inet_addr */
#include "ini_file.h" #include "ini_file.h"
#include "str_list.h" /* strList functions */
#define INI_MAX_LINE_LEN 256 /* Maximum length of entire line, includes '\0' */ #define INI_MAX_LINE_LEN 256 /* Maximum length of entire line, includes '\0' */
...@@ -137,7 +138,6 @@ char** iniGetStringList(FILE* fp, const char* section, const char* key ...@@ -137,7 +138,6 @@ char** iniGetStringList(FILE* fp, const char* section, const char* key
char* value; char* value;
char buf[INI_MAX_VALUE_LEN]; char buf[INI_MAX_VALUE_LEN];
char** lp; char** lp;
char** np;
char* token; char* token;
char list[INI_MAX_VALUE_LEN]; char list[INI_MAX_VALUE_LEN];
ulong items=0; ulong items=0;
...@@ -153,32 +153,18 @@ char** iniGetStringList(FILE* fp, const char* section, const char* key ...@@ -153,32 +153,18 @@ char** iniGetStringList(FILE* fp, const char* section, const char* key
token=strtok(list,sep); token=strtok(list,sep);
while(token!=NULL) { while(token!=NULL) {
truncsp(token); truncsp(token);
if((np=realloc(lp,sizeof(char*)*(items+2)))==NULL) if(strListAdd(&lp,token,items++)==NULL)
break; break;
lp=np;
if((lp[items]=malloc(strlen(token)+1))==NULL)
break;
strcpy(lp[items++],token);
token=strtok(NULL,sep); token=strtok(NULL,sep);
} }
lp[items]=NULL; /* terminate list */
return(lp); return(lp);
} }
void* iniFreeStringList(char** list) void* iniFreeStringList(char** list)
{ {
ulong i; strListFree(&list);
return(list);
if(list==NULL)
return(NULL);
for(i=0;list[i]!=NULL;i++)
free(list[i]);
free(list);
return(NULL);
} }
void* iniFreeNamedStringList(named_string_t** list) void* iniFreeNamedStringList(named_string_t** list)
...@@ -205,7 +191,6 @@ char** iniGetSectionList(FILE* fp, const char* prefix) ...@@ -205,7 +191,6 @@ char** iniGetSectionList(FILE* fp, const char* prefix)
char* p; char* p;
char* tp; char* tp;
char** lp; char** lp;
char** np;
char str[INI_MAX_LINE_LEN]; char str[INI_MAX_LINE_LEN];
ulong items=0; ulong items=0;
...@@ -234,16 +219,10 @@ char** iniGetSectionList(FILE* fp, const char* prefix) ...@@ -234,16 +219,10 @@ char** iniGetSectionList(FILE* fp, const char* prefix)
if(prefix!=NULL) if(prefix!=NULL)
if(strnicmp(p,prefix,strlen(prefix))!=0) if(strnicmp(p,prefix,strlen(prefix))!=0)
continue; continue;
if((np=realloc(lp,sizeof(char*)*(items+2)))==NULL) if(strListAdd(&lp,p,items++)==NULL)
break; break;
lp=np;
if((lp[items]=malloc(strlen(p)+1))==NULL)
break;
strcpy(lp[items++],p);
} }
lp[items]=NULL; /* terminate list */
return(lp); return(lp);
} }
...@@ -252,7 +231,6 @@ char** iniGetKeyList(FILE* fp, const char* section) ...@@ -252,7 +231,6 @@ char** iniGetKeyList(FILE* fp, const char* section)
char* p; char* p;
char* tp; char* tp;
char** lp; char** lp;
char** np;
char str[INI_MAX_LINE_LEN]; char str[INI_MAX_LINE_LEN];
ulong items=0; ulong items=0;
...@@ -283,16 +261,10 @@ char** iniGetKeyList(FILE* fp, const char* section) ...@@ -283,16 +261,10 @@ char** iniGetKeyList(FILE* fp, const char* section)
continue; continue;
*tp=0; *tp=0;
truncsp(p); truncsp(p);
if((np=realloc(lp,sizeof(char*)*(items+2)))==NULL) if(strListAdd(&lp,p,items++)==NULL)
break;
lp=np;
if((lp[items]=malloc(strlen(p)+1))==NULL)
break; break;
strcpy(lp[items++],p);
} }
lp[items]=NULL; /* terminate list */
return(lp); return(lp);
} }
......
/* str_list.c */
/* Functions to deal with NULL-terminated string lists */
/* $Id$ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* See the GNU Lesser General Public License for more details: lgpl.txt or *
* http://www.fsf.org/copyleft/lesser.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
#include <stdlib.h> /* malloc */
#include "str_list.h"
size_t strListCount(char** list)
{
size_t i;
if(list==NULL)
return(0);
for(i=0;list[i]!=NULL;i++)
;
return(i);
}
char** strListAdd(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);
*list=lp;
if((lp[count]=malloc(strlen(str)+1))==NULL)
return(NULL);
strcpy(lp[count++],str);
lp[count]=NULL; /* terminate list */
return(lp);
}
void strListFree(char*** list)
{
size_t i;
if(*list!=NULL) {
for(i=0;*list[i]!=NULL;i++)
free(*list[i]);
free(*list);
}
}
/* str_list.h */
/* Functions to deal with NULL-terminated string lists */
/* $Id$ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* See the GNU Lesser General Public License for more details: lgpl.txt or *
* http://www.fsf.org/copyleft/lesser.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
#ifndef _STR_LIST_H
#define _STR_LIST_H
#include "gen_defs.h"
#if defined(__cplusplus)
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 the updated list or NULL on error */
char** strListAdd(char*** list, char* str, size_t count);
/* 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
#endif /* Don't add anything after this line */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment