root/trunk/src/data_string.c

Revision 2263, 2.3 kB (checked in by stbuehler, 4 weeks ago)

Replace buffer_{append,copy}_string with the _len variant where possible (#1732, thx crypt)
Replace BUFFER_{APPEND,COPY}_STRING_CONST with _len(b, CONST_STRL_LEN(x))

  • Property svn:eol-style set to native
Line 
1#include <string.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <assert.h>
5
6#include "array.h"
7
8static 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
18static 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
27static 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
35static 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
51static 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
70static void data_string_print(const data_unset *d, int depth) {
71        data_string *ds = (data_string *)d;
72        UNUSED(depth);
73
74        fprintf(stdout, "\"%s\"", ds->value->used ? ds->value->ptr : "");
75}
76
77
78data_string *data_string_init(void) {
79        data_string *ds;
80
81        ds = calloc(1, sizeof(*ds));
82        assert(ds);
83
84        ds->key = buffer_init();
85        ds->value = buffer_init();
86
87        ds->copy = data_string_copy;
88        ds->free = data_string_free;
89        ds->reset = data_string_reset;
90        ds->insert_dup = data_string_insert_dup;
91        ds->print = data_string_print;
92        ds->type = TYPE_STRING;
93
94        return ds;
95}
96
97data_string *data_response_init(void) {
98        data_string *ds;
99
100        ds = data_string_init();
101        ds->insert_dup = data_response_insert_dup;
102
103        return ds;
104}
Note: See TracBrowser for help on using the browser.