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.
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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
31 default_char2int(CONV
*conv
, const char * str
, ssize_t len
, CHAR_T
**tostr
, size_t *tolen
)
35 BINC_RETW(NULL
, conv
->buffer
, conv
->size
, len
);
36 *tostr
= conv
->buffer
;
39 for (i
= 0; i
< len
; ++i
)
40 (*tostr
)[i
] = (u_char
) str
[i
];
46 default_int2char(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
)
50 BINC_RET(NULL
, conv
->buffer
, conv
->size
, len
);
51 *tostr
= conv
->buffer
;
54 for (i
= 0; i
< len
; ++i
)
61 default_int2disp(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
)
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) {
73 (*tostr
)[j
++] = str
[i
];
80 gb2int(CONV
*conv
, const char * str
, ssize_t len
, CHAR_T
**tostr
, size_t *tolen
)
84 BINC_RETW(NULL
, conv
->buffer
, conv
->size
, len
);
85 *tostr
= conv
->buffer
;
87 for (i
= 0, j
= 0; i
< len
; ++i
) {
89 if (i
+1 < len
&& str
[i
] & 0x80) {
90 (*tostr
)[j
++] = INT9494(F_GB
,str
[i
]&0x7F,str
[i
+1]&0x7F);
93 (*tostr
)[j
++] = INTILL(str
[i
]);
96 (*tostr
)[j
++] = str
[i
];
104 int2gb(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
)
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;
116 (*tostr
)[j
++] = str
[i
] & 0xFF;
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
};
130 conv_init (SCR
*orig
, SCR
*sp
)
133 sp
->conv
= orig
->conv
;
135 sp
->conv
= &default_conv
;
139 conv_enc (SCR
*sp
, char *enc
)
142 sp
->conv
= &default_conv
;
145 if (!strcmp(enc
,"GB")) {