]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: use lib/buffer, remove local implementation
authorKarel Zak <kzak@redhat.com>
Fri, 6 Aug 2021 10:04:05 +0000 (12:04 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Aug 2021 10:13:56 +0000 (12:13 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/Makemodule.am
libsmartcols/src/buffer.c [deleted file]
libsmartcols/src/calculate.c
libsmartcols/src/print-api.c
libsmartcols/src/print.c
libsmartcols/src/smartcolsP.h

index e3f4e7df17e10418830cc33a6ef21e4fdc9b360e..2bb19fdbde0a991b979cbcdfcdbd779dd0f6f033 100644 (file)
@@ -18,7 +18,6 @@ libsmartcols_la_SOURCES= \
        libsmartcols/src/print.c \
        libsmartcols/src/print-api.c \
        libsmartcols/src/version.c \
-       libsmartcols/src/buffer.c \
        libsmartcols/src/calculate.c \
        libsmartcols/src/grouping.c \
        libsmartcols/src/walk.c \
diff --git a/libsmartcols/src/buffer.c b/libsmartcols/src/buffer.c
deleted file mode 100644 (file)
index 8a31379..0000000
+++ /dev/null
@@ -1,154 +0,0 @@
-
-#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;
-       if (!buf->cur || !buf->begin)
-               return -EINVAL;
-
-       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_append_ntimes(struct libscols_buffer *buf, size_t n, const char *str)
-{
-       size_t i;
-
-       for (i = 0; i < n; i++) {
-               int rc = buffer_append_data(buf, str);
-               if (rc)
-                       return rc;
-       }
-       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 (scols_table_is_noencoding(tb)) {
-               *cells = mbs_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 974fc340c5e028080fd07fadd2bee250e66493b2..4f794088e28c6f3d70e29f912811c6cbab5b2127 100644 (file)
@@ -35,7 +35,7 @@ static void dbg_columns(struct libscols_table *tb)
 static int count_cell_width(struct libscols_table *tb,
                struct libscols_line *ln,
                struct libscols_column *cl,
-               struct libscols_buffer *buf)
+               struct ul_buffer *buf)
 {
        size_t len;
        char *data;
@@ -45,7 +45,7 @@ static int count_cell_width(struct libscols_table *tb,
        if (rc)
                return rc;
 
-       data = buffer_get_data(buf);
+       data = ul_buffer_get_data(buf, NULL, NULL);
        if (!data)
                len = 0;
        else if (scols_column_is_customwrap(cl))
@@ -68,7 +68,7 @@ static int count_cell_width(struct libscols_table *tb,
        }
        cl->width = max(len, cl->width);
        if (scols_column_is_tree(cl)) {
-               size_t treewidth = buffer_get_safe_art_size(buf);
+               size_t treewidth = ul_buffer_get_safe_pointer_width(buf, SCOLS_BUFPTR_TREEEND);
                cl->width_treeart = max(cl->width_treeart, treewidth);
        }
        return 0;
@@ -80,7 +80,7 @@ static int walk_count_cell_width(struct libscols_table *tb,
                struct libscols_column *cl,
                void *data)
 {
-       return count_cell_width(tb, ln, cl, (struct libscols_buffer *) data);
+       return count_cell_width(tb, ln, cl, (struct ul_buffer *) data);
 }
 
 /*
@@ -94,7 +94,7 @@ static int walk_count_cell_width(struct libscols_table *tb,
  */
 static int count_column_width(struct libscols_table *tb,
                              struct libscols_column *cl,
-                             struct libscols_buffer *buf)
+                             struct ul_buffer *buf)
 {
        int rc = 0, no_header = 0;
 
@@ -183,7 +183,7 @@ done:
 /*
  * This is core of the scols_* voodoo...
  */
-int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf)
+int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf)
 {
        struct libscols_column *cl;
        struct libscols_iter itr;
index 237d9ae543bfa6a0d640b4c378f53de28d685141..52b26643558017d1e60923efa14b5425318cf494 100644 (file)
@@ -15,7 +15,7 @@ int scols_table_print_range(  struct libscols_table *tb,
                                struct libscols_line *start,
                                struct libscols_line *end)
 {
-       struct libscols_buffer *buf = NULL;
+       struct ul_buffer buf = UL_INIT_BUFFER;
        struct libscols_iter itr;
        int rc;
 
@@ -36,14 +36,14 @@ int scols_table_print_range(        struct libscols_table *tb,
                scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
 
        if (!start || itr.p == tb->tb_lines.next) {
-               rc = __scols_print_header(tb, buf);
+               rc = __scols_print_header(tb, &buf);
                if (rc)
                        goto done;
        }
 
-       rc = __scols_print_range(tb, buf, &itr, end);
+       rc = __scols_print_range(tb, &buf, &itr, end);
 done:
-       __scols_cleanup_printing(tb, buf);
+       __scols_cleanup_printing(tb, &buf);
        return rc;
 }
 
@@ -101,7 +101,7 @@ int scols_table_print_range_to_string(
 static int do_print_table(struct libscols_table *tb, int *is_empty)
 {
        int rc = 0;
-       struct libscols_buffer *buf = NULL;
+       struct ul_buffer buf = UL_INIT_BUFFER;
 
        if (!tb)
                return -EINVAL;
@@ -140,21 +140,21 @@ static int do_print_table(struct libscols_table *tb, int *is_empty)
        if (tb->format == SCOLS_FMT_HUMAN)
                __scols_print_title(tb);
 
-       rc = __scols_print_header(tb, buf);
+       rc = __scols_print_header(tb, &buf);
        if (rc)
                goto done;
 
        if (scols_table_is_tree(tb))
-               rc = __scols_print_tree(tb, buf);
+               rc = __scols_print_tree(tb, &buf);
        else
-               rc = __scols_print_table(tb, buf);
+               rc = __scols_print_table(tb, &buf);
 
        if (scols_table_is_json(tb)) {
                ul_jsonwrt_array_close(&tb->json);
                ul_jsonwrt_root_close(&tb->json);
        }
 done:
-       __scols_cleanup_printing(tb, buf);
+       __scols_cleanup_printing(tb, &buf);
        return rc;
 }
 
index 6f6d88e55f760747cafda4bb2c5ac3ca1ada33aa..bd9dcea7ec2abebe6e74ae9317132b45e084c3d9 100644 (file)
@@ -90,7 +90,7 @@ static int is_next_columns_empty(
 /* returns pointer to the end of used data */
 static int tree_ascii_art_to_buffer(struct libscols_table *tb,
                                    struct libscols_line *ln,
-                                   struct libscols_buffer *buf)
+                                   struct ul_buffer *buf)
 {
        const char *art;
        int rc;
@@ -110,7 +110,7 @@ static int tree_ascii_art_to_buffer(struct libscols_table *tb,
        else
                art = vertical_symbol(tb);
 
-       return buffer_append_data(buf, art);
+       return ul_buffer_append_string(buf, art);
 }
 
 static int grpset_is_empty(    struct libscols_table *tb,
@@ -131,7 +131,7 @@ static int grpset_is_empty( struct libscols_table *tb,
 
 static int groups_ascii_art_to_buffer( struct libscols_table *tb,
                                struct libscols_line *ln,
-                               struct libscols_buffer *buf)
+                               struct ul_buffer *buf)
 {
        int filled = 0;
        size_t i, rest = 0;
@@ -149,46 +149,46 @@ static int groups_ascii_art_to_buffer(    struct libscols_table *tb,
                struct libscols_group *gr = tb->grpset[i];
 
                if (!gr) {
-                       buffer_append_ntimes(buf, SCOLS_GRPSET_CHUNKSIZ, cellpadding_symbol(tb));
+                       ul_buffer_append_ntimes(buf, SCOLS_GRPSET_CHUNKSIZ, cellpadding_symbol(tb));
                        continue;
                }
 
                switch (gr->state) {
                case SCOLS_GSTATE_FIRST_MEMBER:
-                       buffer_append_data(buf, grp_m_first_symbol(tb));
+                       ul_buffer_append_string(buf, grp_m_first_symbol(tb));
                        break;
                case SCOLS_GSTATE_MIDDLE_MEMBER:
-                       buffer_append_data(buf, grp_m_middle_symbol(tb));
+                       ul_buffer_append_string(buf, grp_m_middle_symbol(tb));
                        break;
                case SCOLS_GSTATE_LAST_MEMBER:
-                       buffer_append_data(buf, grp_m_last_symbol(tb));
+                       ul_buffer_append_string(buf, grp_m_last_symbol(tb));
                        break;
                case SCOLS_GSTATE_CONT_MEMBERS:
-                       buffer_append_data(buf, grp_vertical_symbol(tb));
-                       buffer_append_ntimes(buf, 2, filler);
+                       ul_buffer_append_string(buf, grp_vertical_symbol(tb));
+                       ul_buffer_append_ntimes(buf, 2, filler);
                        break;
                case SCOLS_GSTATE_MIDDLE_CHILD:
-                       buffer_append_data(buf, filler);
-                       buffer_append_data(buf, grp_c_middle_symbol(tb));
+                       ul_buffer_append_string(buf, filler);
+                       ul_buffer_append_string(buf, grp_c_middle_symbol(tb));
                        if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
-                               buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
+                               ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
                                filled = 1;
                        }
                        filler = grp_horizontal_symbol(tb);
                        break;
                case SCOLS_GSTATE_LAST_CHILD:
-                       buffer_append_data(buf, cellpadding_symbol(tb));
-                       buffer_append_data(buf, grp_c_last_symbol(tb));
+                       ul_buffer_append_string(buf, cellpadding_symbol(tb));
+                       ul_buffer_append_string(buf, grp_c_last_symbol(tb));
                        if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
-                               buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
+                               ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
                                filled = 1;
                        }
                        filler = grp_horizontal_symbol(tb);
                        break;
                case SCOLS_GSTATE_CONT_CHILDREN:
-                       buffer_append_data(buf, filler);
-                       buffer_append_data(buf, grp_vertical_symbol(tb));
-                       buffer_append_data(buf, filler);
+                       ul_buffer_append_string(buf, filler);
+                       ul_buffer_append_string(buf, grp_vertical_symbol(tb));
+                       ul_buffer_append_string(buf, filler);
                        break;
                }
 
@@ -197,7 +197,7 @@ static int groups_ascii_art_to_buffer(      struct libscols_table *tb,
        }
 
        if (!filled)
-               buffer_append_data(buf, filler);
+               ul_buffer_append_string(buf, filler);
        return 0;
 }
 
@@ -316,18 +316,24 @@ static void print_empty_cell(struct libscols_table *tb,
                        }
                } else {
                        /* use the same draw function as though we were intending to draw an L-shape */
-                       struct libscols_buffer *art = new_buffer(bufsz);
+                       struct ul_buffer art = UL_INIT_BUFFER;
                        char *data;
 
-                       if (art) {
+                       if (ul_buffer_alloc_data(&art, bufsz) == 0) {
                                /* whatever the rc, len_pad will be sensible */
-                               tree_ascii_art_to_buffer(tb, ln, art);
+                               tree_ascii_art_to_buffer(tb, ln, &art);
+
                                if (!list_empty(&ln->ln_branch) && has_pending_data(tb))
-                                       buffer_append_data(art, vertical_symbol(tb));
-                               data = buffer_get_safe_data(tb, art, &len_pad, NULL);
+                                       ul_buffer_append_string(&art, vertical_symbol(tb));
+
+                               if (scols_table_is_noencoding(tb))
+                                       data = ul_buffer_get_data(&art, NULL, &len_pad);
+                               else
+                                       data = ul_buffer_get_safe_data(&art, NULL, &len_pad, NULL);
+
                                if (data && len_pad)
                                        fputs(data, tb->out);
-                               free_buffer(art);
+                               ul_buffer_free_data(&art);
                        }
                }
        }
@@ -561,7 +567,7 @@ static int print_data(struct libscols_table *tb,
                      struct libscols_column *cl,
                      struct libscols_line *ln, /* optional */
                      struct libscols_cell *ce, /* optional */
-                     struct libscols_buffer *buf)
+                     struct ul_buffer *buf)
 {
        size_t len = 0, i, width, bytes;
        char *data, *nextchunk;
@@ -571,7 +577,7 @@ static int print_data(struct libscols_table *tb,
        assert(tb);
        assert(cl);
 
-       data = buffer_get_data(buf);
+       data = ul_buffer_get_data(buf, NULL, NULL);
        if (!data)
                data = "";
 
@@ -612,10 +618,13 @@ static int print_data(struct libscols_table *tb,
 
        /* Encode. Note that 'len' and 'width' are number of cells, not bytes.
         */
-       data = buffer_get_safe_data(tb, buf, &len, scols_column_get_safechars(cl));
+       if (scols_table_is_noencoding(tb))
+               data = ul_buffer_get_data(buf, &bytes, &len);
+       else
+               data = ul_buffer_get_safe_data(buf, &bytes, &len, scols_column_get_safechars(cl));
+
        if (!data)
                data = "";
-       bytes = strlen(data);
        width = cl->width;
 
        /* custom multi-line cell based */
@@ -689,7 +698,7 @@ static int print_data(struct libscols_table *tb,
 
        if (len > width && !scols_column_is_trunc(cl)) {
                DBG(COL, ul_debugobj(cl, "*** data len=%zu > column width=%zu", len, width));
-               print_newline_padding(tb, cl, ln, ce, buffer_get_size(buf));    /* next column starts on next line */
+               print_newline_padding(tb, cl, ln, ce, ul_buffer_get_bufsiz(buf));       /* next column starts on next line */
 
        } else if (!is_last)
                fputs(colsep(tb), tb->out);             /* columns separator */
@@ -700,7 +709,7 @@ static int print_data(struct libscols_table *tb,
 int __cell_to_buffer(struct libscols_table *tb,
                          struct libscols_line *ln,
                          struct libscols_column *cl,
-                         struct libscols_buffer *buf)
+                         struct ul_buffer *buf)
 {
        const char *data;
        struct libscols_cell *ce;
@@ -712,13 +721,13 @@ int __cell_to_buffer(struct libscols_table *tb,
        assert(buf);
        assert(cl->seqnum <= tb->ncols);
 
-       buffer_reset_data(buf);
+       ul_buffer_reset_data(buf);
 
        ce = scols_line_get_cell(ln, cl->seqnum);
        data = ce ? scols_cell_get_data(ce) : NULL;
 
        if (!scols_column_is_tree(cl))
-               return data ? buffer_set_data(buf, data) : 0;
+               return data ? ul_buffer_append_string(buf, data) : 0;
 
        /*
         * Group stuff
@@ -733,16 +742,16 @@ int __cell_to_buffer(struct libscols_table *tb,
                rc = tree_ascii_art_to_buffer(tb, ln->parent, buf);
 
                if (!rc && is_last_child(ln))
-                       rc = buffer_append_data(buf, right_symbol(tb));
+                       rc = ul_buffer_append_string(buf, right_symbol(tb));
                else if (!rc)
-                       rc = buffer_append_data(buf, branch_symbol(tb));
+                       rc = ul_buffer_append_string(buf, branch_symbol(tb));
        }
 
        if (!rc && (ln->parent || cl->is_groups) && !scols_table_is_json(tb))
-               buffer_set_art_index(buf);
+               ul_buffer_save_pointer(buf, SCOLS_BUFPTR_TREEEND);
 
        if (!rc && data)
-               rc = buffer_append_data(buf, data);
+               rc = ul_buffer_append_string(buf, data);
        return rc;
 }
 
@@ -752,7 +761,7 @@ int __cell_to_buffer(struct libscols_table *tb,
  */
 static int print_line(struct libscols_table *tb,
                      struct libscols_line *ln,
-                     struct libscols_buffer *buf)
+                     struct ul_buffer *buf)
 {
        int rc = 0, pending = 0;
        struct libscols_column *cl;
@@ -760,7 +769,7 @@ static int print_line(struct libscols_table *tb,
 
        assert(ln);
 
-       DBG(LINE, ul_debugobj(ln, "printing line"));
+       DBG(LINE, ul_debugobj(ln, "     printing line"));
 
        fputs_color_line_open(tb, ln);
 
@@ -795,7 +804,7 @@ static int print_line(struct libscols_table *tb,
                                if (rc == 0 && cl->pending_data)
                                        pending = 1;
                        } else
-                               print_empty_cell(tb, cl, ln, NULL, buffer_get_size(buf));
+                               print_empty_cell(tb, cl, ln, NULL, ul_buffer_get_bufsiz(buf));
                }
                fputs_color_line_close(tb);
        }
@@ -904,7 +913,7 @@ done:
        return rc;
 }
 
-int __scols_print_header(struct libscols_table *tb, struct libscols_buffer *buf)
+int __scols_print_header(struct libscols_table *tb, struct ul_buffer *buf)
 {
        int rc = 0;
        struct libscols_column *cl;
@@ -927,19 +936,19 @@ int __scols_print_header(struct libscols_table *tb, struct libscols_buffer *buf)
                if (scols_column_is_hidden(cl))
                        continue;
 
-               buffer_reset_data(buf);
+               ul_buffer_reset_data(buf);
 
                if (cl->is_groups
                    && scols_table_is_tree(tb) && scols_column_is_tree(cl)) {
                        size_t i;
                        for (i = 0; i < tb->grpset_size + 1; i++) {
-                               rc = buffer_append_data(buf, " ");
+                               rc = ul_buffer_append_data(buf, " ", 1);
                                if (rc)
                                        break;
                        }
                }
                if (!rc)
-                       rc = buffer_append_data(buf, scols_cell_get_data(&cl->header));
+                       rc = ul_buffer_append_string(buf, scols_cell_get_data(&cl->header));
                if (!rc)
                        rc = print_data(tb, cl, NULL, &cl->header, buf);
        }
@@ -959,7 +968,7 @@ int __scols_print_header(struct libscols_table *tb, struct libscols_buffer *buf)
 
 
 int __scols_print_range(struct libscols_table *tb,
-                       struct libscols_buffer *buf,
+                       struct ul_buffer *buf,
                        struct libscols_iter *itr,
                        struct libscols_line *end)
 {
@@ -996,7 +1005,7 @@ int __scols_print_range(struct libscols_table *tb,
 
 }
 
-int __scols_print_table(struct libscols_table *tb, struct libscols_buffer *buf)
+int __scols_print_table(struct libscols_table *tb, struct ul_buffer *buf)
 {
        struct libscols_iter itr;
 
@@ -1010,7 +1019,7 @@ static int print_tree_line(struct libscols_table *tb,
                           struct libscols_column *cl __attribute__((__unused__)),
                           void *data)
 {
-       struct libscols_buffer *buf = (struct libscols_buffer *) data;
+       struct ul_buffer *buf = (struct ul_buffer *) data;
        int rc;
 
        DBG(LINE, ul_debugobj(ln, "   printing tree line"));
@@ -1059,7 +1068,7 @@ static int print_tree_line(struct libscols_table *tb,
        return 0;
 }
 
-int __scols_print_tree(struct libscols_table *tb, struct libscols_buffer *buf)
+int __scols_print_tree(struct libscols_table *tb, struct ul_buffer *buf)
 {
        assert(tb);
        DBG(TAB, ul_debugobj(tb, "----printing-tree-----"));
@@ -1083,12 +1092,12 @@ static size_t strlen_line(struct libscols_line *ln)
        return sz;
 }
 
-void __scols_cleanup_printing(struct libscols_table *tb, struct libscols_buffer *buf)
+void __scols_cleanup_printing(struct libscols_table *tb, struct ul_buffer *buf)
 {
        if (!tb)
                return;
 
-       free_buffer(buf);
+       ul_buffer_free_data(buf);
 
        if (tb->priv_symbols) {
                scols_table_set_symbols(tb, NULL);
@@ -1096,7 +1105,7 @@ void __scols_cleanup_printing(struct libscols_table *tb, struct libscols_buffer
        }
 }
 
-int __scols_initialize_printing(struct libscols_table *tb, struct libscols_buffer **buf)
+int __scols_initialize_printing(struct libscols_table *tb, struct ul_buffer *buf)
 {
        size_t bufsz, extra_bufsz = 0;
        struct libscols_line *ln;
@@ -1104,7 +1113,6 @@ int __scols_initialize_printing(struct libscols_table *tb, struct libscols_buffe
        int rc;
 
        DBG(TAB, ul_debugobj(tb, "initialize printing"));
-       *buf = NULL;
 
        if (!tb->symbols) {
                rc = scols_table_set_default_symbols(tb);
@@ -1179,11 +1187,10 @@ int __scols_initialize_printing(struct libscols_table *tb, struct libscols_buffe
                        bufsz = sz;
        }
 
-       *buf = new_buffer(bufsz + 1);   /* data + space for \0 */
-       if (!*buf) {
-               rc = -ENOMEM;
+       /* pre-allocate space for data */
+       rc = ul_buffer_alloc_data(buf, bufsz + 1);      /* data + space for \0 */
+       if (rc)
                goto err;
-       }
 
        /*
         * Make sure groups members are in the same orders as the tree
@@ -1192,14 +1199,14 @@ int __scols_initialize_printing(struct libscols_table *tb, struct libscols_buffe
                scols_groups_fix_members_order(tb);
 
        if (tb->format == SCOLS_FMT_HUMAN) {
-               rc = __scols_calculate(tb, *buf);
+               rc = __scols_calculate(tb, buf);
                if (rc != 0)
                        goto err;
        }
 
        return 0;
 err:
-       __scols_cleanup_printing(tb, *buf);
+       __scols_cleanup_printing(tb, buf);
        return rc;
 }
 
index 810cc0f5b090e2176d061119b5d5d8722eb4c416..d2b14e49cc43e3e72f74764cb73bb19f6760da24 100644 (file)
@@ -17,6 +17,7 @@
 #include "color-names.h"
 #include "jsonwrt.h"
 #include "debug.h"
+#include "buffer.h"
 
 #include "libsmartcols.h"
 
@@ -41,6 +42,8 @@ UL_DEBUG_DECLARE_MASK(libsmartcols);
 #define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(libsmartcols)
 #include "debugobj.h"
 
+#define SCOLS_BUFPTR_TREEEND   0
+
 /*
  * Generic iterator
  */
@@ -295,25 +298,6 @@ int scols_table_next_group(struct libscols_table *tb,
                           struct libscols_iter *itr,
                           struct libscols_group **gr);
 
-/*
- * 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_append_ntimes(struct libscols_buffer *buf, size_t n, 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);
-
 /*
  * grouping.c
  */
@@ -341,7 +325,7 @@ extern int scols_walk_is_last(struct libscols_table *tb, struct libscols_line *l
 /*
  * calculate.c
  */
-extern int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf);
+extern int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf);
 
 /*
  * print.c
@@ -349,16 +333,16 @@ extern int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *
 extern int __cell_to_buffer(struct libscols_table *tb,
                           struct libscols_line *ln,
                           struct libscols_column *cl,
-                          struct libscols_buffer *buf);
+                          struct ul_buffer *buf);
 
-void __scols_cleanup_printing(struct libscols_table *tb, struct libscols_buffer *buf);
-int __scols_initialize_printing(struct libscols_table *tb, struct libscols_buffer **buf);
-int __scols_print_tree(struct libscols_table *tb, struct libscols_buffer *buf);
-int __scols_print_table(struct libscols_table *tb, struct libscols_buffer *buf);
-int __scols_print_header(struct libscols_table *tb, struct libscols_buffer *buf);
+void __scols_cleanup_printing(struct libscols_table *tb, struct ul_buffer *buf);
+int __scols_initialize_printing(struct libscols_table *tb, struct ul_buffer *buf);
+int __scols_print_tree(struct libscols_table *tb, struct ul_buffer *buf);
+int __scols_print_table(struct libscols_table *tb, struct ul_buffer *buf);
+int __scols_print_header(struct libscols_table *tb, struct ul_buffer *buf);
 int __scols_print_title(struct libscols_table *tb);
 int __scols_print_range(struct libscols_table *tb,
-                        struct libscols_buffer *buf,
+                        struct ul_buffer *buf,
                         struct libscols_iter *itr,
                         struct libscols_line *end);