mirror of
https://github.com/systemd/systemd.git
synced 2025-01-12 13:18:14 +03:00
basic/ordered-set: export networkd function to print string sets
Tests are added.
This commit is contained in:
parent
ef79eae09a
commit
53ae3f6467
@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "fileio.h"
|
||||
#include "ordered-set.h"
|
||||
#include "strv.h"
|
||||
|
||||
@ -63,3 +64,19 @@ int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void ordered_set_print(FILE *f, const char *field, OrderedSet *s) {
|
||||
bool space = false;
|
||||
Iterator i;
|
||||
char *p;
|
||||
|
||||
if (ordered_set_isempty(s))
|
||||
return;
|
||||
|
||||
fputs(field, f);
|
||||
|
||||
ORDERED_SET_FOREACH(p, s, i)
|
||||
fputs_with_space(f, p, NULL, &space);
|
||||
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "hashmap.h"
|
||||
|
||||
typedef struct OrderedSet OrderedSet;
|
||||
@ -56,6 +58,7 @@ int ordered_set_consume(OrderedSet *s, void *p);
|
||||
int ordered_set_put_strdup(OrderedSet *s, const char *p);
|
||||
int ordered_set_put_strdupv(OrderedSet *s, char **l);
|
||||
int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l);
|
||||
void ordered_set_print(FILE *f, const char *field, OrderedSet *s);
|
||||
|
||||
#define ORDERED_SET_FOREACH(e, s, i) \
|
||||
for ((i) = ITERATOR_FIRST; ordered_set_iterate((s), &(i), (void**)&(e)); )
|
||||
|
@ -1059,22 +1059,6 @@ static int ordered_set_put_in4_addrv(OrderedSet *s,
|
||||
return c;
|
||||
}
|
||||
|
||||
static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
|
||||
bool space = false;
|
||||
Iterator i;
|
||||
char *p;
|
||||
|
||||
if (ordered_set_isempty(s))
|
||||
return;
|
||||
|
||||
fputs(field, f);
|
||||
|
||||
ORDERED_SET_FOREACH(p, s, i)
|
||||
fputs_with_space(f, p, NULL, &space);
|
||||
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
static int manager_save(Manager *m) {
|
||||
_cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
|
||||
Link *link;
|
||||
@ -1197,10 +1181,10 @@ static int manager_save(Manager *m) {
|
||||
"# This is private data. Do not parse.\n"
|
||||
"OPER_STATE=%s\n", operstate_str);
|
||||
|
||||
print_string_set(f, "DNS=", dns);
|
||||
print_string_set(f, "NTP=", ntp);
|
||||
print_string_set(f, "DOMAINS=", search_domains);
|
||||
print_string_set(f, "ROUTE_DOMAINS=", route_domains);
|
||||
ordered_set_print(f, "DNS=", dns);
|
||||
ordered_set_print(f, "NTP=", ntp);
|
||||
ordered_set_print(f, "DOMAINS=", search_domains);
|
||||
ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
|
||||
|
||||
r = routing_policy_serialize_rules(m->rules, f);
|
||||
if (r < 0)
|
||||
|
@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ordered-set.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
@ -16,12 +18,16 @@ static void test_set_steal_first(void) {
|
||||
assert_se(ordered_set_put(m, (void*) "22") == 1);
|
||||
assert_se(ordered_set_put(m, (void*) "333") == 1);
|
||||
|
||||
ordered_set_print(stdout, "SET=", m);
|
||||
|
||||
while ((val = ordered_set_steal_first(m)))
|
||||
seen[strlen(val) - 1]++;
|
||||
|
||||
assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
|
||||
|
||||
assert_se(ordered_set_isempty(m));
|
||||
|
||||
ordered_set_print(stdout, "SET=", m);
|
||||
}
|
||||
|
||||
typedef struct Item {
|
||||
@ -70,6 +76,8 @@ static void test_set_put(void) {
|
||||
assert_se(streq(t[1], "22"));
|
||||
assert_se(streq(t[2], "333"));
|
||||
assert_se(!t[3]);
|
||||
|
||||
ordered_set_print(stdout, "FOO=", m);
|
||||
}
|
||||
|
||||
static void test_set_put_string_set(void) {
|
||||
@ -99,6 +107,8 @@ static void test_set_put_string_set(void) {
|
||||
|
||||
assert_se(final = ordered_set_get_strv(m));
|
||||
assert_se(strv_equal(final, STRV_MAKE("1", "22", "333", "11", "33")));
|
||||
|
||||
ordered_set_print(stdout, "BAR=", m);
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
|
Loading…
Reference in New Issue
Block a user