libutil: Add function to print separator line

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 <ifranzki@linux.ibm.com>
Acked-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Ingo Franzki
2018-11-27 17:18:57 +01:00
committed by Jan Höppner
parent 5f2ddad6a8
commit b165500b69
2 changed files with 48 additions and 0 deletions

View File

@@ -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);

View File

@@ -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
*