Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/Make/Grass.make
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ LRSDEPS = $(DBMILIB) $(GISLIB)
MANAGEDEPS = $(VECTORLIB) $(GISLIB)
NVIZDEPS = $(OGSFLIB) $(GISLIB) $(OPENGLLIB)
OGSFDEPS = $(BITMAPLIB) $(RASTER3DLIB) $(VECTORLIB) $(DBMILIB) $(RASTERLIB) $(GISLIB) $(TIFFLIBPATH) $(TIFFLIB) $(OPENGLLIB) $(OPENGLULIB) $(MATHLIB)
PARSONDEPS = $(GISLIB)
PNGDRIVERDEPS = $(DRIVERLIB) $(GISLIB) $(PNGLIB) $(MATHLIB)
PSDRIVERDEPS = $(DRIVERLIB) $(GISLIB) $(MATHLIB)
RASTERDEPS = $(GISLIB) $(GPROJLIB) $(MATHLIB) $(PARSONLIB) $(GDALLIBS)
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build_library_in_subdir(

add_subdirectory(proj)

build_library_in_subdir(external/parson NAME grass_parson HEADERS "gjson.h")
build_library_in_subdir(external/parson NAME grass_parson DEPENDS grass_gis HEADERS "gjson.h")
build_program_in_subdir(external/parson/test NAME test.gjson.lib DEPENDS grass_gis grass_parson)

build_library_in_subdir(
Expand Down
1 change: 1 addition & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SUBDIRS = \
datetime \
gis \
proj \
external/parson \
raster \
gmath \
linkm \
Expand Down
1 change: 0 additions & 1 deletion lib/external/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ MODULE_TOPDIR = ../..

SUBDIRS = \
ccmath \
parson \
shapelib

include $(MODULE_TOPDIR)/include/Make/Dir.make
Expand Down
93 changes: 93 additions & 0 deletions lib/external/parson/gjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
*
*****************************************************************************/

#ifndef __STDC_NO_ATOMICS__
#include <stdatomic.h>
#endif

#include <grass/gis.h>

#include "gjson.h"
#include "parson.h"

Expand All @@ -33,44 +39,100 @@ typedef struct json_array_t G_json_array_t;
/// \since version 8.5
typedef struct json_value_t G_json_value_t;

#define G_PARSON_MALLOC_ATTR \
G_ATTR_MALLOC G_ATTR_ALLOC_SIZE1(1) G_ATTR_OWNSHIP_RET G_ATTR_RET_NONNULL

static void *G__parson_malloc(size_t size) G_PARSON_MALLOC_ATTR;

static void *G__parson_malloc(size_t size)
{
return G_malloc(size);
}

#undef G_PARSON_MALLOC_ATTR

#if defined __STDC_NO_ATOMICS__
static volatile int parson_initialized = 0;
#else
typedef enum { UNINITIALIZED, INITIALIZING, INITIALIZED } init_state_t;
static _Atomic int parson_init_state = UNINITIALIZED;
#endif

void ensure_parson_initialized(void)
{
#if defined __STDC_NO_ATOMICS__
if (!parson_initialized) {
json_set_allocation_functions(G__parson_malloc, G_free);
parson_initialized = 1;
}
#else
if (atomic_load_explicit(&parson_init_state, memory_order_acquire) ==
INITIALIZED) {
return;
}

int expected = UNINITIALIZED;

if (atomic_compare_exchange_strong(&parson_init_state, &expected,
INITIALIZING)) {

json_set_allocation_functions(G__parson_malloc, G_free);

atomic_store_explicit(&parson_init_state, INITIALIZED,
memory_order_release);
}
else {
while (atomic_load_explicit(&parson_init_state, memory_order_acquire) !=
INITIALIZED)
;
}
#endif
}

/* *************************************************************** */
/* ***** WRAPPER FOR PARSON FUNCTIONS USED IN GRASS ************** */
/* *************************************************************** */

/// \since version 8.5
G_JSON_Value *G_json_value_init_object(void)
{
ensure_parson_initialized();
return (G_JSON_Value *)json_value_init_object();
}

/// \since version 8.5
G_JSON_Value *G_json_value_init_array(void)
{
ensure_parson_initialized();
return (G_JSON_Value *)json_value_init_array();
}

/// \since version 8.5
G_JSON_Value_Type G_json_value_get_type(const G_JSON_Value *value)
{
ensure_parson_initialized();
return json_value_get_type((const JSON_Value *)value);
}

/// \since version 8.5
G_JSON_Object *G_json_value_get_object(const G_JSON_Value *value)
{
ensure_parson_initialized();
return (G_JSON_Object *)json_value_get_object((const JSON_Value *)value);
}

/// \since version 8.5
G_JSON_Object *G_json_object(const G_JSON_Value *value)
{
ensure_parson_initialized();
return (G_JSON_Object *)json_object((const JSON_Value *)value);
}

/// \since version 8.5
G_JSON_Object *G_json_object_get_object(const G_JSON_Object *object,
const char *name)
{
ensure_parson_initialized();
return (G_JSON_Object *)json_object_get_object((const JSON_Object *)object,
name);
}
Expand All @@ -79,6 +141,7 @@ G_JSON_Object *G_json_object_get_object(const G_JSON_Object *object,
G_JSON_Array *G_json_object_get_array(const G_JSON_Object *object,
const char *name)
{
ensure_parson_initialized();
return (G_JSON_Array *)json_object_get_array((const JSON_Object *)object,
name);
}
Expand All @@ -87,6 +150,7 @@ G_JSON_Array *G_json_object_get_array(const G_JSON_Object *object,
G_JSON_Value *G_json_object_get_value(const G_JSON_Object *object,
const char *name)
{
ensure_parson_initialized();
return (G_JSON_Value *)json_object_get_value((const JSON_Object *)object,
name);
}
Expand All @@ -95,24 +159,28 @@ G_JSON_Value *G_json_object_get_value(const G_JSON_Object *object,
const char *G_json_object_get_string(const G_JSON_Object *object,
const char *name)
{
ensure_parson_initialized();
return json_object_get_string((const JSON_Object *)object, name);
}

/// \since version 8.5
double G_json_object_get_number(const G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_get_number((const JSON_Object *)object, name);
}

/// \since version 8.5
int G_json_object_get_boolean(const G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_get_boolean((const JSON_Object *)object, name);
}

/// \since version 8.5
G_JSON_Value *G_json_object_get_wrapping_value(const G_JSON_Object *object)
{
ensure_parson_initialized();
return (G_JSON_Value *)json_object_get_wrapping_value(
(const JSON_Object *)object);
}
Expand All @@ -121,6 +189,7 @@ G_JSON_Value *G_json_object_get_wrapping_value(const G_JSON_Object *object)
G_JSON_Status G_json_object_set_value(G_JSON_Object *object, const char *name,
G_JSON_Value *value)
{
ensure_parson_initialized();
return json_object_set_value((JSON_Object *)object, name,
(JSON_Value *)value);
}
Expand All @@ -129,150 +198,174 @@ G_JSON_Status G_json_object_set_value(G_JSON_Object *object, const char *name,
G_JSON_Status G_json_object_set_string(G_JSON_Object *object, const char *name,
const char *string)
{
ensure_parson_initialized();
return json_object_set_string((JSON_Object *)object, name, string);
}

/// \since version 8.5
G_JSON_Status G_json_object_set_number(G_JSON_Object *object, const char *name,
double number)
{
ensure_parson_initialized();
return json_object_set_number((JSON_Object *)object, name, number);
}

/// \since version 8.5
G_JSON_Status G_json_object_set_boolean(G_JSON_Object *object, const char *name,
int boolean)
{
ensure_parson_initialized();
return json_object_set_boolean((JSON_Object *)object, name, boolean);
}

/// \since version 8.5
G_JSON_Status G_json_object_set_null(G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_set_null((JSON_Object *)object, name);
}

/// \since version 8.5
G_JSON_Status G_json_object_dotset_string(G_JSON_Object *object,
const char *name, const char *string)
{
ensure_parson_initialized();
return json_object_dotset_string((JSON_Object *)object, name, string);
}

/// \since version 8.5
const char *G_json_object_dotget_string(G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_dotget_string((JSON_Object *)object, name);
}

/// \since version 8.5
G_JSON_Status G_json_object_dotset_number(G_JSON_Object *object,
const char *name, double number)
{
ensure_parson_initialized();
return json_object_dotset_number((JSON_Object *)object, name, number);
}

/// \since version 8.5
double G_json_object_dotget_number(G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_dotget_number((JSON_Object *)object, name);
}

/// \since version 8.5
G_JSON_Status G_json_object_dotset_null(G_JSON_Object *object, const char *name)
{
ensure_parson_initialized();
return json_object_dotset_null((JSON_Object *)object, name);
}

/// \since version 8.5
G_JSON_Array *G_json_array(const G_JSON_Value *value)
{
ensure_parson_initialized();
return (G_JSON_Array *)json_array((const JSON_Value *)value);
}

/// \since version 8.5
G_JSON_Value *G_json_array_get_value(const G_JSON_Array *array, size_t index)
{
ensure_parson_initialized();
return (G_JSON_Value *)json_array_get_value((const JSON_Array *)array,
index);
}

/// \since version 8.5
const char *G_json_array_get_string(const G_JSON_Array *array, size_t index)
{
ensure_parson_initialized();
return json_array_get_string((const JSON_Array *)array, index);
}

/// \since version 8.5
double G_json_array_get_number(const G_JSON_Array *array, size_t index)
{
ensure_parson_initialized();
return json_array_get_number((const JSON_Array *)array, index);
}

/// \since version 8.5
int G_json_array_get_boolean(const G_JSON_Array *array, size_t index)
{
ensure_parson_initialized();
return json_array_get_boolean((const JSON_Array *)array, index);
}

/// \since version 8.5
G_JSON_Status G_json_array_append_value(G_JSON_Array *array,
G_JSON_Value *value)
{
ensure_parson_initialized();
return json_array_append_value((JSON_Array *)array, (JSON_Value *)value);
}

/// \since version 8.5
G_JSON_Status G_json_array_append_string(G_JSON_Array *array,
const char *string)
{
ensure_parson_initialized();
return json_array_append_string((JSON_Array *)array, string);
}

/// \since version 8.5
G_JSON_Status G_json_array_append_number(G_JSON_Array *array, double number)
{
ensure_parson_initialized();
return json_array_append_number((JSON_Array *)array, number);
}

/// \since version 8.5
G_JSON_Status G_json_array_append_boolean(G_JSON_Array *array, int boolean)
{
ensure_parson_initialized();
return json_array_append_boolean((JSON_Array *)array, boolean);
}

/// \since version 8.5
G_JSON_Status G_json_array_append_null(G_JSON_Array *array)
{
ensure_parson_initialized();
return json_array_append_null((JSON_Array *)array);
}

/// \since version 8.5
void G_json_set_float_serialization_format(const char *format)
{
ensure_parson_initialized();
json_set_float_serialization_format(format);
}

/// \since version 8.5
char *G_json_serialize_to_string_pretty(const G_JSON_Value *value)
{
ensure_parson_initialized();
return json_serialize_to_string_pretty((const JSON_Value *)value);
}

/// \since version 8.5
char *G_json_serialize_to_string(const G_JSON_Value *value)
{
ensure_parson_initialized();
return json_serialize_to_string((const JSON_Value *)value);
}

/// \since version 8.5
void G_json_free_serialized_string(char *string)
{
ensure_parson_initialized();
json_free_serialized_string(string);
}

/// \since version 8.5
void G_json_value_free(G_JSON_Value *value)
{
ensure_parson_initialized();
json_value_free((JSON_Value *)value);
}
6 changes: 4 additions & 2 deletions lib/external/parson/gjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stddef.h>

#include <grass/gis.h>

/* *************************************************************** */
/* ***** WRAPPER FOR PARSON FUNCTIONS USED IN GRASS ************** */
/* *************************************************************** */
Expand All @@ -25,8 +27,8 @@ typedef int G_JSON_Value_Type;
enum g_json_result_t { G_JSONSuccess = 0, G_JSONFailure = -1 };
typedef int G_JSON_Status;

extern G_JSON_Value *G_json_value_init_object(void);
extern G_JSON_Value *G_json_value_init_array(void);
extern G_JSON_Value *G_json_value_init_object(void) G_ATTR_RET_NONNULL;
extern G_JSON_Value *G_json_value_init_array(void) G_ATTR_RET_NONNULL;

extern G_JSON_Value_Type G_json_value_get_type(const G_JSON_Value *value);
extern G_JSON_Object *G_json_value_get_object(const G_JSON_Value *);
Expand Down
Loading