1 /*
2  * Copyright (c) 2009-2014 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 #include <stddef.h>
12 #include "jansson.h"
13 #include "hashtable.h"
14 #include "strbuffer.h"
15 #include "plat_types.h"
16 #define container_of(ptr_, type_, member_)  \
17     ((type_ *)((char *)ptr_ - OFFSETOF(type_, member_)))
18 
19 /* On some platforms, max() may already be defined */
20 #ifndef max
21 #define max(a, b)  ((a) > (b) ? (a) : (b))
22 #endif
23 
24 /* va_copy is a C99 feature. In C89 implementations, it's sometimes
25    available as __va_copy. If not, memcpy() should do the trick. */
26 #ifndef va_copy
27 #ifdef __va_copy
28 #define va_copy __va_copy
29 #else
30 #define va_copy(a, b)  memcpy(&(a), &(b), sizeof(va_list))
31 #endif
32 #endif
33 
34 typedef struct {
35     json_t json;
36     hashtable_t hashtable;
37     size_t serial;
38     int visited;
39 } json_object_t;
40 
41 typedef struct {
42     json_t json;
43     size_t size;
44     size_t entries;
45     json_t **table;
46     int visited;
47 } json_array_t;
48 
49 typedef struct {
50     json_t json;
51     char *value;
52     size_t length;
53 } json_string_t;
54 
55 typedef struct {
56     json_t json;
57     double value;
58 } json_real_t;
59 
60 typedef struct {
61     json_t json;
62     json_int_t value;
63 } json_integer_t;
64 
65 #define json_to_object(json_)  container_of(json_, json_object_t, json)
66 #define json_to_array(json_)   container_of(json_, json_array_t, json)
67 #define json_to_string(json_)  container_of(json_, json_string_t, json)
68 #define json_to_real(json_)    container_of(json_, json_real_t, json)
69 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
70 
71 /* Create a string by taking ownership of an existing buffer */
72 json_t *jsonp_stringn_nocheck_own(const char *value, size_t len);
73 
74 /* Error message formatting */
75 void jsonp_error_init(json_error_t *error, const char *source);
76 void jsonp_error_set_source(json_error_t *error, const char *source);
77 void jsonp_error_set(json_error_t *error, int line, int column,
78                      size_t position, const char *msg, ...);
79 void jsonp_error_vset(json_error_t *error, int line, int column,
80                       size_t position, const char *msg, va_list ap);
81 
82 /* Locale independent string<->double conversions */
83 int jsonp_strtod(strbuffer_t *strbuffer, double *out);
84 int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
85 
86 /* Wrappers for custom memory functions */
87 void* jsonp_malloc(size_t size);
88 void jsonp_free(void *ptr);
89 char *jsonp_strndup(const char *str, size_t length);
90 char *jsonp_strdup(const char *str);
91 char *jsonp_strndup(const char *str, size_t len);
92 
93 /* Windows compatibility */
94 #ifdef _WIN32
95 #define snprintf _snprintf
96 #define vsnprintf _vsnprintf
97 #endif
98 
99 #endif
100