jansson_private.h 2.3 KB

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