Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / grid-utf8.c
blob0c8297112adc8668a68762667898f379d43edd34
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <string.h>
23 #include "tmux.h"
26 * Grid UTF-8 utility functions.
29 /* Calculate UTF-8 grid cell size. Data is terminated by 0xff. */
30 size_t
31 grid_utf8_size(const struct grid_utf8 *gu)
33 size_t size;
35 for (size = 0; size < sizeof gu->data; size++) {
36 if (gu->data[size] == 0xff)
37 break;
39 return (size);
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)
46 size_t size;
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);
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)
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;
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)
73 size_t old_size;
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);
84 /* Compare two UTF-8 cells. */
85 int
86 grid_utf8_compare(const struct grid_utf8 *gu1, const struct grid_utf8 *gu2)
88 size_t size;
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);
95 return (1);