From d47b350e8cbcb4766f9278cf7532439f5631cfca Mon Sep 17 00:00:00 2001 From: Boris Faure Date: Sun, 6 Nov 2016 11:42:17 +0100 Subject: [PATCH] ty: add sb.{c,h} (was in termio.c) --- Marrakesh.mrk | 2 ++ src/bin/Makefile.am | 3 +- src/bin/sb.c | 45 ++++++++++++++++++++++++++ src/bin/sb.h | 17 ++++++++++ src/bin/termio.c | 79 +++++++++++---------------------------------- 5 files changed, 84 insertions(+), 62 deletions(-) create mode 100644 src/bin/sb.c create mode 100644 src/bin/sb.h diff --git a/Marrakesh.mrk b/Marrakesh.mrk index 0feb5215..25299771 100644 --- a/Marrakesh.mrk +++ b/Marrakesh.mrk @@ -66,6 +66,8 @@ src/bin/options_video.h src/bin/options_wallpaper.c src/bin/options_wallpaper.h src/bin/private.h +src/bin/sb.c +src/bin/sb.h src/bin/sel.c src/bin/sel.h src/bin/termcmd.c diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am index 57301cbf..bbb58146 100644 --- a/src/bin/Makefile.am +++ b/src/bin/Makefile.am @@ -60,7 +60,8 @@ win.c win.h \ utils.c utils.h \ extns.c extns.h \ gravatar.c gravatar.h \ -tty_keys.h +tty_keys.h \ +sb.c sb.h tybg_SOURCES = \ tycommon.c tycommon.h \ diff --git a/src/bin/sb.c b/src/bin/sb.c new file mode 100644 index 00000000..e780b36e --- /dev/null +++ b/src/bin/sb.c @@ -0,0 +1,45 @@ +#include "private.h" +#include "sb.h" + +#include +#include +#include + +int +ty_sb_add(struct ty_sb *sb, const char *s, size_t len) +{ + size_t new_len = sb->len + len; + + if ((new_len >= sb->alloc) || !sb->buf) + { + size_t new_alloc = ((new_len + 15) / 16) * 24; + char *new_buf; + + new_buf = realloc(sb->buf, new_alloc); + if (new_buf == NULL) + return -1; + sb->buf = new_buf; + sb->alloc = new_alloc; + } + memcpy(sb->buf + sb->len, s, len); + sb->len += len; + sb->buf[sb->len] = '\0'; + return 0; +} + +/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */ +void +ty_sb_spaces_rtrim(struct ty_sb *sb) +{ + if (!sb->buf) + return; + + while (sb->len > 0) + { + char c = sb->buf[sb->len - 1]; + if ((c != ' ') && (c != '\t') && (c != '\f')) + break; + sb->len--; + } + sb->buf[sb->len] = '\0'; +} diff --git a/src/bin/sb.h b/src/bin/sb.h new file mode 100644 index 00000000..d504fe32 --- /dev/null +++ b/src/bin/sb.h @@ -0,0 +1,17 @@ +#ifndef _SB_H__ +#define _SB_H__ + +#include + +struct ty_sb { + char *buf; + size_t len; + size_t alloc; +}; + +int +ty_sb_add(struct ty_sb *sb, const char *s, size_t len); +void +ty_sb_spaces_rtrim(struct ty_sb *sb); + +#endif diff --git a/src/bin/termio.c b/src/bin/termio.c index 020c0f9e..565882f9 100644 --- a/src/bin/termio.c +++ b/src/bin/termio.c @@ -16,6 +16,7 @@ #include "media.h" #include "miniview.h" #include "gravatar.h" +#include "sb.h" #if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__)) # include @@ -2025,50 +2026,6 @@ _mouse_in_selection(Termio *sd, int cx, int cy) return EINA_FALSE; } -struct termio_sb { - char *buf; - size_t len; - size_t alloc; -}; - -static int -_sb_add(struct termio_sb *sb, const char *s, size_t len) -{ - size_t new_len = sb->len + len; - - if ((new_len >= sb->alloc) || !sb->buf) - { - size_t new_alloc = ((new_len + 15) / 16) * 24; - char *new_buf; - - new_buf = realloc(sb->buf, new_alloc); - if (new_buf == NULL) - return -1; - sb->buf = new_buf; - sb->alloc = new_alloc; - } - memcpy(sb->buf + sb->len, s, len); - sb->len += len; - sb->buf[sb->len] = '\0'; - return 0; -} - -/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */ -static void -_sb_spaces_rtrim(struct termio_sb *sb) -{ - if (!sb->buf) - return; - - while (sb->len > 0) - { - char c = sb->buf[sb->len - 1]; - if ((c != ' ') && (c != '\t') && (c != '\f')) - break; - sb->len--; - } - sb->buf[sb->len] = '\0'; -} char * @@ -2077,7 +2034,7 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, Eina_Bool rtrim) { Termio *sd = evas_object_smart_data_get(obj); - struct termio_sb sb = {.buf = NULL, .len = 0, .alloc = 0}; + struct ty_sb sb = {.buf = NULL, .len = 0, .alloc = 0}; int x, y; EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL); @@ -2093,15 +2050,15 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, cells = termpty_cellrow_get(sd->pty, y, &w); if (!cells || !w) { - if (_sb_add(&sb, "\n", 1) < 0) goto err; + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; continue; } if (w > sd->grid.w) w = sd->grid.w; if (y == c1y && c1x >= w) { if (rtrim) - _sb_spaces_rtrim(&sb); - if (_sb_add(&sb, "\n", 1) < 0) goto err; + ty_sb_spaces_rtrim(&sb); + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; continue; } start_x = c1x; @@ -2130,14 +2087,14 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, if ((y != c2y) || (x != end_x)) { if (rtrim) - _sb_spaces_rtrim(&sb); - if (_sb_add(&sb, "\n", 1) < 0) goto err; + ty_sb_spaces_rtrim(&sb); + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; } break; } else if (cells[x].att.tab) { - if (_sb_add(&sb, "\t", 1) < 0) goto err; + if (ty_sb_add(&sb, "\t", 1) < 0) goto err; x = ((x + 8) / 8) * 8; x--; /* counter the ++ of the for loop */ } @@ -2156,21 +2113,21 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, last0 = -1; while (v >= 0) { - if (_sb_add(&sb, " ", 1) < 0) goto err; + if (ty_sb_add(&sb, " ", 1) < 0) goto err; v--; } } txtlen = codepoint_to_utf8(cells[x].codepoint, txt); if (txtlen > 0) - if (_sb_add(&sb, txt, txtlen) < 0) goto err; + if (ty_sb_add(&sb, txt, txtlen) < 0) goto err; if ((x == (w - 1)) && ((x != c2x) || (y != c2y))) { if (!cells[x].att.autowrapped) { if (rtrim) - _sb_spaces_rtrim(&sb); - if (_sb_add(&sb, "\n", 1) < 0) goto err; + ty_sb_spaces_rtrim(&sb); + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; } } } @@ -2201,8 +2158,8 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, if (!have_more) { if (rtrim) - _sb_spaces_rtrim(&sb); - if (_sb_add(&sb, "\n", 1) < 0) goto err; + ty_sb_spaces_rtrim(&sb); + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; } else { @@ -2215,22 +2172,22 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, else break; } if (x >= w) break; - if (_sb_add(&sb, " ", 1) < 0) goto err; + if (ty_sb_add(&sb, " ", 1) < 0) goto err; } } } else { if (rtrim) - _sb_spaces_rtrim(&sb); - if (_sb_add(&sb, "\n", 1) < 0) goto err; + ty_sb_spaces_rtrim(&sb); + if (ty_sb_add(&sb, "\n", 1) < 0) goto err; } } } termpty_backlog_unlock(); if (rtrim) - _sb_spaces_rtrim(&sb); + ty_sb_spaces_rtrim(&sb); if (lenp) *lenp = sb.len;