From: Karel Zak Date: Wed, 23 Mar 2022 10:59:27 +0000 (+0100) Subject: libsmartcols: add scols_column_set_properties() X-Git-Tag: v2.39-rc1~749 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=576f626b3ab9080d1458bfce3108fa9f9ab553d9;p=thirdparty%2Futil-linux.git libsmartcols: add scols_column_set_properties() This function can set all column properties from comma separated string. Signed-off-by: Karel Zak --- diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index aa7bb15475..42a401e161 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -42,6 +42,7 @@ scols_column_set_color scols_column_set_flags scols_column_set_json_type scols_column_set_name +scols_column_set_properties scols_column_set_safechars scols_column_set_whint scols_column_set_wrapfunc diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index d5a00f0739..0283b976f4 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -23,7 +23,7 @@ #include #include "mbsalign.h" - +#include "strutils.h" #include "smartcolsP.h" /** @@ -641,3 +641,96 @@ int scols_column_is_customwrap(const struct libscols_column *cl) && cl->wrap_chunksize && cl->wrap_nextchunk ? 1 : 0; } + +/** + * scols_column_set_properties: + * @cl: a pointer to a struct libscols_column instance + * @opts: options string + * + * Set properties from string, the string is comma seprated list, like + * "trunc,right,json=number", ... + * + * Returns: 0 on success, <0 on error + * + * Since: 2.39 + */ +int scols_column_set_properties(struct libscols_column *cl, const char *opts) +{ + char *str = (char *) opts; + char *name, *value; + size_t namesz, valuesz; + unsigned int flags = 0; + int rc = 0; + + DBG(COL, ul_debugobj(cl, "apply properties '%s'", opts)); + + while (rc == 0 + && !ul_optstr_next(&str, &name, &namesz, &value, &valuesz)) { + + if (strncmp(name, "trunc", namesz) == 0) + flags |= SCOLS_FL_TRUNC; + + else if (strncmp(name, "tree", namesz) == 0) + flags |= SCOLS_FL_TREE; + + else if (strncmp(name, "right", namesz) == 0) + flags |= SCOLS_FL_RIGHT; + + else if (strncmp(name, "strictwidth", namesz) == 0) + flags |= SCOLS_FL_STRICTWIDTH; + + else if (strncmp(name, "noextremes", namesz) == 0) + flags |= SCOLS_FL_STRICTWIDTH; + + else if (strncmp(name, "hidden", namesz) == 0) + flags |= SCOLS_FL_HIDDEN; + + else if (strncmp(name, "wrap", namesz) == 0) + flags |= SCOLS_FL_WRAP; + + else if (value && strncmp(name, "json", namesz) == 0) { + + if (strncmp(value, "string", valuesz) == 0) + rc = scols_column_set_json_type(cl, SCOLS_JSON_STRING); + else if (strncmp(value, "number", valuesz) == 0) + rc = scols_column_set_json_type(cl, SCOLS_JSON_NUMBER); + else if (strncmp(value, "array-string", valuesz) == 0) + rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_STRING); + else if (strncmp(value, "array-number", valuesz) == 0) + rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_NUMBER); + else if (strncmp(value, "boolean", valuesz) == 0) + rc = scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN); + + } else if (value && strncmp(name, "width", namesz) == 0) { + + char *end = NULL; + double x = strtod(value, &end); + if (errno || str == end) + return -EINVAL; + + rc = scols_column_set_whint(cl, x); + + } else if (value && strncmp(name, "color", namesz) == 0) { + + char *x = strndup(value, valuesz); + if (x) { + scols_column_set_color(cl, x); + free(x); + } + + } else if (value && strncmp(name, "name", namesz) == 0) { + + char *x = strndup(value, valuesz); + if (x) { + scols_column_set_name(cl, x); + free(x); + } + } + } + + if (!rc && flags) + rc = scols_column_set_flags(cl, flags); + + return rc; +} + diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 6b64bc81f8..f5820e9c76 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -194,6 +194,8 @@ extern int scols_column_set_name(struct libscols_column *cl, const char *name); extern const char *scols_column_get_name(struct libscols_column *cl); extern const char *scols_column_get_name_as_shellvar(struct libscols_column *cl); +extern int scols_column_set_properties(struct libscols_column *cl, const char *opts); + extern int scols_column_set_cmpfunc(struct libscols_column *cl, int (*cmp)(struct libscols_cell *a, struct libscols_cell *b, void *), @@ -290,6 +292,7 @@ extern const char *scols_table_get_line_separator(const struct libscols_table *t extern size_t scols_table_get_ncols(const struct libscols_table *tb); extern size_t scols_table_get_nlines(const struct libscols_table *tb); extern struct libscols_column *scols_table_get_column(struct libscols_table *tb, size_t n); +struct libscols_column *scols_table_get_column_by_name(struct libscols_table *tb, const char *name); extern int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln); extern int scols_table_remove_line(struct libscols_table *tb, struct libscols_line *ln); extern void scols_table_remove_lines(struct libscols_table *tb); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 309535f7b9..4499908cfa 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -209,3 +209,9 @@ SMARTCOLS_2.38 { scols_table_is_shellvar; scols_table_enable_shellvar; } SMARTCOLS_2.35; + + +SMARTCOLS_2.39 { + scols_column_set_properties; + scols_table_get_column_by_name; +} SMARTCOLS_2.38; diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 30e9194fc5..cab2b455e7 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -425,7 +425,7 @@ struct libscols_column *scols_table_new_column(struct libscols_table *tb, if (!cl) return NULL; - if (scols_column_set_name(cl, name)) + if (name && scols_column_set_name(cl, name)) goto err; scols_column_set_whint(cl, whint); scols_column_set_flags(cl, flags); @@ -607,6 +607,35 @@ struct libscols_column *scols_table_get_column(struct libscols_table *tb, return NULL; } +/** + * scols_table_get_column_ny_name + * @tb: table + * @name: column name + * + * Returns: pointer to column or NULL + * + * Since: 2.39 + */ +struct libscols_column *scols_table_get_column_by_name( + struct libscols_table *tb, const char *name) +{ + struct libscols_iter itr; + struct libscols_column *cl; + + if (!tb || !name) + return NULL; + + scols_reset_iter(&itr, SCOLS_ITER_FORWARD); + while (scols_table_next_column(tb, &itr, &cl) == 0) { + const char *cn = scols_column_get_name(cl); + + if (cn && strcmp(cn, name) == 0) + return cl; + } + return NULL; +} + + /** * scols_table_add_line: * @tb: table