format-table: add field type that outputs hex values prefixed with 0x

This commit is contained in:
Lennart Poettering
2024-06-04 11:04:01 +02:00
parent 7ecc69c33b
commit 86279dc970
2 changed files with 34 additions and 0 deletions

View File

@@ -313,12 +313,14 @@ static size_t table_data_size(TableDataType type, const void *data) {
case TABLE_INT64:
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
case TABLE_BPS:
return sizeof(uint64_t);
case TABLE_INT32:
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
return sizeof(uint32_t);
case TABLE_INT16:
@@ -1028,12 +1030,14 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
buffer.uint32 = va_arg(ap, uint32_t);
data = &buffer.uint32;
break;
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
buffer.uint64 = va_arg(ap, uint64_t);
data = &buffer.uint64;
break;
@@ -1455,10 +1459,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
return CMP(a->uint32, b->uint32);
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
return CMP(a->uint64, b->uint64);
case TABLE_PERCENT:
@@ -1859,6 +1865,18 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
break;
}
case TABLE_UINT32_HEX_0x: {
_cleanup_free_ char *p = NULL;
p = new(char, 2 + 8 + 1);
if (!p)
return NULL;
sprintf(p, "0x%" PRIx32, d->uint32);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_UINT64: {
_cleanup_free_ char *p = NULL;
@@ -1883,6 +1901,18 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
break;
}
case TABLE_UINT64_HEX_0x: {
_cleanup_free_ char *p = NULL;
p = new(char, 2 + 16 + 1);
if (!p)
return NULL;
sprintf(p, "0x%" PRIx64, d->uint64);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_PERCENT: {
_cleanup_free_ char *p = NULL;
@@ -2822,10 +2852,12 @@ static int table_data_to_json(TableData *d, sd_json_variant **ret) {
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
return sd_json_variant_new_unsigned(ret, d->uint32);
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
return sd_json_variant_new_unsigned(ret, d->uint64);
case TABLE_PERCENT:

View File

@@ -40,8 +40,10 @@ typedef enum TableDataType {
TABLE_UINT16,
TABLE_UINT32,
TABLE_UINT32_HEX,
TABLE_UINT32_HEX_0x,
TABLE_UINT64,
TABLE_UINT64_HEX,
TABLE_UINT64_HEX_0x,
TABLE_PERCENT,
TABLE_IFINDEX,
TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */