From: Lennart Poettering Date: Tue, 22 May 2018 10:06:54 +0000 (+0200) Subject: string-table: add new DUMP_STRING_TABLE() macro X-Git-Tag: v239~226^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d34f3bc4e5862e22453c5d001b33ddd447b1f2a4;p=platform%2Fupstream%2Fsystemd.git string-table: add new DUMP_STRING_TABLE() macro The macro is inspired by the other string table macros, and takes the same arguments in the same order and dumps a string table to stdout. Since it's typesafe it's nice to implement this as macro rather than regular function. This new macro is useful for implementing commands such as "systemctl -t help" and similar, i.e. wherever we want to dump all values of an enum to stdout. --- diff --git a/src/basic/string-table.h b/src/basic/string-table.h index a39206f..92825c4 100644 --- a/src/basic/string-table.h +++ b/src/basic/string-table.h @@ -102,3 +102,18 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static) #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static) + +#define DUMP_STRING_TABLE(name,type,max) \ + do { \ + type _k; \ + flockfile(stdout); \ + for (_k = 0; _k < (max); _k++) { \ + const char *_t; \ + _t = name##_to_string(_k); \ + if (!_t) \ + continue; \ + fputs_unlocked(_t, stdout); \ + fputc_unlocked('\n', stdout); \ + } \ + funlockfile(stdout); \ + } while(false)