]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add printf api to fill in column data
authorRobin Jarry <robin@jarry.cc>
Thu, 31 Oct 2024 22:16:56 +0000 (23:16 +0100)
committerRobin Jarry <robin@jarry.cc>
Thu, 31 Oct 2024 23:40:37 +0000 (00:40 +0100)
Add new API functions using printf(3) compatible format strings to fill
in column data.

These can be used to avoid intermediate buffers in the calling
application code.

Signed-off-by: Robin Jarry <robin@jarry.cc>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/line.c

index 5b343d01eee0228ec49e296c17dec2009411daca..5f4c736a74a30bad61c6a09dd40f4d2b6811859b 100644 (file)
@@ -120,6 +120,10 @@ scols_line_set_color
 scols_line_set_column_data
 scols_line_set_data
 scols_line_set_userdata
+scols_line_sprintf
+scols_line_sprintf_column
+scols_line_vprintf
+scols_line_vprintf_column
 scols_new_line
 scols_ref_line
 scols_unref_line
index c97651f35023641f5d264edfbd5ad300f46dacde..3947673ba2965cbc74ab627146996dfb90e69b23 100644 (file)
@@ -14,6 +14,7 @@
 extern "C" {
 #endif
 
+#include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -291,10 +292,18 @@ extern struct libscols_cell *scols_line_get_column_cell(
                                        struct libscols_column *cl);
 extern int scols_line_set_data(struct libscols_line *ln, size_t n, const char *data);
 extern int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data);
+extern int scols_line_vprintf(struct libscols_line *ln, size_t n, const char *fmt, va_list ap)
+       __attribute__((format(printf, 3, 0)));
+extern int scols_line_sprintf(struct libscols_line *ln, size_t n, const char *fmt, ...)
+       __attribute__((format(printf, 3, 4)));
 extern int scols_line_is_filled(struct libscols_line *ln, size_t n);
 extern int scols_line_set_column_data(struct libscols_line *ln, struct libscols_column *cl, const char *data);
 extern const char *scols_line_get_column_data(struct libscols_line *ln, struct libscols_column *cl);
 extern int scols_line_refer_column_data(struct libscols_line *ln, struct libscols_column *cl, char *data);
+extern int scols_line_vprintf_column(struct libscols_line *ln, struct libscols_column *cl, const char *fmt, va_list ap)
+       __attribute__((format(printf, 3, 0)));
+extern int scols_line_sprintf_column(struct libscols_line *ln, struct libscols_column *cl, const char *fmt, ...)
+       __attribute__((format(printf, 3, 4)));
 extern struct libscols_line *scols_copy_line(const struct libscols_line *ln);
 
 /* table */
index 41c74552e967d82674a7d4bf9d25ccc4ed453514..e74d928627b2d6340421a8f614ca3985ddc2ba71 100644 (file)
@@ -244,3 +244,10 @@ SMARTCOLS_2.40 {
        scols_column_set_data_type;
        scols_column_get_data_type;
 } SMARTCOLS_2.39;
+
+SMARTCOLS_2.41 {
+       scols_line_vprintf;
+       scols_line_sprintf;
+       scols_line_vprintf_column;
+       scols_line_sprintf_column;
+} SMARTCOLS_2.40;
index 2289db0f9ec4918c21923750ac575495ff570241..ffefcfba018a14fac8342bad856f83b4a4e0e5e9 100644 (file)
@@ -504,6 +504,61 @@ int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data)
        return scols_cell_refer_data(ce, data);
 }
 
+/**
+ * scols_line_vprintf:
+ * @ln: a pointer to a struct libscols_line instance
+ * @n: number of the cell which will refer to @data
+ * @fmt: a printf(3) compatible format string used to generate cell data
+ * @ap: a variable argument list that was initialized with va_start(3)
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.41
+ */
+int scols_line_vprintf(struct libscols_line *ln, size_t n,
+                      const char *fmt, va_list ap)
+{
+       struct libscols_cell *ce = scols_line_get_cell(ln, n);
+       char *data = NULL;
+       int ret;
+
+       if (!ce)
+               return -EINVAL;
+
+       if (vasprintf(&data, fmt, ap) < 0)
+               return errno ? -errno : -ENOMEM;
+
+       ret = scols_cell_refer_data(ce, data);
+       if (ret < 0)
+               free(data);
+
+       return ret;
+}
+
+/**
+ * scols_line_sprintf:
+ * @ln: a pointer to a struct libscols_line instance
+ * @n: number of the cell which will refer to @data
+ * @fmt: an printf(3) compatible format string used to generate cell data
+ * @...: variable argument list
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.41
+ */
+int scols_line_sprintf(struct libscols_line *ln, size_t n,
+                      const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       va_start(ap, fmt);
+       ret = scols_line_vprintf(ln, n, fmt, ap);
+       va_end(ap);
+
+       return ret;
+}
+
 /**
  * scols_line_is_filled:
  * @ln: a pointer to a struct libscols_line instance
@@ -537,6 +592,55 @@ int scols_line_refer_column_data(struct libscols_line *ln,
        return scols_line_refer_data(ln, cl->seqnum, data);
 }
 
+/**
+ * scols_line_vprintf_column:
+ * @ln: a pointer to a struct libscols_line instance
+ * @cl: column, whose data is to be set
+ * @fmt: a printf(3) compatible format string used to generate column data
+ * @ap: a variable argument list that was initialized with va_start(3)
+ *
+ * The same as scols_line_vprintf() but cell is referenced by column object.
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.41
+ */
+int scols_line_vprintf_column(struct libscols_line *ln,
+                             struct libscols_column *cl,
+                             const char *fmt,
+                             va_list ap)
+{
+       return scols_line_vprintf(ln, cl->seqnum, fmt, ap);
+}
+
+
+/**
+ * scols_line_sprintf_column:
+ * @ln: a pointer to a struct libscols_line instance
+ * @cl: column, whose data is to be set
+ * @fmt: a printf(3) compatible format string used to generate column data
+ * @...: variable argument list
+ *
+ * The same as scols_line_sprintf() but cell is referenced by column object.
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.41
+ */
+int scols_line_sprintf_column(struct libscols_line *ln,
+                             struct libscols_column *cl,
+                             const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       va_start(ap, fmt);
+       ret = scols_line_vprintf(ln, cl->seqnum, fmt, ap);
+       va_end(ap);
+
+       return ret;
+}
+
 /**
  * scols_copy_line:
  * @ln: a pointer to a struct libscols_line instance