daandemeyer / rpms / systemd

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