if(tbl_template->column_order) {
int cols_to_output = 0;
for(; ; cols_to_output++) {
- if(tbl_template->column_order[cols_to_output].idx == (uint) ~0) break;
+ 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);
{
uint cols_to_output = 0;
for(; ; cols_to_output++) {
- if(col_order[cols_to_output].idx == (uint) ~0) break;
+ 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)
}
}
bool table_col_is_printed(struct table *tbl, uint col_def_idx)
{
- if(tbl->ll_headers[col_def_idx] == -1) return 0;
+ if(tbl->ll_headers[col_def_idx] == -1) return false;
- return 1;
-}
-
-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)
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;
}
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.
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++;
}
}
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) {
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++;
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;