提交 3c23bf85 编写于 作者: E Emmanuel Leblond

iniparser_getseckeys doesn't return a malloc ptr anymore (see #30)

上级 85023c97
......@@ -310,36 +310,26 @@ int iniparser_getsecnkeys(const dictionary * d, const char * s)
/*-------------------------------------------------------------------------*/
/**
@brief Get the number of keys in a section of a dictionary.
@param d Dictionary to examine
@param s Section name of dictionary to examine
@return pointer to statically allocated character strings
@param d Dictionary to examine
@param s Section name of dictionary to examine
@param keys Already allocated array to store the keys in
@return The pointer passed as `keys` argument or NULL in case of error
This function queries a dictionary and finds all keys in a given section.
The returned pointer is obtained with malloc(3), and can be freed
with free(3).
The keys argument should be an array of pointers which size has been
determined by calling `iniparser_getsecnkeys` function prior to this one.
Each pointer in the returned char pointer-to-pointer is pointing to
a string allocated in the dictionary; do not free or modify them.
This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/
const char ** iniparser_getseckeys(const dictionary * d, const char * s)
const char ** iniparser_getseckeys(const dictionary * d, const char * s, const char ** keys)
{
int i, j, seclen ;
char keym[ASCIILINESZ+1];
const char **keys;
int i, j ;
char keym[ASCIILINESZ+1];
int seclen, nkeys ;
keys = NULL;
if (d==NULL) return keys;
if (! iniparser_find_entry(d, s)) return keys;
nkeys = iniparser_getsecnkeys(d, s);
keys = (const char**) malloc(nkeys*sizeof(char*));
if (d==NULL || keys==NULL) return NULL;
if (! iniparser_find_entry(d, s)) return NULL;
seclen = (int)strlen(s);
sprintf(keym, "%s:", s);
......@@ -356,7 +346,6 @@ const char ** iniparser_getseckeys(const dictionary * d, const char * s)
}
return keys;
}
/*-------------------------------------------------------------------------*/
......
......@@ -128,20 +128,21 @@ int iniparser_getsecnkeys(const dictionary * d, const char * s);
/*-------------------------------------------------------------------------*/
/**
@brief Get the number of keys in a section of a dictionary.
@param d Dictionary to examine
@param s Section name of dictionary to examine
@return pointer to statically allocated character strings
@param d Dictionary to examine
@param s Section name of dictionary to examine
@param keys Already allocated array to store the keys in
@return The pointer passed as `keys` argument or NULL in case of error
This function queries a dictionary and finds all keys in a given section.
The returned pointer is obtained with malloc(3), and can be freed
with free(3).
The keys argument should be an array of pointers which size has been
determined by calling `iniparser_getsecnkeys` function prior to this one.
Each pointer in the returned char pointer-to-pointer is pointing to
a string allocated in the dictionary; do not free or modify them.
This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/
const char ** iniparser_getseckeys(const dictionary * d, const char * s);
const char ** iniparser_getseckeys(const dictionary * d, const char * s, const char ** keys);
/*-------------------------------------------------------------------------*/
/**
......
......@@ -195,28 +195,32 @@ void Test_iniparser_getseckeys(CuTest *tc)
unsigned i;
char key_name[64];
dictionary *dic;
const char ** sections;
int nkeys;
const char * keys[10]; /* At most 10 elements per section */
/* NULL test */
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, NULL));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, "dummy"));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, NULL, NULL));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, "dummy", NULL));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, "dummy", keys));
/* Empty dictionary */
dic = dictionary_new(10);
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy"));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL, keys));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy", keys));
dictionary_del(dic);
/* Generic dictionary */
dic = generate_dictionary(100, 10);
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy"));
sections = iniparser_getseckeys(dic, "sec42");
CuAssertPtrNotNull(tc, sections);
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL, keys));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy", keys));
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "sec0", NULL));
nkeys = iniparser_getsecnkeys(dic, "sec42");
CuAssertIntEquals(tc, nkeys, 10);
CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "sec42", keys));
for (i = 0; i < 10; ++i) {
sprintf(key_name, "sec42:key%d", i);
CuAssertStrEquals(tc, key_name, sections[i]);
CuAssertStrEquals(tc, key_name, keys[i]);
}
free(sections);
/* Remove some keys to make the dictionary more real */
dictionary_unset(dic, "sec42");
......@@ -225,21 +229,22 @@ void Test_iniparser_getseckeys(CuTest *tc)
dictionary_unset(dic, "sec0:key1");
dictionary_unset(dic, "sec0:key2");
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "sec42"));
sections = iniparser_getseckeys(dic, "sec99");
CuAssertPtrNotNull(tc, sections);
CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "sec42", keys));
nkeys = iniparser_getsecnkeys(dic, "sec99");
CuAssertIntEquals(tc, nkeys, 9);
CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "sec99", keys));
for (i = 0; i < 9; ++i) {
sprintf(key_name, "sec99:key%d", i);
CuAssertStrEquals(tc, key_name, sections[i]);
CuAssertStrEquals(tc, key_name, keys[i]);
}
free(sections);
sections = iniparser_getseckeys(dic, "sec0");
CuAssertPtrNotNull(tc, sections);
nkeys = iniparser_getsecnkeys(dic, "sec0");
CuAssertIntEquals(tc, nkeys, 7);
CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "sec0", keys));
for (i = 0; i < 7; ++i) {
sprintf(key_name, "sec0:key%d", i + 3);
CuAssertStrEquals(tc, key_name, sections[i]);
CuAssertStrEquals(tc, key_name, keys[i]);
}
free(sections);
dictionary_del(dic);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册