]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: support \x?? for data by samples/fromfile.c
authorKarel Zak <kzak@redhat.com>
Mon, 23 Oct 2023 12:35:21 +0000 (14:35 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Oct 2023 19:54:01 +0000 (21:54 +0200)
The test (sample) binary already supports \n in strings to covert it
to the real line-break. It seem more generic to use \x?? (hex) to
support arbitrary byte in the column data.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/samples/fromfile.c
tests/ts/libsmartcols/files/data-string-nl

index 0fdc9295e8574fe12dd6119dc527045934b4b0c4..5c418758a6a10b35901e3d4c156d2ff3ac5dabc4 100644 (file)
@@ -18,6 +18,7 @@
 #include "strutils.h"
 #include "xalloc.h"
 #include "optutils.h"
+#include "mangle.h"
 
 #include "libsmartcols.h"
 
@@ -103,6 +104,7 @@ static struct libscols_column *parse_column(FILE *f)
                                                scols_wrapnl_nextchunk,
                                                NULL);
                                scols_column_set_safechars(cl, "\n");
+
                        }
                        break;
                }
@@ -129,25 +131,35 @@ static int parse_column_data(FILE *f, struct libscols_table *tb, int col)
 {
        size_t len = 0, nlines = 0;
        int i;
-       char *str = NULL;
+       char *str = NULL, *p;
 
        while ((i = getline(&str, &len, f)) != -1) {
-
                struct libscols_line *ln;
-               char *p = strrchr(str, '\n');
-               if (p)
-                       *p = '\0';
-
-               while ((p = strrchr(str, '\\')) && *(p + 1) == 'n') {
-                       *p = '\n';
-                       memmove(p + 1, p + 2, i - (p + 2 - str));
-               }
+               int rc;
 
                ln = scols_table_get_line(tb, nlines++);
                if (!ln)
                        break;
 
-               if (*str && scols_line_set_data(ln, col, str) != 0)
+               p = strrchr(str, '\n');
+               if (p) {
+                       *p = '\0';
+                       len = p - str;
+               }
+               if (!*str)
+                       continue;
+
+               /* convert \x?? to real bytes */
+               if (strstr(str, "\\x")) {
+                       struct libscols_cell *ce = scols_line_get_cell(ln, col);
+                       char *buf = xcalloc(1, ++len);
+
+                       len = unhexmangle_to_buffer(str, buf, len);
+                       if (len)
+                               scols_cell_refer_memory(ce, buf, len);
+               } else
+                       rc = scols_line_set_data(ln, col, str);
+               if (rc)
                        err(EXIT_FAILURE, "failed to add output data");
        }
 
index 7822e57bccc50805f572207e872569dbfca20e7b..ef16a724100e682db0b7282649d557c254983ace 100644 (file)
@@ -1,10 +1,10 @@
 aaa
 bbbbb
-cccc\nCCCC
-dddddddd\nDDDD\nDD
-hello\nbaby
-aaa\nbbb\nccc\nddd
+cccc\x0ACCCC
+dddddddd\x0ADDDD\x0ADD
+hello\x0Ababy
+aaa\x0Abbb\x0Accc\x0Addd
 eee
 fffff
-g\nhhhhh
+g\x0Ahhhhh
 ppppppppp