serialize: add serialize_string_set()
This commit is contained in:
@@ -219,6 +219,27 @@ int serialize_item_base64mem(FILE *f, const char *key, const void *p, size_t l)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int serialize_string_set(FILE *f, const char *key, Set *s) {
|
||||
const char *e;
|
||||
int r;
|
||||
|
||||
assert(f);
|
||||
assert(key);
|
||||
|
||||
if (set_isempty(s))
|
||||
return 0;
|
||||
|
||||
/* Serialize as individual items, as each element might contain separators and escapes */
|
||||
|
||||
SET_FOREACH(e, s) {
|
||||
r = serialize_item(f, key, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int deserialize_read_line(FILE *f, char **ret) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
int r;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "fdset.h"
|
||||
#include "macro.h"
|
||||
#include "pidref.h"
|
||||
#include "set.h"
|
||||
#include "string-util.h"
|
||||
#include "time-util.h"
|
||||
|
||||
@@ -19,6 +20,7 @@ int serialize_usec(FILE *f, const char *key, usec_t usec);
|
||||
int serialize_dual_timestamp(FILE *f, const char *key, const dual_timestamp *t);
|
||||
int serialize_strv(FILE *f, const char *key, char **l);
|
||||
int serialize_pidref(FILE *f, FDSet *fds, const char *key, PidRef *pidref);
|
||||
int serialize_string_set(FILE *f, const char *key, Set *s);
|
||||
|
||||
static inline int serialize_bool(FILE *f, const char *key, bool b) {
|
||||
return serialize_item(f, key, yes_no(b));
|
||||
|
||||
@@ -225,6 +225,37 @@ TEST(serialize_item_base64mem) {
|
||||
assert_se(streq(line, "a=////"));
|
||||
}
|
||||
|
||||
TEST(serialize_string_set) {
|
||||
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX";
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_set_free_free_ Set *s = NULL;
|
||||
_cleanup_free_ char *line1 = NULL, *line2 = NULL;
|
||||
char *p, *q;
|
||||
|
||||
assert_se(fmkostemp_safe(fn, "r+", &f) == 0);
|
||||
log_info("/* %s (%s) */", __func__, fn);
|
||||
|
||||
assert_se(set_ensure_allocated(&s, &string_hash_ops) >= 0);
|
||||
|
||||
assert_se(serialize_string_set(f, "a", s) == 0);
|
||||
|
||||
assert_se(set_put_strsplit(s, "abc def,ghi jkl", ",", 0) >= 0);
|
||||
|
||||
assert_se(serialize_string_set(f, "a", s) == 1);
|
||||
|
||||
rewind(f);
|
||||
|
||||
assert_se(read_line(f, LONG_LINE_MAX, &line1) > 0);
|
||||
assert_se((p = startswith(line1, "a=")));
|
||||
|
||||
assert_se(read_line(f, LONG_LINE_MAX, &line2) > 0);
|
||||
assert_se((q = startswith(line2, "a=")));
|
||||
|
||||
assert_se(!streq(p, q));
|
||||
assert_se(STR_IN_SET(p, "abc def", "ghi jkl"));
|
||||
assert_se(STR_IN_SET(q, "abc def", "ghi jkl"));
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
memset(long_string, 'x', sizeof(long_string)-1);
|
||||
char_array_0(long_string);
|
||||
|
||||
Reference in New Issue
Block a user