From a087bcabb57091398927765fbf111e9dd3385bdc Mon Sep 17 00:00:00 2001 From: rafagafe Date: Thu, 15 Nov 2018 00:43:30 +0100 Subject: [PATCH] Remove GNU extensions and some refactors are made. --- example-03.c | 6 +++--- makefile | 20 ++++++++++---------- tiny-json.c | 20 ++++++++++++-------- tiny-json.h | 41 +++++++++++++++++++++-------------------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/example-03.c b/example-03.c index d9a37b6..df5c83a 100644 --- a/example-03.c +++ b/example-03.c @@ -44,13 +44,13 @@ typedef struct jsonStaticPool_s { } jsonStaticPool_t; static json_t* poolInit( jsonPool_t* pool ) { - jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool); + jsonStaticPool_t* spool = json_containerOf(pool, jsonStaticPool_t, pool); spool->nextFree = 1; return &spool->mem[0]; } static json_t* poolNew( jsonPool_t* pool ) { - jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool); + jsonStaticPool_t* spool = json_containerOf(pool, jsonStaticPool_t, pool); if ( spool->nextFree >= sizeof spool->mem / sizeof spool->mem[0] ) return 0; return &spool->mem[spool->nextFree++]; } @@ -74,7 +74,7 @@ int main( void ) { "}\n"; puts( str ); jsonStaticPool_t spool = { .pool = { .init = poolInit, .new = poolNew } }; - json_t const* json = json_create_pool( str, &spool.pool ); + json_t const *json = json_createWithPool( str, &spool.pool ); if ( !json ) { puts("Error json create."); return EXIT_FAILURE; diff --git a/makefile b/makefile index 1549fff..539fe48 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ - +CFLAGS = -std=c99 -Wall -pedantic build: example-01.exe example-02.exe example-03.exe @@ -12,28 +12,28 @@ test: test.exe ./test.exe example-01.exe: example-01.o tiny-json.o - gcc -std=c99 -Wall -o example-01.exe example-01.o tiny-json.o + gcc $(CFLAGS) -o $@ $^ example-02.exe: example-02.o tiny-json.o - gcc -std=c99 -Wall -o example-02.exe example-02.o tiny-json.o + gcc $(CFLAGS) -o $@ $^ example-03.exe: example-03.o tiny-json.o - gcc -std=c99 -Wall -o example-03.exe example-03.o tiny-json.o + gcc $(CFLAGS) -o $@ $^ test.exe: tests.o tiny-json.o - gcc -std=c99 -Wall -o test.exe tests.o tiny-json.o + gcc $(CFLAGS) -o $@ $^ tiny-json.o: tiny-json.c tiny-json.h - gcc -std=c99 -Wall -c tiny-json.c + gcc $(CFLAGS) -c tiny-json.c example-01.o: example-01.c tiny-json.h - gcc -std=c99 -Wall -c example-01.c + gcc $(CFLAGS) -c example-01.c example-02.o: example-02.c tiny-json.h - gcc -std=c99 -Wall -c example-02.c + gcc $(CFLAGS) -c example-02.c example-03.o: example-03.c tiny-json.h - gcc -std=c99 -Wall -c example-03.c + gcc $(CFLAGS) -c example-03.c tests.o: tests.c tiny-json.h - gcc -std=c99 -Wall -c tests.c + gcc $(CFLAGS) -c tests.c diff --git a/tiny-json.c b/tiny-json.c index ecc2aca..40ab8ab 100644 --- a/tiny-json.c +++ b/tiny-json.c @@ -67,7 +67,7 @@ static char* setToNull( char* ch ); static bool isEndOfPrimitive( char ch ); /* Parse a string to get a json. */ -json_t const* json_create_pool( char* str, jsonPool_t* pool ) { +json_t const* json_createWithPool( char *str, jsonPool_t *pool ) { char* ptr = goBlank( str ); if ( !ptr || *ptr != '{' ) return 0; json_t* obj = pool->init( pool ); @@ -82,10 +82,14 @@ json_t const* json_create_pool( char* str, jsonPool_t* pool ) { /* Parse a string to get a json. */ json_t const* json_create( char* str, json_t mem[], unsigned int qty ) { jsonStaticPool_t spool = { - .mem = mem, .qty = qty, - .pool = { .init = poolInit, .new = poolNew } + .mem = mem, + .qty = qty, + .pool = { + .init = poolInit, + .new = poolNew + } }; - return json_create_pool( str, &spool.pool ); + return json_createWithPool( str, &spool.pool ); } /** Get a special character with its escape character. Examples: @@ -383,9 +387,9 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) { * @param pool The handler of the pool. * @return a instance of a json. */ static json_t* poolInit( jsonPool_t* pool ) { - jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool); + jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool ); spool->nextFree = 1; - return &spool->mem[0]; + return spool->mem; } /** Create an instance of a json from a pool. @@ -393,9 +397,9 @@ static json_t* poolInit( jsonPool_t* pool ) { * @retval The handler of the new instance if success. * @retval Null pointer if the pool was empty. */ static json_t* poolNew( jsonPool_t* pool ) { - jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool); + jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool ); if ( spool->nextFree >= spool->qty ) return 0; - return &spool->mem[spool->nextFree++]; + return spool->mem + spool->nextFree++; } /** Checks whether an character belongs to set. diff --git a/tiny-json.h b/tiny-json.h index d5e5d28..2984558 100644 --- a/tiny-json.h +++ b/tiny-json.h @@ -35,13 +35,12 @@ extern "C" { #endif #include +#include #include #include -#include -#define json_container_of(ptr, type, member) ({ \ - void *__mptr = (void *)(ptr); \ - ((type *)(__mptr - offsetof(type, member))); }) +#define json_containerOf( ptr, type, member ) \ + ((type*)( (char*)ptr - offsetof( type, member ) )) /** @defgroup tinyJson Tiny JSON parser. * @{ */ @@ -66,13 +65,6 @@ typedef struct json_s { jsonType_t type; } json_t; -/** Structure to handle a heap of JSON properties. */ -typedef struct jsonPool_s jsonPool_t; -typedef struct jsonPool_s { - json_t* (*init) ( jsonPool_t* pool ); - json_t* (*new) ( jsonPool_t* pool ); -} jsonPool_t; - /** Parse a string to get a json. * @param str String pointer with a JSON object. It will be modified. * @param mem Array of json properties to allocate. @@ -82,14 +74,6 @@ typedef struct jsonPool_s { * This property is always unnamed and its type is JSON_OBJ. */ json_t const* json_create( char* str, json_t mem[], unsigned int qty ); -/** Parse a string to get a json. - * @param str String pointer with a JSON object. It will be modified. - * @param pool Custom json pool pointer. - * @retval Null pointer if any was wrong in the parse process. - * @retval If the parser process was successfully a valid handler of a json. - * This property is always unnamed and its type is JSON_OBJ. */ -json_t const* json_create_pool( char* str, jsonPool_t *pool ); - /** Get the name of a json property. * @param json A valid handler of a json property. * @retval Pointer to null-terminated if property has name. @@ -156,7 +140,7 @@ static inline bool json_getBoolean( json_t const* property ) { * @param property A valid handler of a json object. Its type must be JSON_INTEGER. * @return The value stdint. */ static inline int64_t json_getInteger( json_t const* property ) { - return (int64_t)atoll( property->u.value ); + return atoll( property->u.value ); } /** Get the value of a json real property. @@ -166,6 +150,23 @@ static inline double json_getReal( json_t const* property ) { return atof( property->u.value ); } + + +/** Structure to handle a heap of JSON properties. */ +typedef struct jsonPool_s jsonPool_t; +struct jsonPool_s { + json_t* (*init)( jsonPool_t* pool ); + json_t* (*new)( jsonPool_t* pool ); +}; + +/** Parse a string to get a json. + * @param str String pointer with a JSON object. It will be modified. + * @param pool Custom json pool pointer. + * @retval Null pointer if any was wrong in the parse process. + * @retval If the parser process was successfully a valid handler of a json. + * This property is always unnamed and its type is JSON_OBJ. */ +json_t const* json_createWithPool( char* str, jsonPool_t* pool ); + /** @ } */ #ifdef __cplusplus