From 4baab7dfd36523ede61159c02744b5cf78928da0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 14 Apr 2014 13:44:23 +0200 Subject: libsmartcols: improve line and cell separators Signed-off-by: Karel Zak --- libsmartcols/src/libsmartcols.h.in | 4 ++-- libsmartcols/src/table.c | 30 ++++++++++++++++++------------ libsmartcols/src/test.c | 5 ++--- 3 files changed, 22 insertions(+), 17 deletions(-) (limited to 'libsmartcols/src') diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 1307013a1..9468226f2 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -174,8 +174,8 @@ extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable); extern int scols_table_enable_export(struct libscols_table *tb, int enable); extern int scols_table_enable_maxout(struct libscols_table *tb, int enable); -extern int scols_table_set_column_separator(struct libscols_table *tb, char *sep); -extern int scols_table_set_line_separator(struct libscols_table *tb, char *sep); +extern int scols_table_set_column_separator(struct libscols_table *tb, const char *sep); +extern int scols_table_set_line_separator(struct libscols_table *tb, const char *sep); extern struct libscols_table *scols_new_table(void); extern void scols_ref_table(struct libscols_table *tb); diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 34b43b508..d4c61eea3 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -898,20 +898,23 @@ int scols_table_is_tree(struct libscols_table *tb) * * Returns: 0, a negative value in case of an error. */ -int scols_table_set_column_separator(struct libscols_table *tb, char *sep) +int scols_table_set_column_separator(struct libscols_table *tb, const char *sep) { + char *p = NULL; + assert (tb); if (!tb) return -EINVAL; - sep = strdup(sep); - if (!sep) - return -ENOMEM; + if (sep) { + p = strdup(sep); + if (!p) + return -ENOMEM; + } free(tb->colsep); - tb->colsep = sep; - + tb->colsep = p; return 0; } @@ -924,20 +927,23 @@ int scols_table_set_column_separator(struct libscols_table *tb, char *sep) * * Returns: 0, a negative value in case of an error. */ -int scols_table_set_line_separator(struct libscols_table *tb, char *sep) +int scols_table_set_line_separator(struct libscols_table *tb, const char *sep) { + char *p = NULL; + assert (tb); if (!tb) return -EINVAL; - sep = strdup(sep); - if (!sep) - return -ENOMEM; + if (sep) { + p = strdup(sep); + if (!p) + return -ENOMEM; + } free(tb->linesep); - tb->linesep = sep; - + tb->linesep = p; return 0; } diff --git a/libsmartcols/src/test.c b/libsmartcols/src/test.c index eaff49e7e..98975b77a 100644 --- a/libsmartcols/src/test.c +++ b/libsmartcols/src/test.c @@ -215,10 +215,9 @@ int main(int argc, char *argv[]) notree = 1; break; case 'c': - scols_table_set_line_separator(tb, ","); - /* a column separator should always take up one cell */ - scols_table_set_column_separator(tb, ":"); + scols_table_set_column_separator(tb, ","); scols_table_enable_raw(tb, 1); + notree = 1; break; case 'C': clonetb = 1; -- cgit v1.2.3-55-g7522