From add08ce21205eb3a1bf1a078b7c487b9a35dc479 Mon Sep 17 00:00:00 2001 From: rswindell <> Date: Fri, 28 May 2004 01:45:50 +0000 Subject: [PATCH] Changed use of strListAdd(). Removed ustrListAddAt() (not needed). Created iniReadFile() and iniWriteFile(). --- src/xpdev/ini_file.c | 31 ++++++++++++++++++++++++++++--- src/xpdev/ini_file.h | 6 ++++++ src/xpdev/str_list.c | 18 ++++++++---------- src/xpdev/str_list.h | 5 +---- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/xpdev/ini_file.c b/src/xpdev/ini_file.c index 11592b2232..14ee5cda1c 100644 --- a/src/xpdev/ini_file.c +++ b/src/xpdev/ini_file.c @@ -39,6 +39,7 @@ #include <string.h> /* strlen */ #include <ctype.h> /* isdigit */ #include "sockwrap.h" /* inet_addr */ +#include "filewrap.h" /* chsize */ #include "ini_file.h" #define INI_MAX_LINE_LEN 256 /* Maximum length of entire line, includes '\0' */ @@ -152,7 +153,7 @@ str_list_t iniGetStringList(FILE* fp, const char* section, const char* key token=strtok(list,sep); while(token!=NULL) { truncsp(token); - if(strListAddAt(&lp,token,items++)==NULL) + if(strListAdd(&lp,token,items++)==NULL) break; token=strtok(NULL,sep); } @@ -215,7 +216,7 @@ str_list_t iniGetSectionList(FILE* fp, const char* prefix) if(prefix!=NULL) if(strnicmp(p,prefix,strlen(prefix))!=0) continue; - if(strListAddAt(&lp,p,items++)==NULL) + if(strListAdd(&lp,p,items++)==NULL) break; } @@ -255,7 +256,7 @@ str_list_t iniGetKeyList(FILE* fp, const char* section) continue; *tp=0; truncsp(p); - if(strListAddAt(&lp,p,items++)==NULL) + if(strListAdd(&lp,p,items++)==NULL) break; } @@ -433,3 +434,27 @@ ulong iniGetBitField(FILE* fp, const char* section, const char* key, return(v); } + +str_list_t iniReadFile(FILE* fp) +{ + size_t i; + str_list_t list; + + rewind(fp); + + list = strListReadFile(fp, NULL, INI_MAX_VALUE_LEN); + if(list!=NULL) { + /* truncate the white-space off end of strings */ + for(i=0; list[i]!=NULL; i++) + truncsp(list[i]); + } + + return(list); +} + +BOOL iniWriteFile(FILE* fp, const str_list_t list) +{ + rewind(fp); + chsize(fileno(fp),0); /* truncate */ + return(strListWriteFile(fp,list,"\n") == strListCount(list)); +} diff --git a/src/xpdev/ini_file.h b/src/xpdev/ini_file.h index d438b75f4f..fcc452e69d 100644 --- a/src/xpdev/ini_file.h +++ b/src/xpdev/ini_file.h @@ -85,6 +85,12 @@ void* iniFreeStringList(str_list_t list); /* Free named string list returned from iniGetNamedStringList */ void* iniFreeNamedStringList(named_string_t** list); + +/* File I/O Functions */ +str_list_t iniReadFile(FILE* fp); +BOOL iniWriteFile(FILE* fp, const str_list_t list); + + #if defined(__cplusplus) } #endif diff --git a/src/xpdev/str_list.c b/src/xpdev/str_list.c index dc6fd9203c..ba8baf8dcc 100644 --- a/src/xpdev/str_list.c +++ b/src/xpdev/str_list.c @@ -122,7 +122,7 @@ str_list_t strListRemove(str_list_t* list, size_t index) return(lp); } -str_list_t strListAddAt(str_list_t* list, const char* str, size_t count) +str_list_t strListAdd(str_list_t* list, const char* str, size_t index) { char* buf; @@ -131,12 +131,10 @@ str_list_t strListAddAt(str_list_t* list, const char* str, size_t count) strcpy(buf,str); - return(str_list_append(list,buf,count)); -} + if(index==0) + index=strListCount(*list); -str_list_t strListAdd(str_list_t* list, const char* str) -{ - return strListAddAt(list,str,strListCount(*list)); + return(str_list_append(list,buf,index)); } str_list_t strListAddList(str_list_t* list, const str_list_t add_list) @@ -146,7 +144,7 @@ str_list_t strListAddList(str_list_t* list, const str_list_t add_list) count=strListCount(*list); for(i=0; add_list[i]!=NULL; i++) - strListAddAt(list,add_list[i],count++); + strListAdd(list,add_list[i],count++); return(*list); } @@ -183,7 +181,7 @@ str_list_t strListSplit(str_list_t* list, char* str, const char* delimit) } for(token = strtok(str, delimit); token!=NULL; token=strtok(NULL, delimit)) - strListAdd(list, token); + strListAdd(list, token, 0); return(*list); } @@ -294,7 +292,7 @@ str_list_t strListReadFile(FILE* fp, str_list_t* list, size_t max_line_len) while(!feof(fp)) { if(fgets(buf,max_line_len+1,fp)==NULL) break; - strListAddAt(list, buf, count++); + strListAdd(list, buf, count++); } free(buf); @@ -312,7 +310,7 @@ size_t strListWriteFile(FILE* fp, const str_list_t list, const char* separator) for(i=0; list[i]!=NULL; i++) { if(fputs(list[i],fp)==EOF) break; - if(seperator!=NULL && fputs(seperator,fp)==EOF) + if(separator!=NULL && fputs(separator,fp)==EOF) break; } diff --git a/src/xpdev/str_list.h b/src/xpdev/str_list.h index 2a993dd0e2..3c5ac12910 100644 --- a/src/xpdev/str_list.h +++ b/src/xpdev/str_list.h @@ -57,10 +57,7 @@ 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 strListAdd(str_list_t* list, const char* str); - -/* Adds a string into the list at a specific index */ -str_list_t strListAddAt(str_list_t* list, const char* str, size_t index); +str_list_t strListAdd(str_list_t* list, const char* str, size_t index); /* Append a string list onto another string list */ str_list_t strListAddList(str_list_t* list, const str_list_t append_list); -- GitLab