the script used to extract a release
[nvi.git] / common / conv.c
blob6d8dc3d8512b66987d0a6e52e6f6d54f76f57cbd
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: conv.c,v 1.7 2000/08/27 17:15:04 skimo Exp $ (Berkeley) $Date: 2000/08/27 17:15:04 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "common.h"
30 int
31 default_char2int(CONV *conv, const char * str, ssize_t len, CHAR_T **tostr, size_t *tolen, size_t *blen)
33 int i;
35 BINC_RETW(NULL, *tostr, *blen, len);
37 *tolen = len;
38 for (i = 0; i < len; ++i)
39 (*tostr)[i] = (u_char) str[i];
41 return 0;
44 int
45 default_int2char(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen, size_t *blen)
47 int i;
49 BINC_RET(NULL, *tostr, *blen, len);
51 *tolen = len;
52 for (i = 0; i < len; ++i)
53 (*tostr)[i] = str[i];
55 return 0;
58 int
59 default_int2disp(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen, size_t *blen)
61 int i, j;
63 BINC_RET(NULL, *tostr, *blen, len * 2);
65 for (i = 0, j = 0; i < len; ++i)
66 if (CHAR_WIDTH(NULL, str[i]) > 1) {
67 (*tostr)[j++] = '[';
68 (*tostr)[j++] = ']';
69 } else
70 (*tostr)[j++] = str[i];
71 *tolen = j;
73 return 0;
76 int
77 gb2int(CONV *conv, const char * str, ssize_t len, CHAR_T **tostr, size_t *tolen, size_t *blen)
79 int i, j;
81 BINC_RETW(NULL, *tostr, *blen, len);
83 for (i = 0, j = 0; i < len; ++i) {
84 if (str[i] & 0x80) {
85 if (i+1 < len && str[i+1] & 0x80) {
86 (*tostr)[j++] = INT9494(F_GB,str[i]&0x7F,str[i+1]&0x7F);
87 ++i;
88 } else {
89 (*tostr)[j++] = INTILL(str[i]);
91 } else
92 (*tostr)[j++] = str[i];
94 *tolen = j;
96 return 0;
99 int
100 int2gb(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen, size_t *blen)
102 int i, j;
104 BINC_RET(NULL, *tostr, *blen, len * 2);
106 for (i = 0, j = 0; i < len; ++i) {
107 if (INTIS9494(str[i])) {
108 (*tostr)[j++] = INT9494R(str[i]) | 0x80;
109 (*tostr)[j++] = INT9494C(str[i]) | 0x80;
110 } else {
111 (*tostr)[j++] = str[i] & 0xFF;
114 *tolen = j;
116 return 0;
119 int
120 utf82int(CONV *conv, const char * str, ssize_t len, CHAR_T **tostr, size_t *tolen, size_t *blen)
122 int i, j;
123 CHAR_T c;
125 BINC_RETW(NULL, *tostr, *blen, len);
127 for (i = 0, j = 0; i < len; ++i) {
128 if (str[i] & 0x80) {
129 if ((str[i] & 0xe0) == 0xc0 && i+1 < len && str[i+1] & 0x80) {
130 c = (str[i] & 0x1f) << 6;
131 c |= (str[i+1] & 0x3f);
132 (*tostr)[j++] = c;
133 ++i;
134 } else if ((str[i] & 0xf0) == 0xe0 && i+2 < len &&
135 str[i+1] & 0x80 && str[i+2] & 0x80) {
136 c = (str[i] & 0xf) << 12;
137 c |= (str[i+1] & 0x3f) << 6;
138 c |= (str[i+2] & 0x3f);
139 (*tostr)[j++] = c;
140 i += 2;
141 } else {
142 (*tostr)[j++] = INTILL(str[i]);
144 } else
145 (*tostr)[j++] = str[i];
147 *tolen = j;
149 return 0;
153 int2utf8(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen, size_t *blen)
155 BINC_RET(NULL, *tostr, *blen, len * 3);
157 *tolen = ucs2utf8(str, len, *tostr);
159 return 0;
163 CONV default_conv = { 0, 0, default_char2int, default_int2char,
164 default_char2int, default_int2char, default_int2disp };
165 CONV gb_conv = { 0, 0, default_char2int, default_int2char,
166 gb2int, int2gb, default_int2disp };
167 CONV utf8_conv = { 0, 0, default_char2int, default_int2char,
168 utf82int, int2utf8, default_int2disp };
170 void
171 conv_init (SCR *orig, SCR *sp)
173 if (orig != NULL)
174 sp->conv = orig->conv;
175 else
176 sp->conv = &default_conv;
180 conv_enc (SCR *sp, char *enc)
182 if (!*enc) {
183 sp->conv = &default_conv;
184 return 0;
186 if (!strcmp(enc,"GB")) {
187 sp->conv = &gb_conv;
188 return 0;
190 if (!strcmp(enc,"UTF-8")) {
191 sp->conv = &utf8_conv;
192 return 0;
194 return 1;