1: #include "first.h"
2:
3: #include "array.h"
4:
5: #include <string.h>
6: #include <stdio.h>
7: #include <stdlib.h>
8: #include <assert.h>
9:
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();
13:
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;
18: }
19:
20: static void data_string_free(data_unset *d) {
21: data_string *ds = (data_string *)d;
22:
23: buffer_free(ds->key);
24: buffer_free(ds->value);
25:
26: free(d);
27: }
28:
29: static void data_string_reset(data_unset *d) {
30: data_string *ds = (data_string *)d;
31:
32: /* reused array elements */
33: buffer_reset(ds->key);
34: buffer_reset(ds->value);
35: }
36:
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;
40:
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);
46: }
47:
48: src->free(src);
49:
50: return 0;
51: }
52:
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;
56:
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);
64: }
65:
66: src->free(src);
67:
68: return 0;
69: }
70:
71:
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);
76:
77: /* empty and uninitialized strings */
78: if (buffer_string_is_empty(ds->value)) {
79: fputs("\"\"", stdout);
80: return;
81: }
82:
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);
92: }
93: }
94: putc('"', stdout);
95: }
96:
97:
98: data_string *data_string_init(void) {
99: data_string *ds;
100:
101: ds = calloc(1, sizeof(*ds));
102: force_assert(NULL != ds);
103:
104: ds->key = buffer_init();
105: ds->value = buffer_init();
106:
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;
113:
114: return ds;
115: }
116:
117: data_string *data_response_init(void) {
118: data_string *ds;
119:
120: ds = data_string_init();
121: ds->insert_dup = data_response_insert_dup;
122:
123: return ds;
124: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>