Skip to content
Snippets Groups Projects
Commit 461f6fef authored by rswindell's avatar rswindell
Browse files

Created iniGet/ReadExistingString(), which do nothing (just return NULL) if

the key doesn't exist.
parent ac733071
No related branches found
No related tags found
No related merge requests found
...@@ -298,9 +298,12 @@ BOOL iniKeyExists(str_list_t list, const char* section, const char* key) ...@@ -298,9 +298,12 @@ BOOL iniKeyExists(str_list_t list, const char* section, const char* key)
char val[INI_MAX_VALUE_LEN]; char val[INI_MAX_VALUE_LEN];
size_t i; size_t i;
if(list==NULL)
return(FALSE);
i=get_value(list, section, key, val); i=get_value(list, section, key, val);
if(list==NULL || list[i]==NULL || *(list[i])==INI_OPEN_SECTION_CHAR) if(list[i]==NULL || *(list[i])==INI_OPEN_SECTION_CHAR)
return(FALSE); return(FALSE);
return(TRUE); return(TRUE);
...@@ -647,14 +650,19 @@ char* iniSetStringList(str_list_t* list, const char* section, const char* key ...@@ -647,14 +650,19 @@ char* iniSetStringList(str_list_t* list, const char* section, const char* key
return iniSetString(list, section, key, value, style); return iniSetString(list, section, key, value, style);
} }
char* iniReadString(FILE* fp, const char* section, const char* key, const char* deflt, char* value) static char* default_value(const char* deflt, char* value)
{ {
if(read_value(fp,section,key,value)==NULL || *value==0 /* blank */) {
if(deflt!=NULL && deflt!=value) if(deflt!=NULL && deflt!=value)
sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,deflt); sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,deflt);
return((char*)deflt);
return(deflt);
} }
char* iniReadString(FILE* fp, const char* section, const char* key, const char* deflt, char* value)
{
if(read_value(fp,section,key,value)==NULL || *value==0 /* blank */)
return default_value(deflt,value);
return(value); return(value);
} }
...@@ -662,12 +670,38 @@ char* iniGetString(str_list_t list, const char* section, const char* key, const ...@@ -662,12 +670,38 @@ char* iniGetString(str_list_t list, const char* section, const char* key, const
{ {
get_value(list, section, key, value); get_value(list, section, key, value);
if(*value==0 /* blank value or missing key */) { if(*value==0 /* blank value or missing key */)
if(deflt!=NULL && deflt!=value) return default_value(deflt,value);
sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,deflt);
return((char*)deflt); return(value);
} }
char* iniReadExistingString(FILE* fp, const char* section, const char* key, const char* deflt, char* value)
{
if(read_value(fp,section,key,value)==NULL)
return(NULL);
if(*value==0 /* blank */)
return default_value(deflt,value);
return(value);
}
char* iniGetExistingString(str_list_t list, const char* section, const char* key, const char* deflt, char* value)
{
size_t i;
if(list==NULL)
return(NULL);
i=get_value(list, section, key, value);
if(list[i]==NULL || *(list[i])==INI_OPEN_SECTION_CHAR) /* missing key */
return(NULL);
if(*value==0 /* blank value */)
return default_value(deflt,value);
return(value); return(value);
} }
......
/* ini_file.h */ /* ini_file.h */
/* Functions to parse ini files */ /* Functions to parse ini (initialization / configuration) files */
/* $Id$ */ /* $Id$ */
...@@ -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 2005 Rob Swindell - http://www.synchro.net/copyright.html * * Copyright 2007 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 *
...@@ -79,6 +79,9 @@ str_list_t iniLogLevelStringList(void); ...@@ -79,6 +79,9 @@ str_list_t iniLogLevelStringList(void);
/* These functions read a single key of the specified type */ /* These functions read a single key of the specified type */
char* iniReadString(FILE*, const char* section, const char* key char* iniReadString(FILE*, const char* section, const char* key
,const char* deflt, char* value); ,const char* deflt, char* value);
/* If the key doesn't exist, iniReadExistingString just returns NULL */
char* iniReadExistingString(FILE*, const char* section, const char* key
,const char* deflt, char* value);
str_list_t iniReadStringList(FILE*, const char* section, const char* key str_list_t iniReadStringList(FILE*, const char* section, const char* key
,const char* sep, const char* deflt); ,const char* sep, const char* deflt);
long iniReadInteger(FILE*, const char* section, const char* key long iniReadInteger(FILE*, const char* section, const char* key
...@@ -128,6 +131,9 @@ named_string_t** ...@@ -128,6 +131,9 @@ named_string_t**
char* iniGetString(str_list_t, const char* section, const char* key char* iniGetString(str_list_t, const char* section, const char* key
,const char* deflt, char* value); ,const char* deflt, char* value);
/* If the key doesn't exist, iniGetExistingString just returns NULL */
char* iniGetExistingString(str_list_t, const char* section, const char* key
,const char* deflt, char* value);
str_list_t iniGetStringList(str_list_t, const char* section, const char* key str_list_t iniGetStringList(str_list_t, const char* section, const char* key
,const char* sep, const char* deflt); ,const char* sep, const char* deflt);
long iniGetInteger(str_list_t, const char* section, const char* key long iniGetInteger(str_list_t, const char* section, const char* key
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment