summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2016-11-06 11:42:17 +0100
committerBoris Faure <billiob@gmail.com>2016-11-06 11:42:17 +0100
commitd47b350e8cbcb4766f9278cf7532439f5631cfca (patch)
tree8019cd6d39bb04af8be939dc6c7c7bcb0b8282f3
parentfbe747fbfdda338fd176e62d9e876512605b4de3 (diff)
ty: add sb.{c,h} (was in termio.c)
-rw-r--r--Marrakesh.mrk2
-rw-r--r--src/bin/Makefile.am3
-rw-r--r--src/bin/sb.c45
-rw-r--r--src/bin/sb.h17
-rw-r--r--src/bin/termio.c79
5 files changed, 84 insertions, 62 deletions
diff --git a/Marrakesh.mrk b/Marrakesh.mrk
index 0feb521..2529977 100644
--- a/Marrakesh.mrk
+++ b/Marrakesh.mrk
@@ -66,6 +66,8 @@ src/bin/options_video.h
66src/bin/options_wallpaper.c 66src/bin/options_wallpaper.c
67src/bin/options_wallpaper.h 67src/bin/options_wallpaper.h
68src/bin/private.h 68src/bin/private.h
69src/bin/sb.c
70src/bin/sb.h
69src/bin/sel.c 71src/bin/sel.c
70src/bin/sel.h 72src/bin/sel.h
71src/bin/termcmd.c 73src/bin/termcmd.c
diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am
index 57301cb..bbb5814 100644
--- a/src/bin/Makefile.am
+++ b/src/bin/Makefile.am
@@ -60,7 +60,8 @@ win.c win.h \
60utils.c utils.h \ 60utils.c utils.h \
61extns.c extns.h \ 61extns.c extns.h \
62gravatar.c gravatar.h \ 62gravatar.c gravatar.h \
63tty_keys.h 63tty_keys.h \
64sb.c sb.h
64 65
65tybg_SOURCES = \ 66tybg_SOURCES = \
66tycommon.c tycommon.h \ 67tycommon.c tycommon.h \
diff --git a/src/bin/sb.c b/src/bin/sb.c
new file mode 100644
index 0000000..e780b36
--- /dev/null
+++ b/src/bin/sb.c
@@ -0,0 +1,45 @@
1#include "private.h"
2#include "sb.h"
3
4#include <stdlib.h>
5#include <stddef.h>
6#include <string.h>
7
8int
9ty_sb_add(struct ty_sb *sb, const char *s, size_t len)
10{
11 size_t new_len = sb->len + len;
12
13 if ((new_len >= sb->alloc) || !sb->buf)
14 {
15 size_t new_alloc = ((new_len + 15) / 16) * 24;
16 char *new_buf;
17
18 new_buf = realloc(sb->buf, new_alloc);
19 if (new_buf == NULL)
20 return -1;
21 sb->buf = new_buf;
22 sb->alloc = new_alloc;
23 }
24 memcpy(sb->buf + sb->len, s, len);
25 sb->len += len;
26 sb->buf[sb->len] = '\0';
27 return 0;
28}
29
30/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */
31void
32ty_sb_spaces_rtrim(struct ty_sb *sb)
33{
34 if (!sb->buf)
35 return;
36
37 while (sb->len > 0)
38 {
39 char c = sb->buf[sb->len - 1];
40 if ((c != ' ') && (c != '\t') && (c != '\f'))
41 break;
42 sb->len--;
43 }
44 sb->buf[sb->len] = '\0';
45}
diff --git a/src/bin/sb.h b/src/bin/sb.h
new file mode 100644
index 0000000..d504fe3
--- /dev/null
+++ b/src/bin/sb.h
@@ -0,0 +1,17 @@
1#ifndef _SB_H__
2#define _SB_H__
3
4#include <stddef.h>
5
6struct ty_sb {
7 char *buf;
8 size_t len;
9 size_t alloc;
10};
11
12int
13ty_sb_add(struct ty_sb *sb, const char *s, size_t len);
14void
15ty_sb_spaces_rtrim(struct ty_sb *sb);
16
17#endif
diff --git a/src/bin/termio.c b/src/bin/termio.c
index 020c0f9..565882f 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -16,6 +16,7 @@
16#include "media.h" 16#include "media.h"
17#include "miniview.h" 17#include "miniview.h"
18#include "gravatar.h" 18#include "gravatar.h"
19#include "sb.h"
19 20
20#if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__)) 21#if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__))
21# include <sys/proc_info.h> 22# include <sys/proc_info.h>
@@ -2025,50 +2026,6 @@ _mouse_in_selection(Termio *sd, int cx, int cy)
2025 return EINA_FALSE; 2026 return EINA_FALSE;
2026} 2027}
2027 2028
2028struct termio_sb {
2029 char *buf;
2030 size_t len;
2031 size_t alloc;
2032};
2033
2034static int
2035_sb_add(struct termio_sb *sb, const char *s, size_t len)
2036{
2037 size_t new_len = sb->len + len;
2038
2039 if ((new_len >= sb->alloc) || !sb->buf)
2040 {
2041 size_t new_alloc = ((new_len + 15) / 16) * 24;
2042 char *new_buf;
2043
2044 new_buf = realloc(sb->buf, new_alloc);
2045 if (new_buf == NULL)
2046 return -1;
2047 sb->buf = new_buf;
2048 sb->alloc = new_alloc;
2049 }
2050 memcpy(sb->buf + sb->len, s, len);
2051 sb->len += len;
2052 sb->buf[sb->len] = '\0';
2053 return 0;
2054}
2055
2056/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */
2057static void
2058_sb_spaces_rtrim(struct termio_sb *sb)
2059{
2060 if (!sb->buf)
2061 return;
2062
2063 while (sb->len > 0)
2064 {
2065 char c = sb->buf[sb->len - 1];
2066 if ((c != ' ') && (c != '\t') && (c != '\f'))
2067 break;
2068 sb->len--;
2069 }
2070 sb->buf[sb->len] = '\0';
2071}
2072 2029
2073 2030
2074char * 2031char *
@@ -2077,7 +2034,7 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
2077 Eina_Bool rtrim) 2034 Eina_Bool rtrim)
2078{ 2035{
2079 Termio *sd = evas_object_smart_data_get(obj); 2036 Termio *sd = evas_object_smart_data_get(obj);
2080 struct termio_sb sb = {.buf = NULL, .len = 0, .alloc = 0}; 2037 struct ty_sb sb = {.buf = NULL, .len = 0, .alloc = 0};
2081 int x, y; 2038 int x, y;
2082 2039
2083 EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL); 2040 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,
2093 cells = termpty_cellrow_get(sd->pty, y, &w); 2050 cells = termpty_cellrow_get(sd->pty, y, &w);
2094 if (!cells || !w) 2051 if (!cells || !w)
2095 { 2052 {
2096 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2053 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2097 continue; 2054 continue;
2098 } 2055 }
2099 if (w > sd->grid.w) w = sd->grid.w; 2056 if (w > sd->grid.w) w = sd->grid.w;
2100 if (y == c1y && c1x >= w) 2057 if (y == c1y && c1x >= w)
2101 { 2058 {
2102 if (rtrim) 2059 if (rtrim)
2103 _sb_spaces_rtrim(&sb); 2060 ty_sb_spaces_rtrim(&sb);
2104 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2061 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2105 continue; 2062 continue;
2106 } 2063 }
2107 start_x = c1x; 2064 start_x = c1x;
@@ -2130,14 +2087,14 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
2130 if ((y != c2y) || (x != end_x)) 2087 if ((y != c2y) || (x != end_x))
2131 { 2088 {
2132 if (rtrim) 2089 if (rtrim)
2133 _sb_spaces_rtrim(&sb); 2090 ty_sb_spaces_rtrim(&sb);
2134 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2091 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2135 } 2092 }
2136 break; 2093 break;
2137 } 2094 }
2138 else if (cells[x].att.tab) 2095 else if (cells[x].att.tab)
2139 { 2096 {
2140 if (_sb_add(&sb, "\t", 1) < 0) goto err; 2097 if (ty_sb_add(&sb, "\t", 1) < 0) goto err;
2141 x = ((x + 8) / 8) * 8; 2098 x = ((x + 8) / 8) * 8;
2142 x--; /* counter the ++ of the for loop */ 2099 x--; /* counter the ++ of the for loop */
2143 } 2100 }
@@ -2156,21 +2113,21 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
2156 last0 = -1; 2113 last0 = -1;
2157 while (v >= 0) 2114 while (v >= 0)
2158 { 2115 {
2159 if (_sb_add(&sb, " ", 1) < 0) goto err; 2116 if (ty_sb_add(&sb, " ", 1) < 0) goto err;
2160 v--; 2117 v--;
2161 } 2118 }
2162 } 2119 }
2163 txtlen = codepoint_to_utf8(cells[x].codepoint, txt); 2120 txtlen = codepoint_to_utf8(cells[x].codepoint, txt);
2164 if (txtlen > 0) 2121 if (txtlen > 0)
2165 if (_sb_add(&sb, txt, txtlen) < 0) goto err; 2122 if (ty_sb_add(&sb, txt, txtlen) < 0) goto err;
2166 if ((x == (w - 1)) && 2123 if ((x == (w - 1)) &&
2167 ((x != c2x) || (y != c2y))) 2124 ((x != c2x) || (y != c2y)))
2168 { 2125 {
2169 if (!cells[x].att.autowrapped) 2126 if (!cells[x].att.autowrapped)
2170 { 2127 {
2171 if (rtrim) 2128 if (rtrim)
2172 _sb_spaces_rtrim(&sb); 2129 ty_sb_spaces_rtrim(&sb);
2173 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2130 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2174 } 2131 }
2175 } 2132 }
2176 } 2133 }
@@ -2201,8 +2158,8 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
2201 if (!have_more) 2158 if (!have_more)
2202 { 2159 {
2203 if (rtrim) 2160 if (rtrim)
2204 _sb_spaces_rtrim(&sb); 2161 ty_sb_spaces_rtrim(&sb);
2205 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2162 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2206 } 2163 }
2207 else 2164 else
2208 { 2165 {
@@ -2215,22 +2172,22 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
2215 else break; 2172 else break;
2216 } 2173 }
2217 if (x >= w) break; 2174 if (x >= w) break;
2218 if (_sb_add(&sb, " ", 1) < 0) goto err; 2175 if (ty_sb_add(&sb, " ", 1) < 0) goto err;
2219 } 2176 }
2220 } 2177 }
2221 } 2178 }
2222 else 2179 else
2223 { 2180 {
2224 if (rtrim) 2181 if (rtrim)
2225 _sb_spaces_rtrim(&sb); 2182 ty_sb_spaces_rtrim(&sb);
2226 if (_sb_add(&sb, "\n", 1) < 0) goto err; 2183 if (ty_sb_add(&sb, "\n", 1) < 0) goto err;
2227 } 2184 }
2228 } 2185 }
2229 } 2186 }
2230 termpty_backlog_unlock(); 2187 termpty_backlog_unlock();
2231 2188
2232 if (rtrim) 2189 if (rtrim)
2233 _sb_spaces_rtrim(&sb); 2190 ty_sb_spaces_rtrim(&sb);
2234 2191
2235 if (lenp) 2192 if (lenp)
2236 *lenp = sb.len; 2193 *lenp = sb.len;