]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add support for zero separated wrap data
authorKarel Zak <kzak@redhat.com>
Wed, 18 Oct 2023 10:28:40 +0000 (12:28 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Oct 2023 19:54:00 +0000 (21:54 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/column.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym

index f9c6a61f2cca8ed9021fd38204b06088ec39e6e0..3fce60035ed0ea86598185e92b5dde883bb9cf12 100644 (file)
@@ -390,6 +390,37 @@ char *scols_wrapnl_nextchunk(const struct libscols_column *cl __attribute__((unu
        return NULL;
 }
 
+/**
+ * scols_wrapzero_nextchunk:
+ * @cl: a pointer to a struct libscols_column instance
+ * @data: string
+ * @userdata: callback private data
+ *
+ * This is built-in function for scols_column_set_wrapfunc(). This function
+ * walk string separated by \0.
+ *
+ * For example for data "AAA\0BBB\0CCC" the next chunk is "BBB".
+ *
+ * Returns: next chunk
+ *
+ * Since: 2.40
+ */
+char *scols_wrapzero_nextchunk(const struct libscols_column *cl,
+                       char *data,
+                       void *userdata __attribute__((unused)))
+{
+       char *start = NULL;
+       size_t sz = 0;
+
+       if (!data)
+               return NULL;
+       scols_column_get_wrap_data(cl, &start, &sz, NULL, NULL);
+       if (!start || !sz)
+               return NULL;
+       return ul_next_string(data, start + sz);
+}
+
+
 /**
  * scols_wrapnl_chunksize:
  * @cl: a pointer to a struct libscols_column instance
@@ -472,6 +503,10 @@ int scols_column_set_cmpfunc(struct libscols_column *cl,
  * Note that since 2.40 the @wrap_chunksize is unnecessary. The library calculates
  * the size itself.
  *
+ * The wrap functions do not work directly with cell data, but with buffer used
+ * by library to compose output data. The wrap_nextchunk() function can access
+ * additional details about wrap data by scols_column_get_wrap_data().
+ *
  * Returns: 0, a negative value in case of an error.
  *
  * Since: 2.29
@@ -493,6 +528,35 @@ int scols_column_set_wrapfunc(struct libscols_column *cl,
        return 0;
 }
 
+/**
+ * scols_column_get_wrap_data:
+ * @cl: column
+ * @data: return wrap data
+ * @datasiz: return wrap buffer size
+ * @cur: the current pozition in the buffer
+ * @next: the next pozition
+ *
+ * This function returns the current status of wrapping cell data (for multi-line cells).
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_column_get_wrap_data(const struct libscols_column *cl,
+               char **data, size_t *datasiz, char **cur, char **next)
+{
+       if (!cl)
+               return -EINVAL;
+       if (data)
+               *data = cl->wrap_data;
+       if (datasiz)
+               *datasiz = cl->wrap_datasz;
+       if (cur)
+               *cur = cl->wrap_cur;
+       if (next)
+               *next = cl->wrap_next;
+       return 0;
+}
+
+
 /**
  * scols_column_set_safechars:
  * @cl: a pointer to a struct libscols_column instance
@@ -768,7 +832,7 @@ static int scols_column_init_wrap(
        cl->wrap_cell = ce;
        if (data) {
                void *tmp;
-               cl->wrap_datasz = strlen(data) + 1;     /* TODO: use scols_cell_get_datasiz() */
+               cl->wrap_datasz = scols_cell_get_datasiz(ce);
 
                if (cl->wrap_datasz > cl->wrap_datamax) {
                        cl->wrap_datamax = cl->wrap_datasz;
index a0b5255cab2b3238c71e52f3bde3d86572bf1497..d58c4fd1de476bd92ecf62d5a0d4059128dce179 100644 (file)
@@ -216,6 +216,11 @@ extern int scols_column_set_wrapfunc(struct libscols_column *cl,
 extern char *scols_wrapnl_nextchunk(const struct libscols_column *cl, char *data, void *userdata);
 extern size_t scols_wrapnl_chunksize(const struct libscols_column *cl, const char *data, void *userdata);
 
+extern char *scols_wrapzero_nextchunk(const struct libscols_column *cl, char *data, void *userdata);
+
+extern int scols_column_get_wrap_data(const struct libscols_column *cl,
+                char **data, size_t *datasiz, char **cur, char **next);
+
 /* line.c */
 extern struct libscols_line *scols_new_line(void);
 extern void scols_ref_line(struct libscols_line *ln);
index 909139884ea7e49ef9455a715bdfa1df8102ea15..28811691172aaf80b283f5ebbe820d1a5f5d0cae 100644 (file)
@@ -219,4 +219,6 @@ SMARTCOLS_2.40 {
        scols_table_get_cursor;
        scols_cell_refer_memory;
        scols_cell_get_datasiz;
+       scols_wrapzero_nextchunk;
+       scols_column_get_wrap_data;
 } SMARTCOLS_2.39;