]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: use scols_column_set_properties() in 'fromfile' sample
authorKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 13:58:08 +0000 (14:58 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 21:25:47 +0000 (22:25 +0100)
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 <kzak@redhat.com>
15 files changed:
libsmartcols/samples/fromfile.c
tests/expected/libsmartcols/fromfile-tree-json
tests/ts/libsmartcols/files/col-hidden
tests/ts/libsmartcols/files/col-id
tests/ts/libsmartcols/files/col-name
tests/ts/libsmartcols/files/col-noextremes
tests/ts/libsmartcols/files/col-number
tests/ts/libsmartcols/files/col-parent
tests/ts/libsmartcols/files/col-strict
tests/ts/libsmartcols/files/col-string
tests/ts/libsmartcols/files/col-tree
tests/ts/libsmartcols/files/col-trunc
tests/ts/libsmartcols/files/col-wrap
tests/ts/libsmartcols/files/col-wrapnl
tests/ts/libsmartcols/files/col-wrapzero

index 0875e135016e04570a9e880ca7237c3d0c9355aa..2efac4cf1ab686b2720b0488b15bd3723ee8d1e3 100644 (file)
 #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':
index 3d651838742e19faeb389b70542d668b3de6ec1e..aada9f0b3c4ca5c4d257b05432d58ff901cc19cc 100644 (file)
@@ -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"
             }
index 83182a8eed1847174cdedff2da02fcae4c118564..9bbb15f4238b0bffb23c5c01de86182f711aa8b5 100644 (file)
@@ -1,3 +1 @@
-FOO
-0
-hidden
+name=FOO,hidden
index 0188f42a01fec62ab1eb07d2baef58351c40c308..8a800fcb05d2df8383dd9cb88bfaba1dbc301ce8 100644 (file)
@@ -1,3 +1 @@
-ID
-0
-right
+name=ID,right,type=number,json=number
index 0a98f29cf8fbe8e208f95d6bbaefb0584963f195..4fed26fc3a0b96fb0d4fcb78a9f1b42712f8d976 100644 (file)
@@ -1,3 +1 @@
-NAME
-0
-none
+name=NAME
index 715edce7e7873c1cde50261407904a3cb96426d6..7df2e2f51e124d8bb138b3b9f41ef8738e1ec1f9 100644 (file)
@@ -1,3 +1 @@
-NOEXTREME
-0
-noextremes
+name=NOEXTREME,noextremes
index 34a70e4a4d69a334000576184b368910379bf640..0f880ff7ef56bc8bef916e6b8d376ffa60c310e2 100644 (file)
@@ -1,3 +1 @@
-NUM
-0
-right
+name=NUM,right,type=number,json=number
index 86fe08cedfebf8e4223218bdf119692f1f41a6ff..be25a829d05e62e925ffeec47c8ccc3d5433b064 100644 (file)
@@ -1,3 +1 @@
-PARENT
-0
-right
+name=PARENT,right,type=number
index 62bb96b9a807c6d9f09b26918c46b9099e341026..8381f92ba1e559d0aca5cc31e75a983f2b76509e 100644 (file)
@@ -1,3 +1 @@
-STRICT
-20
-strictwidth,right
+name=STRICT,strictwidth,right,width=20
index 7e2904b9fb1c00c64f2076dae5f418d79092e5b9..e96c37fd60aa6baf03512945463cac13223fca1d 100644 (file)
@@ -1,3 +1 @@
-STRINGS
-0
-none
+name=STRINGS
index 5076880000f10c7757d47b3a28364701f98eef08..6c69ca0476f81b0376183a165d7c7d880f43c3c3 100644 (file)
@@ -1,3 +1 @@
-TREE
-0
-tree
+name=TREE,tree
index 2887b4314bc3f881399fdc551882c36478f17b2d..55c9a70e693ea65d1e4757ab43941b6e7895b132 100644 (file)
@@ -1,3 +1 @@
-TRUNC
-0
-trunc
+name=TRUNC,trunc
index dc4ca340e11774e2d2248e940b9188638474b8b2..c8e622db21604f2e52a5a1daab9f6fde5501d9c8 100644 (file)
@@ -1,3 +1 @@
-WRAP
-0
-wrap
+name=WRAP,wrap
index 0a18fd14605f8d533c6d917474cb657c0dbf2d53..fcbc217a02773746fb27d026360272d39189205f 100644 (file)
@@ -1,3 +1 @@
-WRAPNL
-0
-wrapnl
+name=WRAPNL,wrapnl
index 0849187c2139c824ca7be8c3e3bdfc3bbc9a564c..b1970143298ef683a5099e05d69b5b308af62540 100644 (file)
@@ -1,3 +1 @@
-WRAPZERO
-0
-wrapzero
+name=WRAPZERO,wrapzero