Commit efb12a1d authored by Florian Bezdeka's avatar Florian Bezdeka Committed by Jan Kiszka
Browse files

lib/boilerplate/iniparser: Allow building with GCC 10.2 2020101

Updating to upstream revision f858275f7f307eecba84c2f5429483f9f28007f8.
Upstream repository is located at [1].

The reason for updating was the following compiler error when trying
to compile with GCC 10.2 10.2.1 20201016. As it turned out the problem
was already addressed upstream:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may
overlap destination object ‘buf’ [-Werror=restrict]
   616 |             sprintf(tmp, "%s:%s", section, key);
       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I reviewed especially the API changes. Most of them are cleanups only
but two things should be pointed out:

  - The type of the size field of struct _dictionary_ changed from int
    to ssize_t. The only user of this struct is
    lib/analogy/calibration.c which uses this structure for internal
    things only. It is never exposed to any public API so updating is
    OK and fully backward compatible.

  - dictionary_new changed its signature
      from dictionary_new(int size)
      to   dictionary_new(size_t size).
    This function is not part of any public API. So updating does not
    break backward compatibility.

[1] https://github.com/ndevilla/iniparser

Signed-off-by: default avatarFlorian Bezdeka <florian.bezdeka@siemens.com>
Signed-off-by: Jan Kiszka's avatarJan Kiszka <jan.kiszka@siemens.com>
parent e1c30cf3
......@@ -2,8 +2,6 @@
/**
@file dictionary.c
@author N. Devillard
@date Sep 2007
@version $Revision: 1.27 $
@brief Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list
......@@ -12,10 +10,6 @@
*/
/*--------------------------------------------------------------------------*/
/*
$Id: dictionary.c,v 1.27 2007-11-23 21:39:18 ndevilla Exp $
$Revision: 1.27 $
*/
/*---------------------------------------------------------------------------
Includes
---------------------------------------------------------------------------*/
......@@ -39,21 +33,6 @@
Private functions
---------------------------------------------------------------------------*/
/* Doubles the allocated size associated to a pointer */
/* 'size' is the current allocated size. */
static void * mem_double(void * ptr, int size)
{
void * newptr ;
newptr = calloc(2*size, 1);
if (newptr==NULL) {
return NULL ;
}
memcpy(newptr, ptr, size);
free(ptr);
return newptr ;
}
/*-------------------------------------------------------------------------*/
/**
@brief Duplicate a string
......@@ -67,15 +46,60 @@ static void * mem_double(void * ptr, int size)
static char * xstrdup(const char * s)
{
char * t ;
size_t len ;
if (!s)
return NULL ;
t = malloc(strlen(s)+1) ;
len = strlen(s) + 1 ;
t = (char*) malloc(len) ;
if (t) {
strcpy(t,s);
memcpy(t, s, len) ;
}
return t ;
}
/*-------------------------------------------------------------------------*/
/**
@brief Double the size of the dictionary
@param d Dictionary to grow
@return This function returns non-zero in case of failure
*/
/*--------------------------------------------------------------------------*/
static int dictionary_grow(dictionary * d)
{
char ** new_val ;
char ** new_key ;
unsigned * new_hash ;
new_val = (char**) calloc(d->size * 2, sizeof *d->val);
new_key = (char**) calloc(d->size * 2, sizeof *d->key);
new_hash = (unsigned*) calloc(d->size * 2, sizeof *d->hash);
if (!new_val || !new_key || !new_hash) {
/* An allocation failed, leave the dictionary unchanged */
if (new_val)
free(new_val);
if (new_key)
free(new_key);
if (new_hash)
free(new_hash);
return -1 ;
}
/* Initialize the newly allocated space */
memcpy(new_val, d->val, d->size * sizeof(char *));
memcpy(new_key, d->key, d->size * sizeof(char *));
memcpy(new_hash, d->hash, d->size * sizeof(unsigned));
/* Delete previous data */
free(d->val);
free(d->key);
free(d->hash);
/* Actually update the dictionary */
d->size *= 2 ;
d->val = new_val;
d->key = new_key;
d->hash = new_hash;
return 0 ;
}
/*---------------------------------------------------------------------------
Function codes
---------------------------------------------------------------------------*/
......@@ -93,9 +117,12 @@ static char * xstrdup(const char * s)
/*--------------------------------------------------------------------------*/
unsigned dictionary_hash(const char * key)
{
int len ;
size_t len ;
unsigned hash ;
int i ;
size_t i ;
if (!key)
return 0 ;
len = strlen(key);
for (hash=0, i=0 ; i<len ; i++) {
......@@ -119,21 +146,22 @@ unsigned dictionary_hash(const char * key)
it. If you do not know in advance (roughly) the number of entries in the
dictionary, give size=0.
*/
/*--------------------------------------------------------------------------*/
dictionary * dictionary_new(int size)
/*-------------------------------------------------------------------------*/
dictionary * dictionary_new(size_t size)
{
dictionary * d ;
/* If no size was specified, allocate space for DICTMINSZ */
if (size<DICTMINSZ) size=DICTMINSZ ;
if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
return NULL;
}
d = (dictionary*) calloc(1, sizeof *d) ;
if (d) {
d->size = size ;
d->val = (char **)calloc(size, sizeof(char*));
d->key = (char **)calloc(size, sizeof(char*));
d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
d->val = (char**) calloc(size, sizeof *d->val);
d->key = (char**) calloc(size, sizeof *d->key);
d->hash = (unsigned*) calloc(size, sizeof *d->hash);
}
return d ;
}
......@@ -148,7 +176,7 @@ dictionary * dictionary_new(int size)
/*--------------------------------------------------------------------------*/
void dictionary_del(dictionary * d)
{
int i ;
ssize_t i ;
if (d==NULL) return ;
for (i=0 ; i<d->size ; i++) {
......@@ -178,10 +206,10 @@ void dictionary_del(dictionary * d)
dictionary object, you should not try to free it or modify it.
*/
/*--------------------------------------------------------------------------*/
const char * dictionary_get(dictionary * d, const char * key, const char * def)
const char * dictionary_get(const dictionary * d, const char * key, const char * def)
{
unsigned hash ;
int i ;
ssize_t i ;
hash = dictionary_hash(key);
for (i=0 ; i<d->size ; i++) {
......@@ -226,7 +254,7 @@ const char * dictionary_get(dictionary * d, const char * key, const char * def)
/*--------------------------------------------------------------------------*/
int dictionary_set(dictionary * d, const char * key, const char * val)
{
int i ;
ssize_t i ;
unsigned hash ;
if (d==NULL || key==NULL) return -1 ;
......@@ -243,7 +271,7 @@ int dictionary_set(dictionary * d, const char * key, const char * val)
/* Found a value: modify and return */
if (d->val[i]!=NULL)
free(d->val[i]);
d->val[i] = val ? xstrdup(val) : NULL ;
d->val[i] = (val ? xstrdup(val) : NULL);
/* Value has been modified: return */
return 0 ;
}
......@@ -253,29 +281,20 @@ int dictionary_set(dictionary * d, const char * key, const char * val)
/* Add a new value */
/* See if dictionary needs to grow */
if (d->n==d->size) {
/* Reached maximum size: reallocate dictionary */
d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
/* Cannot grow dictionary */
return -1 ;
}
/* Double size */
d->size *= 2 ;
if (dictionary_grow(d) != 0)
return -1;
}
/* Insert key in the first empty slot */
for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL) {
/* Add key here */
break ;
}
/* Insert key in the first empty slot. Start at d->n and wrap at
d->size. Because d->n < d->size this will necessarily
terminate. */
for (i=d->n ; d->key[i] ; ) {
if(++i == d->size) i = 0;
}
/* Copy key */
d->key[i] = xstrdup(key);
d->val[i] = val ? xstrdup(val) : NULL ;
d->val[i] = (val ? xstrdup(val) : NULL) ;
d->hash[i] = hash;
d->n ++ ;
return 0 ;
......@@ -295,9 +314,9 @@ int dictionary_set(dictionary * d, const char * key, const char * val)
void dictionary_unset(dictionary * d, const char * key)
{
unsigned hash ;
int i ;
ssize_t i ;
if (key == NULL) {
if (key == NULL || d == NULL) {
return;
}
......@@ -333,7 +352,7 @@ void dictionary_unset(dictionary * d, const char * key)
/**
@brief Dump a dictionary to an opened file pointer.
@param d Dictionary to dump
@param out Opened file pointer.
@param f Opened file pointer.
@return void
Dumps a dictionary onto an opened file pointer. Key pairs are printed out
......@@ -341,9 +360,9 @@ void dictionary_unset(dictionary * d, const char * key)
output file pointers.
*/
/*--------------------------------------------------------------------------*/
void dictionary_dump(dictionary * d, FILE * out)
void dictionary_dump(const dictionary * d, FILE * out)
{
int i ;
ssize_t i ;
if (d==NULL || out==NULL) return ;
if (d->n<1) {
......@@ -359,47 +378,3 @@ void dictionary_dump(dictionary * d, FILE * out)
}
return ;
}
/* Test code */
#ifdef TESTDIC
#define NVALS 20000
int main(int argc, char *argv[])
{
dictionary * d ;
char * val ;
int i ;
char cval[90] ;
/* Allocate dictionary */
printf("allocating...\n");
d = dictionary_new(0);
/* Set values in dictionary */
printf("setting %d values...\n", NVALS);
for (i=0 ; i<NVALS ; i++) {
sprintf(cval, "%04d", i);
dictionary_set(d, cval, "salut");
}
printf("getting %d values...\n", NVALS);
for (i=0 ; i<NVALS ; i++) {
sprintf(cval, "%04d", i);
val = dictionary_get(d, cval, DICT_INVALID_KEY);
if (val==DICT_INVALID_KEY) {
printf("cannot get value for key [%s]\n", cval);
}
}
printf("unsetting %d values...\n", NVALS);
for (i=0 ; i<NVALS ; i++) {
sprintf(cval, "%04d", i);
dictionary_unset(d, cval);
}
if (d->n != 0) {
printf("error deleting values\n");
}
printf("deallocating...\n");
dictionary_del(d);
return 0 ;
}
#endif
/* vim: set ts=4 et sw=4 tw=75 */
......@@ -3,8 +3,6 @@
/**
@file dictionary.h
@author N. Devillard
@date Sep 2007
@version $Revision: 1.12 $
@brief Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list
......@@ -13,13 +11,6 @@
*/
/*--------------------------------------------------------------------------*/
/*
$Id: dictionary.h,v 1.12 2007-11-23 21:37:00 ndevilla Exp $
$Author: ndevilla $
$Date: 2007-11-23 21:37:00 $
$Revision: 1.12 $
*/
#ifndef _DICTIONARY_H_
#define _DICTIONARY_H_
......@@ -32,6 +23,10 @@
#include <string.h>
#include <unistd.h>
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------------------
New types
---------------------------------------------------------------------------*/
......@@ -49,7 +44,7 @@
/*-------------------------------------------------------------------------*/
typedef struct _dictionary_ {
int n ; /** Number of entries in dictionary */
int size ; /** Storage size */
ssize_t size ; /** Storage size */
char ** val ; /** List of string values */
char ** key ; /** List of string keys */
unsigned * hash ; /** List of hash values for keys */
......@@ -85,7 +80,7 @@ unsigned dictionary_hash(const char * key);
dictionary, give size=0.
*/
/*--------------------------------------------------------------------------*/
dictionary * dictionary_new(int size);
dictionary * dictionary_new(size_t size);
/*-------------------------------------------------------------------------*/
/**
......@@ -112,7 +107,7 @@ void dictionary_del(dictionary * vd);
dictionary object, you should not try to free it or modify it.
*/
/*--------------------------------------------------------------------------*/
const char * dictionary_get(dictionary * d, const char * key, const char * def);
const char * dictionary_get(const dictionary * d, const char * key, const char * def);
/*-------------------------------------------------------------------------*/
......@@ -161,7 +156,7 @@ void dictionary_unset(dictionary * d, const char * key);
/**
@brief Dump a dictionary to an opened file pointer.
@param d Dictionary to dump
@param out Opened file pointer.
@param f Opened file pointer.
@return void
Dumps a dictionary onto an opened file pointer. Key pairs are printed out
......@@ -169,6 +164,10 @@ void dictionary_unset(dictionary * d, const char * key);
output file pointers.
*/
/*--------------------------------------------------------------------------*/
void dictionary_dump(dictionary * d, FILE * out);
void dictionary_dump(const dictionary * d, FILE * out);
#ifdef __cplusplus
}
#endif
#endif
This diff is collapsed.
......@@ -3,17 +3,10 @@
/**
@file iniparser.h
@author N. Devillard
@date Sep 2007
@version 3.0
@brief Parser for ini files.
*/
/*--------------------------------------------------------------------------*/
/*
$Id: iniparser.h,v 1.24 2007-11-23 21:38:19 ndevilla Exp $
$Revision: 1.24 $
*/
#ifndef _INIPARSER_H_
#define _INIPARSER_H_
......@@ -34,12 +27,21 @@
#include "dictionary.h"
/*---------------------------------------------------------------------------
Macros
---------------------------------------------------------------------------*/
/** For backwards compatibility only */
#define iniparser_getstr(d, k) iniparser_getstring(d, k, NULL)
#define iniparser_setstr iniparser_setstring
#ifdef __cplusplus
extern "C" {
#endif
/*-------------------------------------------------------------------------*/
/**
@brief Configure a function to receive the error messages.
@param errback Function to call.
By default, the error will be printed on stderr. If a null pointer is passed
as errback the error callback will be switched back to default.
*/
/*--------------------------------------------------------------------------*/
void iniparser_set_error_callback(int (*errback)(const char *, ...));
/*-------------------------------------------------------------------------*/
/**
......@@ -60,7 +62,7 @@
*/
/*--------------------------------------------------------------------------*/
int iniparser_getnsec(dictionary * d);
int iniparser_getnsec(const dictionary * d);
/*-------------------------------------------------------------------------*/
......@@ -78,7 +80,7 @@ int iniparser_getnsec(dictionary * d);
*/
/*--------------------------------------------------------------------------*/
const char * iniparser_getsecname(dictionary * d, int n);
const char * iniparser_getsecname(const dictionary * d, int n);
/*-------------------------------------------------------------------------*/
......@@ -93,7 +95,22 @@ const char * iniparser_getsecname(dictionary * d, int n);
*/
/*--------------------------------------------------------------------------*/
void iniparser_dump_ini(dictionary * d, FILE * f);
void iniparser_dump_ini(const dictionary * d, FILE * f);
/*-------------------------------------------------------------------------*/
/**
@brief Save a dictionary section to a loadable ini file
@param d Dictionary to dump
@param s Section name of dictionary to dump
@param f Opened file pointer to dump to
@return void
This function dumps a given section of a given dictionary into a loadable ini
file. It is Ok to specify @c stderr or @c stdout as output files.
*/
/*--------------------------------------------------------------------------*/
void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f);
/*-------------------------------------------------------------------------*/
/**
......@@ -108,7 +125,36 @@ void iniparser_dump_ini(dictionary * d, FILE * f);
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
void iniparser_dump(dictionary * d, FILE * f);
void iniparser_dump(const dictionary * d, FILE * f);
/*-------------------------------------------------------------------------*/
/**
@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 Number of keys in section
*/
/*--------------------------------------------------------------------------*/
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
@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 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.
*/
/*--------------------------------------------------------------------------*/
const char ** iniparser_getseckeys(const dictionary * d, const char * s, const char ** keys);
/*-------------------------------------------------------------------------*/
/**
......@@ -125,7 +171,7 @@ void iniparser_dump(dictionary * d, FILE * f);
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
const char * iniparser_getstring(dictionary * d, const char * key, const char * def);
const char * iniparser_getstring(const dictionary * d, const char * key, const char * def);
/*-------------------------------------------------------------------------*/
/**
......@@ -154,7 +200,35 @@ const char * iniparser_getstring(dictionary * d, const char * key, const char *
Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
int iniparser_getint(dictionary * d, const char * key, int notfound);
int iniparser_getint(const dictionary * d, const char * key, int notfound);
/*-------------------------------------------------------------------------*/
/**
@brief Get the string associated to a key, convert to an long int
@param d Dictionary to search
@param key Key string to look for
@param notfound Value to return in case of error
@return integer
This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the notfound value is returned.
Supported values for integers include the usual C notation
so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
are supported. Examples:
- "42" -> 42
- "042" -> 34 (octal -> decimal)
- "0x42" -> 66 (hexa -> decimal)
Warning: the conversion may overflow in various ways. Conversion is
totally outsourced to strtol(), see the associated man page for overflow
handling.
*/
/*--------------------------------------------------------------------------*/
long int iniparser_getlongint(const dictionary * d, const char * key, long int notfound);
/*-------------------------------------------------------------------------*/
/**
......@@ -169,7 +243,7 @@ int iniparser_getint(dictionary * d, const char * key, int notfound);
the notfound value is returned.
*/
/*--------------------------------------------------------------------------*/
double iniparser_getdouble(dictionary * d, const char * key, double notfound);
double iniparser_getdouble(const dictionary * d, const char * key, double notfound);
/*-------------------------------------------------------------------------*/
/**
......@@ -203,7 +277,7 @@ double iniparser_getdouble(dictionary * d, const char * key, double notfound);
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
int iniparser_getboolean(dictionary * d, const char * key, int notfound);
int iniparser_getboolean(const dictionary * d, const char * key, int notfound);
/*-------------------------------------------------------------------------*/
......@@ -215,14 +289,13 @@ int iniparser_getboolean(dictionary * d, const char * key, int notfound);
@return int 0 if Ok, -1 otherwise.
If the given entry can be found in the dictionary, it is modified to
contain the provided value. If it cannot be found, -1 is returned.
contain the provided value. If it cannot be found, the entry is created.
It is Ok to set val to NULL.
*/
/*--------------------------------------------------------------------------*/
int iniparser_setstring(dictionary * ini, const char * entry, const char * val);
int iniparser_set(dictionary * ini, const char * entry, const char * val);
/*-------------------------------------------------------------------------*/
/**
@brief Delete an entry in a dictionary
......@@ -247,7 +320,7 @@ void iniparser_unset(dictionary * ini, const char * entry);
of querying for the presence of sections in a dictionary.
*/
/*--------------------------------------------------------------------------*/
int iniparser_find_entry(dictionary * ini, const char * entry) ;
int iniparser_find_entry(const dictionary * ini, const char * entry) ;
/*-------------------------------------------------------------------------*/
/**
......@@ -278,4 +351,8 @@ dictionary * iniparser_load(const char * ininame);
/*--------------------------------------------------------------------------*/
void iniparser_freedict(dictionary * d);
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment