2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / convert / natOutput_SJIS.cc
blob3cde58dc1ba24b52cf29594faaeb32e67fec9312
1 /* Copyright (C) 1999 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 #include <config.h>
10 #include <gcj/cni.h>
11 #include <gnu/gcj/convert/Output_SJIS.h>
13 extern unsigned short Unicode_to_JIS[];
15 extern int trie_lookup (unsigned short *trie, unsigned short key);
17 static jint
18 convert_TO_SJIS (gnu::gcj::convert::Output_SJIS *encoder,
19 jchar *ptr, jint inlength)
21 int orig_inlength = inlength;
22 jint outbuf_length = encoder->buf->length;
23 for (;;)
25 if (encoder->count >= outbuf_length)
26 break;
27 if (encoder->pending >= 0)
29 elements(encoder->buf)[encoder->count++] = encoder->pending;
30 encoder->pending = -1;
31 continue;
33 if (inlength == 0)
34 break;
35 jchar ch = *ptr++;
36 inlength--;
37 unsigned short val = trie_lookup(Unicode_to_JIS, ch);
38 if (val < 0xFF)
40 if (val == 0xffff)
41 val = '?';
43 else
45 int b1 = val >> 8;
46 int b2 = val & 0xff;
47 // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999:
48 int rowOffset = b1 < 95 ? 112 : 176;
49 int cellOffset = (b1 & 1) != 0 ? (b2 > 95 ? 32 : 31) : 126;
50 b1 = ((b1 + 1) >> 1) + rowOffset;
51 b2 += cellOffset;
52 val = b1;
53 encoder->pending = b2;
55 elements(encoder->buf)[encoder->count++] = val;
57 return orig_inlength - inlength;
60 jint
61 gnu::gcj::convert::Output_SJIS::write (jcharArray inbuffer,
62 jint inpos, jint inlength)
64 return convert_TO_SJIS(this, &elements(inbuffer)[inpos], inlength);
67 jint
68 gnu::gcj::convert::Output_SJIS::write (jstring str, jint inpos,
69 jint inlength, jcharArray)
71 return convert_TO_SJIS(this, _Jv_GetStringChars(str)+inpos, inlength);