|
|
a3e2b5 |
From 85ccda9eabb6b89e644cedd9faafb5dbe97e8341 Mon Sep 17 00:00:00 2001
|
|
|
a3e2b5 |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
a3e2b5 |
Date: Wed, 7 Nov 2018 15:25:51 +0100
|
|
|
a3e2b5 |
Subject: [PATCH] format-table: add option to store/format percent and uint64_t
|
|
|
a3e2b5 |
values in cells
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
(cherry picked from commit a4661181fa702a8bff4644210ba7ea14bea51a4a)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
Related: #1689832
|
|
|
a3e2b5 |
---
|
|
|
a3e2b5 |
src/basic/format-table.c | 48 ++++++++++++++++++++++++++++++++++++++++
|
|
|
a3e2b5 |
src/basic/format-table.h | 2 ++
|
|
|
a3e2b5 |
src/basic/macro.h | 9 ++++++++
|
|
|
a3e2b5 |
3 files changed, 59 insertions(+)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
|
|
|
a3e2b5 |
index 17be7285cd..64b9eb4108 100644
|
|
|
a3e2b5 |
--- a/src/basic/format-table.c
|
|
|
a3e2b5 |
+++ b/src/basic/format-table.c
|
|
|
a3e2b5 |
@@ -70,6 +70,8 @@ typedef struct TableData {
|
|
|
a3e2b5 |
uint64_t size;
|
|
|
a3e2b5 |
char string[0];
|
|
|
a3e2b5 |
uint32_t uint32;
|
|
|
a3e2b5 |
+ uint64_t uint64;
|
|
|
a3e2b5 |
+ int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
|
|
|
a3e2b5 |
/* … add more here as we start supporting more cell data types … */
|
|
|
a3e2b5 |
};
|
|
|
a3e2b5 |
} TableData;
|
|
|
a3e2b5 |
@@ -235,11 +237,15 @@ static size_t table_data_size(TableDataType type, const void *data) {
|
|
|
a3e2b5 |
return sizeof(usec_t);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
case TABLE_SIZE:
|
|
|
a3e2b5 |
+ case TABLE_UINT64:
|
|
|
a3e2b5 |
return sizeof(uint64_t);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
case TABLE_UINT32:
|
|
|
a3e2b5 |
return sizeof(uint32_t);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ case TABLE_PERCENT:
|
|
|
a3e2b5 |
+ return sizeof(int);
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
default:
|
|
|
a3e2b5 |
assert_not_reached("Uh? Unexpected cell type");
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
@@ -599,6 +605,8 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
|
|
|
a3e2b5 |
uint64_t size;
|
|
|
a3e2b5 |
usec_t usec;
|
|
|
a3e2b5 |
uint32_t uint32;
|
|
|
a3e2b5 |
+ uint64_t uint64;
|
|
|
a3e2b5 |
+ int percent;
|
|
|
a3e2b5 |
bool b;
|
|
|
a3e2b5 |
} buffer;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
@@ -633,6 +641,16 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
|
|
|
a3e2b5 |
data = &buffer.uint32;
|
|
|
a3e2b5 |
break;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ case TABLE_UINT64:
|
|
|
a3e2b5 |
+ buffer.uint64 = va_arg(ap, uint64_t);
|
|
|
a3e2b5 |
+ data = &buffer.uint64;
|
|
|
a3e2b5 |
+ break;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ case TABLE_PERCENT:
|
|
|
a3e2b5 |
+ buffer.percent = va_arg(ap, int);
|
|
|
a3e2b5 |
+ data = &buffer.percent;
|
|
|
a3e2b5 |
+ break;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
case _TABLE_DATA_TYPE_MAX:
|
|
|
a3e2b5 |
/* Used as end marker */
|
|
|
a3e2b5 |
va_end(ap);
|
|
|
a3e2b5 |
@@ -772,6 +790,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
|
|
|
a3e2b5 |
return 1;
|
|
|
a3e2b5 |
return 0;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ case TABLE_UINT64:
|
|
|
a3e2b5 |
+ return CMP(a->uint64, b->uint64);
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ case TABLE_PERCENT:
|
|
|
a3e2b5 |
+ return CMP(a->percent, b->percent);
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
default:
|
|
|
a3e2b5 |
;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
@@ -894,6 +918,30 @@ static const char *table_data_format(TableData *d) {
|
|
|
a3e2b5 |
break;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ case TABLE_UINT64: {
|
|
|
a3e2b5 |
+ _cleanup_free_ char *p;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ p = new(char, DECIMAL_STR_WIDTH(d->uint64) + 1);
|
|
|
a3e2b5 |
+ if (!p)
|
|
|
a3e2b5 |
+ return NULL;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ sprintf(p, "%" PRIu64, d->uint64);
|
|
|
a3e2b5 |
+ d->formatted = TAKE_PTR(p);
|
|
|
a3e2b5 |
+ break;
|
|
|
a3e2b5 |
+ }
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ case TABLE_PERCENT: {
|
|
|
a3e2b5 |
+ _cleanup_free_ char *p;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ p = new(char, DECIMAL_STR_WIDTH(d->percent) + 2);
|
|
|
a3e2b5 |
+ if (!p)
|
|
|
a3e2b5 |
+ return NULL;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ sprintf(p, "%i%%" , d->percent);
|
|
|
a3e2b5 |
+ d->formatted = TAKE_PTR(p);
|
|
|
a3e2b5 |
+ break;
|
|
|
a3e2b5 |
+ }
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
default:
|
|
|
a3e2b5 |
assert_not_reached("Unexpected type?");
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
diff --git a/src/basic/format-table.h b/src/basic/format-table.h
|
|
|
a3e2b5 |
index 9978a8baf2..2db2084062 100644
|
|
|
a3e2b5 |
--- a/src/basic/format-table.h
|
|
|
a3e2b5 |
+++ b/src/basic/format-table.h
|
|
|
a3e2b5 |
@@ -15,6 +15,8 @@ typedef enum TableDataType {
|
|
|
a3e2b5 |
TABLE_TIMESPAN,
|
|
|
a3e2b5 |
TABLE_SIZE,
|
|
|
a3e2b5 |
TABLE_UINT32,
|
|
|
a3e2b5 |
+ TABLE_UINT64,
|
|
|
a3e2b5 |
+ TABLE_PERCENT,
|
|
|
a3e2b5 |
_TABLE_DATA_TYPE_MAX,
|
|
|
a3e2b5 |
_TABLE_DATA_TYPE_INVALID = -1,
|
|
|
a3e2b5 |
} TableDataType;
|
|
|
a3e2b5 |
diff --git a/src/basic/macro.h b/src/basic/macro.h
|
|
|
a3e2b5 |
index d1365f7058..79ab02b27a 100644
|
|
|
a3e2b5 |
--- a/src/basic/macro.h
|
|
|
a3e2b5 |
+++ b/src/basic/macro.h
|
|
|
a3e2b5 |
@@ -222,6 +222,15 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
|
|
|
a3e2b5 |
UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) - UNIQ_T(B,bq) : 0; \
|
|
|
a3e2b5 |
})
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+#define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
|
|
|
a3e2b5 |
+#define __CMP(aq, a, bq, b) \
|
|
|
a3e2b5 |
+ ({ \
|
|
|
a3e2b5 |
+ const typeof(a) UNIQ_T(A, aq) = (a); \
|
|
|
a3e2b5 |
+ const typeof(b) UNIQ_T(B, bq) = (b); \
|
|
|
a3e2b5 |
+ UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
|
|
|
a3e2b5 |
+ UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
|
|
|
a3e2b5 |
+ })
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
#undef CLAMP
|
|
|
a3e2b5 |
#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
|
|
|
a3e2b5 |
#define __CLAMP(xq, x, lowq, low, highq, high) \
|