]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/table.c
xtypes: added first shot on unit parser
[libucw.git] / ucw / table.c
index af805f9906dea4ad1b2b8dec7c931b2f8b6141e6..0c0294e5e9b92188c94a8cf884863fcd9087c90b 100644 (file)
@@ -28,7 +28,7 @@ static struct table *table_make_instance(const struct table_template *tbl_templa
   new_inst->pool = pool;
 
   // initialize column definitions
-  int col_count = 0; // count the number of columns in the struct table
+  uint col_count = 0; // count the number of columns in the struct table
   for(;;) {
     if(tbl_template->columns[col_count].name == NULL &&
        tbl_template->columns[col_count].width == 0 &&
@@ -41,8 +41,11 @@ static struct table *table_make_instance(const struct table_template *tbl_templa
   }
   new_inst->column_count = col_count;
 
-  new_inst->columns = mp_alloc_zero(new_inst->pool, sizeof(struct table_column) * new_inst->column_count);
-  memcpy(new_inst->columns, tbl_template->columns, sizeof(struct table_column) * new_inst->column_count);
+  new_inst->columns = tbl_template->columns;
+  new_inst->ll_headers = mp_alloc(new_inst->pool, sizeof(int) * col_count);
+  for(uint i = 0; i < col_count; i++) {
+    new_inst->ll_headers[i] = -1;
+  }
 
   // initialize column_order
   if(tbl_template->column_order) {
@@ -52,17 +55,17 @@ static struct table *table_make_instance(const struct table_template *tbl_templa
       new_inst->column_order[i].cell_content = NULL;
       int col_idx = new_inst->column_order[i].idx;
       new_inst->column_order[i].col_def = new_inst->columns + col_idx;
-      new_inst->column_order[i].output_type = tbl_template->columns[col_idx].fmt;
+      new_inst->column_order[i].fmt = tbl_template->columns[col_idx].fmt;
     }
 
     new_inst->cols_to_output = tbl_template->cols_to_output;
   }
 
   new_inst->col_delimiter = tbl_template->col_delimiter;
-  new_inst->print_header = 1;
+  new_inst->print_header = true;
   new_inst->out = 0;
   new_inst->last_printed_col = -1;
-  new_inst->row_printing_started = 0;
+  new_inst->row_printing_started = false;
   new_inst->col_out = -1;
   new_inst->formatter = tbl_template->formatter;
   new_inst->data = NULL;
@@ -77,7 +80,7 @@ struct table *table_init(const struct table_template *tbl_template)
     tbl->formatter = &table_fmt_human_readable;
   }
 
-  tbl->print_header = 1; // by default, print header
+  tbl->print_header = true; // by default, print header
   return tbl;
 }
 
@@ -99,7 +102,7 @@ static void table_make_default_column_order(struct table *tbl)
 void table_start(struct table *tbl, struct fastbuf *out)
 {
   tbl->last_printed_col = -1;
-  tbl->row_printing_started = 0;
+  tbl->row_printing_started = false;
   tbl->out = out;
 
   ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
@@ -119,7 +122,7 @@ void table_start(struct table *tbl, struct fastbuf *out)
 void table_end(struct table *tbl)
 {
   tbl->last_printed_col = -1;
-  tbl->row_printing_started = 0;
+  tbl->row_printing_started = false;
 
   mp_restore(tbl->pool, &tbl->pool_state);
 
@@ -128,7 +131,7 @@ void table_end(struct table *tbl)
 
 /*** Configuration ***/
 
-void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
+void table_set_formatter(struct table *tbl, const struct table_formatter *fmt)
 {
   tbl->formatter = fmt;
 }
@@ -159,7 +162,7 @@ static void table_update_ll(struct table *tbl)
   int cols_to_output = tbl->cols_to_output;
 
   for(int i = 0; i < tbl->column_count; i++) {
-    tbl->columns[i].first_column = -1;
+    tbl->ll_headers[i] = -1;
   }
 
   for(int i = 0; i < cols_to_output; i++) {
@@ -168,21 +171,17 @@ static void table_update_ll(struct table *tbl)
   }
 
   for(int i = 0; i < cols_to_output; i++) {
-    int first = tbl->column_order[i].col_def->first_column;
-    tbl->column_order[i].col_def->first_column = i;
-
-    if(first != -1) {
-      tbl->column_order[i].next_column = first;
-    } else {
-      tbl->column_order[i].next_column = -1;
-    }
+    int col_def_idx = tbl->column_order[i].idx;
+    int first = tbl->ll_headers[col_def_idx];
+    tbl->ll_headers[col_def_idx] = i;
+    tbl->column_order[i].next_column = first;
   }
 }
 
 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
 {
   for(int i = 0; i < cols_to_output; i++) {
-    ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d)", col_order[i], tbl->column_count - 1);
+    ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d).", col_order[i], tbl->column_count - 1);
   }
 
   tbl->cols_to_output = cols_to_output;
@@ -192,14 +191,14 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
     tbl->column_order[i].idx = col_idx;
     tbl->column_order[i].col_def = tbl->columns + col_idx;
     tbl->column_order[i].cell_content = NULL;
-    tbl->column_order[i].output_type = tbl->columns[col_idx].fmt;
+    tbl->column_order[i].fmt = tbl->columns[col_idx].fmt;
   }
   table_update_ll(tbl);
 }
 
 bool table_col_is_printed(struct table *tbl, uint col_idx)
 {
-  if(tbl->columns[col_idx].first_column == -1) return 0;
+  if(tbl->ll_headers[col_idx] == -1) return 0;
 
   return 1;
 }
@@ -216,26 +215,22 @@ static char * table_parse_col_arg(char *col_def)
   return left_br;
 }
 
-/**
- * Setting options for basic table types (as defined in table.h)
- **/
-bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err)
+const char *table_set_col_opt(struct table *tbl, uint col_idx, const char *col_opt)
 {
-  struct table_column *col_def = tbl->column_order[col_idx].col_def;
+  const struct table_column *col_def = tbl->column_order[col_idx].col_def;
+  if(col_def && col_def->set_col_opt && col_def->set_col_opt != table_set_col_opt) {
+    return col_def->set_col_opt(tbl, col_idx, col_opt);
+  }
 
-  if(col_def->type_def == COL_TYPE_DOUBLE) {
-    uint precision = 0;
-    const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0);
-    if(tmp_err) {
-      *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s", tmp_err);
-      return false;
-    }
-    tbl->column_order[col_idx].output_type = precision; // FIXME: shift the value of precision
-    return true;
+  if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
+    uint fmt = 0;
+    const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
+    if(tmp_err) return tmp_err;
+    tbl->column_order[col_idx].fmt = fmt;
+    return NULL;
   }
 
-  *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx);
-  return false;
+  return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx);
 }
 
 /**
@@ -245,12 +240,7 @@ bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_a
 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
 {
   if(col_order_str[0] == '*') {
-    int *col_order_int = alloca(sizeof(int) * tbl->column_count);
-    for(int i = 0; i < tbl->column_count; i++) {
-      col_order_int[i] = i;
-    }
-    table_set_col_order(tbl, col_order_int, tbl->column_count);
-
+    table_make_default_column_order(tbl);
     return NULL;
   }
 
@@ -288,11 +278,10 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
     }
     tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
     tbl->column_order[curr_col_idx].idx = col_idx;
-    tbl->column_order[curr_col_idx].cell_content = NULL;
-    tbl->column_order[curr_col_idx].output_type = tbl->columns[col_idx].fmt;
-    if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) {
-      char *err = NULL;
-      tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err);
+    tbl->column_order[curr_col_idx].fmt = tbl->columns[col_idx].fmt;
+    if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_opt) {
+      const char *err = NULL;
+      err = tbl->columns[col_idx].set_col_opt(tbl, curr_col_idx, arg);
       if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
     }
 
@@ -319,12 +308,10 @@ void table_col_generic_format(struct table *tbl, int col, void *value, const str
   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
   ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
   tbl->last_printed_col = col;
-  tbl->row_printing_started = 1;
-  const char *cell_content = NULL;
+  tbl->row_printing_started = true;
   TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
-    enum xtype_fmt fmt = curr_col->output_type;
-    cell_content = expected_type->format(value, fmt, tbl->pool);
-    curr_col->cell_content = cell_content;
+    enum xtype_fmt fmt = curr_col->fmt;
+    curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
   } TBL_COL_ITER_END
 }
 
@@ -332,7 +319,7 @@ void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
 {
   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
   tbl->last_printed_col = col;
-  tbl->row_printing_started = 1;
+  tbl->row_printing_started = true;
   va_list args;
   va_start(args, fmt);
   char *cell_content = mp_vprintf(tbl->pool, fmt, args);
@@ -348,10 +335,7 @@ TABLE_COL_BODY(uintmax, uintmax_t)
 TABLE_COL_BODY(s64, s64)
 TABLE_COL_BODY(u64, u64)
 TABLE_COL_BODY(bool, bool)
-
-void table_col_str(struct table *tbl, int col, const char *val) {
-  table_col_generic_format(tbl, col, (void*)val, &xt_str);
-}
+TABLE_COL_BODY(str, const char *)
 
 void table_reset_row(struct table *tbl)
 {
@@ -360,13 +344,13 @@ void table_reset_row(struct table *tbl)
   }
   mp_restore(tbl->pool, &tbl->pool_state);
   tbl->last_printed_col = -1;
-  tbl->row_printing_started = 0;
+  tbl->row_printing_started = false;
 }
 
 void table_end_row(struct table *tbl)
 {
   ASSERT(tbl->formatter->row_output);
-  if(tbl->row_printing_started == 0) return;
+  if(tbl->row_printing_started == false) return;
   tbl->formatter->row_output(tbl);
   table_reset_row(tbl);
 }
@@ -395,7 +379,7 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
   // Options with no value
   if(value == NULL || (value != NULL && strlen(value) == 0)) {
     if(strcmp(key, "noheader") == 0) {
-      tbl->print_header = 0;
+      tbl->print_header = false;
       return NULL;
     }
   }
@@ -403,12 +387,13 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
   // Options with a value
   if(value) {
     if(strcmp(key, "header") == 0) {
-      if(value[1] != 0)
-        return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
-      uint tmp = value[0] - '0';
-      if(tmp > 1)
+      bool tmp;
+      const char *err = xt_bool.parse(value, &tmp, tbl->pool);
+      if(err)
         return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
+
       tbl->print_header = tmp;
+
       return NULL;
     } else if(strcmp(key, "cols") == 0) {
       return table_set_col_order_by_name(tbl, value);
@@ -420,12 +405,20 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
         return "Invalid argument to output-type option.";
       }
       return NULL;
-    } else if(strcmp(key, "cell-fmt") == 0) {
+    } else if(strcmp(key, "cells") == 0) {
       u32 fmt = 0;
       const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
-      if(err) return mp_printf(tbl->pool, "??? Invalid cell format: '%s'.", err);
+      if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
       for(uint i = 0; i < tbl->cols_to_output; i++) {
-        tbl->column_order[i].output_type = fmt;
+        tbl->column_order[i].fmt = fmt;
+      }
+      return NULL;
+    } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
+      u32 fmt = 0;
+      const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
+      if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
+      for(uint i = 0; i < tbl->cols_to_output; i++) {
+        tbl->column_order[i].fmt = fmt;
       }
       return NULL;
     } else if(strcmp(key, "col-delim") == 0) {
@@ -473,7 +466,7 @@ const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
 static void table_row_human_readable(struct table *tbl)
 {
   for(uint i = 0; i < tbl->cols_to_output; i++) {
-    struct table_column *col_def = tbl->column_order[i].col_def;
+    const struct table_column *col_def = tbl->column_order[i].col_def;
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
@@ -487,7 +480,7 @@ static void table_row_human_readable(struct table *tbl)
 static void table_write_header(struct table *tbl)
 {
   for(uint i = 0; i < tbl->cols_to_output; i++) {
-    struct table_column *col_def = tbl->column_order[i].col_def;
+    const struct table_column *col_def = tbl->column_order[i].col_def;
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
@@ -504,12 +497,12 @@ static void table_start_human_readable(struct table *tbl)
     tbl->col_delimiter = " ";
   }
 
-  if(tbl->print_header != 0) {
+  if(tbl->print_header != false) {
     table_write_header(tbl);
   }
 }
 
-struct table_formatter table_fmt_human_readable = {
+const struct table_formatter table_fmt_human_readable = {
   .row_output = table_row_human_readable,
   .table_start = table_start_human_readable,
 };
@@ -533,7 +526,7 @@ static void table_start_machine_readable(struct table *tbl)
     tbl->col_delimiter = "\t";
   }
 
-  if(tbl->print_header != 0 && tbl->cols_to_output > 0) {
+  if(tbl->print_header != false && tbl->cols_to_output > 0) {
     bputs(tbl->out, tbl->column_order[0].col_def->name);
     for(uint i = 1; i < tbl->cols_to_output; i++) {
       bputs(tbl->out, tbl->col_delimiter);
@@ -543,7 +536,7 @@ static void table_start_machine_readable(struct table *tbl)
   }
 }
 
-struct table_formatter table_fmt_machine_readable = {
+const struct table_formatter table_fmt_machine_readable = {
   .row_output = table_row_machine_readable,
   .table_start = table_start_machine_readable,
 };
@@ -554,7 +547,7 @@ struct table_formatter table_fmt_machine_readable = {
 static void table_row_blockline_output(struct table *tbl)
 {
   for(uint i = 0; i < tbl->cols_to_output; i++) {
-    struct table_column *col_def = tbl->column_order[i].col_def;
+    const struct table_column *col_def = tbl->column_order[i].col_def;
     bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
   }
   bputc(tbl->out, '\n');
@@ -567,7 +560,7 @@ static void table_start_blockline(struct table *tbl)
   }
 }
 
-struct table_formatter table_fmt_blockline = {
+const struct table_formatter table_fmt_blockline = {
   .row_output = table_row_blockline_output,
   .table_start = table_start_blockline
 };
@@ -579,22 +572,22 @@ struct table_formatter table_fmt_blockline = {
 #include <stdio.h>
 
 enum test_table_cols {
-  test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
+  TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
 };
 
-static struct table_col_instance test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
+static struct table_col_instance test_column_order[] = { TBL_COL(TEST_COL3_BOOL), TBL_COL(TEST_COL4_DOUBLE), TBL_COL(TEST_COL2_UINT), TBL_COL(TEST_COL1_INT), TBL_COL(TEST_COL0_STR) };
 
 static struct table_template test_tbl = {
   TBL_COLUMNS {
-    [test_col0_str] = TBL_COL_STR("col0_str", 20),
-    [test_col1_int] = TBL_COL_INT("col1_int", 8),
-    [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
-    [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
-    [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11),
+    [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
+    [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
+    [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
+    [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
+    [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
     TBL_COL_END
   },
   TBL_COL_ORDER(test_column_order),
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
@@ -603,21 +596,21 @@ static struct table_template test_tbl = {
  **/
 static void do_print1(struct table *test_tbl)
 {
-  table_col_str(test_tbl, test_col0_str, "sdsdf");
-  table_col_int(test_tbl, test_col1_int, -10);
-  table_col_int(test_tbl, test_col1_int, 10000);
-  table_col_uint(test_tbl, test_col2_uint, 10);
-  table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
-  table_col_bool(test_tbl, test_col3_bool, true);
-  table_col_double(test_tbl, test_col4_double, 1.5);
-  table_col_printf(test_tbl, test_col4_double, "AAA");
+  table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
+  table_col_int(test_tbl, TEST_COL1_INT, -10);
+  table_col_int(test_tbl, TEST_COL1_INT, 10000);
+  table_col_uint(test_tbl, TEST_COL2_UINT, 10);
+  table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
+  table_col_bool(test_tbl, TEST_COL3_BOOL, true);
+  table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
+  table_col_printf(test_tbl, TEST_COL4_DOUBLE, "AAA");
   table_end_row(test_tbl);
 
-  table_col_str(test_tbl, test_col0_str, "test");
-  table_col_int(test_tbl, test_col1_int, -100);
-  table_col_uint(test_tbl, test_col2_uint, 100);
-  table_col_bool(test_tbl, test_col3_bool, false);
-  table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
+  table_col_str(test_tbl, TEST_COL0_STR, "test");
+  table_col_int(test_tbl, TEST_COL1_INT, -100);
+  table_col_uint(test_tbl, TEST_COL2_UINT, 100);
+  table_col_bool(test_tbl, TEST_COL3_BOOL, false);
+  table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
   table_end_row(test_tbl);
 }
 
@@ -638,11 +631,11 @@ static void test_simple1(struct fastbuf *out)
   table_end(tbl);
 
   // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
-  tbl->print_header = 0;
+  tbl->print_header = false;
   table_start(tbl, out);
   do_print1(tbl);
   table_end(tbl);
-  tbl->print_header = 1;
+  tbl->print_header = true;
 
   table_set_col_order_by_name(tbl, "col3_bool");
   table_start(tbl, out);
@@ -685,7 +678,7 @@ static struct table_template test_any_tbl = {
     TBL_COL_END
   },
   TBL_COL_ORDER(test_any_column_order),
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
@@ -693,8 +686,6 @@ static void test_any_type(struct fastbuf *out)
 {
   struct table *tbl = table_init(&test_any_tbl);
 
-  tbl->columns[TEST_ANY_COL1_ANY].fmt = XTYPE_FMT_PRETTY;
-
   table_start(tbl, out);
 
   table_col_int(tbl, TEST_ANY_COL0_INT, -10);