From: Karel Zak Date: Mon, 20 Nov 2023 13:58:08 +0000 (+0100) Subject: tests: use scols_column_set_properties() in 'fromfile' sample X-Git-Tag: v2.40-rc1~151^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=762298be2ca78ddbf3fa02de8c41ab3fcffaca85;p=thirdparty%2Futil-linux.git tests: use scols_column_set_properties() in 'fromfile' sample The library already can read column properties from a column separated string. Let's use it in 'fromfile' sample to avoid duplicate code and to test this function too. Signed-off-by: Karel Zak --- diff --git a/libsmartcols/samples/fromfile.c b/libsmartcols/samples/fromfile.c index 0875e13501..2efac4cf1a 100644 --- a/libsmartcols/samples/fromfile.c +++ b/libsmartcols/samples/fromfile.c @@ -19,118 +19,26 @@ #include "xalloc.h" #include "optutils.h" #include "mangle.h" +#include "path.h" #include "libsmartcols.h" -struct column_flag { - const char *name; - int mask; -}; - -static const struct column_flag flags[] = { - { "trunc", SCOLS_FL_TRUNC }, - { "tree", SCOLS_FL_TREE }, - { "right", SCOLS_FL_RIGHT }, - { "strictwidth",SCOLS_FL_STRICTWIDTH }, - { "noextremes", SCOLS_FL_NOEXTREMES }, - { "hidden", SCOLS_FL_HIDDEN }, - { "wrap", SCOLS_FL_WRAP }, - { "wrapnl", SCOLS_FL_WRAP }, - { "wrapzero", SCOLS_FL_WRAP }, - { "none", 0 } -}; - -static long name_to_flag(const char *name, size_t namesz) +static struct libscols_column *parse_column(const char *path) { - size_t i; + char buf[BUFSIZ]; + struct libscols_column *cl; - for (i = 0; i < ARRAY_SIZE(flags); i++) { - const char *cn = flags[i].name; + if (ul_path_read_buffer(NULL, buf, sizeof(buf), path) < 0) + err(EXIT_FAILURE, "failed to read column: %s", path); - if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) - return flags[i].mask; - } - warnx("unknown flag: %s", name); - return -1; -} - -static int parse_column_flags(char *str) -{ - unsigned long num_flags = 0; - - if (string_to_bitmask(str, &num_flags, name_to_flag)) - err(EXIT_FAILURE, "failed to parse column flags"); - - return num_flags; -} - -static struct libscols_column *parse_column(FILE *f) -{ - size_t len = 0; - char *line = NULL; - int nlines = 0; - - struct libscols_column *cl = NULL; - - while (getline(&line, &len, f) != -1) { - - char *p = strrchr(line, '\n'); - if (p) - *p = '\0'; + cl = scols_new_column(); + if (!cl) + err(EXIT_FAILURE, "failed to allocate column"); - switch (nlines) { - case 0: /* NAME */ - cl = scols_new_column(); - if (!cl) - goto fail; - if (scols_column_set_name(cl, line) != 0) - goto fail; - break; - - case 1: /* WIDTH-HINT */ - { - double whint = strtod_or_err(line, "failed to parse column whint"); - if (scols_column_set_whint(cl, whint)) - goto fail; - break; - } - case 2: /* FLAGS */ - { - int num_flags = parse_column_flags(line); - if (scols_column_set_flags(cl, num_flags)) - goto fail; - if (strcmp(line, "wrapnl") == 0) { - scols_column_set_wrapfunc(cl, - NULL, - scols_wrapnl_nextchunk, - NULL); - scols_column_set_safechars(cl, "\n"); - - } else if (strcmp(line, "wrapzero") == 0) { - scols_column_set_wrapfunc(cl, - NULL, - scols_wrapzero_nextchunk, - NULL); - } - break; - } - case 3: /* COLOR */ - if (scols_column_set_color(cl, line)) - goto fail; - break; - default: - break; - } - - nlines++; - } + if (scols_column_set_properties(cl, buf) != 0) + err(EXIT_FAILURE, "failed to set column properties"); - free(line); return cl; -fail: - free(line); - scols_unref_column(cl); - return NULL; } static int parse_column_data(FILE *f, struct libscols_table *tb, int col) @@ -360,16 +268,11 @@ int main(int argc, char *argv[]) switch(c) { case 'c': /* add column from file */ { - struct libscols_column *cl; - FILE *f = fopen(optarg, "r"); + struct libscols_column *cl = parse_column(optarg); - if (!f) - err(EXIT_FAILURE, "%s: open failed", optarg); - cl = parse_column(f); if (cl && scols_table_add_column(tb, cl)) err(EXIT_FAILURE, "%s: failed to add column", optarg); scols_unref_column(cl); - fclose(f); break; } case 'd': diff --git a/tests/expected/libsmartcols/fromfile-tree-json b/tests/expected/libsmartcols/fromfile-tree-json index 3d65183874..aada9f0b3c 100644 --- a/tests/expected/libsmartcols/fromfile-tree-json +++ b/tests/expected/libsmartcols/fromfile-tree-json @@ -2,56 +2,56 @@ "testtable": [ { "tree": "aaaa", - "id": "1", + "id": 1, "parent": "0", "strings": "qqqqqqqqqqqqqqqqqX", "children": [ { "tree": "bbb", - "id": "2", + "id": 2, "parent": "1", "strings": "dddddddddddddX", "children": [ { "tree": "ee", - "id": "5", + "id": 5, "parent": "2", "strings": "ddddddddddddddddddddddddddX" },{ "tree": "ffff", - "id": "6", + "id": 6, "parent": "2", "strings": "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjX" } ] },{ "tree": "ccccc", - "id": "3", + "id": 3, "parent": "1", "strings": "ffffffffffffffffffffffffffffffffffffffffX", "children": [ { "tree": "gggggg", - "id": "7", + "id": 7, "parent": "3", "strings": "mmmmmmmmmmmmmmmmmmmX", "children": [ { "tree": "hhh", - "id": "8", + "id": 8, "parent": "7", "strings": "lllllllllllllllllllllllllllllllllllllX", "children": [ { "tree": "iiiiii", - "id": "9", + "id": 9, "parent": "8", "strings": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyX" } ] },{ "tree": "jj", - "id": "10", + "id": 10, "parent": "7", "strings": "pppppppppX" } @@ -60,7 +60,7 @@ ] },{ "tree": "dddddd", - "id": "4", + "id": 4, "parent": "1", "strings": "ssssssssssX" } diff --git a/tests/ts/libsmartcols/files/col-hidden b/tests/ts/libsmartcols/files/col-hidden index 83182a8eed..9bbb15f423 100644 --- a/tests/ts/libsmartcols/files/col-hidden +++ b/tests/ts/libsmartcols/files/col-hidden @@ -1,3 +1 @@ -FOO -0 -hidden +name=FOO,hidden diff --git a/tests/ts/libsmartcols/files/col-id b/tests/ts/libsmartcols/files/col-id index 0188f42a01..8a800fcb05 100644 --- a/tests/ts/libsmartcols/files/col-id +++ b/tests/ts/libsmartcols/files/col-id @@ -1,3 +1 @@ -ID -0 -right +name=ID,right,type=number,json=number diff --git a/tests/ts/libsmartcols/files/col-name b/tests/ts/libsmartcols/files/col-name index 0a98f29cf8..4fed26fc3a 100644 --- a/tests/ts/libsmartcols/files/col-name +++ b/tests/ts/libsmartcols/files/col-name @@ -1,3 +1 @@ -NAME -0 -none +name=NAME diff --git a/tests/ts/libsmartcols/files/col-noextremes b/tests/ts/libsmartcols/files/col-noextremes index 715edce7e7..7df2e2f51e 100644 --- a/tests/ts/libsmartcols/files/col-noextremes +++ b/tests/ts/libsmartcols/files/col-noextremes @@ -1,3 +1 @@ -NOEXTREME -0 -noextremes +name=NOEXTREME,noextremes diff --git a/tests/ts/libsmartcols/files/col-number b/tests/ts/libsmartcols/files/col-number index 34a70e4a4d..0f880ff7ef 100644 --- a/tests/ts/libsmartcols/files/col-number +++ b/tests/ts/libsmartcols/files/col-number @@ -1,3 +1 @@ -NUM -0 -right +name=NUM,right,type=number,json=number diff --git a/tests/ts/libsmartcols/files/col-parent b/tests/ts/libsmartcols/files/col-parent index 86fe08cedf..be25a829d0 100644 --- a/tests/ts/libsmartcols/files/col-parent +++ b/tests/ts/libsmartcols/files/col-parent @@ -1,3 +1 @@ -PARENT -0 -right +name=PARENT,right,type=number diff --git a/tests/ts/libsmartcols/files/col-strict b/tests/ts/libsmartcols/files/col-strict index 62bb96b9a8..8381f92ba1 100644 --- a/tests/ts/libsmartcols/files/col-strict +++ b/tests/ts/libsmartcols/files/col-strict @@ -1,3 +1 @@ -STRICT -20 -strictwidth,right +name=STRICT,strictwidth,right,width=20 diff --git a/tests/ts/libsmartcols/files/col-string b/tests/ts/libsmartcols/files/col-string index 7e2904b9fb..e96c37fd60 100644 --- a/tests/ts/libsmartcols/files/col-string +++ b/tests/ts/libsmartcols/files/col-string @@ -1,3 +1 @@ -STRINGS -0 -none +name=STRINGS diff --git a/tests/ts/libsmartcols/files/col-tree b/tests/ts/libsmartcols/files/col-tree index 5076880000..6c69ca0476 100644 --- a/tests/ts/libsmartcols/files/col-tree +++ b/tests/ts/libsmartcols/files/col-tree @@ -1,3 +1 @@ -TREE -0 -tree +name=TREE,tree diff --git a/tests/ts/libsmartcols/files/col-trunc b/tests/ts/libsmartcols/files/col-trunc index 2887b4314b..55c9a70e69 100644 --- a/tests/ts/libsmartcols/files/col-trunc +++ b/tests/ts/libsmartcols/files/col-trunc @@ -1,3 +1 @@ -TRUNC -0 -trunc +name=TRUNC,trunc diff --git a/tests/ts/libsmartcols/files/col-wrap b/tests/ts/libsmartcols/files/col-wrap index dc4ca340e1..c8e622db21 100644 --- a/tests/ts/libsmartcols/files/col-wrap +++ b/tests/ts/libsmartcols/files/col-wrap @@ -1,3 +1 @@ -WRAP -0 -wrap +name=WRAP,wrap diff --git a/tests/ts/libsmartcols/files/col-wrapnl b/tests/ts/libsmartcols/files/col-wrapnl index 0a18fd1460..fcbc217a02 100644 --- a/tests/ts/libsmartcols/files/col-wrapnl +++ b/tests/ts/libsmartcols/files/col-wrapnl @@ -1,3 +1 @@ -WRAPNL -0 -wrapnl +name=WRAPNL,wrapnl diff --git a/tests/ts/libsmartcols/files/col-wrapzero b/tests/ts/libsmartcols/files/col-wrapzero index 0849187c21..b197014329 100644 --- a/tests/ts/libsmartcols/files/col-wrapzero +++ b/tests/ts/libsmartcols/files/col-wrapzero @@ -1,3 +1 @@ -WRAPZERO -0 -wrapzero +name=WRAPZERO,wrapzero