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
} 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;
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 {
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;
break;
case TABLE_BOOLEAN:
- buffer.b = !!va_arg(ap, int);
+ buffer.b = va_arg(ap, int);
data = &buffer.b;
break;
break;
}
+ va_end(ap);
return 0;
}
if (column == (size_t) -1)
break;
}
+ va_end(ap);
return 0;
}
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:
;
}
/* 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;
}
/* 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) {
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;
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;
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)
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;
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;
} 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;