summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2016-04-22 13:59:06 +0200
committerKarel Zak2016-04-22 14:17:21 +0200
commit8fcdce8fffeb243184d1f0bf1460a23cd0c598e3 (patch)
tree70afca7890e959f15661606f6673928b8643685a
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>
-rw-r--r--libsmartcols/src/cell.c41
-rw-r--r--libsmartcols/src/column.c27
-rw-r--r--libsmartcols/src/line.c27
-rw-r--r--libsmartcols/src/smartcolsP.h1
-rw-r--r--libsmartcols/src/symbols.c60
-rw-r--r--libsmartcols/src/table.c49
6 files changed, 33 insertions, 172 deletions
diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c
index a9e69c0b8..3d613f447 100644
--- a/libsmartcols/src/cell.c
+++ b/libsmartcols/src/cell.c
@@ -62,18 +62,7 @@ int scols_reset_cell(struct libscols_cell *ce)
*/
int scols_cell_set_data(struct libscols_cell *ce, const char *str)
{
- char *p = NULL;
-
- if (!ce)
- return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
- free(ce->data);
- ce->data = p;
- return 0;
+ return strdup_to_struct_member(ce, data, str);
}
/**
@@ -169,32 +158,20 @@ int scols_cmpstr_cells(struct libscols_cell *a,
/**
* scols_cell_set_color:
* @ce: a pointer to a struct libscols_cell instance
- * @color: color name or ESC sequence
+ * @co: color name or ESC sequence
*
- * Set the color of @ce to @color.
+ * Set the color of @ce to @co.
*
* Returns: 0, a negative value in case of an error.
*/
-int scols_cell_set_color(struct libscols_cell *ce, const char *color)
+int scols_cell_set_color(struct libscols_cell *ce, const char *co)
{
- char *p = NULL;
-
- if (!ce)
- return -EINVAL;
- if (color) {
- if (isalpha(*color)) {
- color = color_sequence_from_colorname(color);
-
- if (!color)
- return -EINVAL;
- }
- p = strdup(color);
- if (!p)
- return -ENOMEM;
+ if (co && isalpha(*co)) {
+ co = color_sequence_from_colorname(co);
+ if (!co)
+ return -EINVAL;
}
- free(ce->color);
- ce->color = p;
- return 0;
+ return strdup_to_struct_member(ce, color, co);
}
/**
diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c
index 5e7050192..a49d3de5c 100644
--- a/libsmartcols/src/column.c
+++ b/libsmartcols/src/column.c
@@ -196,7 +196,7 @@ struct libscols_cell *scols_column_get_header(struct libscols_column *cl)
/**
* scols_column_set_color:
* @cl: a pointer to a struct libscols_column instance
- * @color: color name or ESC sequence
+ * @co: color name or ESC sequence
*
* The default color for data cells and column header.
*
@@ -208,27 +208,14 @@ struct libscols_cell *scols_column_get_header(struct libscols_column *cl)
*
* Returns: 0, a negative value in case of an error.
*/
-int scols_column_set_color(struct libscols_column *cl, const char *color)
+int scols_column_set_color(struct libscols_column *cl, const char *co)
{
- char *p = NULL;
-
- if (!cl)
- return -EINVAL;
- if (color) {
- if (isalpha(*color)) {
- color = color_sequence_from_colorname(color);
-
- if (!color)
- return -EINVAL;
- }
- p = strdup(color);
- if (!p)
- return -ENOMEM;
+ if (co && isalpha(*co)) {
+ co = color_sequence_from_colorname(co);
+ if (!co)
+ return -EINVAL;
}
-
- free(cl->color);
- cl->color = p;
- return 0;
+ return strdup_to_struct_member(cl, color, co);
}
/**
diff --git a/libsmartcols/src/line.c b/libsmartcols/src/line.c
index 93cd09b99..a0e3a856c 100644
--- a/libsmartcols/src/line.c
+++ b/libsmartcols/src/line.c
@@ -285,31 +285,18 @@ int scols_line_next_child(struct libscols_line *ln,
/**
* scols_line_set_color:
* @ln: a pointer to a struct libscols_line instance
- * @color: color name or ESC sequence
+ * @co: color name or ESC sequence
*
* Returns: 0, a negative value in case of an error.
*/
-int scols_line_set_color(struct libscols_line *ln, const char *color)
+int scols_line_set_color(struct libscols_line *ln, const char *co)
{
- char *p = NULL;
-
- if (!ln)
- return -EINVAL;
- if (color) {
- if (isalnum(*color)) {
- color = color_sequence_from_colorname(color);
-
- if (!color)
- return -EINVAL;
- }
- p = strdup(color);
- if (!p)
- return -ENOMEM;
+ if (co && isalnum(*co)) {
+ co = color_sequence_from_colorname(co);
+ if (!co)
+ return -EINVAL;
}
-
- free(ln->color);
- ln->color = p;
- return 0;
+ return strdup_to_struct_member(ln, color, co);
}
/**
diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h
index 377ab8045..e29ec7d93 100644
--- a/libsmartcols/src/smartcolsP.h
+++ b/libsmartcols/src/smartcolsP.h
@@ -13,6 +13,7 @@
#include "c.h"
#include "list.h"
+#include "strutils.h"
#include "color-names.h"
#include "debug.h"
diff --git a/libsmartcols/src/symbols.c b/libsmartcols/src/symbols.c
index f74327c7d..947e32c83 100644
--- a/libsmartcols/src/symbols.c
+++ b/libsmartcols/src/symbols.c
@@ -76,20 +76,7 @@ void scols_unref_symbols(struct libscols_symbols *sy)
*/
int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str)
{
- char *p = NULL;
-
- assert(sb);
-
- if (!sb)
- return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
- free(sb->branch);
- sb->branch = p;
- return 0;
+ return strdup_to_struct_member(sb, branch, str);
}
/**
@@ -101,20 +88,7 @@ int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str)
*/
int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str)
{
- char *p = NULL;
-
- assert(sb);
-
- if (!sb)
- return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
- free(sb->vert);
- sb->vert = p;
- return 0;
+ return strdup_to_struct_member(sb, vert, str);
}
/**
@@ -126,20 +100,7 @@ int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str)
*/
int scols_symbols_set_right(struct libscols_symbols *sb, const char *str)
{
- char *p = NULL;
-
- assert(sb);
-
- if (!sb)
- return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
- free(sb->right);
- sb->right = p;
- return 0;
+ return strdup_to_struct_member(sb, right, str);
}
/**
@@ -156,20 +117,7 @@ int scols_symbols_set_right(struct libscols_symbols *sb, const char *str)
*/
int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str)
{
- char *p = NULL;
-
- assert(sb);
-
- if (!sb)
- return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
- free(sb->title_padding);
- sb->title_padding = p;
- return 0;
+ return strdup_to_struct_member(sb, title_padding, str);
}
/**
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);
}
/**