b4743f3b0f44acb55d6f9547e8c9c9ae9db352ca
[jansson.git] / src / jansson_private.h
1 /*
2  * Copyright (c) 2009-2011 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 <stdarg.h>
13 #include "jansson.h"
14 #include "hashtable.h"
15
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 typedef struct {
25     json_t json;
26     hashtable_t hashtable;
27     size_t serial;
28     int visited;
29 } json_object_t;
30
31 typedef struct {
32     json_t json;
33     size_t size;
34     size_t entries;
35     json_t **table;
36     int visited;
37 } json_array_t;
38
39 typedef struct {
40     json_t json;
41     char *value;
42 } json_string_t;
43
44 typedef struct {
45     json_t json;
46     double value;
47 } json_real_t;
48
49 typedef struct {
50     json_t json;
51     json_int_t value;
52 } json_integer_t;
53
54 #define json_to_object(json_)  container_of(json_, json_object_t, json)
55 #define json_to_array(json_)   container_of(json_, json_array_t, json)
56 #define json_to_string(json_)  container_of(json_, json_string_t, json)
57 #define json_to_real(json_)   container_of(json_, json_real_t, json)
58 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
59
60 size_t jsonp_hash_key(const void *ptr);
61 int jsonp_key_equal(const void *ptr1, const void *ptr2);
62
63 typedef struct {
64     size_t serial;
65     char key[1];
66 } object_key_t;
67
68 const object_key_t *jsonp_object_iter_fullkey(void *iter);
69
70 void jsonp_error_init(json_error_t *error, const char *source);
71 void jsonp_error_set(json_error_t *error, int line, int column,
72                      size_t position, const char *msg, ...);
73 void jsonp_error_vset(json_error_t *error, int line, int column,
74                       size_t position, const char *msg, va_list ap);
75
76 /* Wrappers for custom memory functions */
77 void* jsonp_malloc(size_t size);
78 void jsonp_free(void *ptr);
79 char *jsonp_strdup(const char *str);
80
81 #endif