[core] consolidate duplicated read-to-close code
[lighttpd.git] / src / data_string.c
blobc1573a9674d1e5107776fcc4c8998e533ec9f6dd
1 #include "first.h"
3 #include "array.h"
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
10 static data_unset *data_string_copy(const data_unset *s) {
11 data_string *src = (data_string *)s;
12 data_string *ds = data_string_init();
14 buffer_copy_buffer(ds->key, src->key);
15 buffer_copy_buffer(ds->value, src->value);
16 ds->is_index_key = src->is_index_key;
17 return (data_unset *)ds;
20 static void data_string_free(data_unset *d) {
21 data_string *ds = (data_string *)d;
23 buffer_free(ds->key);
24 buffer_free(ds->value);
26 free(d);
29 static void data_string_reset(data_unset *d) {
30 data_string *ds = (data_string *)d;
32 /* reused array elements */
33 buffer_reset(ds->key);
34 buffer_reset(ds->value);
37 static int data_string_insert_dup(data_unset *dst, data_unset *src) {
38 data_string *ds_dst = (data_string *)dst;
39 data_string *ds_src = (data_string *)src;
41 if (!buffer_is_empty(ds_dst->value)) {
42 buffer_append_string_len(ds_dst->value, CONST_STR_LEN(", "));
43 buffer_append_string_buffer(ds_dst->value, ds_src->value);
44 } else {
45 buffer_copy_buffer(ds_dst->value, ds_src->value);
48 src->free(src);
50 return 0;
53 static int data_response_insert_dup(data_unset *dst, data_unset *src) {
54 data_string *ds_dst = (data_string *)dst;
55 data_string *ds_src = (data_string *)src;
57 if (!buffer_is_empty(ds_dst->value)) {
58 buffer_append_string_len(ds_dst->value, CONST_STR_LEN("\r\n"));
59 buffer_append_string_buffer(ds_dst->value, ds_dst->key);
60 buffer_append_string_len(ds_dst->value, CONST_STR_LEN(": "));
61 buffer_append_string_buffer(ds_dst->value, ds_src->value);
62 } else {
63 buffer_copy_buffer(ds_dst->value, ds_src->value);
66 src->free(src);
68 return 0;
72 static void data_string_print(const data_unset *d, int depth) {
73 data_string *ds = (data_string *)d;
74 size_t i, len;
75 UNUSED(depth);
77 /* empty and uninitialized strings */
78 if (buffer_string_is_empty(ds->value)) {
79 fputs("\"\"", stdout);
80 return;
83 /* print out the string as is, except prepend " with backslash */
84 putc('"', stdout);
85 len = buffer_string_length(ds->value);
86 for (i = 0; i < len; i++) {
87 unsigned char c = ds->value->ptr[i];
88 if (c == '"') {
89 fputs("\\\"", stdout);
90 } else {
91 putc(c, stdout);
94 putc('"', stdout);
98 data_string *data_string_init(void) {
99 data_string *ds;
101 ds = calloc(1, sizeof(*ds));
102 force_assert(NULL != ds);
104 ds->key = buffer_init();
105 ds->value = buffer_init();
107 ds->copy = data_string_copy;
108 ds->free = data_string_free;
109 ds->reset = data_string_reset;
110 ds->insert_dup = data_string_insert_dup;
111 ds->print = data_string_print;
112 ds->type = TYPE_STRING;
114 return ds;
117 data_string *data_response_init(void) {
118 data_string *ds;
120 ds = data_string_init();
121 ds->insert_dup = data_response_insert_dup;
123 return ds;