diff options
author | Karel Zak | 2016-09-23 14:20:24 +0200 |
---|---|---|
committer | Karel Zak | 2016-09-23 14:20:24 +0200 |
commit | 63168cf9b5172c3cccd4bd5541d0e186c798bf9b (patch) | |
tree | 59f71cb583c8ff0d8969d6e0f3df0720842f5ddd | |
parent | findmnt: (verify) add docs (diff) | |
download | kernel-qcow2-util-linux-63168cf9b5172c3cccd4bd5541d0e186c798bf9b.tar.gz kernel-qcow2-util-linux-63168cf9b5172c3cccd4bd5541d0e186c798bf9b.tar.xz kernel-qcow2-util-linux-63168cf9b5172c3cccd4bd5541d0e186c798bf9b.zip |
libsmartcols: cleanup scols_table_set_symbols() API
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>
-rw-r--r-- | libsmartcols/docs/libsmartcols-sections.txt | 2 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.h.in | 2 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.sym | 2 | ||||
-rw-r--r-- | libsmartcols/src/smartcolsP.h | 1 | ||||
-rw-r--r-- | libsmartcols/src/table.c | 96 | ||||
-rw-r--r-- | libsmartcols/src/table_print.c | 34 |
6 files changed, 103 insertions, 34 deletions
diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index 324f6e15f..ccda20196 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -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 diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index de2f271cf..02c691160 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index d599a5030..62698bac6 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -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; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index ca6ea1be7..8e8bd768e 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -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 */ diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index fcb53b64a..a433e42b4 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -667,14 +667,62 @@ err: } /** + * 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,38 +732,34 @@ 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 * @enable: 1 or 0 diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 7612682cd..5771111b3 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -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; } |