From: Clark C . E. <cc...@cl...> - 2001-05-20 00:16:18
|
/************************************************************************* ** RCS-ID: $Id: yaml.h,v 1.3 2001/05/15 16:03:13 cce Exp $ ** Copyright: (c) 2001 Clark Evans (cc...@cl...) ** License: You may use under the Python, Perl, or LGPL license ** Credits: Brian Ingerson and Oren Ben-Kiki ** Version: This is a prototype API and is not yet ** ready for implementing ************************************************************************/ /************************************************************************* * CONTEXT AND CONSTANTS ************************************************************************/ struct yaml_context; typedef char yaml_char_t; /* typedef for unicode */ typedef size_t yaml_size_t /* buffer size in bytes */ typedef void * yaml_user_t; /* user defined ptr */ typedef const yaml_char_t * yaml_key_t; /* null terminated str */ typedef const yaml_char_t * yaml_name_t; /* null terminated str */ typedef struct yaml_context * yaml_context_t; /* iter/visit context */ typedef void * yaml_reference_t; /* opaque handle */ typedef enum { YAML_ERROR = -1, YAML_SUCCESS = 0, } yaml_result_t; typedef enum { YAML_NODE_SCALAR = 1, YAML_NODE_LIST = 2, YAML_NODE_MAP = 3, YAML_NODE_REF = 4 } yaml_type_t; struct yaml_context /* this structure holds the current iterator or visitor context, which is the path up to the root node. */ { yaml_context_t parent; /* the parent context, maintained for you */ yaml_type_t type; /* the type of node, this is Map, List, */ yaml_key_t key; /* if an entry in a map, this is the key */ yaml_reference_t reference; /* the reference for the value's content */ yaml_name_t name; /* the class name or data type */ yaml_user_t user; /* user defined object for this context */ }; /************************************************************************* * VISITOR INTERFACE ************************************************************************/ struct yaml_visitor; /* the visitor event handler and data pointer */ typedef struct yaml_visitor * yaml_visitor_t; typedef enum { YAML_VISIT_ERROR = YAML_ERROR, YAML_VISIT_CONTINUE = YAML_SUCCESS, /* continue visiting */ YAML_VISIT_STOP, /* stop visiting, free visitor */ YAML_VISIT_SKIP /* skip children */ } yaml_visit_t; typedef /* when entering a node (map, list, or scalar) */ yaml_visit_t (*yaml_visit_enter_t)( yaml_visitor_t visitor, yaml_context_t context); typedef /* when exiting node (map, list, or scalar) */ yaml_visit_t (*yaml_visit_exit_t)( yaml_visitor_t visitor, yaml_context_t context); typedef /* for scalar content, can be called more than once per node */ yaml_visit_t (*yaml_visit_chars_t)( yaml_visitor_t visitor, yaml_context_t context, const yaml_char_t* data, yaml_size_t size ); typedef /* reference is encountered, note this does not dereference */ yaml_visit_t(*yaml_visit_ref_t) (yaml_visitor_t visitor, yaml_context_t context); struct yaml_visitor { yaml_visit_ref_t ref; /* function to handle reference events */ yaml_visit_enter_t enter; /* function to handle begin node event */ yaml_visit_exit_t exit; /* function to handle end node event */ yaml_visit_chars_t chars; /* function to handle scalar content */ yaml_user_t user; /* user data for the consumer */ }; /************************************************************************* * ITERATOR INTERFACE ************************************************************************/ struct yaml_iterator; /* the iterator data producer and user data ptr */ typedef struct yaml_iterator * yaml_iterator_t; typedef enum { YAML_ITER_ERROR = YAML_ERROR, /* bad, some error occurred */ YAML_ITER_NONE = YAML_SUCCESS, /* good, but no node encountered */ YAML_ITER_SCALAR = YAML_TYPE_SCALAR, /* good, scalar node encountered */ YAML_ITER_LIST = YAML_TYPE_LIST, /* good, list node encountered */ YAML_ITER_MAP = YAML_TYPE_MAP, /* good, map encountered */ YAML_ITER_REF = YAML_TYPE_REF /* good, reference encountered */ } yaml_iter_t; typedef /* iterates through list of siblings in a list or map */ yaml_iter_t (*yaml_iter_next_t)( yaml_iterator_t iterator, yaml_context_t *context); typedef /* iterates into the first child of a list or map */ yaml_context_t *context); typedef /* allows the return of scalar data, returns amount read */ yaml_size_t (*yaml_iter_chars_t)( yaml_iterator_t iterator, yaml_char_t* data, yaml_size_t size); typedef /* stops the iteration and cleans up iterator */ yaml_iter_t (*yaml_iter_stop_t)( yaml_iterator_t iterator); struct yaml_iterator { yaml_iter_next_t next; /* function to return next yaml node */ yaml_iter_first_t first; /* function to return first yaml node */ yaml_iter_chars_t chars; /* function to return scalar content */ yaml_iter_stop_t stop; /* function to stop the iteration */ yaml_user_t user; /* user data for the producer */ }; /************************************************************************* * PARSER/EMITTER API ************************************************************************/ yaml_iterator_t yaml_parse( FILE * ); yaml_visitor_t yaml_emit( FILE * ); yaml_result_t yaml_exec(yaml_iterator_t,yaml_visitor_t); /* To stream a document though the system: * * yaml_exec( yaml_parse(fopen("file.in","r")), * yaml_emit(fopen("file.out","w"))); */ /************************************************************************* * PARSER/EMITTER API ************************************************************************/ typedef struct { yaml_iterator_t iterator, yaml_visitor_t visitor } * yaml_convert_t; yaml_convert_t yaml_convert(void); |