diff --git a/src/xpdev/ini_file.c b/src/xpdev/ini_file.c index 57ffa4a96f77a16f79aaa1ba6c05f0af97ccef2a..99fe700c136e8de35f0b23d14239c148aba87bde 100644 --- a/src/xpdev/ini_file.c +++ b/src/xpdev/ini_file.c @@ -160,21 +160,24 @@ static char* get_value(FILE* fp, const char* section, const char* key, char* val return(NULL); } -static size_t find_value_index(str_list_t list, const char* section, const char* key) +static size_t find_value_index(str_list_t list, const char* section, const char* key, char* value) { char str[INI_MAX_LINE_LEN]; char* p; char* vp; size_t i; + value[0]=0; for(i=find_section_index(list, section); list[i]!=NULL; i++) { SAFECOPY(str, list[i]); if((p=key_name(str,&vp))==NULL) continue; if(p==NEW_SECTION) break; - if(stricmp(p,key)==0) - return(i); + if(stricmp(p,key)!=0) + continue; + strcpy(value,vp); + return(i); } return(i); @@ -198,6 +201,7 @@ char* iniSetString(str_list_t* list, const char* section, const char* key, const ,ini_style_t* style) { char str[INI_MAX_LINE_LEN]; + char curval[INI_MAX_VALUE_LEN]; size_t i; iniAddSection(list, section); @@ -209,10 +213,13 @@ char* iniSetString(str_list_t* list, const char* section, const char* key, const if(style->value_separator==NULL) style->value_separator="="; sprintf(str, "%s%-*s%s%s", style->key_prefix, style->key_len, key, style->value_separator, value); - i=find_value_index(*list, section, key); + i=find_value_index(*list, section, key, curval); if((*list)[i]==NULL) return strListInsert(list, str, i); + if(strcmp(curval,value)==0) + return((*list)[i]); /* no change */ + return strListReplace(*list, i, str); } @@ -266,8 +273,8 @@ char* iniSetBool(str_list_t* list, const char* section, const char* key, BOOL va return iniSetString(list, section, key, value ? "true":"false", style); } -char* iniSetBitField(str_list_t* list, const char* section, const char* key, ulong value - ,ini_bitdesc_t* bitdesc, ini_style_t* style) +char* iniSetBitField(str_list_t* list, const char* section, const char* key + ,ini_bitdesc_t* bitdesc, ulong value, ini_style_t* style) { char str[INI_MAX_VALUE_LEN]; int i; @@ -291,6 +298,27 @@ char* iniSetBitField(str_list_t* list, const char* section, const char* key, ulo return iniSetString(list, section, key, str, style); } +char* iniSetStringList(str_list_t* list, const char* section, const char* key + ,const char* sep, str_list_t val_list, ini_style_t* style) +{ + char value[INI_MAX_VALUE_LEN]; + size_t i; + + value[0]=0; + + if(sep==NULL) + sep=","; + + if(val_list!=NULL) + for(i=0; val_list[i]!=NULL; i++) { + if(value[0]) + strcat(value,sep); + strcat(value,val_list[i]); + } + + return iniSetString(list, section, key, value, style); +} + char* iniGetString(FILE* fp, const char* section, const char* key, const char* deflt, char* value) { if(get_value(fp,section,key,value)==NULL || *value==0 /* blank */) { @@ -320,6 +348,9 @@ str_list_t iniGetStringList(FILE* fp, const char* section, const char* key if((lp=strListInit())==NULL) return(NULL); + if(sep==NULL) + sep=","; + token=strtok(list,sep); while(token!=NULL) { truncsp(token); diff --git a/src/xpdev/ini_file.h b/src/xpdev/ini_file.h index e1d6e329b6a581385ba161c9063f94a2cf6c8e0f..ad6f0924073ca424521930cde731a7a7b9a6697a 100644 --- a/src/xpdev/ini_file.h +++ b/src/xpdev/ini_file.h @@ -61,29 +61,29 @@ extern "C" { /* Read all section names and return as an allocated string list */ /* Optionally (if prefix!=NULL), returns a subset of section names */ -str_list_t iniGetSectionList (FILE* fp, const char* prefix); +str_list_t iniGetSectionList (FILE*, const char* prefix); /* Read all key names and return as an allocated string list */ -str_list_t iniGetKeyList (FILE* fp, const char* section); +str_list_t iniGetKeyList (FILE*, const char* section); /* Read all key and value pairs and return as a named string list */ named_string_t** - iniGetNamedStringList (FILE* fp, const char* section); + iniGetNamedStringList (FILE*, const char* section); /* These functions read a single key of the specified type */ -char* iniGetString (FILE* fp, const char* section, const char* key, +char* iniGetString (FILE*, const char* section, const char* key, const char* deflt, char* value); -str_list_t iniGetStringList(FILE* fp, const char* section, const char* key +str_list_t iniGetStringList(FILE*, const char* section, const char* key ,const char* sep, const char* deflt); -long iniGetInteger (FILE* fp, const char* section, const char* key, +long iniGetInteger (FILE*, const char* section, const char* key, long deflt); -ushort iniGetShortInt (FILE* fp, const char* section, const char* key, +ushort iniGetShortInt (FILE*, const char* section, const char* key, ushort deflt); -ulong iniGetIpAddress(FILE* fp, const char* section, const char* key, +ulong iniGetIpAddress(FILE*, const char* section, const char* key, ulong deflt); -double iniGetFloat (FILE* fp, const char* section, const char* key, +double iniGetFloat (FILE*, const char* section, const char* key, double deflt); -BOOL iniGetBool (FILE* fp, const char* section, const char* key, +BOOL iniGetBool (FILE*, const char* section, const char* key, BOOL deflt); -ulong iniGetBitField (FILE* fp, const char* section, const char* key, +ulong iniGetBitField (FILE*, const char* section, const char* key, ini_bitdesc_t* bitdesc, ulong deflt); /* Free string list returned from iniGet*List functions */ @@ -94,27 +94,29 @@ 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); +str_list_t iniReadFile(FILE*); +BOOL iniWriteFile(FILE*, const str_list_t); /* StringList functions */ -size_t iniAddSection(str_list_t* list, const char* section); -char* iniSetString(str_list_t* list, const char* section, const char* key, const char* value +size_t iniAddSection(str_list_t*, const char* section); +char* iniSetString(str_list_t*, const char* section, const char* key, const char* value ,ini_style_t*); -char* iniSetInteger(str_list_t* list, const char* section, const char* key, long value +char* iniSetInteger(str_list_t*, const char* section, const char* key, long value ,ini_style_t*); -char* iniSetShortInt(str_list_t* list, const char* section, const char* key, ushort value +char* iniSetShortInt(str_list_t*, const char* section, const char* key, ushort value ,ini_style_t*); -char* iniSetHexInt(str_list_t* list, const char* section, const char* key, ulong value +char* iniSetHexInt(str_list_t*, const char* section, const char* key, ulong value ,ini_style_t*); -char* iniSetFloat(str_list_t* list, const char* section, const char* key, double value +char* iniSetFloat(str_list_t*, const char* section, const char* key, double value ,ini_style_t*); -char* iniSetIpAddress(str_list_t* list, const char* section, const char* key, ulong value +char* iniSetIpAddress(str_list_t*, const char* section, const char* key, ulong value ,ini_style_t*); -char* iniSetBool(str_list_t* list, const char* section, const char* key, BOOL value +char* iniSetBool(str_list_t*, const char* section, const char* key, BOOL value ,ini_style_t*); -char* iniSetBitField(str_list_t* list, const char* section, const char* key, ulong value - ,ini_bitdesc_t*, ini_style_t*); +char* iniSetBitField(str_list_t*, const char* section, const char* key, ini_bitdesc_t*, ulong value + ,ini_style_t*); +char* iniSetStringList(str_list_t*, const char* section, const char* key + ,const char* sep, str_list_t value, ini_style_t*); #if defined(__cplusplus) }