Ignore:
Timestamp:
05/29/06 16:08:09 (8 years ago)
Author:
xi
Message:

Working on the decoding code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libyaml/trunk/src/api.c

    r179 r180  
    4747    yaml_parser_t *parser; 
    4848 
     49    /* Allocate the parser structure. */ 
     50 
    4951    parser = yaml_malloc(sizeof(yaml_parser_t)); 
    5052    if (!parser) return NULL; 
    5153 
    5254    memset(parser, 0, sizeof(yaml_parser_t)); 
     55 
     56    /* Allocate the raw buffer. */ 
     57 
     58    parser->raw_buffer = yaml_malloc(YAML_RAW_BUFFER_SIZE); 
     59    if (!parser->raw_buffer) { 
     60        yaml_free(parser); 
     61        return NULL; 
     62    } 
     63    parser->raw_pointer = parser->raw_buffer; 
     64    parser->raw_unread = 0; 
     65 
     66    /* Allocate the character buffer. */ 
     67 
     68    parser->buffer = yaml_malloc(YAML_BUFFER_SIZE); 
     69    if (!parser->buffer) { 
     70        yaml_free(parser->raw_buffer); 
     71        yaml_free(parser); 
     72        return NULL; 
     73    } 
     74    parser->buffer_end = parser->buffer; 
     75    parser->pointer = parser->buffer; 
     76    parser->unread = 0; 
    5377 
    5478    return parser; 
     
    6589 
    6690    yaml_free(parser->buffer); 
    67     if (!parser->raw_buffer_foreign) 
    68         yaml_free(parser->raw_buffer); 
     91    yaml_free(parser->raw_buffer); 
    6992 
    7093    memset(parser, 0, sizeof(yaml_parser_t)); 
     
    7497 
    7598/* 
    76  * String read handler (always returns error). 
     99 * String read handler. 
    77100 */ 
    78101 
     
    81104        size_t *size_read) 
    82105{ 
    83     *size_read = 0; 
     106    yaml_string_input_t *input = data; 
     107 
     108    if (input->current == input->end) { 
     109        *size_read = 0; 
     110        return 1; 
     111    } 
     112 
     113    if (size > (input->end - input->current)) { 
     114        size = input->end - input->current; 
     115    } 
     116 
     117    memcpy(buffer, input->current, size); 
     118    input->current += size; 
     119    *size_read = size; 
    84120    return 1; 
    85121} 
     
    93129        size_t *size_read) 
    94130{ 
    95     *size_read = fread(buffer, 1, size, (FILE *)ext); 
    96     return !ferror((FILE *)ext); 
     131    *size_read = fread(buffer, 1, size, (FILE *)data); 
     132    return !ferror((FILE *)data); 
    97133} 
    98134 
     
    106142{ 
    107143    assert(parser); /* Non-NULL parser object expected. */ 
    108     assert(!parser->reader); /* You can set the source only once. */ 
     144    assert(!parser->read_handler); /* You can set the source only once. */ 
    109145    assert(input);  /* Non-NULL input string expected. */ 
    110146 
     147    parser->string_input.start = input; 
     148    parser->string_input.current = input; 
     149    parser->string_input.end = input+size; 
     150 
    111151    parser->read_handler = yaml_string_read_handler; 
    112     parser->read_handler_data = NULL; 
    113  
    114     /* We use the input string as a raw (undecoded) buffer. */ 
    115     parser->raw_buffer = input;  
    116     parser->raw_buffer_size = size; 
    117     parser->raw_buffer_foreign = 1; 
     152    parser->read_handler_data = &parser->string_input; 
    118153} 
    119154 
     
    126161{ 
    127162    assert(parser); /* Non-NULL parser object expected. */ 
    128     assert(!parser->reader); /* You can set the source only once. */ 
     163    assert(!parser->read_handler); /* You can set the source only once. */ 
    129164    assert(file);   /* Non-NULL file object expected. */ 
    130165 
     
    142177{ 
    143178    assert(parser); /* Non-NULL parser object expected. */ 
    144     assert(!parser->reader); /* You can set the source only once. */ 
     179    assert(!parser->read_handler); /* You can set the source only once. */ 
    145180    assert(handler);    /* Non-NULL read handler expected. */ 
    146181 
    147182    parser->read_handler = handler; 
    148     parser->read_handler_data = data 
     183    parser->read_handler_data = data; 
    149184} 
    150185 
Note: See TracChangeset for help on using the changeset viewer.