Changes between Version 2 and Version 3 of LibYAML


Ignore:
Timestamp:
05/16/06 14:01:18 (9 years ago)
Author:
xi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LibYAML

    v2 v3  
    103103   * `end_mark` - the position of the event end; `offset` (in bytes), `index` (in characters), 
    104104     `line` and `column` (starting from `0`). 
     105 
     106== API == 
     107 
     108Note: the API may change drastically. You may also check the header file: 
     109http://pyyaml.org/browser/libyaml/trunk/include/yaml.h. 
     110 
     111=== Parser API Synopsis === 
     112 
     113{{{ 
     114#!c 
     115#include <yaml.h> 
     116 
     117yaml_parser_t *parser; 
     118yaml_event_t *event; 
     119 
     120/* Create the Parser object. */ 
     121parser = yaml_parser_new(); 
     122 
     123/* Set a string input. */ 
     124char *input = "..."; 
     125size_t length = strlen(input); 
     126 
     127yaml_parser_set_string_input(parser, input, length); 
     128 
     129/* Set a file input. */ 
     130FILE *input = fopen("...", "rb"); 
     131 
     132yaml_parser_set_file_input(parser, input); 
     133 
     134/* Set a generic reader. */ 
     135void *ext = ...; 
     136int read_handler(void *ext, char *buffer, int size, int *length) { 
     137    /* ... */ 
     138    *buffer = ...; 
     139    *length = ...; 
     140    /* ... */ 
     141    return error ? 0 : 1; 
     142} 
     143 
     144yaml_parser_set_input(parser, read_handler, ext); 
     145 
     146/* Read the event sequence. */ 
     147do { 
     148    /* Get the next event. */ 
     149    event = yaml_parser_get_event(parser); 
     150 
     151    if (!event) goto error; 
     152 
     153    /* 
     154      ... 
     155      Process the event. 
     156      ... 
     157    */ 
     158 
     159    /* The application is responsible for destroing the event object. */ 
     160    yaml_event_delete(event); 
     161 
     162} while(event->type != YAML_STREAM_END_EVENT); 
     163 
     164/* Destroy the Parser object. */ 
     165yaml_parser_delete(parser); 
     166 
     167/* On error. */ 
     168error: 
     169 
     170/* Error type: YAML_READER_ERROR, YAML_SCANNER_ERROR, YAML_PARSER_ERROR. */ 
     171yaml_error_t error; 
     172 
     173/* What parser was doing when the problem occured (may be NULL). */ 
     174char *context; 
     175 
     176/* The starting position of the context (valid only if context != NULL). */ 
     177yaml_mark_t context_mark; 
     178 
     179/* The problem description. */ 
     180char *problem; 
     181 
     182/* The exact position of the problematic place in the stream. */ 
     183yaml_mark_t problem_mark; 
     184 
     185/* Get the error data. */ 
     186yaml_parser_get_error(&error, &context, &context_mark, &problem, &problem_mark); 
     187 
     188/* 
     189  ... 
     190  Report the problem to the user. 
     191  ... 
     192*/ 
     193 
     194/* Destroy the Parser object. */ 
     195yaml_parser_delete(parser); 
     196}}}