Commit 7c23faec authored by Robert Izzard's avatar Robert Izzard
Browse files

update to 1.52

bug fixes in cdict_json_to_c, updated ryu double formatting to make sure doubles are doubles, e.g. "1.0" not "1" (which would be an int), shift cprint_asprintf to its own file, can now read doubles starting with "+", add entry->pre_output_function, started to make code for cdict array type. Note still some small memory leaks in cdict_json_to().
parent 4774f218
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -28,3 +28,5 @@
15/06/2024 : V1.5 added cdict_iter, an iterator for nested cdicts.

07/07/2024 : V1.51 restructed structures to save ~25% memory, other minor fixes/cleaning of code, few extra subroutines to improve allocating memory.

30/07/2024 : V1.52 bug fixes in cdict_json_to_c, updated ryu double formatting to make sure doubles are doubles, e.g. "1.0" not "1" (which would be an int), shift cprint_asprintf to its own file, can now read doubles starting with "+", add entry->pre_output_function, started to make code for cdict array type. Note still some small memory leaks in cdict_json_to(). 
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
#include "cdict_prototypes.h"
#include "cdict_API.h"

#define CDICT_VERSION "1.51"
#define CDICT_VERSION "1.52"

/*
 * Global variables - very bad, I know, but these
+32 −2
Original line number Diff line number Diff line
@@ -145,11 +145,16 @@
#define __CDict_set_3(CDICT,                                    \
                      KEY,                                      \
                      VALUE)                                    \
    __extension__({                                             \
            printf("set3 %d\n",                                 \
                   __CDict_valuetype_num(VALUE)                 \
                );                                              \
            CDict_set_with_types((CDICT),                       \
                                 (KEY),                         \
                                 __CDict_keytype_num(KEY),      \
                                 (VALUE),                       \
                         __CDict_valuetype_num(VALUE))
                                 __CDict_valuetype_num(VALUE)); \
        })

/* 4 arguments : set the metadata, assume it doesn't need freeing */
#define __CDict_set_4(CDICT,                                        \
@@ -717,7 +722,6 @@
 *
 * Note that we append by default
 */

#define CDict_nest(CDICT,KEY,VALUE,...)         \
    __CDict_nest_with_metadata(                 \
        (CDICT),                                \
@@ -925,6 +929,11 @@
/*
 * From a nested cdict, get the data value at the passed in
 * location.
 *
 * Note that the type of VAR must match the type of the
 * data in the value, so if you're looking for a double
 * value, VAR must be a double. If you don't know the
 * type, all bets are off!
 */
#define CDict_nest_get_data_value(...)          \
    CDict_nest_get_data_value_implementation(   \
@@ -978,6 +987,27 @@
        strlen(Concat3(string,LABEL,LINE)) <= 0 ? NULL : (char*)Concat3(string,LABEL,LINE); \
    })

/*
 * As above for n characters.
 */
#define CDict_stringn(...)                        \
    CDict_stringn_implementation(                 \
        CDict_stringn_implementation,             \
        __COUNTER__,                              \
        __VA_ARGS__)

#define CDict_stringn_implementation(LABEL,                             \
                                    LINE,                               \
                                    CDICT,                              \
                                    STRING,                             \
                                    N)                                  \
    __extension__                                                       \
    ({                                                                  \
        char * Concat3(string,LABEL,LINE) = strndup((STRING),(N));      \
        cdict_push_tofree((CDICT),Concat3(string,LABEL,LINE));          \
        strlen(Concat3(string,LABEL,LINE)) <= 0 ? NULL : (char*)Concat3(string,LABEL,LINE); \
    })

#define CDict_asprintf(...)                     \
    CDict_asprintf_implementation(              \
        CDict_asprintf,                         \
+2 −34
Original line number Diff line number Diff line
@@ -8,12 +8,6 @@
/*
 * Wrappers for malloc, calloc, realloc, free used in libcdict
 */
static void cdict_push_pointer(struct cdict_t * const cdict,
                               void * const ptr,
                               const size_t size,
                               unsigned int mode);
static void cdict_del_pointer(struct cdict_t * const cdict,
                              void * const ptr);
static struct cdict_pointer_list_item_t * cdict_lookup_pointer(
    struct cdict_t * const cdict,
    const void * const ptr,
@@ -100,32 +94,6 @@ void cdict_free_alloc_function(struct cdict_t * const cdict,
    }
}

int cdict_asprintf(struct cdict_t * const cdict,
                   char ** const strp,
                   const char * fmt,
                   ...)
{
    /*
     * Wrapper for asprintf to include cdict alloc statistics
     * if required
     */
    va_list vp,vpc;
    va_start(vp,fmt);
    va_copy(vpc,vp);
    const int ret = vasprintf(strp,fmt,vpc);
    if(cdict != NULL &&
       strp != NULL &&
       cdict->ancestor != NULL &&
       cdict->ancestor->stats != NULL)
    {
        cdict->ancestor->stats->asprintf_count++;
        cdict->ancestor->stats->asprintf_size += (size_t)ret;
        cdict_push_pointer(cdict,*strp,(size_t)ret,CDICT_MODE_ASPRINTF);
    }
    va_end(vpc);
    va_end(vp);
    return ret;
}

static struct cdict_pointer_list_item_t *
cdict_lookup_pointer(struct cdict_t * const cdict,
@@ -151,7 +119,7 @@ cdict_lookup_pointer(struct cdict_t * const cdict,
    return NULL;
}

static void cdict_del_pointer(struct cdict_t * const cdict,
void cdict_del_pointer(struct cdict_t * const cdict,
                       void * const ptr)
{
    struct cdict_pointer_list_item_t * l = NULL;
@@ -171,7 +139,7 @@ static void cdict_del_pointer(struct cdict_t * const cdict,
    }
}

static void cdict_push_pointer(struct cdict_t * const cdict,
void cdict_push_pointer(struct cdict_t * const cdict,
                        void * const ptr,
                        const size_t size,
                        unsigned int mode)

src/cdict_asprintf.c

0 → 100644
+33 −0
Original line number Diff line number Diff line
#define _GNU_SOURCE
#include <stdio.h>

#include "cdict.h"
#include <stdarg.h>

CDict_API_function
int cdict_asprintf(struct cdict_t * const cdict,
                   char ** const strp,
                   const char * fmt,
                   ...)
{
    /*
     * Wrapper for asprintf to include cdict alloc statistics
     * if required
     */
    va_list vp,vpc;
    va_start(vp,fmt);
    va_copy(vpc,vp);
    const int ret = vasprintf(strp,fmt,vpc);
    if(cdict != NULL &&
       strp != NULL &&
       cdict->ancestor != NULL &&
       cdict->ancestor->stats != NULL)
    {
        cdict->ancestor->stats->asprintf_count++;
        cdict->ancestor->stats->asprintf_size += (size_t)ret;
        cdict_push_pointer(cdict,*strp,(size_t)ret,CDICT_MODE_ASPRINTF);
    }
    va_end(vpc);
    va_end(vp);
    return ret;
}
Loading