2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / convert / natInput_SJIS.cc
blobc7f11ade0785866524c428e7aee1f409df2272da
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/Input_SJIS.h>
13 #define ERROR_CHAR 0xFFFD
15 extern unsigned short JIS0208_to_Unicode[84][94];
16 extern unsigned short JIS0212_to_Unicode[76][94];
18 jint
19 gnu::gcj::convert::Input_SJIS::read(jcharArray outbuffer, jint outpos,
20 jint count)
22 jint start_outpos = outpos;
23 for (;;)
25 if (outpos - start_outpos >= count)
26 break;
27 if (inpos >= inlength)
28 break;
29 int b = ((unsigned char*) elements(inbuffer))[inpos++];
30 if (first_byte == 0)
32 if (b < 128)
34 #if 1
35 // Technically, we should translate 0x5c to Yen symbol;
36 // in practice, it is not clear.
37 if (b == 0x5c)
38 b = 0x00A5; // Yen sign.
39 #endif
40 elements(outbuffer)[outpos++] = (char) b;
42 else if (b >= 0xA1 && b <= 0xDF)
44 b += 0xFF61 - 0xA1;
45 elements(outbuffer)[outpos++] = b;
47 else
48 first_byte = b;
50 else
52 // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999, p 420:
53 bool adjust = b < 159;
54 int rowOffset = first_byte < 160 ? 112 : 176;
55 int cellOffset = adjust ? (b > 127 ? 32 : 31) : 126;
56 first_byte = ((first_byte - rowOffset) << 1) - adjust;
57 b -= cellOffset;
59 first_byte -= 33;
60 b -= 33;
62 if ((unsigned) first_byte >= 84 || (unsigned) b >= 94)
63 b = ERROR_CHAR;
64 else
66 b = JIS0208_to_Unicode[first_byte][b];
67 if (b == 0)
68 b = ERROR_CHAR;
70 elements(outbuffer)[outpos++] = b;
72 first_byte = 0;
75 return outpos - start_outpos;