2009-07-28 16:01:52 +08:00
|
|
|
/*
|
2014-01-28 15:16:05 +08:00
|
|
|
* Copyright (c) 2009-2014 Petri Lehtinen <petri@digip.org>
|
2009-07-28 16:01:52 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
2009-07-14 02:03:09 +08:00
|
|
|
#ifndef JANSSON_PRIVATE_H
|
|
|
|
#define JANSSON_PRIVATE_H
|
|
|
|
|
2010-08-13 02:34:02 +08:00
|
|
|
#include <stddef.h>
|
2009-10-16 02:02:27 +08:00
|
|
|
#include "jansson.h"
|
|
|
|
#include "hashtable.h"
|
2011-10-03 02:27:53 +08:00
|
|
|
#include "strbuffer.h"
|
2009-10-16 02:02:27 +08:00
|
|
|
|
|
|
|
#define container_of(ptr_, type_, member_) \
|
2010-08-13 02:34:02 +08:00
|
|
|
((type_ *)((char *)ptr_ - offsetof(type_, member_)))
|
2009-10-16 02:02:27 +08:00
|
|
|
|
2010-09-06 01:54:37 +08:00
|
|
|
/* On some platforms, max() may already be defined */
|
|
|
|
#ifndef max
|
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2011-03-21 03:15:39 +08:00
|
|
|
/* va_copy is a C99 feature. In C89 implementations, it's sometimes
|
|
|
|
available as __va_copy. If not, memcpy() should do the trick. */
|
|
|
|
#ifndef va_copy
|
|
|
|
#ifdef __va_copy
|
|
|
|
#define va_copy __va_copy
|
|
|
|
#else
|
|
|
|
#define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-10-16 02:02:27 +08:00
|
|
|
typedef struct {
|
|
|
|
json_t json;
|
|
|
|
hashtable_t hashtable;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t serial;
|
2009-10-16 02:02:27 +08:00
|
|
|
int visited;
|
|
|
|
} json_object_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
json_t json;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t size;
|
|
|
|
size_t entries;
|
2009-10-16 02:02:27 +08:00
|
|
|
json_t **table;
|
|
|
|
int visited;
|
|
|
|
} json_array_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
json_t json;
|
|
|
|
char *value;
|
2012-04-04 09:00:29 +08:00
|
|
|
size_t length;
|
2009-10-16 02:02:27 +08:00
|
|
|
} json_string_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
json_t json;
|
|
|
|
double value;
|
|
|
|
} json_real_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
json_t json;
|
2010-08-14 03:06:01 +08:00
|
|
|
json_int_t value;
|
2009-10-16 02:02:27 +08:00
|
|
|
} json_integer_t;
|
|
|
|
|
|
|
|
#define json_to_object(json_) container_of(json_, json_object_t, json)
|
|
|
|
#define json_to_array(json_) container_of(json_, json_array_t, json)
|
|
|
|
#define json_to_string(json_) container_of(json_, json_string_t, json)
|
2012-04-04 09:00:29 +08:00
|
|
|
#define json_to_real(json_) container_of(json_, json_real_t, json)
|
2009-10-16 02:02:27 +08:00
|
|
|
#define json_to_integer(json_) container_of(json_, json_integer_t, json)
|
|
|
|
|
2012-04-04 09:00:29 +08:00
|
|
|
/* Create a string by taking ownership of an existing buffer */
|
|
|
|
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len);
|
|
|
|
|
|
|
|
/* Error message formatting */
|
2010-10-27 04:36:24 +08:00
|
|
|
void jsonp_error_init(json_error_t *error, const char *source);
|
2011-02-23 01:08:41 +08:00
|
|
|
void jsonp_error_set_source(json_error_t *error, const char *source);
|
2010-10-27 04:36:24 +08:00
|
|
|
void jsonp_error_set(json_error_t *error, int line, int column,
|
2011-02-22 16:47:02 +08:00
|
|
|
size_t position, const char *msg, ...);
|
2011-01-24 03:14:19 +08:00
|
|
|
void jsonp_error_vset(json_error_t *error, int line, int column,
|
2011-02-22 16:47:02 +08:00
|
|
|
size_t position, const char *msg, va_list ap);
|
2010-10-27 04:36:24 +08:00
|
|
|
|
2011-10-03 02:27:53 +08:00
|
|
|
/* Locale independent string<->double conversions */
|
|
|
|
int jsonp_strtod(strbuffer_t *strbuffer, double *out);
|
2014-04-30 17:46:33 +08:00
|
|
|
int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
|
2011-10-03 02:27:53 +08:00
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
/* Wrappers for custom memory functions */
|
|
|
|
void* jsonp_malloc(size_t size);
|
|
|
|
void jsonp_free(void *ptr);
|
2013-08-15 02:54:11 +08:00
|
|
|
char *jsonp_strndup(const char *str, size_t length);
|
2011-02-17 16:10:53 +08:00
|
|
|
char *jsonp_strdup(const char *str);
|
2012-04-04 09:00:29 +08:00
|
|
|
char *jsonp_strndup(const char *str, size_t len);
|
2011-02-17 16:10:53 +08:00
|
|
|
|
2012-04-30 03:09:29 +08:00
|
|
|
/* Windows compatibility */
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif
|
|
|
|
|
2009-07-14 02:03:09 +08:00
|
|
|
#endif
|