small screen updating optimization in gtk front end
[nvi.git] / common / conv.c
blobd5cade51d0bee54bd43a33d97c317e1ede1fb685
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.5 2000/07/21 17:35:02 skimo Exp $ (Berkeley) $Date: 2000/07/21 17:35:02 $";
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)
33 int i;
35 BINC_RETW(NULL, conv->buffer, conv->size, len);
36 *tostr = conv->buffer;
38 *tolen = len;
39 for (i = 0; i < len; ++i)
40 (*tostr)[i] = (u_char) str[i];
42 return 0;
45 int
46 default_int2char(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen)
48 int i;
50 BINC_RET(NULL, conv->buffer, conv->size, len);
51 *tostr = conv->buffer;
53 *tolen = len;
54 for (i = 0; i < len; ++i)
55 (*tostr)[i] = str[i];
57 return 0;
60 int
61 default_int2disp(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen)
63 int i, j;
65 BINC_RET(NULL, conv->buffer, conv->size, len * 2);
66 *tostr = conv->buffer;
68 for (i = 0, j = 0; i < len; ++i)
69 if (CHAR_WIDTH(NULL, str[i]) > 1) {
70 (*tostr)[j++] = '[';
71 (*tostr)[j++] = ']';
72 } else
73 (*tostr)[j++] = str[i];
74 *tolen = j;
76 return 0;
79 int
80 gb2int(CONV *conv, const char * str, ssize_t len, CHAR_T **tostr, size_t *tolen)
82 int i, j;
84 BINC_RETW(NULL, conv->buffer, conv->size, len);
85 *tostr = conv->buffer;
87 for (i = 0, j = 0; i < len; ++i) {
88 if (str[i] & 0x80) {
89 if (i+1 < len && str[i] & 0x80) {
90 (*tostr)[j++] = INT9494(F_GB,str[i]&0x7F,str[i+1]&0x7F);
91 ++i;
92 } else {
93 (*tostr)[j++] = INTILL(str[i]);
95 } else
96 (*tostr)[j++] = str[i];
98 *tolen = j;
100 return 0;
104 int2gb(CONV *conv, const CHAR_T * str, ssize_t len, char **tostr, size_t *tolen)
106 int i, j;
108 BINC_RET(NULL, conv->buffer, conv->size, len * 2);
109 *tostr = conv->buffer;
111 for (i = 0, j = 0; i < len; ++i) {
112 if (INTIS9494(str[i])) {
113 (*tostr)[j++] = INT9494R(str[i]) | 0x80;
114 (*tostr)[j++] = INT9494C(str[i]) | 0x80;
115 } else {
116 (*tostr)[j++] = str[i] & 0xFF;
119 *tolen = j;
121 return 0;
124 CONV default_conv = { 0, 0, default_char2int, default_int2char,
125 default_char2int, default_int2char, default_int2disp };
126 CONV gb_conv = { 0, 0, default_char2int, default_int2char,
127 gb2int, int2gb, default_int2disp };
129 void
130 conv_init (SCR *orig, SCR *sp)
132 if (orig != NULL)
133 sp->conv = orig->conv;
134 else
135 sp->conv = &default_conv;
139 conv_enc (SCR *sp, char *enc)
141 if (!*enc) {
142 sp->conv = &default_conv;
143 return 0;
145 if (!strcmp(enc,"GB")) {
146 sp->conv = &gb_conv;
147 return 0;
149 return 1;