]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: move buffer stuff to buffer.c
authorKarel Zak <kzak@redhat.com>
Thu, 25 Oct 2018 13:39:47 +0000 (15:39 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 7 Dec 2018 11:33:34 +0000 (12:33 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/Makemodule.am
libsmartcols/src/buffer.c [new file with mode: 0644]
libsmartcols/src/smartcolsP.h
libsmartcols/src/table_print.c

index 665b2aa7ffebb5d5a6815c97557b23819e17e9c1..392e5ddea3e9f9335f5cbaaab8cccbdd5bfb4754 100644 (file)
@@ -17,6 +17,7 @@ libsmartcols_la_SOURCES= \
        libsmartcols/src/table.c \
        libsmartcols/src/table_print.c \
        libsmartcols/src/version.c \
+       libsmartcols/src/buffer.c \
        libsmartcols/src/init.c
 
 libsmartcols_la_LIBADD = $(LDADD) libcommon.la
diff --git a/libsmartcols/src/buffer.c b/libsmartcols/src/buffer.c
new file mode 100644 (file)
index 0000000..46f270d
--- /dev/null
@@ -0,0 +1,140 @@
+
+#include "smartcolsP.h"
+#include "mbsalign.h"
+
+/* This is private struct to work with output data */
+struct libscols_buffer {
+       char    *begin;         /* begin of the buffer */
+       char    *cur;           /* current end of  the buffer */
+       char    *encdata;       /* encoded buffer mbs_safe_encode() */
+
+       size_t  bufsz;          /* size of the buffer */
+       size_t  art_idx;        /* begin of the tree ascii art or zero */
+};
+
+struct libscols_buffer *new_buffer(size_t sz)
+{
+       struct libscols_buffer *buf = malloc(sz + sizeof(struct libscols_buffer));
+
+       if (!buf)
+               return NULL;
+
+       buf->cur = buf->begin = ((char *) buf) + sizeof(struct libscols_buffer);
+       buf->encdata = NULL;
+       buf->bufsz = sz;
+
+       DBG(BUFF, ul_debugobj(buf, "alloc (size=%zu)", sz));
+       return buf;
+}
+
+void free_buffer(struct libscols_buffer *buf)
+{
+       if (!buf)
+               return;
+       DBG(BUFF, ul_debugobj(buf, "dealloc"));
+       free(buf->encdata);
+       free(buf);
+}
+
+int buffer_reset_data(struct libscols_buffer *buf)
+{
+       if (!buf)
+               return -EINVAL;
+
+       /*DBG(BUFF, ul_debugobj(buf, "reset data"));*/
+       buf->begin[0] = '\0';
+       buf->cur = buf->begin;
+       buf->art_idx = 0;
+       return 0;
+}
+
+int buffer_append_data(struct libscols_buffer *buf, const char *str)
+{
+       size_t maxsz, sz;
+
+       if (!buf)
+               return -EINVAL;
+       if (!str || !*str)
+               return 0;
+
+       sz = strlen(str);
+       maxsz = buf->bufsz - (buf->cur - buf->begin);
+
+       if (maxsz <= sz)
+               return -EINVAL;
+       memcpy(buf->cur, str, sz + 1);
+       buf->cur += sz;
+       return 0;
+}
+
+int buffer_set_data(struct libscols_buffer *buf, const char *str)
+{
+       int rc = buffer_reset_data(buf);
+       return rc ? rc : buffer_append_data(buf, str);
+}
+
+/* save the current buffer position to art_idx */
+void buffer_set_art_index(struct libscols_buffer *buf)
+{
+       if (buf) {
+               buf->art_idx = buf->cur - buf->begin;
+               /*DBG(BUFF, ul_debugobj(buf, "art index: %zu", buf->art_idx));*/
+       }
+}
+
+char *buffer_get_data(struct libscols_buffer *buf)
+{
+       return buf ? buf->begin : NULL;
+}
+
+size_t buffer_get_size(struct libscols_buffer *buf)
+{
+       return buf ? buf->bufsz : 0;
+}
+
+/* encode data by mbs_safe_encode() to avoid control and non-printable chars */
+char *buffer_get_safe_data(struct libscols_table *tb,
+                                 struct libscols_buffer *buf,
+                                 size_t *cells,
+                                 const char *safechars)
+{
+       char *data = buffer_get_data(buf);
+       char *res = NULL;
+
+       if (!data)
+               goto nothing;
+
+       if (!buf->encdata) {
+               buf->encdata = malloc(mbs_safe_encode_size(buf->bufsz) + 1);
+               if (!buf->encdata)
+                       goto nothing;
+       }
+
+       if (tb->no_encode) {
+               *cells = mbs_safe_width(data);
+               strcpy(buf->encdata, data);
+               res = buf->encdata;
+       } else {
+               res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars);
+       }
+
+       if (!res || !*cells || *cells == (size_t) -1)
+               goto nothing;
+       return res;
+nothing:
+       *cells = 0;
+       return NULL;
+}
+
+/* returns size in bytes of the ascii art (according to art_idx) in safe encoding */
+size_t buffer_get_safe_art_size(struct libscols_buffer *buf)
+{
+       char *data = buffer_get_data(buf);
+       size_t bytes = 0;
+
+       if (!data || !buf->art_idx)
+               return 0;
+
+       mbs_safe_nwidth(data, buf->art_idx, &bytes);
+       return bytes;
+}
index aab3890c17e17f4ffb70afea392529f2dd14053c..6fa739045001f151fab19ffe5bb02380bf6432c4 100644 (file)
@@ -131,6 +131,7 @@ struct libscols_line {
        struct list_head        ln_lines;       /* table lines */
        struct list_head        ln_branch;      /* begin of branch (head of ln_children) */
        struct list_head        ln_children;
+       struct list_head        ln_group;
 
        struct libscols_line    *parent;
 };
@@ -213,6 +214,25 @@ static inline int scols_iter_is_last(const struct libscols_iter *itr)
        return itr->p == itr->head;
 }
 
+/*
+ * buffer.c
+ */
+struct libscols_buffer;
+extern struct libscols_buffer *new_buffer(size_t sz);
+extern void free_buffer(struct libscols_buffer *buf);
+extern int buffer_reset_data(struct libscols_buffer *buf);
+extern int buffer_append_data(struct libscols_buffer *buf, const char *str);
+extern int buffer_set_data(struct libscols_buffer *buf, const char *str);
+extern void buffer_set_art_index(struct libscols_buffer *buf);
+extern char *buffer_get_data(struct libscols_buffer *buf);
+extern size_t buffer_get_size(struct libscols_buffer *buf);
+extern char *buffer_get_safe_data(struct libscols_table *tb,
+                                 struct libscols_buffer *buf,
+                                 size_t *cells,
+                                 const char *safechars);
+extern size_t buffer_get_safe_art_size(struct libscols_buffer *buf);
+
+
 static inline int is_last_child(struct libscols_line *ln)
 {
        if (!ln || !ln->parent)
index 2bdb5d90f31a22f1e0562c56dbb1632d7da9ee3f..2c024ca5ad509a713328857fa2bccf8792efb729 100644 (file)
 #define want_repeat_header(tb) (!(tb)->header_repeat || (tb)->header_next <= (tb)->termlines_used)
 
 
-/* This is private struct to work with output data */
-struct libscols_buffer {
-       char    *begin;         /* begin of the buffer */
-       char    *cur;           /* current end of  the buffer */
-       char    *encdata;       /* encoded buffer mbs_safe_encode() */
-
-       size_t  bufsz;          /* size of the buffer */
-       size_t  art_idx;        /* begin of the tree ascii art or zero */
-};
-
-static struct libscols_buffer *new_buffer(size_t sz)
-{
-       struct libscols_buffer *buf = malloc(sz + sizeof(struct libscols_buffer));
-
-       if (!buf)
-               return NULL;
-
-       buf->cur = buf->begin = ((char *) buf) + sizeof(struct libscols_buffer);
-       buf->encdata = NULL;
-       buf->bufsz = sz;
-
-       DBG(BUFF, ul_debugobj(buf, "alloc (size=%zu)", sz));
-       return buf;
-}
-
-static void free_buffer(struct libscols_buffer *buf)
-{
-       if (!buf)
-               return;
-       DBG(BUFF, ul_debugobj(buf, "dealloc"));
-       free(buf->encdata);
-       free(buf);
-}
-
-static int buffer_reset_data(struct libscols_buffer *buf)
-{
-       if (!buf)
-               return -EINVAL;
-
-       /*DBG(BUFF, ul_debugobj(buf, "reset data"));*/
-       buf->begin[0] = '\0';
-       buf->cur = buf->begin;
-       buf->art_idx = 0;
-       return 0;
-}
-
-static int buffer_append_data(struct libscols_buffer *buf, const char *str)
-{
-       size_t maxsz, sz;
-
-       if (!buf)
-               return -EINVAL;
-       if (!str || !*str)
-               return 0;
-
-       sz = strlen(str);
-       maxsz = buf->bufsz - (buf->cur - buf->begin);
-
-       if (maxsz <= sz)
-               return -EINVAL;
-       memcpy(buf->cur, str, sz + 1);
-       buf->cur += sz;
-       return 0;
-}
-
-static int buffer_set_data(struct libscols_buffer *buf, const char *str)
-{
-       int rc = buffer_reset_data(buf);
-       return rc ? rc : buffer_append_data(buf, str);
-}
-
-/* save the current buffer position to art_idx */
-static void buffer_set_art_index(struct libscols_buffer *buf)
-{
-       if (buf) {
-               buf->art_idx = buf->cur - buf->begin;
-               /*DBG(BUFF, ul_debugobj(buf, "art index: %zu", buf->art_idx));*/
-       }
-}
-
-static char *buffer_get_data(struct libscols_buffer *buf)
-{
-       return buf ? buf->begin : NULL;
-}
-
-/* encode data by mbs_safe_encode() to avoid control and non-printable chars */
-static char *buffer_get_safe_data(struct libscols_table *tb,
-                                 struct libscols_buffer *buf,
-                                 size_t *cells,
-                                 const char *safechars)
-{
-       char *data = buffer_get_data(buf);
-       char *res = NULL;
-
-       if (!data)
-               goto nothing;
-
-       if (!buf->encdata) {
-               buf->encdata = malloc(mbs_safe_encode_size(buf->bufsz) + 1);
-               if (!buf->encdata)
-                       goto nothing;
-       }
-
-       if (tb->no_encode) {
-               *cells = mbs_safe_width(data);
-               strcpy(buf->encdata, data);
-               res = buf->encdata;
-       } else {
-               res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars);
-       }
-
-       if (!res || !*cells || *cells == (size_t) -1)
-               goto nothing;
-       return res;
-nothing:
-       *cells = 0;
-       return NULL;
-}
-
-/* returns size in bytes of the ascii art (according to art_idx) in safe encoding */
-static size_t buffer_get_safe_art_size(struct libscols_buffer *buf)
-{
-       char *data = buffer_get_data(buf);
-       size_t bytes = 0;
-
-       if (!data || !buf->art_idx)
-               return 0;
-
-       mbs_safe_nwidth(data, buf->art_idx, &bytes);
-       return bytes;
-}
-
 /* returns pointer to the end of used data */
 static int line_ascii_art_to_buffer(struct libscols_table *tb,
                                    struct libscols_line *ln,
@@ -560,7 +428,7 @@ static int print_data(struct libscols_table *tb,
                return 0;
 
        if (len > width && !scols_column_is_trunc(cl))
-               print_newline_padding(tb, cl, ln, buf->bufsz);  /* next column starts on next line */
+               print_newline_padding(tb, cl, ln, buffer_get_size(buf));        /* next column starts on next line */
        else
                fputs(colsep(tb), tb->out);             /* columns separator */
 
@@ -750,7 +618,7 @@ static int print_line(struct libscols_table *tb,
                                if (rc == 0 && cl->pending_data)
                                        pending = 1;
                        } else
-                               print_empty_cell(tb, cl, ln, buf->bufsz);
+                               print_empty_cell(tb, cl, ln, buffer_get_size(buf));
                }
        }