From b165500b6987f3fcbbb6daee6d84d66af7fbd41f Mon Sep 17 00:00:00 2001 From: Ingo Franzki Date: Tue, 27 Nov 2018 17:18:57 +0100 Subject: [PATCH] libutil: Add function to print separator line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In wide format the header is separated from the rest of the records by a separator line. Add support to print such a separator line also between some records. Add function util_rec_print_separator() that prints the separator line for wide format only. For other formats this is a NOP. Signed-off-by: Ingo Franzki Acked-by: Jan Höppner Signed-off-by: Jan Höppner --- include/lib/util_rec.h | 1 + libutil/util_rec.c | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/lib/util_rec.h b/include/lib/util_rec.h index f6f72079..7a0c2f66 100644 --- a/include/lib/util_rec.h +++ b/include/lib/util_rec.h @@ -68,6 +68,7 @@ const char *util_rec_get(struct util_rec *rec, const char *key); void util_rec_print_hdr(struct util_rec *rec); void util_rec_print(struct util_rec *rec); +void util_rec_print_separator(struct util_rec *rec); void util_rec_set_indent(struct util_rec *rec, int indent); diff --git a/libutil/util_rec.c b/libutil/util_rec.c index 2f9295bb..c216cf95 100644 --- a/libutil/util_rec.c +++ b/libutil/util_rec.c @@ -138,6 +138,35 @@ static inline void rec_print_indention(int indent) printf("%*s", indent, ""); } +/* + * Print record separator in "wide" output format + */ +static void rec_print_wide_separator(struct util_rec *rec) +{ + const char *hdr_sep = rec->fmt.d.wide_p.hdr_sep; + int size = 0, field_count = 0; + struct util_rec_fld *fld; + char *buf; + + if (!hdr_sep) + return; + + util_list_iterate(rec->list, fld) { + if (fld->hdr) { + size += fld->width; + field_count++; + } + } + + size += field_count - 1; + buf = util_malloc(size + 1); + memset(buf, (int)hdr_sep[0], size); + buf[size] = 0; + rec_print_indention(rec->fmt.indent); + printf("%s\n", buf); + free(buf); +} + /* * Print record header in "wide" output format */ @@ -497,6 +526,24 @@ void util_rec_print_hdr(struct util_rec *rec) } } +/** + * Print record separator according to output format + * + * @param[in] rec Record pointer + */ +void util_rec_print_separator(struct util_rec *rec) +{ + switch (rec->fmt.type) { + case REC_FMT_WIDE: + rec_print_wide_separator(rec); + break; + case REC_FMT_LONG: + break; + case REC_FMT_CSV: + break; + } +} + /** * Set a field value to an argz vector *