]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: cleanup scols_table_set_symbols() API
authorKarel Zak <kzak@redhat.com>
Fri, 23 Sep 2016 12:20:24 +0000 (14:20 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 23 Sep 2016 12:20:24 +0000 (14:20 +0200)
Change behavior:
  * scols_table_set_symbols(tb, NULL) remove reference to the current symbols setting
    and does not set default symbols at all

Add new functions:
  * scols_table_get_symbols()
  * scols_table_set_default_symbols()

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 324f6e15fb43bad3d922bc103cc1ad2c2630a26f..ccda2019630e14ba14ab138f8a057ed4e41769b5 100644 (file)
@@ -116,6 +116,7 @@ scols_table_get_line_separator
 scols_table_get_ncols
 scols_table_get_nlines
 scols_table_get_stream
+scols_table_get_symbols
 scols_table_get_termforce
 scols_table_get_termwidth
 scols_table_get_title
@@ -137,6 +138,7 @@ scols_table_remove_columns
 scols_table_remove_line
 scols_table_remove_lines
 scols_table_set_column_separator
+scols_table_set_default_symbols
 scols_table_set_line_separator
 scols_table_set_name
 scols_table_set_stream
index de2f271cfec9ba06c24e52b35182e2fc6b7291ae..02c691160b48c1d700d23603588933f319a58f03 100644 (file)
@@ -239,6 +239,8 @@ extern struct libscols_line *scols_table_new_line(struct libscols_table *tb, str
 extern struct libscols_line *scols_table_get_line(struct libscols_table *tb, size_t n);
 extern struct libscols_table *scols_copy_table(struct libscols_table *tb);
 extern int scols_table_set_symbols(struct libscols_table *tb, struct libscols_symbols *sy);
+extern int scols_table_set_default_symbols(struct libscols_table *tb);
+extern struct libscols_symbols *scols_table_get_symbols(const struct libscols_table *tb);
 
 extern int scols_table_set_stream(struct libscols_table *tb, FILE *stream);
 extern FILE *scols_table_get_stream(const struct libscols_table *tb);
index d599a5030daa5d99f9ceed4be8423abe6d62a2d5..62698bac608a8cdef835ee65c4bdaca0795aa0cb 100644 (file)
@@ -141,8 +141,10 @@ SMARTCOLS_2.29 {
 global:
        scols_column_is_wrapnl;
        scols_symbols_set_cell_padding;
+       scols_table_get_symbols;
        scols_table_get_termforce;
        scols_table_get_termwidth;
+       scols_table_set_default_symbols;
        scols_table_set_termforce;
        scols_table_set_termwidth;
 } SMARTCOLS_2.28;
index ca6ea1be7c40cf18055fa78c6751101809225fcc..8e8bd768ef07f07f866942f74b0e29277d50aff1 100644 (file)
@@ -161,6 +161,7 @@ struct libscols_table {
                        padding_debug   :1,     /* output visible padding chars */
                        maxout          :1,     /* maximize output */
                        header_printed  :1,     /* header already printed */
+                       priv_symbols    :1,     /* default private symbols */
                        no_headings     :1,     /* don't print header */
                        no_linesep      :1,     /* don't print line separator */
                        no_wrap         :1;     /* never wrap lines */
index fcb53b64aa5b1f7c4756ae73372db16098170862..a433e42b4bc3999de6af90e2a50f4c3bbc685a7e 100644 (file)
@@ -666,15 +666,63 @@ err:
        return NULL;
 }
 
+/**
+ * scols_table_set_default_symbols:
+ * @tb: table
+ *
+ * The library check the current environment to select ASCII or UTF8 symbols.
+ * This default behavior could be controlled by scols_table_enable_ascii().
+ *
+ * Use scols_table_set_symbols() to unset symbols or use your own setting.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_table_set_default_symbols(struct libscols_table *tb)
+{
+       struct libscols_symbols *sy;
+       int rc;
+
+       if (!tb)
+               return -EINVAL;
+
+       DBG(TAB, ul_debugobj(tb, "setting default symbols"));
+
+       sy = scols_new_symbols();
+       if (!sy)
+               return -ENOMEM;
+
+#if defined(HAVE_WIDECHAR)
+       if (!scols_table_is_ascii(tb) &&
+           !strcmp(nl_langinfo(CODESET), "UTF-8")) {
+               scols_symbols_set_branch(sy, UTF_VR UTF_H);
+               scols_symbols_set_vertical(sy, UTF_V " ");
+               scols_symbols_set_right(sy, UTF_UR UTF_H);
+       } else
+#endif
+       {
+               scols_symbols_set_branch(sy, "|-");
+               scols_symbols_set_vertical(sy, "| ");
+               scols_symbols_set_right(sy, "`-");
+       }
+       scols_symbols_set_title_padding(sy, " ");
+       scols_symbols_set_cell_padding(sy, " ");
+
+       rc = scols_table_set_symbols(tb, sy);
+       scols_unref_symbols(sy);
+       return rc;
+}
+
+
 /**
  * scols_table_set_symbols:
  * @tb: table
  * @sy: symbols or NULL
  *
  * Add a reference to @sy from the table. The symbols are used by library to
- * draw tree output. If no symbols are specified then library checks the
- * current environment to select ASCII or UTF8 symbols. This default behavior
- * could be controlled by scols_table_enable_ascii().
+ * draw tree output. If no symbols are used for the table then library creates
+ * default temporary symbols to draw output by scols_table_set_default_symbols().
+ *
+ * If @sy is NULL then remove reference from the currenly uses symbols.
  *
  * Returns: 0, a negative value in case of an error.
  */
@@ -684,37 +732,33 @@ int scols_table_set_symbols(struct libscols_table *tb,
        if (!tb)
                return -EINVAL;
 
-       DBG(TAB, ul_debugobj(tb, "setting alternative symbols %p", sy));
-
-       if (tb->symbols)                                /* unref old */
+       /* remove old */
+       if (tb->symbols) {
+               DBG(TAB, ul_debugobj(tb, "remove symbols %p refrence", tb->symbols));
                scols_unref_symbols(tb->symbols);
+               tb->symbols = NULL;
+       }
+
+       /* set new */
        if (sy) {                                       /* ref user defined */
+               DBG(TAB, ul_debugobj(tb, "set symbols so %p", sy));
                tb->symbols = sy;
                scols_ref_symbols(sy);
-       } else {                                        /* default symbols */
-               tb->symbols = scols_new_symbols();
-               if (!tb->symbols)
-                       return -ENOMEM;
-#if defined(HAVE_WIDECHAR)
-               if (!scols_table_is_ascii(tb) &&
-                   !strcmp(nl_langinfo(CODESET), "UTF-8")) {
-                       scols_symbols_set_branch(tb->symbols, UTF_VR UTF_H);
-                       scols_symbols_set_vertical(tb->symbols, UTF_V " ");
-                       scols_symbols_set_right(tb->symbols, UTF_UR UTF_H);
-               } else
-#endif
-               {
-                       scols_symbols_set_branch(tb->symbols, "|-");
-                       scols_symbols_set_vertical(tb->symbols, "| ");
-                       scols_symbols_set_right(tb->symbols, "`-");
-               }
-               scols_symbols_set_title_padding(tb->symbols, " ");
-               scols_symbols_set_cell_padding(tb->symbols, " ");
        }
-
        return 0;
 }
 
+/**
+ * scols_table_get_symbols:
+ * @tb: table
+ *
+ * Returns: pointer to symbols table.
+ */
+struct libscols_symbols *scols_table_get_symbols(const struct libscols_table *tb)
+{
+       return tb->symbols;
+}
+
 /**
  * scols_table_enable_nolinesep
  * @tb: table
index 7612682cd20fe9c0c3ffabf8567ddf960d13b2c9..5771111b3f84970be844f73bdc5211bf61f3d609 100644 (file)
@@ -1339,6 +1339,19 @@ static size_t strlen_line(struct libscols_line *ln)
        return sz;
 }
 
+static void cleanup_printing(struct libscols_table *tb, struct libscols_buffer *buf)
+{
+       if (!tb)
+               return;
+
+       free_buffer(buf);
+
+       if (tb->priv_symbols) {
+               scols_table_set_symbols(tb, NULL);
+               tb->priv_symbols = 0;
+       }
+}
+
 static int initialize_printing(struct libscols_table *tb, struct libscols_buffer **buf)
 {
        size_t bufsz, extra_bufsz = 0;
@@ -1348,8 +1361,11 @@ static int initialize_printing(struct libscols_table *tb, struct libscols_buffer
 
        DBG(TAB, ul_debugobj(tb, "initialize printing"));
 
-       if (!tb->symbols)
-               scols_table_set_symbols(tb, NULL);      /* use default */
+       if (!tb->symbols) {
+               scols_table_set_default_symbols(tb);
+               tb->priv_symbols = 1;
+       } else
+               tb->priv_symbols = 0;
 
        if (tb->format == SCOLS_FMT_HUMAN)
                tb->is_term = tb->termforce == SCOLS_TERMFORCE_NEVER  ? 0 :
@@ -1414,8 +1430,10 @@ static int initialize_printing(struct libscols_table *tb, struct libscols_buffer
        }
 
        *buf = new_buffer(bufsz + 1);   /* data + space for \0 */
-       if (!*buf)
-               return -ENOMEM;
+       if (!*buf) {
+               rc = -ENOMEM;
+               goto err;
+       }
 
        if (tb->format == SCOLS_FMT_HUMAN) {
                rc = recount_widths(tb, *buf);
@@ -1425,7 +1443,7 @@ static int initialize_printing(struct libscols_table *tb, struct libscols_buffer
 
        return 0;
 err:
-       free_buffer(*buf);
+       cleanup_printing(tb, *buf);
        return rc;
 }
 
@@ -1436,7 +1454,7 @@ err:
  * @end: last printed line or NULL to print all from start.
  *
  * If the start is the first line in the table than prints table header too.
- * The header is printed only once.
+ * The header is printed only once. This does not work for trees.
  *
  * Returns: 0, a negative value in case of an error.
  */
@@ -1472,7 +1490,7 @@ int scols_table_print_range(      struct libscols_table *tb,
 
        rc = print_range(tb, buf, &itr, end);
 done:
-       free_buffer(buf);
+       cleanup_printing(tb, buf);
        return rc;
 }
 
@@ -1564,7 +1582,7 @@ static int __scols_print_table(struct libscols_table *tb, int *is_empty)
 
        fput_table_close(tb);
 done:
-       free_buffer(buf);
+       cleanup_printing(tb, buf);
        return rc;
 }