#include <ctype.h>
#include "mbsalign.h"
-
+#include "strutils.h"
#include "smartcolsP.h"
/**
&& 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;
+}
+
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 *),
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);
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);
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