Ignore:
Timestamp:
12/27/07 07:05:17 (7 years ago)
Author:
xi
Message:

Completed the first phase of API refactoring.

File:
1 edited

Legend:

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

    r239 r263  
    66 */ 
    77 
    8 static int 
    9 yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem); 
    10  
    118YAML_DECLARE(int) 
    129yaml_emitter_flush(yaml_emitter_t *emitter); 
    13  
    14 /* 
    15  * Set the writer error and return 0. 
    16  */ 
    17  
    18 static int 
    19 yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem) 
    20 { 
    21     emitter->error = YAML_WRITER_ERROR; 
    22     emitter->problem = problem; 
    23  
    24     return 0; 
    25 } 
    2610 
    2711/* 
     
    3519 
    3620    assert(emitter);    /* Non-NULL emitter object is expected. */ 
    37     assert(emitter->write_handler); /* Write handler must be set. */ 
     21    assert(emitter->writer);    /* Write handler must be set. */ 
    3822    assert(emitter->encoding);  /* Output encoding must be set. */ 
    39  
    40     emitter->buffer.last = emitter->buffer.pointer; 
    41     emitter->buffer.pointer = emitter->buffer.start; 
    4223 
    4324    /* Check if the buffer is empty. */ 
    4425 
    45     if (emitter->buffer.start == emitter->buffer.last) { 
     26    if (!emitter->output.pointer) { 
    4627        return 1; 
    4728    } 
     29 
     30    /* Switch the pointer to the beginning of the buffer. */ 
     31 
     32    emitter->output.capacity = emitter->output.pointer; 
     33    emitter->output.pointer = 0; 
    4834 
    4935    /* If the output encoding is UTF-8, we don't need to recode the buffer. */ 
     
    5137    if (emitter->encoding == YAML_UTF8_ENCODING) 
    5238    { 
    53         if (emitter->write_handler(emitter->write_handler_data, 
    54                     emitter->buffer.start, 
    55                     emitter->buffer.last - emitter->buffer.start)) { 
    56             emitter->buffer.last = emitter->buffer.start; 
    57             emitter->buffer.pointer = emitter->buffer.start; 
     39        if (emitter->writer(emitter->writer_data, 
     40                    emitter->output.buffer, emitter->output.capacity)) { 
     41            emitter->offset += emitter->output.capacity; 
     42            emitter->output.capacity = OUTPUT_BUFFER_CAPACITY; 
    5843            return 1; 
    5944        } 
    6045        else { 
    61             return yaml_emitter_set_writer_error(emitter, "Write error"); 
     46            return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset); 
    6247        } 
    6348    } 
     
    6853    high = (emitter->encoding == YAML_UTF16LE_ENCODING ? 1 : 0); 
    6954 
    70     while (emitter->buffer.pointer != emitter->buffer.last) 
     55    while (emitter->output.pointer != emitter->output.capacity) 
    7156    { 
    7257        unsigned char octet; 
    7358        unsigned int width; 
    7459        unsigned int value; 
    75         size_t k; 
     60        size_t idx; 
    7661 
    7762        /*  
     
    8267        /* Read the next UTF-8 character. */ 
    8368 
    84         octet = emitter->buffer.pointer[0]; 
     69        octet = OCTET(emitter->output); 
    8570 
    8671        width = (octet & 0x80) == 0x00 ? 1 : 
     
    9479                (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 
    9580 
    96         for (k = 1; k < width; k ++) { 
    97             octet = emitter->buffer.pointer[k]; 
     81        for (idx = 1; idx < width; idx ++) { 
     82            octet = OCTET_AT(emitter->output, idx); 
    9883            value = (value << 6) + (octet & 0x3F); 
    9984        } 
    10085 
    101         emitter->buffer.pointer += width; 
     86        emitter->output.pointer += width; 
    10287 
    10388        /* Write the character. */ 
     
    10590        if (value < 0x10000) 
    10691        { 
    107             emitter->raw_buffer.last[high] = value >> 8; 
    108             emitter->raw_buffer.last[low] = value & 0xFF; 
     92            OCTET_AT(emitter->raw_output, high) = value >> 8; 
     93            OCTET_AT(emitter->raw_output, low) = value & 0xFF; 
    10994 
    110             emitter->raw_buffer.last += 2; 
     95            emitter->raw_output.pointer += 2; 
    11196        } 
    11297        else 
     
    115100 
    116101            value -= 0x10000; 
    117             emitter->raw_buffer.last[high] = 0xD8 + (value >> 18); 
    118             emitter->raw_buffer.last[low] = (value >> 10) & 0xFF; 
    119             emitter->raw_buffer.last[high+2] = 0xDC + ((value >> 8) & 0xFF); 
    120             emitter->raw_buffer.last[low+2] = value & 0xFF; 
     102            OCTET_AT(emitter->raw_output, high) = 0xD8 + (value >> 18); 
     103            OCTET_AT(emitter->raw_output, low) = (value >> 10) & 0xFF; 
     104            OCTET_AT(emitter->raw_output, high+2) = 0xDC + ((value >> 8) & 0xFF); 
     105            OCTET_AT(emitter->raw_output, low+2) = value & 0xFF; 
    121106 
    122             emitter->raw_buffer.last += 4; 
     107            emitter->raw_output.pointer += 4; 
    123108        } 
    124109    } 
     
    126111    /* Write the raw buffer. */ 
    127112 
    128     if (emitter->write_handler(emitter->write_handler_data, 
    129                 emitter->raw_buffer.start, 
    130                 emitter->raw_buffer.last - emitter->raw_buffer.start)) { 
    131         emitter->buffer.last = emitter->buffer.start; 
    132         emitter->buffer.pointer = emitter->buffer.start; 
    133         emitter->raw_buffer.last = emitter->raw_buffer.start; 
    134         emitter->raw_buffer.pointer = emitter->raw_buffer.start; 
     113    if (emitter->writer(emitter->writer_data, 
     114                emitter->raw_output.buffer, emitter->raw_output.pointer)) { 
     115        emitter->output.pointer = 0; 
     116        emitter->output.capacity = OUTPUT_BUFFER_CAPACITY; 
     117        emitter->offset += emitter->raw_output.pointer; 
     118        emitter->raw_output.pointer = 0; 
    135119        return 1; 
    136120    } 
    137121    else { 
    138         return yaml_emitter_set_writer_error(emitter, "Write error"); 
     122        return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset); 
    139123    } 
    140124} 
Note: See TracChangeset for help on using the changeset viewer.