Skip to content
Snippets Groups Projects
Commit 380b3621 authored by rswindell's avatar rswindell
Browse files

iniGet[Existing]String() can now be called with a NULL value argument (buffer),

which returns a pointer directly to the key value (no copy).
Most of the iniGet* functions now perform no unnecessary copying of key values.
parent c1a3d379
No related branches found
No related tags found
No related merge requests found
/* ini_file.c */
/* Functions to parse ini files */
/* Functions to create and parse .ini files */
/* $Id$ */
......@@ -8,7 +8,7 @@
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html *
* Copyright 2011 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 *
......@@ -255,14 +255,17 @@ static char* read_value(FILE* fp, const char* section, const char* key, char* va
return(NULL);
}
static size_t get_value(str_list_t list, const char* section, const char* key, char* value)
static size_t get_value(str_list_t list, const char* section, const char* key, char* value, char** vpp)
{
char str[INI_MAX_LINE_LEN];
char* p;
char* vp;
size_t i;
value[0]=0;
if(value!=NULL)
value[0]=0;
if(vpp!=NULL)
*vpp=NULL;
if(list==NULL)
return 0;
......@@ -276,7 +279,10 @@ static size_t get_value(str_list_t list, const char* section, const char* key, c
break;
if(stricmp(p,key)!=0)
continue;
sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,vp);
if(value!=NULL)
sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,vp);
if(vpp!=NULL)
*vpp=vp;
return(i);
}
......@@ -322,13 +328,12 @@ str_list_t iniGetSection(str_list_t list, const char *section)
BOOL iniKeyExists(str_list_t list, const char* section, const char* key)
{
char val[INI_MAX_VALUE_LEN];
size_t i;
if(list==NULL)
return(FALSE);
i=get_value(list, section, key, val);
i=get_value(list, section, key, NULL, NULL);
if(list[i]==NULL || *(list[i])==INI_OPEN_SECTION_CHAR)
return(FALSE);
......@@ -338,21 +343,21 @@ BOOL iniKeyExists(str_list_t list, const char* section, const char* key)
BOOL iniValueExists(str_list_t list, const char* section, const char* key)
{
char val[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, val);
get_value(list, section, key, NULL, &vp);
return(val[0]!=0);
return(vp!=NULL && *vp!=0);
}
BOOL iniRemoveKey(str_list_t* list, const char* section, const char* key)
{
char val[INI_MAX_VALUE_LEN];
size_t i;
char* vp=NULL;
i=get_value(*list, section, key, val);
i=get_value(*list, section, key, NULL, &vp);
if((*list)[i]==NULL || *(*list)[i]==INI_OPEN_SECTION_CHAR)
if(vp==NULL)
return(FALSE);
return(strListDelete(list,i));
......@@ -360,18 +365,15 @@ BOOL iniRemoveKey(str_list_t* list, const char* section, const char* key)
BOOL iniRemoveValue(str_list_t* list, const char* section, const char* key)
{
char val[INI_MAX_VALUE_LEN];
size_t i;
char* p;
char* vp=NULL;
i=get_value(*list, section, key, val);
i=get_value(*list, section, key, NULL, &vp);
p=key_name((*list)[i], &vp);
if(vp==NULL)
return(FALSE);
strcat(p,"=");
*vp=0;
return(TRUE);
}
......@@ -468,7 +470,7 @@ char* iniSetString(str_list_t* list, const char* section, const char* key, const
value="";
safe_snprintf(str, sizeof(str), "%s%-*s%s%s"
, style->key_prefix, style->key_len, key, style->value_separator, value);
i=get_value(*list, section, key, curval);
i=get_value(*list, section, key, curval, NULL);
if((*list)[i]==NULL || *(*list)[i]==INI_OPEN_SECTION_CHAR) {
while(i && *(*list)[i-1]==0) i--; /* Insert before blank lines, not after */
return strListInsert(list, str, i);
......@@ -699,7 +701,7 @@ char* iniSetStringList(str_list_t* list, const char* section, const char* key
static char* default_value(const char* deflt, char* value)
{
if(deflt!=NULL && deflt!=value)
if(deflt!=NULL && deflt!=value && value!=NULL)
sprintf(value,"%.*s",INI_MAX_VALUE_LEN-1,deflt);
return((char*)deflt);
......@@ -715,12 +717,14 @@ char* iniReadString(FILE* fp, const char* section, const char* key, const char*
char* iniGetString(str_list_t list, const char* section, const char* key, const char* deflt, char* value)
{
get_value(list, section, key, value);
char* vp=NULL;
get_value(list, section, key, value, &vp);
if(*value==0 /* blank value or missing key */)
if(vp==NULL || *vp==0 /* blank value or missing key */)
return default_value(deflt,value);
return(value);
return(vp);
}
char* iniPopKey(str_list_t* list, const char* section, const char* key, char* value)
......@@ -730,7 +734,7 @@ char* iniPopKey(str_list_t* list, const char* section, const char* key, char* va
if(list==NULL || *list==NULL)
return NULL;
i=get_value(*list, section, key, value);
i=get_value(*list, section, key, value, NULL);
if((*list)[i]==NULL)
return NULL;
......@@ -807,7 +811,7 @@ str_list_t iniGetStringList(str_list_t list, const char* section, const char* ke
{
char value[INI_MAX_VALUE_LEN];
get_value(list, section, key, value);
get_value(list, section, key, value, NULL);
if(*value==0 /* blank value or missing key */) {
if(deflt==NULL)
......@@ -1133,14 +1137,14 @@ long iniReadInteger(FILE* fp, const char* section, const char* key, long deflt)
long iniGetInteger(str_list_t list, const char* section, const char* key, long deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseInteger(value));
return(parseInteger(vp));
}
ushort iniReadShortInt(FILE* fp, const char* section, const char* key, ushort deflt)
......@@ -1169,14 +1173,14 @@ ulong iniReadLongInt(FILE* fp, const char* section, const char* key, ulong deflt
ulong iniGetLongInt(str_list_t list, const char* section, const char* key, ulong deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseLongInteger(value));
return(parseLongInteger(vp));
}
static int64_t parseBytes(const char* value, ulong unit)
......@@ -1226,14 +1230,14 @@ int64_t iniReadBytes(FILE* fp, const char* section, const char* key, ulong unit,
int64_t iniGetBytes(str_list_t list, const char* section, const char* key, ulong unit, int64_t deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseBytes(value,unit));
return(parseBytes(vp,unit));
}
#if !defined(NO_SOCKET_SUPPORT)
......@@ -1309,14 +1313,14 @@ ulong iniReadIpAddress(FILE* fp, const char* section, const char* key, ulong def
ulong iniGetIpAddress(str_list_t list, const char* section, const char* key, ulong deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseIpAddress(value));
return(parseIpAddress(vp));
}
#endif /* !NO_SOCKET_SUPPORT */
......@@ -1380,14 +1384,14 @@ double iniReadFloat(FILE* fp, const char* section, const char* key, double deflt
double iniGetFloat(str_list_t list, const char* section, const char* key, double deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(atof(value));
return(atof(vp));
}
BOOL iniReadBool(FILE* fp, const char* section, const char* key, BOOL deflt)
......@@ -1406,14 +1410,14 @@ BOOL iniReadBool(FILE* fp, const char* section, const char* key, BOOL deflt)
BOOL iniGetBool(str_list_t list, const char* section, const char* key, BOOL deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseBool(value));
return(parseBool(vp));
}
static BOOL validDate(struct tm* tm)
......@@ -1546,14 +1550,14 @@ time_t iniReadDateTime(FILE* fp, const char* section, const char* key, time_t de
time_t iniGetDateTime(str_list_t list, const char* section, const char* key, time_t deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseDateTime(value));
return(parseDateTime(vp));
}
static unsigned parseEnum(const char* value, str_list_t names)
......@@ -1654,20 +1658,20 @@ unsigned* iniReadEnumList(FILE* fp, const char* section, const char* key
unsigned iniGetEnum(str_list_t list, const char* section, const char* key, str_list_t names, unsigned deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseEnum(value,names));
return(parseEnum(vp,names));
}
unsigned* iniGetEnumList(str_list_t list, const char* section, const char* key
,str_list_t names, unsigned* cp, const char* sep, const char* deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
unsigned count;
if(cp==NULL)
......@@ -1675,14 +1679,14 @@ unsigned* iniGetEnumList(str_list_t list, const char* section, const char* key
*cp=0;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0 /* blank value or missing key */) {
if(vp==NULL || *vp==0 /* blank value or missing key */) {
if(deflt==NULL)
return(NULL);
SAFECOPY(value,deflt);
vp=(char*)deflt;
}
return(parseEnumList(value, sep, names, cp));
return(parseEnumList(vp, sep, names, cp));
}
static long parseNamedInt(const char* value, named_long_t* names)
......@@ -1720,14 +1724,14 @@ long iniReadNamedInt(FILE* fp, const char* section, const char* key
long iniGetNamedInt(str_list_t list, const char* section, const char* key
,named_long_t* names, long deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseNamedInt(value,names));
return(parseNamedInt(vp,names));
}
static double parseNamedFloat(const char* value, named_double_t* names)
......@@ -1765,14 +1769,14 @@ double iniReadNamedFloat(FILE* fp, const char* section, const char* key
double iniGetNamedFloat(str_list_t list, const char* section, const char* key
,named_double_t* names, double deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseNamedFloat(value,names));
return(parseNamedFloat(vp,names));
}
static ulong parseBitField(char* value, ini_bitdesc_t* bitdesc)
......@@ -1822,14 +1826,14 @@ ulong iniReadBitField(FILE* fp, const char* section, const char* key,
ulong iniGetBitField(str_list_t list, const char* section, const char* key,
ini_bitdesc_t* bitdesc, ulong deflt)
{
char value[INI_MAX_VALUE_LEN];
char* vp=NULL;;
get_value(list, section, key, value);
get_value(list, section, key, NULL, &vp);
if(*value==0) /* blank value or missing key */
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseBitField(value,bitdesc));
return(parseBitField(vp,bitdesc));
}
FILE* iniOpenFile(const char* fname, BOOL create)
......
......@@ -8,7 +8,7 @@
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html *
* Copyright 2011 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 *
......@@ -132,10 +132,10 @@ named_string_t**
iniGetNamedStringList(str_list_t list, const char* section);
char* iniGetString(str_list_t, const char* section, const char* key
,const char* deflt, char* value);
,const char* deflt, char* value /* may be NULL */);
/* 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);
,const char* deflt, char* value /* may be NULL */);
str_list_t iniGetStringList(str_list_t, const char* section, const char* key
,const char* sep, const char* deflt);
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.
Finish editing this message first!
Please register or to comment