]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/table.c
Xtype docs: Fixed a typo
[libucw.git] / ucw / table.c
index c406452d48d2ed67aade22cbc706f493d9f64171..1324839a7c371fd59e1097e09e7724faae437b1e 100644 (file)
@@ -49,8 +49,13 @@ struct table *table_init(const struct table_template *tbl_template)
 
   // initialize column_order
   if(tbl_template->column_order) {
-    new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
-    memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
+    int cols_to_output = 0;
+    for(; ; cols_to_output++) {
+      if(tbl_template->column_order[cols_to_output].idx == ~0U) break;
+    }
+
+    new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * cols_to_output);
+    memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * cols_to_output);
     for(uint i = 0; i < new_inst->cols_to_output; i++) {
       new_inst->column_order[i].cell_content = NULL;
       int col_def_idx = new_inst->column_order[i].idx;
@@ -58,7 +63,7 @@ struct table *table_init(const struct table_template *tbl_template)
       new_inst->column_order[i].fmt = tbl_template->columns[col_def_idx].fmt;
     }
 
-    new_inst->cols_to_output = tbl_template->cols_to_output;
+    new_inst->cols_to_output = cols_to_output;
   }
 
   new_inst->col_delimiter = tbl_template->col_delimiter;
@@ -82,7 +87,7 @@ void table_cleanup(struct table *tbl)
 // TODO: test default column order
 static void table_make_default_column_order(struct table *tbl)
 {
-  struct table_col_instance *col_order = alloca(sizeof(struct table_col_instance) * tbl->column_count);
+  struct table_col_instance *col_order = alloca(sizeof(struct table_col_instance) * (tbl->column_count + 1));
   bzero(col_order, sizeof(struct table_col_instance) * tbl->column_count);
 
   for(int i = 0; i < tbl->column_count; i++) {
@@ -90,8 +95,10 @@ static void table_make_default_column_order(struct table *tbl)
     // currently, XTYPE_FMT_DEFAULT is 0, so bzero actually sets it correctly. This makes it more explicit.
     col_order[i].fmt = XTYPE_FMT_DEFAULT;
   }
+  struct table_col_instance tbl_col_order_end = TBL_COL_ORDER_END;
+  col_order[tbl->column_count] = tbl_col_order_end;
 
-  table_set_col_order(tbl, col_order, tbl->column_count);
+  table_set_col_order(tbl, col_order);
 }
 
 void table_start(struct table *tbl, struct fastbuf *out)
@@ -102,10 +109,8 @@ void table_start(struct table *tbl, struct fastbuf *out)
   ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
 
   if(tbl->column_order == NULL) table_make_default_column_order(tbl);
-  else {
-    // update linked lists
-    table_update_ll(tbl);
-  }
+  // update linked lists
+  table_update_ll(tbl);
   if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
 
   mp_save(tbl->pool, &tbl->pool_state);
@@ -171,42 +176,32 @@ static void table_update_ll(struct table *tbl)
   }
 }
 
-void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output)
+void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order)
 {
-  for(uint i = 0; i < cols_to_output; i++) {
-    ASSERT_MSG(col_order[i].idx < (uint) tbl->column_count, "Column %d does not exist; column number should be between 0 and %d(including).", col_order[i].idx, tbl->column_count - 1);
+  uint cols_to_output = 0;
+  for(; ; cols_to_output++) {
+    if(col_order[cols_to_output].idx == ~0U) break;
+    ASSERT_MSG(col_order[cols_to_output].idx < (uint) tbl->column_count,
+               "Column %d does not exist; column number should be between 0 and %d(including).", col_order[cols_to_output].idx, tbl->column_count - 1);
   }
 
   tbl->cols_to_output = cols_to_output;
-  tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
+  tbl->column_order = mp_alloc(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
   memcpy(tbl->column_order, col_order, sizeof(struct table_col_instance) * cols_to_output);
   for(uint i = 0; i < cols_to_output; i++) {
-    int col_def_idx = tbl->column_order[i].idx; // this is given in col_order
+    int col_def_idx = tbl->column_order[i].idx; // this is given in arg @col_order
     tbl->column_order[i].col_def = tbl->columns + col_def_idx;
-    tbl->column_order[i].cell_content = NULL; // cell_content is copied from @col_order, so make sure that it is NULL
+    tbl->column_order[i].cell_content = NULL; // cell_content is copied from arg @col_order, so make sure that it is NULL
     tbl->column_order[i].next_column = -1;
-    // tbl->column_order[i].fmt should be untouched (copied from col_order)
+    // tbl->column_order[i].fmt should be untouched (copied from arg @col_order)
   }
-  table_update_ll(tbl);
 }
 
 bool table_col_is_printed(struct table *tbl, uint col_def_idx)
 {
-  if(tbl->ll_headers[col_def_idx] == -1) return 0;
-
-  return 1;
-}
+  if(tbl->ll_headers[col_def_idx] == -1) return false;
 
-static char * table_parse_col_arg(char *col_def)
-{
-  // FIXME: should be switched to str_sepsplit
-  char * left_br = strchr(col_def, '[');
-  if(left_br == NULL) return NULL;
-  *left_br = 0;
-  left_br++;
-  char *right_br = strchr(left_br, ']');
-  *right_br = 0;
-  return left_br;
+  return true;
 }
 
 const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt)
@@ -216,16 +211,14 @@ const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *
   // Make sure that we do not call table_set_col_opt, which would
   // result in an infinite recursion.
   if(col_def && col_def->set_col_opt) {
-    if(col_def->set_col_opt == table_set_col_opt) {
-      die("table_set_col_opt should not be used as a struct table_column::set_col_opt hook");
-    }
+    ASSERT_MSG(col_def->set_col_opt != table_set_col_opt,"table_set_col_opt should not be used as a struct table_column::set_col_opt hook");
     return col_def->set_col_opt(tbl, col_inst_idx, col_opt);
   }
 
-  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;
+  if(col_def && col_def->type_def) {
+    u32 fmt = 0;
+    const char *tmp_err = xtype_parse_fmt(col_def->type_def, col_opt, &fmt, tbl->pool);
+    if(tmp_err) return mp_printf(tbl->pool, "Invalid column format; xtypes error: '%s'.", tmp_err);
     tbl->column_order[col_inst_idx].fmt = fmt;
     return NULL;
   }
@@ -233,6 +226,41 @@ const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *
   return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_inst_idx);
 }
 
+/**
+ * the input is a null-terminated string that contains: "<col-name>'['<param1>','<param2>\0
+ * i.e., the ']' is missing and is replaced by \0.
+ * the function replace the '[' by \0 and then parses the rest of the string.
+ **/
+static char **table_parse_col_arg2(char *col_def)
+{
+  char * left_br = strchr(col_def, '[');
+  if(left_br == NULL) return NULL;
+
+  *left_br = 0;
+  left_br++;
+
+  char *col_opt = left_br;
+
+  char *next = NULL;
+  char **result = NULL;
+  GARY_INIT(result, 0);
+  for(;;) {
+    next = strchr(col_opt, ',');
+    if(!next) break;
+    if(*next == 0) break;
+    *next = 0;
+    next++;
+    if(*col_opt)
+      *GARY_PUSH(result) = col_opt;
+
+    col_opt = next;
+  }
+  if(*col_opt)
+    *GARY_PUSH(result) = col_opt;
+
+  return result;
+}
+
 /**
  * TODO: This function deliberately leaks memory. When it is called multiple times,
  * previous column orders still remain allocated in the table's memory pool.
@@ -253,8 +281,11 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
   char *tmp_col_order = stk_strdup(col_order_str);
 
   int col_count = 1;
+  bool inside_brackets = false;
   for(int i = 0; col_order_str[i] != 0; i++) {
-    if(col_order_str[i] == ',') {
+    if(col_order_str[i] == '[')  inside_brackets = true;
+    if(col_order_str[i] == ']')  inside_brackets = false;
+    if(!inside_brackets && col_order_str[i] == ',') {
       col_count++;
     }
   }
@@ -265,12 +296,17 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
   int curr_col_inst_idx = 0;
   char *name_start = tmp_col_order;
   while(name_start) {
-    char *next = strchr(name_start, ',');
-    if(next) {
+    char *next = strpbrk(name_start, "[,");
+    if(next && *next == '[') {
+      next = strchr(next, ']');
+      if(!next) return mp_printf(tbl->pool, "Invalid column definition, missing ']'.");
+      *next++ = 0;
+      next = *next == 0 ? NULL : next + 1; // if next points to the last \0 => end the computation
+    } else if(next) {
       *next++ = 0;
     }
 
-    char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
+    char **args = table_parse_col_arg2(name_start); // this sets 0 on the '['
     int col_def_idx = table_get_col_idx(tbl, name_start);
 
     if(col_def_idx == -1) {
@@ -279,18 +315,19 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
     tbl->column_order[curr_col_inst_idx].col_def = tbl->columns + col_def_idx;
     tbl->column_order[curr_col_inst_idx].idx = col_def_idx;
     tbl->column_order[curr_col_inst_idx].fmt = tbl->columns[col_def_idx].fmt;
-    if(arg) {
-      const char *err = NULL;
-      err = table_set_col_opt(tbl, curr_col_inst_idx, arg);
-      if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
-    }
+    if(args) {
+      for(uint i = 0; i < GARY_SIZE(args); i++) {
+        const char *err = NULL;
+        err = table_set_col_opt(tbl, curr_col_inst_idx, args[i]);
+        if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
+      }
+      GARY_FREE(args);
+   }
 
     name_start = next;
     curr_col_inst_idx++;
   }
 
-  table_update_ll(tbl);
-
   return NULL;
 }
 
@@ -468,7 +505,7 @@ const char *table_set_option(struct table *tbl, const char *opt)
 
 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
 {
-  for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
+  for(uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
     const char *rv = table_set_option(tbl, gary_table_opts[i]);
     if(rv != NULL) {
       return rv;
@@ -591,7 +628,8 @@ enum test_table_cols {
   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), TBL_COL_ORDER_END };
 
 static struct table_template test_tbl = {
   TBL_COLUMNS {
@@ -680,8 +718,8 @@ static void test_simple1(struct fastbuf *out)
 
 
   // test table_col_order_fmt
-  struct table_col_instance col_order[] = { TBL_COL(TEST_COL0_STR), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_PRETTY), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_RAW) };
-  table_set_col_order(tbl, col_order, ARRAY_SIZE(col_order));
+  struct table_col_instance col_order[] = { TBL_COL(TEST_COL0_STR), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_PRETTY), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_RAW), TBL_COL_ORDER_END };
+  table_set_col_order(tbl, col_order);
   table_start(tbl, out);
 
   table_col_str(tbl, TEST_COL0_STR, "test");
@@ -701,7 +739,7 @@ enum test_any_table_cols {
   TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
 };
 
-static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY) };
+static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY), TBL_COL_ORDER_END };
 
 static struct table_template test_any_tbl = {
   TBL_COLUMNS {