2 * fromucs.c - convert Unicode to other character sets.
\r
6 #include "internal.h"
\r
8 struct charset_emit_param {
\r
16 static void charset_emit(void *ctx, long int output)
\r
18 struct charset_emit_param *param = (struct charset_emit_param *)ctx;
\r
23 if (output == ERROR) {
\r
25 outlen = param->errlen;
\r
32 if (param->outlen >= outlen) {
\r
33 while (outlen > 0) {
\r
34 *param->output++ = *p++;
\r
43 int charset_from_unicode(wchar_t **input, int *inlen, char *output, int outlen,
\r
44 int charset, charset_state *state,
\r
45 const char *errstr, int errlen)
\r
47 charset_spec const *spec = charset_find_spec(charset);
\r
48 charset_state localstate;
\r
49 struct charset_emit_param param;
\r
51 param.output = output;
\r
52 param.outlen = outlen;
\r
56 * charset_emit will expect a valid errstr.
\r
59 /* *shrug* this is good enough, and consistent across all SBCS... */
\r
63 param.errstr = errstr;
\r
64 param.errlen = errlen;
\r
69 localstate = *state; /* structure copy */
\r
71 state = &localstate;
\r
73 while (*inlen > 0) {
\r
74 int lenbefore = param.output - output;
\r
75 spec->write(spec, **input, &localstate, charset_emit, ¶m);
\r
76 if (param.stopped) {
\r
78 * The emit function has _tried_ to output some
\r
79 * characters, but ran up against the end of the
\r
80 * buffer. Leave immediately, and return what happened
\r
81 * _before_ attempting to process this character.
\r
86 *state = localstate; /* structure copy */
\r
90 return param.output - output;
\r