]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/table-types.c
tableprinter: update of iterator macro; bugfix in table_set_col_opt_size
[libucw.git] / ucw / table-types.c
index c5aa96fa357e07d14a6c97e0bb9f0b0d27e09099..ab2e5b696ab9f0542b72924f1d4159379d0f7cff 100644 (file)
@@ -8,50 +8,82 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
+static const char *unit_suffix[] = {
+  [UNIT_BYTE] = "",
+  [UNIT_KILOBYTE] = "KB",
+  [UNIT_MEGABYTE] = "MB",
+  [UNIT_GIGABYTE] = "GB",
+  [UNIT_TERABYTE] = "TB"
+};
+
+static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
 {
-  fprintf(stdout, "col_copy_idx: %d, col_arg: %s\n", col_copy_idx, col_arg);
-  fflush(stdout);
-
   int col_type_idx = tbl->column_order[col_copy_idx].idx;
-  if(tbl->columns[col_type_idx].type == COL_TYPE_SIZE) {
-    if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
-      tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
-    } else if(strcasecmp(col_arg, "kb") == 0) {
-      tbl->column_order[col_copy_idx].output_type = UNIT_KILOBYTE;
-    } else if(strcasecmp(col_arg, "mb") == 0) {
-      tbl->column_order[col_copy_idx].output_type = UNIT_MEGABYTE;
-    } else if(strcasecmp(col_arg, "gb") == 0) {
-      tbl->column_order[col_copy_idx].output_type = UNIT_GIGABYTE;
-    } else if(strcasecmp(col_arg, "tb") == 0) {
-      tbl->column_order[col_copy_idx].output_type = UNIT_TERABYTE;
-    } else {
-      *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
-      return true;
-    }
+  if(tbl->columns[col_type_idx].type != COL_TYPE_SIZE) {
+    *err = NULL;
+    return false;
+  }
+
+  if(col_arg == NULL) {
     *err = NULL;
     return true;
   }
 
-  if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
-    fprintf(stdout, "setting timestamp format, col_arg: '%s'\n", col_arg);
-    fflush(stdout);
-    if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
-      tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
-    } else if(strcasecmp(col_arg, "datetime") == 0) {
-      tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
-    } else {
-      *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
-      return true;
+  if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
+    tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
+  }
+
+  tbl->column_order[col_copy_idx].output_type = CELL_OUT_UNINITIALIZED;
+  for(uint i = 0; i < ARRAY_SIZE(unit_suffix); i++) {
+    if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
+      tbl->column_order[col_copy_idx].output_type = i;
     }
+  }
+
+  if(tbl->column_order[col_copy_idx].output_type == CELL_OUT_UNINITIALIZED) {
+    *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx);
+    return true;
+  }
+
+  *err = NULL;
+  return true;
+}
+
+struct table_user_type table_type_size = {
+  .set_col_instance_option = table_set_col_opt_size,
+  .type = COL_TYPE_SIZE,
+};
+
+static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
+{
+  int col_type_idx = tbl->column_order[col_copy_idx].idx;
+  if(tbl->columns[col_type_idx].type != COL_TYPE_TIMESTAMP) {
+    *err = NULL;
+    return false;
+  }
+
+  if(col_arg == NULL) {
     *err = NULL;
     return true;
   }
 
-  *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
-  return false;
+  if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
+    tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
+  } else if(strcasecmp(col_arg, "datetime") == 0) {
+    tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
+  } else {
+    *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
+    return true;
+  }
+
+  *err = NULL;
+  return true;
 }
 
+struct table_user_type table_type_timestamp = {
+  .set_col_instance_option = table_set_col_opt_timestamp,
+  .type = COL_TYPE_TIMESTAMP,
+};
 
 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
 {
@@ -69,37 +101,25 @@ void table_col_size(struct table *tbl, int col, u64 val)
 
   static u64 unit_div[] = {
     [UNIT_BYTE] = (u64) 1,
-    [UNIT_KILOBYTE] = (u64) 1024LU,
-    [UNIT_MEGABYTE] = (u64) (1024LU * 1024LU),
-    [UNIT_GIGABYTE] = (u64) (1024LU * 1024LU * 1024LU),
-    [UNIT_TERABYTE] = (u64) (1024LU * 1024LU * 1024LU * 1024LU)
-  };
-
-  static const char *unit_suffix[] = {
-    [UNIT_BYTE] = "",
-    [UNIT_KILOBYTE] = "KB",
-    [UNIT_MEGABYTE] = "MB",
-    [UNIT_GIGABYTE] = "GB",
-    [UNIT_TERABYTE] = "TB"
+    [UNIT_KILOBYTE] = (u64) 1024LLU,
+    [UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
+    [UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
+    [UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
   };
 
-  int curr_col = tbl->columns[col].first_column;
-  while(curr_col != -1) {
-
+  TBL_COL_ITER(tbl, col, curr_col, curr_col_idx) {
     // FIXME: do some rounding?
     uint out_type = 0;
-    if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
+    if(curr_col->output_type == CELL_OUT_UNINITIALIZED) {
       val = val / unit_div[UNIT_BYTE];
       out_type = 0;
     } else {
-      val = val / unit_div[tbl->column_order[curr_col].output_type];
-      out_type = tbl->column_order[curr_col].output_type;
+      val = val / unit_div[curr_col->output_type];
+      out_type = curr_col->output_type;
     }
 
-    tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
-    curr_col = tbl->column_order[curr_col].next_column;
+    curr_col->cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
   }
-
 }
 
 #define FORMAT_TIME_SIZE 20    // Minimum buffer size
@@ -119,10 +139,8 @@ void table_col_timestamp(struct table *tbl, int col, u64 val)
 
   time_t tmp_time = (time_t)val;
   struct tm t = *gmtime(&tmp_time);
-
-  int curr_col = tbl->columns[col].first_column;
-  while(curr_col != -1) {
-    switch (tbl->column_order[curr_col].output_type) {
+  TBL_COL_ITER(tbl, col, curr_col, curr_col_idx) {
+    switch (curr_col->output_type) {
     case TIMESTAMP_EPOCH:
     case CELL_OUT_UNINITIALIZED:
       sprintf(formatted_time_buf, "%lu", val);
@@ -135,9 +153,6 @@ void table_col_timestamp(struct table *tbl, int col, u64 val)
       break;
     }
 
-    //table_col_printf(tbl, col, "%s", formatted_time_buf);
-    tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
-    curr_col = tbl->column_order[curr_col].next_column;
+    curr_col->cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
   }
 }
-