From: Karel Zak Date: Mon, 23 Oct 2023 12:35:21 +0000 (+0200) Subject: libsmartcols: support \x?? for data by samples/fromfile.c X-Git-Tag: v2.40-rc1~182 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=301d02816f070ad4c66f7b8fb789d5d5e121ab9b;p=thirdparty%2Futil-linux.git libsmartcols: support \x?? for data by samples/fromfile.c 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 --- diff --git a/libsmartcols/samples/fromfile.c b/libsmartcols/samples/fromfile.c index 0fdc9295e8..5c418758a6 100644 --- a/libsmartcols/samples/fromfile.c +++ b/libsmartcols/samples/fromfile.c @@ -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"); } diff --git a/tests/ts/libsmartcols/files/data-string-nl b/tests/ts/libsmartcols/files/data-string-nl index 7822e57bcc..ef16a72410 100644 --- a/tests/ts/libsmartcols/files/data-string-nl +++ b/tests/ts/libsmartcols/files/data-string-nl @@ -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