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>
This commit is contained in:
Karel Zak 2016-09-23 14:20:24 +02:00
parent 302419e8da
commit 63168cf9b5
6 changed files with 105 additions and 36 deletions

View 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

View 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);

View 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;

View 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 */

View 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);
if (sy) { /* ref user defined */
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, " ");
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);
}
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

View 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;
}