Debian Codegraph @

terps/tads/tads3/utf8.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/utf8.cpp,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 utf8.cpp - UTF-8 implementation
15 Function
16
17 Notes
18
19 Modified
20 10/17/98 MJRoberts - Creation
21 */
22
23 #include "utf8.h"
24
25 /* ------------------------------------------------------------------------ */
26 /*
27 * encode a string of wide characters into the buffer
28 */
29 size_t utf8_ptr::setwchars(const wchar_t *src, size_t src_count,
30 size_t bufsiz)
31 {
32 /* loop through the source and store the characters */
33 size_t outbytes;
34 for (outbytes = 0 ; src_count > 0 ; --src_count, ++src)
35 {
36 /* figure out how many bytes we need for this character */
37 size_t curbytes = s_wchar_size(*src);
38
39 /* add it to the total output size */
40 outbytes += curbytes;
41
42 /* if we have room, add it to the buffer */
43 if (bufsiz >= curbytes)
44 {
45 /* store it */
46 setch(*src);
47
48 /* deduct this space from the remaining buffer size */
49 bufsiz -= curbytes;
50 }
51 else
52 {
53 /*
54 * there's no room for this - make sure we don't store
55 * anything more (since we might have room for a shorter
56 * character later, but that would put a gap in the output
57 * string - better to just truncate here)
58 */
59 bufsiz = 0;
60 }
61 }
62
63 /* return the total output size used (or needed) */
64 return outbytes;
65 }
66
67 /* ------------------------------------------------------------------------ */
68 /*
69 * encode a null-terminated string of wide characters into the buffer
70 */
71 size_t utf8_ptr::setwcharsz(const wchar_t *src, size_t bufsiz)
72 {
73 /* loop through the source and store the characters */
74 size_t outbytes;
75 for (outbytes = 0 ; *src != 0 ; ++src)
76 {
77 /* figure out how many bytes we need for this character */
78 size_t curbytes = s_wchar_size(*src);
79
80 /* add it to the total output size */
81 outbytes += curbytes;
82
83 /* if we have room, add it to the buffer */
84 if (bufsiz >= curbytes)
85 {
86 /* store it */
87 setch(*src);
88
89 /* deduct this space from the remaining buffer size */
90 bufsiz -= outbytes;
91 }
92 else
93 {
94 /*
95 * there's no room for this - make sure we don't store
96 * anything more (since we might have room for a shorter
97 * character later, but that would put a gap in the output
98 * string - better to just truncate here)
99 */
100 bufsiz = 0;
101 }
102 }
103
104 /* return the total output size used (or needed) */
105 return outbytes;
106 }
107
108 /* ------------------------------------------------------------------------ */
109 /*
110 * Compare this string to the given string
111 */
112 int utf8_ptr::s_compare_to(const char *p1, size_t bytelen1,
113 const char *p2, size_t bytelen2)
114 {
115 /* keep going until one or the other string runs out of bytes */
116 while (bytelen1 != 0 && bytelen2 != 0)
117 {
118 wchar_t c1, c2;
119 size_t siz1, siz2;
120
121 /* get the current character from each string */
122 c1 = s_getch(p1);
123 c2 = s_getch(p2);
124
125 /* compare them */
126 if (c1 > c2)
127 return 1;
128 else if (c1 < c2)
129 return -1;
130
131 /* get the size of each character */
132 siz1 = s_charsize(*p1);
133 siz2 = s_charsize(*p2);
134
135 /* decrement each counter by the byte size of this character */
136 bytelen1 -= siz1;
137 bytelen2 -= siz2;
138
139 /* advance to the next character in each string */
140 p1 += siz1;
141 p2 += siz2;
142 }
143
144 /*
145 * we didn't find any character differences, but one string is
146 * longer than the other -- if they ran out at the same time,
147 * they're identical; otherwise, the one that ran out first is the
148 * lesser one
149 */
150 if (bytelen2 != 0)
151 {
152 /* the first one ran out first, so the first one sorts earlier */
153 return -1;
154 }
155 else if (bytelen1 != 0)
156 {
157 /* the second one ran out first, so the first one sort later */
158 return 1;
159 }
160 else
161 {
162 /* they both ran out at the same time */
163 return 0;
164 }
165 }
166