summaryrefslogtreecommitdiffstats
path: root/grid.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-06-25 16:02:37 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-06-25 16:02:37 +0000
commit853ad681620e9a031f41549a39c78d11b2da8990 (patch)
treeba70dc52296728d064ecf6a369a21971381e2a2e /grid.c
parente6e1b45fa1aecffe5cd371a6492f38fddecdcd59 (diff)
Add a dedicated function to convert a line into a string and use it to simplify the search window function.
Diffstat (limited to 'grid.c')
-rw-r--r--grid.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/grid.c b/grid.c
index 0d8adb8a..2512a847 100644
--- a/grid.c
+++ b/grid.c
@@ -1,4 +1,4 @@
-/* $Id: grid.c,v 1.16 2009-05-04 17:58:26 nicm Exp $ */
+/* $OpenBSD: grid.c,v 1.3 2009/06/24 22:04:18 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -494,4 +494,50 @@ grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
}
}
+/* Convert cells into a string. */
+char *
+grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
+{
+ const struct grid_cell *gc;
+ const struct grid_utf8 *gu;
+ char *buf;
+ size_t len, off;
+ u_int xx;
+
+ GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
+
+ len = 128;
+ buf = xmalloc(len);
+ off = 0;
+
+ for (xx = px; xx < px + nx; xx++) {
+ gc = grid_peek_cell(gd, xx, py);
+ if (gc->flags & GRID_FLAG_PADDING)
+ continue;
+
+ if (gc->flags & GRID_FLAG_UTF8) {
+ while (len < off + UTF8_SIZE + 1) {
+ buf = xrealloc(buf, 2, len);
+ len *= 2;
+ }
+
+ gu = grid_peek_utf8(gd, xx, py);
+ memcpy(buf + off, gu->data, UTF8_SIZE);
+ off += UTF8_SIZE;
+ while (off > 0 && ((u_char) buf[off]) == 0xff)
+ off--;
+ } else {
+ while (len < off + 2) {
+ buf = xrealloc(buf, 2, len);
+ len *= 2;
+ }
+
+ buf[off++] = gc->data;
+ }
+ }
+
+ buf[off] = '\0';
+ return (buf);
+}
+