Add spec file
[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 "jansson.h"
13 #include "hashtable.h"
14
15 #define container_of(ptr_, type_, member_)  \
16     ((type_ *)((char *)ptr_ - offsetof(type_, member_)))
17
18 /* On some platforms, max() may already be defined */
19 #ifndef max
20 #define max(a, b)  ((a) > (b) ? (a) : (b))
21 #endif
22
23 /* va_copy is a C99 feature. In C89 implementations, it's sometimes
24    available as __va_copy. If not, memcpy() should do the trick. */
25 #ifndef va_copy
26 #ifdef __va_copy
27 #define va_copy __va_copy
28 #else
29 #define va_copy(a, b)  memcpy(&(a), &(b), sizeof(va_list))
30 #endif
31 #endif
32
33 typedef struct {
34     json_t json;
35     hashtable_t hashtable;
36     size_t serial;
37     int visited;
38 } json_object_t;
39
40 typedef struct {
41     json_t json;
42     size_t size;
43     size_t entries;
44     json_t **table;
45     int visited;
46 } json_array_t;
47
48 typedef struct {
49     json_t json;
50     char *value;
51 } json_string_t;
52
53 typedef struct {
54     json_t json;
55     double value;
56 } json_real_t;
57
58 typedef struct {
59     json_t json;
60     json_int_t value;
61 } json_integer_t;
62
63 #define json_to_object(json_)  container_of(json_, json_object_t, json)
64 #define json_to_array(json_)   container_of(json_, json_array_t, json)
65 #define json_to_string(json_)  container_of(json_, json_string_t, json)
66 #define json_to_real(json_)   container_of(json_, json_real_t, json)
67 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
68
69 size_t jsonp_hash_key(const void *ptr);
70 int jsonp_key_equal(const void *ptr1, const void *ptr2);
71
72 typedef struct {
73     size_t serial;
74     char key[1];
75 } object_key_t;
76
77 const object_key_t *jsonp_object_iter_fullkey(void *iter);
78
79 void jsonp_error_init(json_error_t *error, const char *source);
80 void jsonp_error_set_source(json_error_t *error, const char *source);
81 void jsonp_error_set(json_error_t *error, int line, int column,
82                      size_t position, const char *msg, ...);
83 void jsonp_error_vset(json_error_t *error, int line, int column,
84                       size_t position, const char *msg, va_list ap);
85
86 /* Wrappers for custom memory functions */
87 void* jsonp_malloc(size_t size);
88 void jsonp_free(void *ptr);
89 char *jsonp_strdup(const char *str);
90
91 #endif