summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/table.c
diff options
context:
space:
mode:
authorKarel Zak2016-04-22 13:59:06 +0200
committerKarel Zak2016-04-22 14:17:21 +0200
commit8fcdce8fffeb243184d1f0bf1460a23cd0c598e3 (patch)
tree70afca7890e959f15661606f6673928b8643685a /libsmartcols/src/table.c
parentlibmount: remove duplicate code (diff)
downloadkernel-qcow2-util-linux-8fcdce8fffeb243184d1f0bf1460a23cd0c598e3.tar.gz
kernel-qcow2-util-linux-8fcdce8fffeb243184d1f0bf1460a23cd0c598e3.tar.xz
kernel-qcow2-util-linux-8fcdce8fffeb243184d1f0bf1460a23cd0c598e3.zip
libsmartcols remove duplicate code
For petty long time we have strdup_to_struct_member() macro to avoid duplicate code when strdup() strings in setter functions. Let's use it for libmount. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/table.c')
-rw-r--r--libsmartcols/src/table.c49
1 files changed, 5 insertions, 44 deletions
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
index 251fe2169..809ad8200 100644
--- a/libsmartcols/src/table.c
+++ b/libsmartcols/src/table.c
@@ -100,7 +100,7 @@ void scols_unref_table(struct libscols_table *tb)
/**
* scols_table_set_name:
* @tb: a pointer to a struct libscols_table instance
- * @name: a name
+ * @str: a name
*
* The table name is used for example for JSON top level object name.
*
@@ -108,21 +108,9 @@ void scols_unref_table(struct libscols_table *tb)
*
* Since: 2.27
*/
-int scols_table_set_name(struct libscols_table *tb, const char *name)
+int scols_table_set_name(struct libscols_table *tb, const char *str)
{
- char *p = NULL;
-
- if (!tb)
- return -EINVAL;
-
- if (name) {
- p = strdup(name);
- if (!p)
- return -ENOMEM;
- }
- free(tb->name);
- tb->name = p;
- return 0;
+ return strdup_to_struct_member(tb, name, str);
}
/**
@@ -1016,20 +1004,7 @@ int scols_table_is_tree(struct libscols_table *tb)
*/
int scols_table_set_column_separator(struct libscols_table *tb, const char *sep)
{
- char *p = NULL;
-
- if (!tb)
- return -EINVAL;
- if (sep) {
- p = strdup(sep);
- if (!p)
- return -ENOMEM;
- }
-
- DBG(TAB, ul_debugobj(tb, "new columns separator: %s", sep));
- free(tb->colsep);
- tb->colsep = p;
- return 0;
+ return strdup_to_struct_member(tb, colsep, sep);
}
/**
@@ -1043,21 +1018,7 @@ int scols_table_set_column_separator(struct libscols_table *tb, const char *sep)
*/
int scols_table_set_line_separator(struct libscols_table *tb, const char *sep)
{
- char *p = NULL;
-
- if (!tb)
- return -EINVAL;
-
- if (sep) {
- p = strdup(sep);
- if (!p)
- return -ENOMEM;
- }
-
- DBG(TAB, ul_debugobj(tb, "new lines separator: %s", sep));
- free(tb->linesep);
- tb->linesep = p;
- return 0;
+ return strdup_to_struct_member(tb, linesep, sep);
}
/**