format-table: add support for formatting uuids/id128 values

This commit is contained in:
Lennart Poettering 2019-12-10 21:28:16 +01:00
parent 1293a168f1
commit 137688dff4
2 changed files with 53 additions and 1 deletions

View File

@ -4,12 +4,15 @@
#include <net/if.h>
#include <unistd.h>
#include "sd-id128.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "format-util.h"
#include "gunicode.h"
#include "id128-util.h"
#include "in-addr-util.h"
#include "locale-util.h"
#include "memory-util.h"
@ -94,6 +97,7 @@ typedef struct TableData {
int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
int ifindex;
union in_addr_union address;
sd_id128_t id128;
/* … add more here as we start supporting more cell data types … */
};
} TableData;
@ -289,6 +293,10 @@ static size_t table_data_size(TableDataType type, const void *data) {
case TABLE_IN6_ADDR:
return sizeof(struct in6_addr);
case TABLE_UUID:
case TABLE_ID128:
return sizeof(sd_id128_t);
default:
assert_not_reached("Uh? Unexpected cell type");
}
@ -335,7 +343,6 @@ static bool table_data_matches(
k = table_data_size(type, data);
l = table_data_size(d->type, d->data);
if (k != l)
return false;
@ -778,6 +785,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
int ifindex;
bool b;
union in_addr_union address;
sd_id128_t id128;
} buffer;
switch (type) {
@ -901,6 +909,12 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.address.in6;
break;
case TABLE_UUID:
case TABLE_ID128:
buffer.id128 = va_arg(ap, sd_id128_t);
data = &buffer.id128;
break;
case TABLE_SET_MINIMUM_WIDTH: {
size_t w = va_arg(ap, size_t);
@ -1137,6 +1151,10 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_IN6_ADDR:
return memcmp(&a->address.in6, &b->address.in6, FAMILY_ADDRESS_SIZE(AF_INET6));
case TABLE_UUID:
case TABLE_ID128:
return memcmp(&a->id128, &b->id128, sizeof(sd_id128_t));
default:
;
}
@ -1451,6 +1469,28 @@ static const char *table_data_format(Table *t, TableData *d) {
break;
}
case TABLE_ID128: {
char *p;
p = new(char, SD_ID128_STRING_MAX);
if (!p)
return NULL;
d->formatted = sd_id128_to_string(d->id128, p);
break;
}
case TABLE_UUID: {
char *p;
p = new(char, ID128_UUID_STRING_MAX);
if (!p)
return NULL;
d->formatted = id128_to_uuid_string(d->id128, p);
break;
}
default:
assert_not_reached("Unexpected type?");
}
@ -2155,6 +2195,16 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_IN6_ADDR:
return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6));
case TABLE_ID128: {
char buf[SD_ID128_STRING_MAX];
return json_variant_new_string(ret, sd_id128_to_string(d->id128, buf));
}
case TABLE_UUID: {
char buf[ID128_UUID_STRING_MAX];
return json_variant_new_string(ret, id128_to_uuid_string(d->id128, buf));
}
default:
return -EINVAL;
}

View File

@ -35,6 +35,8 @@ typedef enum TableDataType {
TABLE_IFINDEX,
TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
TABLE_ID128,
TABLE_UUID,
_TABLE_DATA_TYPE_MAX,
/* The following are not really data types, but commands for table_add_cell_many() to make changes to