1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Add field name prefix option to reporting functions.

This commit is contained in:
Alasdair Kergon 2008-04-20 00:11:08 +00:00
parent f44e3ff46a
commit c791f186ce
4 changed files with 72 additions and 4 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.26 -
=================================
Add field name prefix option to reporting functions.
Calculate string size within dm_pool_grow_object.
Version 1.02.25 - 10th April 2008

View File

@ -132,5 +132,6 @@ dm_report_field_int32
dm_report_field_uint32
dm_report_field_uint64
dm_report_field_set_value
dm_report_set_output_field_name_prefix
dm_regex_create
dm_regex_match

View File

@ -739,6 +739,7 @@ struct dm_report_field_type {
#define DM_REPORT_OUTPUT_ALIGNED 0x00000001
#define DM_REPORT_OUTPUT_BUFFERED 0x00000002
#define DM_REPORT_OUTPUT_HEADINGS 0x00000004
#define DM_REPORT_OUTPUT_FIELD_NAME_PREFIX 0x00000008
struct dm_report *dm_report_init(uint32_t *report_types,
const struct dm_report_object_type *types,
@ -752,6 +753,12 @@ int dm_report_object(struct dm_report *rh, void *object);
int dm_report_output(struct dm_report *rh);
void dm_report_free(struct dm_report *rh);
/*
* Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
*/
int dm_report_set_output_field_name_prefix(struct dm_report *rh,
const char *report_prefix);
/*
* Report functions are provided for simple data types.
* They take care of allocating copies of the data.

View File

@ -17,6 +17,8 @@
#include "list.h"
#include "log.h"
#include <ctype.h>
/*
* Internal flags
*/
@ -27,6 +29,7 @@ struct dm_report {
struct dm_pool *mem;
uint32_t report_types;
const char *output_field_name_prefix;
const char *field_prefix;
uint32_t flags;
const char *separator;
@ -551,6 +554,31 @@ void dm_report_free(struct dm_report *rh)
dm_free(rh);
}
static char *_toupperstr(char *str)
{
char *u = str;
do
*u = toupper(*u);
while (*u++);
return str;
}
int dm_report_set_output_field_name_prefix(struct dm_report *rh, const char *output_field_name_prefix)
{
char *prefix;
if (!(prefix = dm_pool_strdup(rh->mem, output_field_name_prefix))) {
log_error("dm_report_set_output_field_name_prefix: dm_pool_strdup failed");
return 0;
}
rh->output_field_name_prefix = _toupperstr(prefix);
return 1;
}
/*
* Create a row of data for an object
*/
@ -771,6 +799,7 @@ int dm_report_output(struct dm_report *rh)
struct row *row = NULL;
struct dm_report_field *field;
const char *repstr;
char *field_id;
char buf[4096];
int32_t width;
uint32_t align;
@ -798,6 +827,30 @@ int dm_report_output(struct dm_report *rh)
if (field->props->flags & FLD_HIDDEN)
continue;
if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) {
if (!(field_id = strdup(rh->fields[field->props->field_num].id))) {
log_error("dm_report: Failed to copy field name");
goto bad;
}
if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) {
log_error("dm_report: Unable to extend output line");
goto bad;
}
if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) {
log_error("dm_report: Unable to extend output line");
goto bad;
}
free(field_id);
if (!dm_pool_grow_object(rh->mem, "=\"", 2)) {
log_error("dm_report: Unable to extend output line");
goto bad;
}
}
repstr = field->report_string;
width = field->props->width;
if (!(rh->flags & DM_REPORT_OUTPUT_ALIGNED)) {
@ -832,6 +885,12 @@ int dm_report_output(struct dm_report *rh)
}
}
if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX)
if (!dm_pool_grow_object(rh->mem, "\"", 1)) {
log_error("dm_report: Unable to extend output line");
goto bad;
}
if (!list_end(&row->fields, fh))
if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) {
log_error("dm_report: Unable to extend output line");