ryantimwilson / rpms / systemd

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