]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
table: add more basic types
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 24 May 2019 09:01:04 +0000 (18:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 29 May 2019 05:21:19 +0000 (14:21 +0900)
src/shared/format-table.c
src/shared/format-table.h

index de72bf02f32c7caf987ee15ec8e7af5fdbb684f6..09357694ddb26e8bf845cb5fa59254b303cebc51 100644 (file)
@@ -73,6 +73,10 @@ typedef struct TableData {
                 usec_t timespan;
                 uint64_t size;
                 char string[0];
+                int int_val;
+                int32_t int32;
+                int64_t int64;
+                unsigned uint_val;
                 uint32_t uint32;
                 uint64_t uint64;
                 int percent;        /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
@@ -226,12 +230,16 @@ static size_t table_data_size(TableDataType type, const void *data) {
                 return sizeof(usec_t);
 
         case TABLE_SIZE:
+        case TABLE_INT64:
         case TABLE_UINT64:
                 return sizeof(uint64_t);
 
+        case TABLE_INT32:
         case TABLE_UINT32:
                 return sizeof(uint32_t);
 
+        case TABLE_INT:
+        case TABLE_UINT:
         case TABLE_PERCENT:
                 return sizeof(int);
 
@@ -678,6 +686,10 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                 union {
                         uint64_t size;
                         usec_t usec;
+                        int int_val;
+                        int32_t int32;
+                        int64_t int64;
+                        unsigned uint_val;
                         uint32_t uint32;
                         uint64_t uint64;
                         int percent;
@@ -710,6 +722,26 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         data = &buffer.size;
                         break;
 
+                case TABLE_INT:
+                        buffer.int_val = va_arg(ap, int);
+                        data = &buffer.int_val;
+                        break;
+
+                case TABLE_INT32:
+                        buffer.int32 = va_arg(ap, int32_t);
+                        data = &buffer.int32;
+                        break;
+
+                case TABLE_INT64:
+                        buffer.int64 = va_arg(ap, int64_t);
+                        data = &buffer.int64;
+                        break;
+
+                case TABLE_UINT:
+                        buffer.uint_val = va_arg(ap, unsigned);
+                        data = &buffer.uint_val;
+                        break;
+
                 case TABLE_UINT32:
                         buffer.uint32 = va_arg(ap, uint32_t);
                         data = &buffer.uint32;
@@ -845,6 +877,18 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_SIZE:
                         return CMP(a->size, b->size);
 
+                case TABLE_INT:
+                        return CMP(a->int_val, b->int_val);
+
+                case TABLE_INT32:
+                        return CMP(a->int32, b->int32);
+
+                case TABLE_INT64:
+                        return CMP(a->int64, b->int64);
+
+                case TABLE_UINT:
+                        return CMP(a->uint_val, b->uint_val);
+
                 case TABLE_UINT32:
                         return CMP(a->uint32, b->uint32);
 
@@ -966,6 +1010,54 @@ static const char *table_data_format(TableData *d) {
                 break;
         }
 
+        case TABLE_INT: {
+                _cleanup_free_ char *p;
+
+                p = new(char, DECIMAL_STR_WIDTH(d->int_val) + 1);
+                if (!p)
+                        return NULL;
+
+                sprintf(p, "%i", d->int_val);
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
+        case TABLE_INT32: {
+                _cleanup_free_ char *p;
+
+                p = new(char, DECIMAL_STR_WIDTH(d->int32) + 1);
+                if (!p)
+                        return NULL;
+
+                sprintf(p, "%" PRIi32, d->int32);
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
+        case TABLE_INT64: {
+                _cleanup_free_ char *p;
+
+                p = new(char, DECIMAL_STR_WIDTH(d->int64) + 1);
+                if (!p)
+                        return NULL;
+
+                sprintf(p, "%" PRIi64, d->int64);
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
+        case TABLE_UINT: {
+                _cleanup_free_ char *p;
+
+                p = new(char, DECIMAL_STR_WIDTH(d->uint_val) + 1);
+                if (!p)
+                        return NULL;
+
+                sprintf(p, "%u", d->uint_val);
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
         case TABLE_UINT32: {
                 _cleanup_free_ char *p;
 
@@ -1505,6 +1597,18 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
 
                 return json_variant_new_unsigned(ret, d->size);
 
+        case TABLE_INT:
+                return json_variant_new_integer(ret, d->int_val);
+
+        case TABLE_INT32:
+                return json_variant_new_integer(ret, d->int32);
+
+        case TABLE_INT64:
+                return json_variant_new_integer(ret, d->int64);
+
+        case TABLE_UINT:
+                return json_variant_new_unsigned(ret, d->uint_val);
+
         case TABLE_UINT32:
                 return json_variant_new_unsigned(ret, d->uint32);
 
index 5a52c43e25a45b167c8c07c8cd62fdb4db696437..c5cf5708d4b15f1bc5d7c9c7c4896c6a4193463e 100644 (file)
@@ -15,6 +15,10 @@ typedef enum TableDataType {
         TABLE_TIMESTAMP,
         TABLE_TIMESPAN,
         TABLE_SIZE,
+        TABLE_INT,
+        TABLE_INT32,
+        TABLE_INT64,
+        TABLE_UINT,
         TABLE_UINT32,
         TABLE_UINT64,
         TABLE_PERCENT,