]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/format-table.c
tree-wide: CMP()ify all the things
[thirdparty/systemd.git] / src / basic / format-table.c
index 582c5b86a4eba1f132078d755c491641a8efb8dc..10e15c9d709de2c90238f5fe1dc1516a9f0093e8 100644 (file)
@@ -36,7 +36,7 @@
    that. The first row is always the header row. If header display is turned off we simply skip outputting the first
    row. Also, when sorting rows we always leave the first row where it is, as the header shouldn't move.
 
- - Note because there's no row and no column object some properties that might be approproate as row/column properties
+ - Note because there's no row and no column object some properties that might be appropriate as row/column properties
    are exposed as cell properties instead. For example, the "weight" of a column (which is used to determine where to
    add/remove space preferable when expanding/compressing tables horizontally) is actually made the "weight" of a
    cell. Given that we usually need it per-column though we will calculate the average across every cell of the column
@@ -73,11 +73,11 @@ typedef struct TableData {
 } TableData;
 
 static size_t TABLE_CELL_TO_INDEX(TableCell *cell) {
-        unsigned i;
+        size_t i;
 
         assert(cell);
 
-        i = PTR_TO_UINT(cell);
+        i = PTR_TO_SIZE(cell);
         assert(i > 0);
 
         return i-1;
@@ -85,7 +85,7 @@ static size_t TABLE_CELL_TO_INDEX(TableCell *cell) {
 
 static TableCell* TABLE_INDEX_TO_CELL(size_t index) {
         assert(index != (size_t) -1);
-        return UINT_TO_PTR((unsigned) (index + 1));
+        return SIZE_TO_PTR(index + 1);
 }
 
 struct Table {
@@ -171,32 +171,16 @@ Table *table_new_internal(const char *first_header, ...) {
         return TAKE_PTR(t);
 }
 
-static TableData *table_data_unref(TableData *d) {
-        if (!d)
-                return NULL;
-
-        assert(d->n_ref > 0);
-        d->n_ref--;
-
-        if (d->n_ref > 0)
-                return NULL;
+static TableData *table_data_free(TableData *d) {
+        assert(d);
 
         free(d->formatted);
         return mfree(d);
 }
 
+DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(TableData, table_data, table_data_free);
 DEFINE_TRIVIAL_CLEANUP_FUNC(TableData*, table_data_unref);
 
-static TableData *table_data_ref(TableData *d) {
-        if (!d)
-                return NULL;
-
-        assert(d->n_ref > 0);
-        d->n_ref++;
-
-        return d;
-}
-
 Table *table_unref(Table *t) {
         size_t i;
 
@@ -572,7 +556,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         break;
 
                 case TABLE_BOOLEAN:
-                        buffer.b = !!va_arg(ap, int);
+                        buffer.b = va_arg(ap, int);
                         data = &buffer.b;
                         break;
 
@@ -704,32 +688,16 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                         return 0;
 
                 case TABLE_TIMESTAMP:
-                        if (a->timestamp < b->timestamp)
-                                return -1;
-                        if (a->timestamp > b->timestamp)
-                                return 1;
-                        return 0;
+                        return CMP(a->timestamp, b->timestamp);
 
                 case TABLE_TIMESPAN:
-                        if (a->timespan < b->timespan)
-                                return -1;
-                        if (a->timespan > b->timespan)
-                                return 1;
-                        return 0;
+                        return CMP(a->timespan, b->timespan);
 
                 case TABLE_SIZE:
-                        if (a->size < b->size)
-                                return -1;
-                        if (a->size > b->size)
-                                return 1;
-                        return 0;
+                        return CMP(a->size, b->size);
 
                 case TABLE_UINT32:
-                        if (a->uint32 < b->uint32)
-                                return -1;
-                        if (a->uint32 > b->uint32)
-                                return 1;
-                        return 0;
+                        return CMP(a->uint32, b->uint32);
 
                 default:
                         ;
@@ -737,17 +705,10 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
         }
 
         /* Generic fallback using the orginal order in which the cells where added. */
-        if (index_a < index_b)
-                return -1;
-        if (index_a > index_b)
-                return 1;
-
-        return 0;
+        return CMP(index_a, index_b);
 }
 
-static int table_data_compare(const void *x, const void *y, void *userdata) {
-        const size_t *a = x, *b = y;
-        Table *t = userdata;
+static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
         size_t i;
         int r;
 
@@ -775,12 +736,7 @@ static int table_data_compare(const void *x, const void *y, void *userdata) {
         }
 
         /* Order identical lines by the order there were originally added in */
-        if (*a < *b)
-                return -1;
-        if (*a > *b)
-                return 1;
-
-        return 0;
+        return CMP(*a, *b);
 }
 
 static const char *table_data_format(TableData *d) {
@@ -882,8 +838,8 @@ static int table_data_requested_width(TableData *d, size_t *ret) {
         return 0;
 }
 
-static char *align_string_mem(const char *str, size_t old_length, size_t new_length, unsigned percent) {
-        size_t w = 0, space, lspace;
+static char *align_string_mem(const char *str, size_t new_length, unsigned percent) {
+        size_t w = 0, space, lspace, old_length;
         const char *p;
         char *ret;
         size_t i;
@@ -893,8 +849,7 @@ static char *align_string_mem(const char *str, size_t old_length, size_t new_len
         assert(str);
         assert(percent <= 100);
 
-        if (old_length == (size_t) -1)
-                old_length = strlen(str);
+        old_length = strlen(str);
 
         /* Determine current width on screen */
         p = str;
@@ -961,7 +916,7 @@ int table_print(Table *t, FILE *f) {
                 for (i = 0; i < n_rows; i++)
                         sorted[i] = i * t->n_columns;
 
-                qsort_r_safe(sorted, n_rows, sizeof(size_t), table_data_compare, t);
+                typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
         }
 
         if (t->display_map)
@@ -1135,14 +1090,12 @@ int table_print(Table *t, FILE *f) {
                                 assert(weight_sum >= column_weight[j]);
                                 weight_sum -= column_weight[j];
 
-                                if (restart)
+                                if (restart && !finalize)
                                         break;
                         }
 
-                        if (finalize) {
-                                assert(!restart);
+                        if (finalize)
                                 break;
-                        }
 
                         if (!restart)
                                 finalize = true;
@@ -1174,7 +1127,7 @@ int table_print(Table *t, FILE *f) {
                         if (l > width[j]) {
                                 /* Field is wider than allocated space. Let's ellipsize */
 
-                                buffer = ellipsize_mem(field, (size_t) -1, width[j], d->ellipsize_percent);
+                                buffer = ellipsize(field, width[j], d->ellipsize_percent);
                                 if (!buffer)
                                         return -ENOMEM;
 
@@ -1183,7 +1136,7 @@ int table_print(Table *t, FILE *f) {
                         } else if (l < width[j]) {
                                 /* Field is shorter than allocated space. Let's align with spaces */
 
-                                buffer = align_string_mem(field, (size_t) -1, width[j], d->align_percent);
+                                buffer = align_string_mem(field, width[j], d->align_percent);
                                 if (!buffer)
                                         return -ENOMEM;