summaryrefslogtreecommitdiff
path: root/src/writer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/writer.c')
-rw-r--r--src/writer.c82
1 files changed, 33 insertions, 49 deletions
diff --git a/src/writer.c b/src/writer.c
index 513d9ac..e03b0f6 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -5,26 +5,10 @@
* Declarations.
*/
-static int
-yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem);
-
YAML_DECLARE(int)
yaml_emitter_flush(yaml_emitter_t *emitter);
/*
- * Set the writer error and return 0.
- */
-
-static int
-yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem)
-{
- emitter->error = YAML_WRITER_ERROR;
- emitter->problem = problem;
-
- return 0;
-}
-
-/*
* Flush the output buffer.
*/
@@ -34,31 +18,32 @@ yaml_emitter_flush(yaml_emitter_t *emitter)
int low, high;
assert(emitter); /* Non-NULL emitter object is expected. */
- assert(emitter->write_handler); /* Write handler must be set. */
+ assert(emitter->writer); /* Write handler must be set. */
assert(emitter->encoding); /* Output encoding must be set. */
- emitter->buffer.last = emitter->buffer.pointer;
- emitter->buffer.pointer = emitter->buffer.start;
-
/* Check if the buffer is empty. */
- if (emitter->buffer.start == emitter->buffer.last) {
+ if (!emitter->output.pointer) {
return 1;
}
+ /* Switch the pointer to the beginning of the buffer. */
+
+ emitter->output.capacity = emitter->output.pointer;
+ emitter->output.pointer = 0;
+
/* If the output encoding is UTF-8, we don't need to recode the buffer. */
if (emitter->encoding == YAML_UTF8_ENCODING)
{
- if (emitter->write_handler(emitter->write_handler_data,
- emitter->buffer.start,
- emitter->buffer.last - emitter->buffer.start)) {
- emitter->buffer.last = emitter->buffer.start;
- emitter->buffer.pointer = emitter->buffer.start;
+ if (emitter->writer(emitter->writer_data,
+ emitter->output.buffer, emitter->output.capacity)) {
+ emitter->offset += emitter->output.capacity;
+ emitter->output.capacity = OUTPUT_BUFFER_CAPACITY;
return 1;
}
else {
- return yaml_emitter_set_writer_error(emitter, "Write error");
+ return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset);
}
}
@@ -67,12 +52,12 @@ yaml_emitter_flush(yaml_emitter_t *emitter)
low = (emitter->encoding == YAML_UTF16LE_ENCODING ? 0 : 1);
high = (emitter->encoding == YAML_UTF16LE_ENCODING ? 1 : 0);
- while (emitter->buffer.pointer != emitter->buffer.last)
+ while (emitter->output.pointer != emitter->output.capacity)
{
unsigned char octet;
unsigned int width;
unsigned int value;
- size_t k;
+ size_t idx;
/*
* See the "reader.c" code for more details on UTF-8 encoding. Note
@@ -81,7 +66,7 @@ yaml_emitter_flush(yaml_emitter_t *emitter)
/* Read the next UTF-8 character. */
- octet = emitter->buffer.pointer[0];
+ octet = OCTET(emitter->output);
width = (octet & 0x80) == 0x00 ? 1 :
(octet & 0xE0) == 0xC0 ? 2 :
@@ -93,49 +78,48 @@ yaml_emitter_flush(yaml_emitter_t *emitter)
(octet & 0xF0) == 0xE0 ? octet & 0x0F :
(octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
- for (k = 1; k < width; k ++) {
- octet = emitter->buffer.pointer[k];
+ for (idx = 1; idx < width; idx ++) {
+ octet = OCTET_AT(emitter->output, idx);
value = (value << 6) + (octet & 0x3F);
}
- emitter->buffer.pointer += width;
+ emitter->output.pointer += width;
/* Write the character. */
if (value < 0x10000)
{
- emitter->raw_buffer.last[high] = value >> 8;
- emitter->raw_buffer.last[low] = value & 0xFF;
+ OCTET_AT(emitter->raw_output, high) = value >> 8;
+ OCTET_AT(emitter->raw_output, low) = value & 0xFF;
- emitter->raw_buffer.last += 2;
+ emitter->raw_output.pointer += 2;
}
else
{
/* Write the character using a surrogate pair (check "reader.c"). */
value -= 0x10000;
- emitter->raw_buffer.last[high] = 0xD8 + (value >> 18);
- emitter->raw_buffer.last[low] = (value >> 10) & 0xFF;
- emitter->raw_buffer.last[high+2] = 0xDC + ((value >> 8) & 0xFF);
- emitter->raw_buffer.last[low+2] = value & 0xFF;
+ OCTET_AT(emitter->raw_output, high) = 0xD8 + (value >> 18);
+ OCTET_AT(emitter->raw_output, low) = (value >> 10) & 0xFF;
+ OCTET_AT(emitter->raw_output, high+2) = 0xDC + ((value >> 8) & 0xFF);
+ OCTET_AT(emitter->raw_output, low+2) = value & 0xFF;
- emitter->raw_buffer.last += 4;
+ emitter->raw_output.pointer += 4;
}
}
/* Write the raw buffer. */
- if (emitter->write_handler(emitter->write_handler_data,
- emitter->raw_buffer.start,
- emitter->raw_buffer.last - emitter->raw_buffer.start)) {
- emitter->buffer.last = emitter->buffer.start;
- emitter->buffer.pointer = emitter->buffer.start;
- emitter->raw_buffer.last = emitter->raw_buffer.start;
- emitter->raw_buffer.pointer = emitter->raw_buffer.start;
+ if (emitter->writer(emitter->writer_data,
+ emitter->raw_output.buffer, emitter->raw_output.pointer)) {
+ emitter->output.pointer = 0;
+ emitter->output.capacity = OUTPUT_BUFFER_CAPACITY;
+ emitter->offset += emitter->raw_output.pointer;
+ emitter->raw_output.pointer = 0;
return 1;
}
else {
- return yaml_emitter_set_writer_error(emitter, "Write error");
+ return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset);
}
}