2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "array.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-10-02 06:16:08 +00:00
|
|
|
__attribute_cold__
|
2005-08-08 16:36:51 +00:00
|
|
|
static data_unset *data_string_copy(const data_unset *s) {
|
2005-08-08 14:40:47 +00:00
|
|
|
data_string *src = (data_string *)s;
|
|
|
|
data_string *ds = data_string_init();
|
|
|
|
|
2019-10-13 07:37:59 +00:00
|
|
|
if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_copy_buffer(&ds->value, &src->value);
|
2005-08-08 14:40:47 +00:00
|
|
|
return (data_unset *)ds;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static void data_string_free(data_unset *d) {
|
|
|
|
data_string *ds = (data_string *)d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-13 07:37:59 +00:00
|
|
|
free(ds->key.ptr);
|
2019-10-13 08:59:57 +00:00
|
|
|
free(ds->value.ptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:16:08 +00:00
|
|
|
__attribute_cold__
|
2005-02-20 14:27:00 +00:00
|
|
|
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;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-13 08:59:57 +00:00
|
|
|
if (!buffer_is_empty(&ds_dst->value)) {
|
|
|
|
buffer_append_string_len(&ds_dst->value, CONST_STR_LEN(", "));
|
|
|
|
buffer_append_string_buffer(&ds_dst->value, &ds_src->value);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_copy_buffer(&ds_dst->value, &ds_src->value);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-09-23 02:12:51 +00:00
|
|
|
src->fn->free(src);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:16:08 +00:00
|
|
|
__attribute_cold__
|
2005-08-08 16:36:51 +00:00
|
|
|
static void data_string_print(const data_unset *d, int depth) {
|
2005-02-20 14:27:00 +00:00
|
|
|
data_string *ds = (data_string *)d;
|
2015-02-08 19:10:44 +00:00
|
|
|
size_t i, len;
|
2005-08-08 17:25:06 +00:00
|
|
|
UNUSED(depth);
|
2005-08-08 10:16:53 +00:00
|
|
|
|
2010-07-04 07:45:21 +00:00
|
|
|
/* empty and uninitialized strings */
|
2019-10-13 08:59:57 +00:00
|
|
|
if (buffer_string_is_empty(&ds->value)) {
|
2010-05-28 15:16:39 +00:00
|
|
|
fputs("\"\"", stdout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-04 07:45:21 +00:00
|
|
|
/* print out the string as is, except prepend " with backslash */
|
2010-05-28 15:16:39 +00:00
|
|
|
putc('"', stdout);
|
2019-10-13 08:59:57 +00:00
|
|
|
len = buffer_string_length(&ds->value);
|
2015-02-08 19:10:44 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2019-10-13 08:59:57 +00:00
|
|
|
unsigned char c = ds->value.ptr[i];
|
2010-05-28 15:16:39 +00:00
|
|
|
if (c == '"') {
|
|
|
|
fputs("\\\"", stdout);
|
|
|
|
} else {
|
|
|
|
putc(c, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc('"', stdout);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data_string *data_string_init(void) {
|
2018-09-23 02:12:51 +00:00
|
|
|
static const struct data_methods fn = {
|
|
|
|
data_string_copy,
|
|
|
|
data_string_free,
|
|
|
|
data_string_insert_dup,
|
|
|
|
data_string_print,
|
|
|
|
};
|
2005-02-20 14:27:00 +00:00
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ds = calloc(1, sizeof(*ds));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != ds);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ds->type = TYPE_STRING;
|
2018-09-23 02:12:51 +00:00
|
|
|
ds->fn = &fn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return ds;
|
|
|
|
}
|