]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/table.h
xtypes: overflow detection updated
[libucw.git] / ucw / table.h
index 783c802eff2591dbe7e246a91ac4696ee39b0a4d..c1bf73e314184d175b8c597de5f3089f8d728f5f 100644 (file)
@@ -81,8 +81,9 @@ struct table_column {
   uint fmt;                     // [*] default format of the column
   const struct xtype *type_def; // [*] pointer to xtype of this column
 
-  const char * (*set_col_opt)(struct table *tbl, uint col, const char *value);
-       // [*] process table option for a column instance
+  const char * (*set_col_opt)(struct table *tbl, uint col_inst_idx, const char *col_opt);
+       // [*] process table option for a column instance. @col_inst_idx is the index of the column to which the @col_opt is set.
+       // FIXME: Comment on arguments and return value
 };
 
 // FIXME: is it correct to have idx and col_def? idx is sufficient and in fact a duplicity of idx
@@ -105,7 +106,7 @@ struct table_template {
   uint cols_to_output;                      // [*] Number of columns that are printed
   const char *col_delimiter;                // [*] Delimiter that is placed between columns
   // Back-end used for table formatting and its private data
-  const struct table_formatter *formatter; // FIXME: should be const?
+  const struct table_formatter *formatter;
 };
 
 /**
@@ -128,15 +129,13 @@ struct table {
   bool print_header;                   // [*] false indicates that table header should not be printed
 
   struct fastbuf *out;                 // [*] Fastbuffer to which the table is printed
-  int last_printed_col;                        // Index of the last column which was set. -1 indicates start of row.
-                                       // Used for example for appending to the current column.
-  bool row_printing_started;           // Indicates that a row has been started. Duplicates last_printed_col, but harmlessly.
+  bool row_printing_started;           // Indicates that a row has been started.
   struct fbpool fb_col_out;            // Per-cell fastbuf, see table_col_fbstart()
   int col_out;                         // Index of the column that is currently printed using fb_col_out
 
   // Back-end used for table formatting and its private data
   const struct table_formatter *formatter;
-  void *data;
+  void *formatter_data;
 };
 
 /****
@@ -198,6 +197,7 @@ struct table {
 #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
+#define TBL_FMT(_fmt)              .formatter = _fmt
 
 /**
  * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in
@@ -245,7 +245,7 @@ void table_end(struct table *tbl);
  * For each column type, there are functions for filling of cells
  * of the particular type:
  *
- *   * `table_col_`'type'`(table, idx, value)` sets the cell in column `idx`
+ *   * `table_col_`'type'`(table, col_def_idx, value)` sets the cell in column `col_def_idx`
  *     to the `value`
  ***/
 
@@ -311,20 +311,26 @@ void table_reset_row(struct table *tbl);
  ***/
 
 /**
- * Find the index of a column with name @col_name. Returns -1 if there is no such column.
+ * Find the index of a column definition with name @col_name. Returns -1 if there is no such column.
  **/
 int table_get_col_idx(struct table *tbl, const char *col_name);
 
-
 /**
- * Sets a string option to an instance of a columnt type. This is the default version that checks
+ * Sets a string option to an instance of a column type. This is the default version that checks
  * whether the xtype::parse_fmt can be called and calls it. However, there are situation in which
  * the xtype::parse_fmt is not sufficient, e.g., column decoration, post-processing, etc.
  *
  * Each struct table_column has a pointer to a customized version of table_set_col_opt which is
  * called instead of this (default) version of table_set_col_opt
+ *
+ * FIXME: Make table_set_col_opt() a front-end function used by everybody,
+ * which checks if the set_col_opt hook is defined and either calls it or
+ * processes the options in the generic way. Nobody else should call the
+ * hook directly.
+ *     RK: that is the current solution the only confusion can be that
+ *     the hook and this function has the same prototype.
  **/
-const char *table_set_col_opt(struct table *tbl, uint col_idx, const char *col_opt);
+const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt);
 
 /**
  * Returns a comma-and-space-separated list of column names, allocated from table's internal
@@ -333,17 +339,16 @@ const char *table_set_col_opt(struct table *tbl, uint col_idx, const char *col_o
 const char *table_get_col_list(struct table *tbl);
 
 /**
- * Sets the order in which the columns are printed. The @col_order parameter is used until
- * @table_end() or @table_cleanup() is called. The table converts the integers in @col_order into
- * internal representation stored in @column_order. Options to column instances can be set using
- * @table_set_col_opt.
- **/
-void table_set_col_order(struct table *tbl, int *col_order, int col_order_size);
-
-/**
- * Returns true if col_idx will be printed, false otherwise.
+ * Sets the order in which the columns are printed. The columns are specified by struct
+ *
+ * Sets the order in which the columns are printed.
+ * The table converts the integers in @col_order into an internal representation stored
+ * in `column_order`. Options to column instances can be set using @table_set_col_opt().
+ *
+ * @table_col_instance. This allows specification of format. The user should make an array of struct
+ * @table_col_instance and fill the array using the TBL_COL and TBL_COL_FMT.
  **/
-bool table_col_is_printed(struct table *tbl, uint col_idx);
+void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output);
 
 /**
  * Sets the order in which the columns are printed. The specification is a string with comma-separated column
@@ -363,6 +368,14 @@ bool table_col_is_printed(struct table *tbl, uint col_idx);
  **/
 const char *table_set_col_order_by_name(struct table *tbl, const char *col_order);
 
+/**
+ * Returns true if col_idx will be printed, false otherwise.
+ *
+ * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes
+ * columns, but sometimes their instances.
+ **/
+bool table_col_is_printed(struct table *tbl, uint col_def_idx);
+
 /**
  * Sets table formatter. See below for the list of formatters.
  **/
@@ -381,6 +394,8 @@ void table_set_formatter(struct table *tbl, const struct table_formatter *fmt);
  * | `fmt`     | `human`/`machine`/`block`     | set table formatter to one of the built-in formatters
  * | `col-delim`| string                       | set column delimiter
  * | `cells`    | string                        | set column format mode
+ * | `raw`     | 'none'                        | set column format to raw data
+ * | `pretty`  | 'none'                        | set column format to pretty-printing
  * |===================================================================================================
  **/
 const char *table_set_option_value(struct table *tbl, const char *key, const char *value);