summaryrefslogtreecommitdiff
path: root/src/data_string.c
blob: 1b9da692006b12e88d2ba5cdf1fb136a3390dc71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "array.h"

static data_unset *data_string_copy(const data_unset *s) {
	data_string *src = (data_string *)s;
	data_string *ds = data_string_init();

	buffer_copy_string_buffer(ds->key, src->key);
	buffer_copy_string_buffer(ds->value, src->value);
	ds->is_index_key = src->is_index_key;
	return (data_unset *)ds;
}

static void data_string_free(data_unset *d) {
	data_string *ds = (data_string *)d;

	buffer_free(ds->key);
	buffer_free(ds->value);

	free(d);
}

static void data_string_reset(data_unset *d) {
	data_string *ds = (data_string *)d;

	/* reused array elements */
	buffer_reset(ds->key);
	buffer_reset(ds->value);
}

static int data_string_insert_dup(data_unset *dst, data_unset *src) {
	data_string *ds_dst = (data_string *)dst;
	data_string *ds_src = (data_string *)src;

	if (ds_dst->value->used) {
		buffer_append_string_len(ds_dst->value, CONST_STR_LEN(", "));
		buffer_append_string_buffer(ds_dst->value, ds_src->value);
	} else {
		buffer_copy_string_buffer(ds_dst->value, ds_src->value);
	}

	src->free(src);

	return 0;
}

static int data_response_insert_dup(data_unset *dst, data_unset *src) {
	data_string *ds_dst = (data_string *)dst;
	data_string *ds_src = (data_string *)src;

	if (ds_dst->value->used) {
		buffer_append_string_len(ds_dst->value, CONST_STR_LEN("\r\n"));
		buffer_append_string_buffer(ds_dst->value, ds_dst->key);
		buffer_append_string_len(ds_dst->value, CONST_STR_LEN(": "));
		buffer_append_string_buffer(ds_dst->value, ds_src->value);
	} else {
		buffer_copy_string_buffer(ds_dst->value, ds_src->value);
	}

	src->free(src);

	return 0;
}


static void data_string_print(const data_unset *d, int depth) {
	data_string *ds = (data_string *)d;
	unsigned int i = 0;
	UNUSED(depth);

	// empty and uninitialized strings
	if (ds->value->used < 1) {
		fputs("\"\"", stdout);
		return;
	}

	// print out the string as is, except prepend " with backslash
	putc('"', stdout);
	for (i = 0; i < ds->value->used - 1; i++) {
		unsigned char c = ds->value->ptr[i];
		if (c == '"') {
			fputs("\\\"", stdout);
		} else {
			putc(c, stdout);
		}
	}
	putc('"', stdout);
}


data_string *data_string_init(void) {
	data_string *ds;

	ds = calloc(1, sizeof(*ds));
	assert(ds);

	ds->key = buffer_init();
	ds->value = buffer_init();

	ds->copy = data_string_copy;
	ds->free = data_string_free;
	ds->reset = data_string_reset;
	ds->insert_dup = data_string_insert_dup;
	ds->print = data_string_print;
	ds->type = TYPE_STRING;

	return ds;
}

data_string *data_response_init(void) {
	data_string *ds;

	ds = data_string_init();
	ds->insert_dup = data_response_insert_dup;

	return ds;
}