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>
This commit is contained in:
parent
deb1c90327
commit
8fcdce8fff
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include "c.h"
|
||||
#include "list.h"
|
||||
#include "strutils.h"
|
||||
#include "color-names.h"
|
||||
#include "debug.h"
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue