]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/table.c
Merge remote-tracking branch 'origin/master'
[libucw.git] / ucw / table.c
index 06c182f1e90d95822c9f842992d17ac3b9e28adc..0e3f9ae81ea88d719059707e03e160618cc30036 100644 (file)
@@ -54,7 +54,7 @@ static void table_make_default_column_order(struct table *tbl)
   for(int i = 0; i < tbl->column_count; i++) {
     col_order_int[i] = i;
   }
   for(int i = 0; i < tbl->column_count; i++) {
     col_order_int[i] = i;
   }
-  table_col_order(tbl, col_order_int, tbl->column_count);
+  table_set_col_order(tbl, col_order_int, tbl->column_count);
 }
 
 void table_start(struct table *tbl, struct fastbuf *out)
 }
 
 void table_start(struct table *tbl, struct fastbuf *out)
@@ -117,8 +117,7 @@ const char * table_get_col_list(struct table *tbl)
   return tmp;
 }
 
   return tmp;
 }
 
-// FIXME: Shouldn't this be table_SET_col_order() ?
-void table_col_order(struct table *tbl, int *col_order, int cols_to_output)
+void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
 {
   for(int i = 0; i < cols_to_output; i++) {
     ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d)", col_order[i], tbl->column_count - 1);
 {
   for(int i = 0; i < cols_to_output; i++) {
     ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d)", col_order[i], tbl->column_count - 1);
@@ -132,7 +131,7 @@ void table_col_order(struct table *tbl, int *col_order, int cols_to_output)
  * TODO: This function deliberately leaks memory. When it is called multiple times,
  * previous column orders still remain allocated in the table's memory pool.
  **/
  * TODO: This function deliberately leaks memory. When it is called multiple times,
  * previous column orders still remain allocated in the table's memory pool.
  **/
-const char * table_col_order_by_name(struct table *tbl, const char *col_order_str)
+const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
 {
   if(!col_order_str[0]) {
     tbl->column_order = mp_alloc(tbl->pool, 0);
 {
   if(!col_order_str[0]) {
     tbl->column_order = mp_alloc(tbl->pool, 0);
@@ -232,6 +231,7 @@ TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
+TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
 #undef TABLE_COL
 #undef TABLE_COL_FMT
 #undef TABLE_COL_STR
 #undef TABLE_COL
 #undef TABLE_COL_FMT
 #undef TABLE_COL_STR
@@ -273,6 +273,7 @@ TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
 TABLE_APPEND(str, const char *, COL_TYPE_STR)
 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
 TABLE_APPEND(str, const char *, COL_TYPE_STR)
 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
+TABLE_APPEND(u64, u64, COL_TYPE_U64)
 #undef TABLE_APPEND
 
 void table_append_bool(struct table *tbl, int val)
 #undef TABLE_APPEND
 
 void table_append_bool(struct table *tbl, int val)
@@ -342,9 +343,6 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
   // Options with a value
   if(value) {
     if(strcmp(key, "header") == 0) {
   // Options with a value
   if(value) {
     if(strcmp(key, "header") == 0) {
-      // FIXME: Check syntax of value.
-      //tbl->print_header = strtol(value, NULL, 10); //atoi(value);
-      //if(errno != 0) tbl->print_header
       if(value[1] != 0)
         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
       uint tmp = value[0] - '0';
       if(value[1] != 0)
         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
       uint tmp = value[0] - '0';
@@ -353,7 +351,7 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha
       tbl->print_header = tmp;
       return NULL;
     } else if(strcmp(key, "cols") == 0) {
       tbl->print_header = tmp;
       return NULL;
     } else if(strcmp(key, "cols") == 0) {
-      const char *err = table_col_order_by_name(tbl, value);
+      const char *err = table_set_col_order_by_name(tbl, value);
       if(err != NULL) {
         return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
       }
       if(err != NULL) {
         return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
       }
@@ -411,10 +409,11 @@ static void table_row_human_readable(struct table *tbl)
 {
   for(uint i = 0; i < tbl->cols_to_output; i++) {
     int col_idx = tbl->column_order[i];
 {
   for(uint i = 0; i < tbl->cols_to_output; i++) {
     int col_idx = tbl->column_order[i];
-    int col_width = tbl->columns[col_idx].width;
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
+    int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
+    if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
     bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
   }
   bputc(tbl->out, '\n');
     bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
   }
   bputc(tbl->out, '\n');
@@ -427,7 +426,9 @@ static void table_write_header(struct table *tbl)
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
-    bprintf(tbl->out, "%*s", tbl->columns[col_idx].width, tbl->columns[col_idx].name);
+    int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
+    if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
+    bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
   }
   bputc(tbl->out, '\n');
 }
   }
   bputc(tbl->out, '\n');
 }
@@ -550,45 +551,45 @@ static void test_simple1(struct fastbuf *out)
   table_init(&test_tbl);
 
   // print table with header
   table_init(&test_tbl);
 
   // print table with header
-  table_col_order_by_name(&test_tbl, "col3_bool");
+  table_set_col_order_by_name(&test_tbl, "col3_bool");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   // print the same table as in the previous case without header
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   // print the same table as in the previous case without header
-  table_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
+  table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
-  // this also tests whether there is need to call table_col_order_by_name after table_end was called
+  // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
   test_tbl.print_header = 0;
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
   test_tbl.print_header = 1;
 
   test_tbl.print_header = 0;
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
   test_tbl.print_header = 1;
 
-  table_col_order_by_name(&test_tbl, "col3_bool");
+  table_set_col_order_by_name(&test_tbl, "col3_bool");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
-  table_col_order_by_name(&test_tbl, "col3_bool,col0_str");
+  table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
-  table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
+  table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
-  table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
+  table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
 
-  table_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
+  table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);
   table_start(&test_tbl, out);
   do_print1(&test_tbl);
   table_end(&test_tbl);