jansson_private.h 2.4 KB

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