Annotation of src/usr.bin/tmux/grid-utf8.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: grid-utf8.c,v 1.1 2009/11/18 17:02:17 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Grid UTF-8 utility functions.
27: */
28:
29: /* Calculate UTF-8 grid cell size. Data is terminated by 0xff. */
30: size_t
31: grid_utf8_size(const struct grid_utf8 *gu)
32: {
33: size_t size;
34:
35: for (size = 0; size < sizeof gu->data; size++) {
36: if (gu->data[size] == 0xff)
37: break;
38: }
39: return (size);
40: }
41:
42: /* Copy UTF-8 out into a buffer. */
43: size_t
44: grid_utf8_copy(const struct grid_utf8 *gu, char *buf, size_t len)
45: {
46: size_t size;
47:
48: size = grid_utf8_size(gu);
49: if (size > len)
50: fatalx("UTF-8 copy overflow");
51: memcpy(buf, gu->data, size);
52: return (size);
53: }
54:
55: /* Set UTF-8 grid data from input UTF-8. */
56: void
57: grid_utf8_set(struct grid_utf8 *gu, const struct utf8_data *utf8data)
58: {
59: if (utf8data->size == 0)
60: fatalx("UTF-8 data empty");
61: if (utf8data->size > sizeof gu->data)
62: fatalx("UTF-8 data too long");
63: memcpy(gu->data, utf8data->data, utf8data->size);
64: if (utf8data->size != sizeof gu->data)
65: gu->data[utf8data->size] = 0xff;
66: gu->width = utf8data->width;
67: }
68:
69: /* Append UTF-8 character onto the cell data (for combined characters). */
70: int
71: grid_utf8_append(struct grid_utf8 *gu, const struct utf8_data *utf8data)
72: {
73: size_t old_size;
74:
75: old_size = grid_utf8_size(gu);
76: if (old_size + utf8data->size > sizeof gu->data)
77: return (-1);
78: memcpy(gu->data + old_size, utf8data->data, utf8data->size);
79: if (old_size + utf8data->size != sizeof gu->data)
80: gu->data[old_size + utf8data->size] = 0xff;
81: return (0);
82: }
83:
84: /* Compare two UTF-8 cells. */
85: int
86: grid_utf8_compare(const struct grid_utf8 *gu1, const struct grid_utf8 *gu2)
87: {
88: size_t size;
89:
90: size = grid_utf8_size(gu1);
91: if (size != grid_utf8_size(gu2))
92: return (0);
93: if (memcmp(gu1->data, gu2->data, size) != 0)
94: return (0);
1.2 ! nicm 95: return (1);
1.1 nicm 96: }