PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tnetstrings.h

http://github.com/zedshaw/mongrel2
C Header | 102 lines | 54 code | 26 blank | 22 comment | 1 complexity | 8939f02ed844312e6341db7fad2fc00d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense
  1. #ifndef _tnetstrings_h
  2. #define _tnetstrings_h
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <ctype.h>
  6. #include "bstring.h"
  7. #include "adt/hash.h"
  8. #include "adt/darray.h"
  9. typedef struct tns_outbuf_s {
  10. char *buffer;
  11. size_t used_size;
  12. size_t alloc_size;
  13. } tns_outbuf;
  14. typedef enum tns_type_tag_e {
  15. tns_tag_string = ',',
  16. tns_tag_number = '#',
  17. tns_tag_float = '^',
  18. tns_tag_bool = '!',
  19. tns_tag_null = '~',
  20. tns_tag_dict = '}',
  21. tns_tag_list = ']',
  22. tns_tag_invalid = 'Z',
  23. } tns_type_tag;
  24. typedef struct tns_value_t {
  25. tns_type_tag type;
  26. union {
  27. bstring string;
  28. long number;
  29. double fpoint;
  30. int bool;
  31. darray_t *list;
  32. hash_t *dict;
  33. } value;
  34. } tns_value_t;
  35. #define MAX_HASH_COUNT HASHCOUNT_T_MAX
  36. void tns_hnode_free(hnode_t *node, void *notused);
  37. hnode_t *tns_hnode_alloc(void *notused);
  38. void tns_value_destroy(tns_value_t *value);
  39. /**
  40. * Parse an object off the front of a tnetstring.
  41. * Returns a pointer to the parsed object, or NULL if an error occurs.
  42. * The third argument is an output parameter; if non-NULL it will
  43. * receive the unparsed remainder of the string.
  44. */
  45. void *tns_parse(const char *data, size_t len, char** remain);
  46. /**
  47. * Render an object into a string.
  48. * On success this function returns a malloced string containing
  49. * the serialization of the given object. If the second argument
  50. * 'len' is non-NULL it will receive the number of bytes in the string.
  51. * The caller is responsible for freeing the returned string.
  52. * On failure this function returns NULL.
  53. */
  54. char *tns_render(void *val, size_t *len);
  55. /**
  56. * Render an object into a string, in reverse.
  57. * This is just like tns_render but the output string contains the
  58. * rendered data in reverse. This is actually how the internal routines
  59. * produce it since it involves less copying of data. If you need to
  60. * copy the string off somewhere anyway, call tns_render_reversed and
  61. * save yourself the cost of reversing it in-place.
  62. */
  63. char *tns_render_reversed(void *val, size_t *len);
  64. void tns_render_hash_pair(tns_outbuf *outbuf, bstring key, bstring value);
  65. int tns_render_request_headers(tns_outbuf *outbuf, hash_t *headers);
  66. bstring tns_outbuf_to_bstring(tns_outbuf *outbuf);
  67. void tns_outbuf_clamp(tns_outbuf *outbuf, int orig_size);
  68. int tns_render_request_start(tns_outbuf *outbuf);
  69. int tns_render_request_end(tns_outbuf *outbuf, int header_start, bstring uuid, int id, bstring path);
  70. int tns_render_log_start(tns_outbuf *outbuf);
  71. void tns_render_log_end(tns_outbuf *outbuf);
  72. void tns_render_string_prepend(tns_outbuf *outbuf, bstring value);
  73. void tns_render_number_prepend(tns_outbuf *outbuf, long value);
  74. tns_value_t *tns_standard_table(bstring header_data, tns_value_t *rows);
  75. #define tns_get_type(T) (((tns_value_t *)(T)) == NULL ? tns_tag_invalid : ((tns_value_t *)(T))->type)
  76. #endif