| 1 | /* |
| 2 | * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org> |
| 3 | * |
| 4 | * Jansson is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the MIT license. See LICENSE for details. |
| 6 | */ |
| 7 | |
| 8 | #ifndef JANSSON_PRIVATE_H |
| 9 | #define JANSSON_PRIVATE_H |
| 10 | |
| 11 | #ifdef HAVE_CONFIG_H |
| 12 | #include <jansson_private_config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include <stddef.h> |
| 16 | #include "jansson.h" |
| 17 | #include "hashtable.h" |
| 18 | #include "strbuffer.h" |
| 19 | |
| 20 | #define container_of(ptr_, type_, member_) \ |
| 21 | ((type_ *)((char *)ptr_ - offsetof(type_, member_))) |
| 22 | |
| 23 | /* On some platforms, max() may already be defined */ |
| 24 | #ifndef max |
| 25 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 26 | #endif |
| 27 | |
| 28 | /* va_copy is a C99 feature. In C89 implementations, it's sometimes |
| 29 | available as __va_copy. If not, memcpy() should do the trick. */ |
| 30 | #ifndef va_copy |
| 31 | #ifdef __va_copy |
| 32 | #define va_copy __va_copy |
| 33 | #else |
| 34 | #define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list)) |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | typedef struct { |
| 39 | json_t json; |
| 40 | hashtable_t hashtable; |
| 41 | } json_object_t; |
| 42 | |
| 43 | typedef struct { |
| 44 | json_t json; |
| 45 | size_t size; |
| 46 | size_t entries; |
| 47 | json_t **table; |
| 48 | } json_array_t; |
| 49 | |
| 50 | typedef struct { |
| 51 | json_t json; |
| 52 | char *value; |
| 53 | size_t length; |
| 54 | } json_string_t; |
| 55 | |
| 56 | typedef struct { |
| 57 | json_t json; |
| 58 | double value; |
| 59 | } json_real_t; |
| 60 | |
| 61 | typedef struct { |
| 62 | json_t json; |
| 63 | json_int_t value; |
| 64 | } json_integer_t; |
| 65 | |
| 66 | #define json_to_object(json_) container_of(json_, json_object_t, json) |
| 67 | #define json_to_array(json_) container_of(json_, json_array_t, json) |
| 68 | #define json_to_string(json_) container_of(json_, json_string_t, json) |
| 69 | #define json_to_real(json_) container_of(json_, json_real_t, json) |
| 70 | #define json_to_integer(json_) container_of(json_, json_integer_t, json) |
| 71 | |
| 72 | /* Create a string by taking ownership of an existing buffer */ |
| 73 | json_t *jsonp_stringn_nocheck_own(const char *value, size_t len); |
| 74 | |
| 75 | /* Error message formatting */ |
| 76 | void jsonp_error_init(json_error_t *error, const char *source); |
| 77 | void jsonp_error_set_source(json_error_t *error, const char *source); |
| 78 | void jsonp_error_set(json_error_t *error, int line, int column, |
| 79 | size_t position, enum json_error_code code, |
| 80 | const char *msg, ...); |
| 81 | void jsonp_error_vset(json_error_t *error, int line, int column, |
| 82 | size_t position, enum json_error_code code, |
| 83 | const char *msg, va_list ap); |
| 84 | |
| 85 | /* Locale independent string<->double conversions */ |
| 86 | int jsonp_strtod(strbuffer_t *strbuffer, double *out); |
| 87 | int jsonp_dtostr(char *buffer, size_t size, double value, int prec); |
| 88 | |
| 89 | /* Wrappers for custom memory functions */ |
| 90 | void* jsonp_malloc(size_t size) JANSSON_ATTRS(warn_unused_result); |
| 91 | void jsonp_free(void *ptr); |
| 92 | char *jsonp_strndup(const char *str, size_t length) JANSSON_ATTRS(warn_unused_result); |
| 93 | char *jsonp_strdup(const char *str) JANSSON_ATTRS(warn_unused_result); |
| 94 | char *jsonp_strndup(const char *str, size_t len) JANSSON_ATTRS(warn_unused_result); |
| 95 | |
| 96 | |
| 97 | /* Windows compatibility */ |
| 98 | #if defined(_WIN32) || defined(WIN32) |
| 99 | # if defined(_MSC_VER) /* MS compiller */ |
| 100 | # if (_MSC_VER < 1900) && !defined(snprintf) /* snprintf not defined yet & not introduced */ |
| 101 | # define snprintf _snprintf |
| 102 | # endif |
| 103 | # if (_MSC_VER < 1500) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */ |
| 104 | # define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) |
| 105 | # endif |
| 106 | # else /* Other Windows compiller, old definition */ |
| 107 | # define snprintf _snprintf |
| 108 | # define vsnprintf _vsnprintf |
| 109 | # endif |
| 110 | #endif |
| 111 | |
| 112 | #endif |