]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols remove duplicate code
authorKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 11:59:06 +0000 (13:59 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 12:17:21 +0000 (14:17 +0200)
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>
libsmartcols/src/cell.c
libsmartcols/src/column.c
libsmartcols/src/line.c
libsmartcols/src/smartcolsP.h
libsmartcols/src/symbols.c
libsmartcols/src/table.c

index a9e69c0b8b853cc0b6d973336fe6aa058b1be483..3d613f4473c2046320deba59d7e9f493247e3352 100644 (file)
@@ -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);
 }
 
 /**
index 5e7050192853d2f149e60d2a343261f7ebe3ee9b..a49d3de5c50049e884d28eb7974f12ab9e2c6fd3 100644 (file)
@@ -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);
 }
 
 /**
index 93cd09b9923d2ee7b8a2035bb218cb38f18db810..a0e3a856c78c69cb3e50f68b461dbe20dd651af7 100644 (file)
@@ -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);
 }
 
 /**
index 377ab804518cf71123deef5f9b2ba0b816630361..e29ec7d93ccdcaad07c473080d9a65742ad6e904 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "c.h"
 #include "list.h"
+#include "strutils.h"
 #include "color-names.h"
 #include "debug.h"
 
index f74327c7dcc4a1a9fc3ccb1d63a74dcb1645ac4b..947e32c8328f10dd036b9dbbd9612395d9fff9ec 100644 (file)
@@ -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);
 }
 
 /**
index 251fe216944d566d2d4ff0bfcf1a145477c2f271..809ad8200c9d86f73fee2f2ffd44346e1c0a000a 100644 (file)
@@ -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);
 }
 
 /**