1 #include "array.h"
2 
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 
data_string_copy(const data_unset * s)8 static data_unset *data_string_copy(const data_unset *s) {
9 	data_string *src = (data_string *)s;
10 	data_string *ds = data_string_init();
11 
12 	buffer_copy_string_buffer(ds->key, src->key);
13 	buffer_copy_string_buffer(ds->value, src->value);
14 	ds->is_index_key = src->is_index_key;
15 	return (data_unset *)ds;
16 }
17 
data_string_free(data_unset * d)18 static void data_string_free(data_unset *d) {
19 	data_string *ds = (data_string *)d;
20 
21 	buffer_free(ds->key);
22 	buffer_free(ds->value);
23 
24 	free(d);
25 }
26 
data_string_reset(data_unset * d)27 static void data_string_reset(data_unset *d) {
28 	data_string *ds = (data_string *)d;
29 
30 	/* reused array elements */
31 	buffer_reset(ds->key);
32 	buffer_reset(ds->value);
33 }
34 
data_string_insert_dup(data_unset * dst,data_unset * src)35 static int data_string_insert_dup(data_unset *dst, data_unset *src) {
36 	data_string *ds_dst = (data_string *)dst;
37 	data_string *ds_src = (data_string *)src;
38 
39 	if (ds_dst->value->used) {
40 		buffer_append_string_len(ds_dst->value, CONST_STR_LEN(", "));
41 		buffer_append_string_buffer(ds_dst->value, ds_src->value);
42 	} else {
43 		buffer_copy_string_buffer(ds_dst->value, ds_src->value);
44 	}
45 
46 	src->free(src);
47 
48 	return 0;
49 }
50 
data_response_insert_dup(data_unset * dst,data_unset * src)51 static int data_response_insert_dup(data_unset *dst, data_unset *src) {
52 	data_string *ds_dst = (data_string *)dst;
53 	data_string *ds_src = (data_string *)src;
54 
55 	if (ds_dst->value->used) {
56 		buffer_append_string_len(ds_dst->value, CONST_STR_LEN("\r\n"));
57 		buffer_append_string_buffer(ds_dst->value, ds_dst->key);
58 		buffer_append_string_len(ds_dst->value, CONST_STR_LEN(": "));
59 		buffer_append_string_buffer(ds_dst->value, ds_src->value);
60 	} else {
61 		buffer_copy_string_buffer(ds_dst->value, ds_src->value);
62 	}
63 
64 	src->free(src);
65 
66 	return 0;
67 }
68 
69 
data_string_print(const data_unset * d,int depth)70 static void data_string_print(const data_unset *d, int depth) {
71 	data_string *ds = (data_string *)d;
72 	unsigned int i;
73 	UNUSED(depth);
74 
75 	/* empty and uninitialized strings */
76 	if (ds->value->used < 1) {
77 		fputs("\"\"", stdout);
78 		return;
79 	}
80 
81 	/* print out the string as is, except prepend " with backslash */
82 	putc('"', stdout);
83 	for (i = 0; i < ds->value->used - 1; i++) {
84 		unsigned char c = ds->value->ptr[i];
85 		if (c == '"') {
86 			fputs("\\\"", stdout);
87 		} else {
88 			putc(c, stdout);
89 		}
90 	}
91 	putc('"', stdout);
92 }
93 
94 
data_string_init(void)95 data_string *data_string_init(void) {
96 	data_string *ds;
97 
98 	ds = calloc(1, sizeof(*ds));
99 	assert(ds);
100 
101 	ds->key = buffer_init();
102 	ds->value = buffer_init();
103 
104 	ds->copy = data_string_copy;
105 	ds->free = data_string_free;
106 	ds->reset = data_string_reset;
107 	ds->insert_dup = data_string_insert_dup;
108 	ds->print = data_string_print;
109 	ds->type = TYPE_STRING;
110 
111 	return ds;
112 }
113 
data_response_init(void)114 data_string *data_response_init(void) {
115 	data_string *ds;
116 
117 	ds = data_string_init();
118 	ds->insert_dup = data_response_insert_dup;
119 
120 	return ds;
121 }
122