]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: code cleanup, added options
authorRobert Kessl <kesslr@centrum.cz>
Mon, 21 Jul 2014 10:38:48 +0000 (12:38 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Mon, 21 Jul 2014 10:38:48 +0000 (12:38 +0200)
 - renamed TBL_OUTPUT_* -> TBL_FMT_*
 - added "raw" and "pretty" table option
 - added "cells" table option

ucw/table-test-2.c
ucw/table-test-align.c
ucw/table-test.c
ucw/table.c
ucw/table.h
ucw/xtypes-basic.c

index b31295d1e208968bcab3f5fef3fcc47387e5692a..88fec5e22486c90e79806509ae674ac6d7ae9e02 100644 (file)
@@ -20,7 +20,7 @@ static struct table_template test_tbl = {
     [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
     TBL_COL_END
   },
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
 };
 
 static void do_test(void)
@@ -72,7 +72,7 @@ static struct table_template test_tbl2 = {
     [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
     TBL_COL_END
   },
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
 };
 
 static void do_test2(void)
index eb22f2e24b511919ef908ce732c75ea7d729298b..b1d16a97dfe73172c7217f65e9113301e9f1f21d 100644 (file)
@@ -23,7 +23,7 @@ static struct table_template test_tbl = {
     [test_col4_double] = TBL_COL_DOUBLE_FMT("col4_double", 11 | CELL_ALIGN_LEFT, XTYPE_FMT_DEFAULT),
     TBL_COL_END
   },
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
index f23c176d487facb7d6d5a19915bf37d67a800e58..7b6e581d64417f93d3bd22265b6bb5bded460f50 100644 (file)
@@ -28,7 +28,7 @@ static struct table_template test_tbl = {
     TBL_COL_END
   },
   TBL_COL_ORDER(test_column_order),
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
@@ -43,7 +43,7 @@ static struct table_template test_default_order_tbl = {
     [TEST_DEFAULT_ORDER_COL2_INT] = TBL_COL_INT("col2_int", 9),
     TBL_COL_END
   },
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
index f6d0cba0f9538b789e2cc2774b0a755bb00809aa..4b41ae2089f82907767f3267164f20addc878fe1 100644 (file)
@@ -314,11 +314,9 @@ void table_col_generic_format(struct table *tbl, int col, void *value, const str
   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_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;
+    curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
   } TBL_COL_ITER_END
 }
 
@@ -342,10 +340,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)
 {
@@ -414,7 +409,7 @@ 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);
@@ -422,6 +417,14 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
         tbl->column_order[i].output_type = 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].output_type = fmt;
+      }
+      return NULL;
     } else if(strcmp(key, "col-delim") == 0) {
       char * d = mp_printf(tbl->pool, "%s", value);
       tbl->col_delimiter = d;
@@ -588,7 +591,7 @@ static struct table_template test_tbl = {
     TBL_COL_END
   },
   TBL_COL_ORDER(test_column_order),
-  TBL_OUTPUT_HUMAN_READABLE,
+  TBL_FMT_HUMAN_READABLE,
   TBL_COL_DELIMITER("\t"),
 };
 
@@ -679,7 +682,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"),
 };
 
index 9273138ebee81766f53418604302358c45b8abd5..6b3f3765fdb3ce3e73b5fab88eb94a486c9b1393 100644 (file)
@@ -176,9 +176,9 @@ struct table {
  *  * `TBL_COL_END` ends the column definitions
  *  * `TBL_COL_ORDER` specifies custom ordering of columns in the output
  *  * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters
- *  * `TBL_OUTPUT_HUMAN_READABLE` requests human-readable formatting (this is the default)
- *  * `TBL_OUTPUT_MACHINE_READABLE` requests machine-readable TSV output
- *  * `TBL_OUTPUT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
+ *  * `TBL_FMT__HUMAN_READABLE` requests human-readable formatting (this is the default)
+ *  * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output
+ *  * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
  *
  ***/
 
@@ -219,9 +219,9 @@ struct table {
 #define TBL_COL(_idx) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1 }
 #define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = _fmt, .next_column = -1 }
 
-#define TBL_OUTPUT_HUMAN_READABLE     .formatter = &table_fmt_human_readable
-#define TBL_OUTPUT_BLOCKLINE          .formatter = &table_fmt_blockline
-#define TBL_OUTPUT_MACHINE_READABLE   .formatter = &table_fmt_machine_readable
+#define TBL_FMT_HUMAN_READABLE     .formatter = &table_fmt_human_readable
+#define TBL_FMT_BLOCKLINE          .formatter = &table_fmt_blockline
+#define TBL_FMT_MACHINE_READABLE   .formatter = &table_fmt_machine_readable
 
 /**
  * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in
@@ -272,8 +272,6 @@ void table_end(struct table *tbl);
  *     a custom printf-like format string
  *   * `table_col_`'type'`_name(table, name, value)` refers to the column
  *     by its name instead of its index.
- *   * `table_append_`'type'`(table, value)` appends a value to the most
- *     recently accessed cell.
  ***/
 
 
@@ -401,7 +399,7 @@ void table_set_formatter(struct table *tbl, const struct table_formatter *fmt);
  * | `cols`    | comma-separated column list   | set order of columns
  * | `fmt`     | `human`/`machine`/`block`     | set table formatter to one of the built-in formatters
  * | `col-delim`| string                       | set column delimiter
-*  | `cell-fmt` | string                        | set column format type
+*  | `cells`    | string                        | set column format mode
  * |===================================================================================================
  **/
 const char *table_set_option_value(struct table *tbl, const char *key, const char *value);
index 5cd74adb26e39268dfb9467f473b04ba773e7fbd..2d365078ecdbdb904eefe3cc96678fe9a6589f8d 100644 (file)
@@ -70,19 +70,14 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool *
   errno = 0;
   double result = strtod(str, &endptr);
   if(*endptr != 0 || endptr == str) return "Could not parse double.";
-  if(errno == ERANGE) return "Could not parse double: overflow happend during parsing.";
+  if(errno == ERANGE) return "Could not parse double.";
 
   *((double *) dest) = result;
 
   return NULL;
 }
 
-const struct xtype xt_double = {
-  .size = sizeof(double),
-  .name = "double",
-  .parse = xt_double_parse,
-  .format = xt_double_format,
-};
+XTYPE_NUM_STRUCT(double, double)
 
 /* bool */
 
@@ -127,18 +122,13 @@ static const char *xt_bool_parse(const char *str, void *dest, struct mempool *po
   return "Could not parse bool.";
 }
 
-const struct xtype xt_bool = {
-  .size = sizeof(bool),
-  .name = "bool",
-  .parse = xt_bool_parse,
-  .format = xt_bool_format,
-};
+XTYPE_NUM_STRUCT(bool, bool)
 
 /* str */
 
 static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
 {
-  return mp_strdup(pool, (const char *) src);
+  return mp_strdup(pool, *((const char **) src));
 }
 
 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)
@@ -147,9 +137,4 @@ static const char *xt_str_parse(const char *str, void *dest, struct mempool *poo
   return NULL;
 }
 
-const struct xtype xt_str = {
-  .size = sizeof(char *),
-  .name = "str",
-  .parse = xt_str_parse,
-  .format = xt_str_format,
-};
+XTYPE_NUM_STRUCT(char *, str)