robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8275b7b491
master
1#ifndef MPACK_OBJECT_H 2#define MPACK_OBJECT_H 3 4#include "conv.h" 5#include "mpack_core.h" 6 7#ifndef MPACK_MAX_OBJECT_DEPTH 8# define MPACK_MAX_OBJECT_DEPTH 32 9#endif 10 11#define MPACK_PARENT_NODE (n ) (((n) - 1)->pos == (size_t)-1 ? NULL : (n) - 1) 12 13#define MPACK_THROW (parser ) \ 14 do { \ 15 parser->status = MPACK_EXCEPTION; \ 16 return; \ 17 } while (0) 18 19enum { 20MPACK_EXCEPTION = -1 , 21MPACK_NOMEM = MPACK_ERROR + 1 22}; 23 24/* Storing integer in pointers in undefined behavior according to the C 25* standard. Define a union type to accommodate arbitrary user data associated 26* with nodes(and with requests in rpc.h). */ 27typedef union { 28void * p ; 29mpack_uintmax_t u ; 30mpack_sintmax_t i ; 31double d ; 32}mpack_data_t ; 33 34typedef struct mpack_node_s { 35mpack_token_t tok ; 36size_t pos ; 37/* flag to determine if the key was visited when traversing a map */ 38int key_visited ; 39/* allow 2 instances mpack_data_t per node. the reason is that when 40* serializing, the user may need to keep track of traversal state besides the 41* parent node reference */ 42mpack_data_t data [2 ]; 43}mpack_node_t ; 44 45#define MPACK_PARSER_STRUCT (c ) \ 46 struct { \ 47 mpack_data_t data; \ 48 mpack_uint32_t size, capacity; \ 49 int status; \ 50 int exiting; \ 51 mpack_tokbuf_t tokbuf; \ 52 mpack_node_t items[c + 1]; \ 53 } 54 55/* Some compilers warn against anonymous structs: 56* https://github.com/libmpack/libmpack/issues/6 */ 57typedef MPACK_PARSER_STRUCT (0 )mpack_one_parser_t ; 58 59#define MPACK_PARSER_STRUCT_SIZE (c ) \ 60 (sizeof (mpack_node_t )* c + \ 61sizeof (mpack_one_parser_t )) 62 63typedef MPACK_PARSER_STRUCT (MPACK_MAX_OBJECT_DEPTH )mpack_parser_t ; 64typedef void (* mpack_walk_cb )(mpack_parser_t * w ,mpack_node_t * n ); 65 66MPACK_API void mpack_parser_init (mpack_parser_t * p ,mpack_uint32_t c ) 67FUNUSED FNONULL ; 68 69MPACK_API int mpack_parse_tok (mpack_parser_t * walker ,mpack_token_t tok , 70mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 71FUNUSED FNONULL_ARG ((1 ,3 ,4 )); 72MPACK_API int mpack_unparse_tok (mpack_parser_t * walker ,mpack_token_t * tok , 73mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 74FUNUSED FNONULL_ARG ((1 ,2 ,3 ,4 )); 75 76MPACK_API int mpack_parse (mpack_parser_t * parser ,const char ** b ,size_t * bl , 77mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 78FUNUSED FNONULL_ARG ((1 ,2 ,3 ,4 ,5 )); 79MPACK_API int mpack_unparse (mpack_parser_t * parser ,char ** b ,size_t * bl , 80mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 81FUNUSED FNONULL_ARG ((1 ,2 ,3 ,4 ,5 )); 82 83MPACK_API void mpack_parser_copy (mpack_parser_t * d ,mpack_parser_t * s ) 84FUNUSED FNONULL ; 85 86#endif /* MPACK_OBJECT_H */