File: e2u.c

package info (click to toggle)
libuconv-ruby 0.4.9-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,752 kB
  • ctags: 135
  • sloc: ansic: 160,433; xml: 13,060; ruby: 396; makefile: 51
file content (138 lines) | stat: -rw-r--r-- 2,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Unicode Conversion Library (EUC-JP to UCS2)
 * 1997-1999 by yoshidam
 *
 */

#ifdef USE_EUC

#include <string.h>
#include "uconv.h"
#include "e2u.h"
#include "hojo2u.h"
#include "ustring.h"

#if 0
/* Convert EUC-JP into UCS2-little */
int
e2u_conv(const unsigned char* e, unsigned char* u)
{
  int i;
  int ui = 0;
  int len = strlen(e);

  for (i = 0; i < len; i++) {
    if (e[i] < 128) {	/* ASCII */
      u[ui++] = e[i];
      u[ui++] = 0;
    }
    else if (e[i] == 0x8e) { /* JIS X 0201 kana */
      u[ui++] = e[i + 1] - 0x40;
      u[ui++] = 0xff;
      i++;
    }
    else if (e[i] == 0x8f) { /* JIS X 0212 */
      int hi = e[i + 1] &  0x7f;
      int low = e[i + 2] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      unsigned short ec = hojo2u_tbl[key];
      u[ui++] = ec & 0xff;
      u[ui++] = ec >> 8;
      i++;
    }
    else if (e[i] < 0xa0) {  /* C1 */
    }
    else {		     /* JIX X 0208 */
      int hi = e[i] &  0x7f;
      int low = e[i + 1] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      unsigned short ec = e2u_tbl[key];
      u[ui++] = ec & 0xff;
      u[ui++] = ec >> 8;
      i++;
    }
  }

  return ui;
}
#endif

int e2u_conv2(const unsigned char* e, UString* u, unknown_euc unknown_e_conv)
{
  int i;
  int len = strlen((char*)e);

  UStr_alloc(u);

  for (i = 0; i < len; i++) {
    if (e[i] < 128) {	/* ASCII */
      UStr_addChar2(u, e[i], 0);
    }
    else if (e[i] == 0x8e) { /* JIS X 0201 kana */
      unsigned char ec = 0;
      if (e[i + 1] >= 0xa1 && e[i + 1] <= 0xdf)
	ec = e[i + 1] - 0x40;
      UStr_addChar2(u, ec, 0xff);
      i++;
    }
    else if (e[i] == 0x8f) { /* JIS X 0212 */
      int hi = e[i + 1] &  0x7f;
      int low = e[i + 2] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      unsigned short ec = 0;
      if (hi >= 32 && hi <= 127 && low >= 32 && low <= 127)
	ec = hojo2u_tbl[key];
      if (ec == 0) {
	ec = '?';
	if (unknown_e_conv != NULL) {
	  unsigned char ue[4];
	  ue[0] = e[i]; ue[1] = e[i+1]; ue[2] = e[i+2]; ue[3] = 0;
	  ec = unknown_e_conv(ue);
	}
      }
      UStr_addChar2(u, ec & 0xff, ec >> 8);
      i += 2;
    }
    else if (e[i] < 0xa0) {  /* C1 */
    }
    else {		     /* JIX X 0208 */
      int hi = e[i] &  0x7f;
      int low = e[i + 1] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      unsigned short ec = 0;
      if (hi >= 32 && hi <= 127 && low >= 32 && low <= 127)
	ec = e2u_tbl[key];
      if (ec == 0) {
	ec = '?';
	if (unknown_e_conv != NULL) {
	  unsigned char ue[3];
	  ue[0] = e[i]; ue[1] = e[i+1]; ue[2] = 0;
	  ec = unknown_e_conv(ue);
	}
      }
      UStr_addChar2(u, ec & 0xff, ec >> 8);
      i++;
    }
  }

  return u->len;
}

#if 0
unsigned short
conv(unsigned char* e)
{
  return '?';
}

int
main(void)
{
  UString u;
  e2u_conv2("Abc", &u, conv);
  UStr_dump(&u);
  return 0;
}
#endif

#endif /* USE_EUC */