Changes between Initial Version and Version 1 of LibSyck


Ignore:
Timestamp:
03/11/06 18:28:05 (8 years ago)
Author:
xi
Comment:

Move  http://trac.xitology.org/pysyck/wiki/LibSyck

Legend:

Unmodified
Added
Removed
Modified
  • LibSyck

    v1 v1  
     1= Syck C API  = 
     2 
     3Stolen from ''syck/README.ext'', ''syck/lib/syck.h'', ''syck/lib/syck.c'', and ''syck/lib/emitter.c''. 
     4 
     5== SyckParser == 
     6 
     7{{{ 
     8#!c 
     9#include <syck.h> 
     10 
     11SyckParser *p = syck_new_parser(); /* Creates a new Syck parser. */ 
     12 
     13void *extension; 
     14p.bonus = extension; /* Sets the pointer for extension's use. */ 
     15 
     16syck_parser_implicit_typing(p, 1); /* Toggles implicit typing of builtin YAML types. Default is on. */ 
     17syck_parser_taguri_expansion(p, 1); /* Toggles expansion of types in full taguri. Default is on (recommended). */ 
     18 
     19SYMID node_handler(SyckParser *p, SyckNode *n); 
     20syck_parser_handler(p, node_handler); /*  Assign a callback function as a node handler. */ 
     21 
     22void error_handler(SyckParser *p, char *str); 
     23syck_parser_error_handler(p, error_handler); /* Assign a callback function as an error handler. */ 
     24 
     25SyckNode *bad_anchor_handler(SyckParser *p, char *anchor); 
     26syck_parser_bad_anchor_handler(p, bad_anchor_handler); /* Assign a callback function as a bad anchor handler. */ 
     27 
     28syck_parser_str(p, ptr, len, NULL); /* Assigns a string as the IO source with the default handler. */ 
     29/* or */ 
     30long io_file_read(char *buf, SyckIoFile *str, long max_size, long skip); 
     31syck_parser_file(p, file, io_file_read); /* Assigns a file as the IO source with a callback function. */ 
     32 
     33SYMID document = syck_parse(p); /* Parses a single document from the YAML stream. */ 
     34/* or */ 
     35while ((document = syck_parse(p))); /* Parses all documents from the YAML stream. */ 
     36 
     37syck_free_parser(p); /* Frees the parser. */ 
     38}}} 
     39 
     40== SyckIoFileRead == 
     41 
     42{{{ 
     43#!c 
     44/* 
     45 * Default FILE IO function 
     46 */ 
     47long 
     48syck_io_file_read( char *buf, SyckIoFile *file, long max_size, long skip ) 
     49{ 
     50    long len = 0; 
     51 
     52    ASSERT( file != NULL ); 
     53 
     54    max_size -= skip; 
     55    len = fread( buf + skip, sizeof( char ), max_size, file->ptr ); 
     56    len += skip; 
     57    buf[len] = '\0'; 
     58 
     59    return len; 
     60} 
     61}}} 
     62 
     63== SyckNodeHandler == 
     64 
     65{{{ 
     66#!c 
     67SyckNode *n; 
     68/* ... */ 
     69 
     70char *type_id = n.type_id; 
     71char *anchor = n.anchor; 
     72 
     73switch (n->kind) { 
     74 
     75    case syck_str_kind: /* Loads a scalar node. */ 
     76        char *ptr = n->data.str->ptr; 
     77        long len = n->data.str->ptr; 
     78        /* ... */ 
     79        break; 
     80 
     81    case syck_seq_kind: /* Loads a sequence node. */ 
     82        for (long i = 0; i < syck_seq_count(n); i++) { 
     83            SYMID val = sym_seq_read(n, i); 
     84            /* ... */ 
     85        } 
     86        break; 
     87 
     88    case syck_map_kind: /* Loads a mapping node. */ 
     89        for (long i = 0; i < syck_map_count(n); i++) { 
     90            SYMID key = sym_map_read(n, map_key, i); 
     91            SYMID val = sym_map_read(n, map_value, i); 
     92            /* ... */ 
     93        } 
     94        break; 
     95} 
     96}}} 
     97 
     98== SyckEmitter == 
     99 
     100{{{ 
     101#!c 
     102#include <syck.h> 
     103 
     104SyckEmitter *e = syck_new_emitter(); /* Creates a new Syck emitter. */ 
     105 
     106void *extension; 
     107e.bonus = extension; /* Sets the pointer for extension's use. */ 
     108 
     109e->headless = 0; /* Headerless doc flag. */ 
     110e->use_header = 0; /* Force header? */ 
     111e->use_version = 0; /* Force version? */ 
     112e->sort_keys = 0; /* Sort hash keys -- not used. */ 
     113e->anchor_format = "id%03d"; /* Anchor format -- dangerous. */ 
     114e->explicit_typing = 0; /* Explicit typing on all collections? */ 
     115e->best_width = 80; /* Best width on folded scalars. */ 
     116e->style = scalar_none; /* Use literal or folded blocks on all text? -- scalar_fold or any other value. */ 
     117e->indent = 2; /* Default indentation. */ 
     118 
     119void output_handler(SyckEmitter *e, char *ptr, long len); 
     120syck_output_handler(e, output_handler); /* Assign a callback as the output handler */ 
     121 
     122void emitter_handler(SyckEmitter *e, st_data_t node_id); 
     123syck_emitter_handler(e, emitter_handler); /* Assign a callback as the emitter handler */ 
     124 
     125syck_emitter_ignore_id(e, sym_id); /* Not used. */ 
     126 
     127st_data_t node_id = 0; 
     128syck_emitter_mark_node(e, node_id); /* Add a node to the symbol table. Allocate an anchor if it has repeated. */ 
     129/* ... */ 
     130 
     131syck_emit(e, root_node_id); /* Start emitting from the given node. */ 
     132syck_emitter_flush(e, 0); /* Flush the internal buffer. */ 
     133 
     134/* ... */ 
     135 
     136syck_free_emitter(e); /* Frees the emitter. */ 
     137}}} 
     138 
     139== SyckEmitterHandler == 
     140 
     141{{{ 
     142#!c 
     143 
     144void 
     145emitter_handler(SyckEmitter *e, st_data_t node_id) 
     146{ 
     147 
     148    /* ... */ 
     149    syck_emit_scalar(e, tag, style, indent, width, chomp, str, len); /* Emit scalar. */ 
     150 
     151    /* ... */ 
     152    syck_emit_seq(e, tag, style); /* Emit sequence. */ 
     153    for (/* ... */) { 
     154        syck_emit_item(e, item_id); 
     155    } 
     156    syck_emit_end(e); 
     157 
     158    /* ... */ 
     159    syck_emit_map(e, tag, style); /* Emit mapping. */ 
     160    for (/* ... */) { 
     161        syck_emit_item(e, key_id); 
     162        syck_emit_item(e, value_id); 
     163    } 
     164    syck_emit_end(e); 
     165} 
     166}}}