File: CharsetRegistry.cxx

package info (click to toggle)
opensp 1.5.2-15.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,168 kB
  • sloc: cpp: 65,784; ansic: 17,124; sh: 11,193; xml: 2,704; makefile: 895; perl: 561; yacc: 288; sed: 16
file content (288 lines) | stat: -rw-r--r-- 7,944 bytes parent folder | download | duplicates (9)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 1994, 1997 James Clark
// See the file COPYING for copying permission.

#ifdef __GNUG__
#pragma implementation
#endif
#include "splib.h"
#include "CharsetRegistry.h"
#include "CharsetInfo.h"
#include "StringC.h"
#include "types.h"
#include "macros.h"

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

CharsetRegistry::Iter::~Iter()
{
}

class CharsetRegistryRangeIter : public CharsetRegistry::Iter {
public:
  CharsetRegistryRangeIter(const UnivCharsetDesc::Range *p, size_t n)
    : p_(p), n_(n) { }
  Boolean next(WideChar &min, WideChar &max, UnivChar &univ) {
    if (n_) {
      min = p_->descMin;
      max = p_->descMin + (p_->count - 1);
      univ = p_->univMin;
      p_++;
      n_--;
      return 1;
    }
    else
      return 0;
  }
private:
  const UnivCharsetDesc::Range *p_;
  size_t n_;
};

class CharsetRegistryDescIter : public CharsetRegistry::Iter {
public:
  CharsetRegistryDescIter(const unsigned short *p)
    : p_(p + 2), n_(p[0]), c_(p[1]) { }
  Boolean next(WideChar &min, WideChar &max, UnivChar &univ) {
    if (n_ == 0) {
      n_ = *p_;
      if (n_ == 0)
	return 0;
      p_++;
      c_ = *p_++;
    }
    int i = 1;
    for (; i < n_; i++)
      if (p_[i] != p_[i - 1] + 1)
	break;
    min = c_;
    max = min + (i - 1);
    univ = p_[0];
    p_ += i;
    c_ += i;
    n_ -= i;
    return 1;
  }
private:
  const unsigned short *p_;
  size_t n_;
  WideChar c_;
};

static struct {
  const char *esc;
  CharsetRegistry::ISORegistrationNumber number;
} escTable[] = {
  { "\x1B\x25\x40", CharsetRegistry::ISO646_ASCII_G0 },
  { "\x1B\x28\x40", CharsetRegistry::ISO646_ASCII_G0 },
  { "\x1B\x28\x42", CharsetRegistry::ISO646_ASCII_G0 }, // ASCII
  { "\x1B\x21\x40", CharsetRegistry::ISO646_C0 },
  { "\x1B\x2D\x41", CharsetRegistry::ISO8859_1 },
  { "\x1B\x2D\x42", CharsetRegistry::ISO8859_2 },
  { "\x1B\x2D\x43", CharsetRegistry::ISO8859_3 },
  { "\x1B\x2D\x44", CharsetRegistry::ISO8859_4 },
  { "\x1B\x2D\x4C", CharsetRegistry::ISO8859_5 },
  { "\x1B\x2D\x47", CharsetRegistry::ISO8859_6 },
  { "\x1B\x2D\x46", CharsetRegistry::ISO8859_7 },
  { "\x1B\x2D\x48", CharsetRegistry::ISO8859_8 },
  { "\x1B\x2D\x4D", CharsetRegistry::ISO8859_9 },
  { "\x1B\x28\x4A", CharsetRegistry::ISO646_JIS_G0 },
  { "\x1B\x28\x49", CharsetRegistry::JIS0201 },
  { "\x1B\x24\x42", CharsetRegistry::JIS0208 },
  { "\x1B\x26\x40\x1B\x24\x42", CharsetRegistry::JIS0208 },
  { "\x1B\x24\x28\x44", CharsetRegistry::JIS0212 },
  { "\x1B\x24\x41", CharsetRegistry::GB2312 },
  { "\x1B\x24\x28\x43", CharsetRegistry::KSC5601 },
  { "\x1B\x25\x2F\x40", CharsetRegistry::ISO10646_UCS2 },
  { "\x1B\x25\x2F\x41", CharsetRegistry::ISO10646_UCS4 },
  { "\x1B\x25\x2F\x43", CharsetRegistry::ISO10646_UCS2 },
  { "\x1B\x25\x2F\x44", CharsetRegistry::ISO10646_UCS4 },
  { "\x1B\x25\x2F\x45", CharsetRegistry::ISO10646_UCS2 },
  { "\x1B\x25\x2F\x46", CharsetRegistry::ISO10646_UCS4 },
};

static const UnivCharsetDesc::Range iso646_ascii[] = {
  { 0, 128, 0 },
};

static const UnivCharsetDesc::Range iso646_C0[] = {
  { 0, 32, 0 },
  { 127, 1, 127 },
};

static const UnivCharsetDesc::Range iso6429[] = {
  { 0, 32, 128 },
};

static const UnivCharsetDesc::Range iso8859_1[] = {
  { 32, 96, 160 },
};

static const UnivCharsetDesc::Range iso10646_ucs2[] = {
  { 0, 65536, 0 },
};

static const UnivCharsetDesc::Range iso10646_ucs4[] = {
  { 0, 0x80000000, 0 },
};

static struct {
  CharsetRegistry::ISORegistrationNumber number;
  const UnivCharsetDesc::Range *ranges;
  size_t nRanges;
} rangeTable[] = {
  { CharsetRegistry::ISO646_ASCII_G0, iso646_ascii, SIZEOF(iso646_ascii) },
  { CharsetRegistry::ISO646_C0, iso646_C0, SIZEOF(iso646_C0) },
  { CharsetRegistry::ISO6429, iso6429, SIZEOF(iso6429) },
  { CharsetRegistry::ISO8859_1, iso8859_1, SIZEOF(iso8859_1) },
  { CharsetRegistry::ISO10646_UCS2, iso10646_ucs2, SIZEOF(iso10646_ucs2) },
  { CharsetRegistry::ISO10646_UCS4, iso10646_ucs4, SIZEOF(iso10646_ucs4) },
};

static const unsigned short iso8859_2[] = {
#include "iso8859-2.h"
};
static const unsigned short iso8859_3[] = {
#include "iso8859-3.h"
};
static const unsigned short iso8859_4[] = {
#include "iso8859-4.h"
};
static const unsigned short iso8859_5[] = {
#include "iso8859-5.h"
};
static const unsigned short iso8859_6[] = {
#include "iso8859-6.h"
};
static const unsigned short iso8859_7[] = {
#include "iso8859-7.h"
};
static const unsigned short iso8859_8[] = {
#include "iso8859-8.h"
};
static const unsigned short iso8859_9[] = {
#include "iso8859-9.h"
};
static const unsigned short koi8_r[] = {
#include "koi8-r.h"
};
static const unsigned short iso646_jis_G0[] = {
#include "iso646-jis.h"
};
static const unsigned short jis0201[] = {
#include "jis0201.h"
};

#ifdef SP_MULTI_BYTE

static const unsigned short jis0208[] = {
#include "jis0208.h"
};
static const unsigned short jis0212[] = {
#include "jis0212.h"
};
static const unsigned short gb2312[] = {
#include "gb2312.h"
};
static const unsigned short ksc5601[] = {
#include "ksc5601.h"
};
static const unsigned short big5[] = {
#include "big5.h"
};

#endif /* SP_MULTI_BYTE */

static const struct {
  CharsetRegistry::ISORegistrationNumber number;
  const unsigned short *desc;
} descTable[] = {
  { CharsetRegistry::ISO8859_2, iso8859_2 },
  { CharsetRegistry::ISO8859_3, iso8859_3 },
  { CharsetRegistry::ISO8859_4, iso8859_4 },
  { CharsetRegistry::ISO8859_5, iso8859_5 },
  { CharsetRegistry::ISO8859_6, iso8859_6 },
  { CharsetRegistry::ISO8859_7, iso8859_7 },
  { CharsetRegistry::ISO8859_8, iso8859_8 },
  { CharsetRegistry::ISO8859_9, iso8859_9 },
  { CharsetRegistry::KOI8_R, koi8_r },
  { CharsetRegistry::ISO646_JIS_G0, iso646_jis_G0 },
  { CharsetRegistry::JIS0201, jis0201 },
#ifdef SP_MULTI_BYTE
  { CharsetRegistry::JIS0208, jis0208 },
  { CharsetRegistry::JIS0212, jis0212 },
  { CharsetRegistry::GB2312, gb2312 },
  { CharsetRegistry::KSC5601, ksc5601 },
  { CharsetRegistry::BIG5, big5 },
#endif
};


CharsetRegistry::ISORegistrationNumber
CharsetRegistry::getRegistrationNumber(const StringC &sequence,
				       const CharsetInfo &charset)
{
  // Canonicalize the escape sequence by mapping esc -> ESC,
  // removing leading zeros from escape sequences, and removing
  // initial spaces.
  StringC s;
  for (size_t i = 0; i < sequence.size(); i++) {
    Char c = sequence[i];
    if (c == charset.execToDesc('e'))
      s += charset.execToDesc('E');
    else if (c == charset.execToDesc('s'))
      s += charset.execToDesc('S');
    else if (c == charset.execToDesc('c'))
      s += charset.execToDesc('C');
    else if (charset.digitWeight(c) >= 0
	     && s.size() > 0
	     && s[s.size() - 1] == charset.execToDesc('0')
	     && (s.size() == 1
		 || charset.digitWeight(s[s.size() - 2]) >= 0))
      s[s.size() - 1] = c;
    else if (c != charset.execToDesc(' ') || s.size() > 0)
      s += c;
  }
  for (size_t i = 0; i < SIZEOF(escTable); i++) {
    StringC esc;
    for (const char *p = escTable[i].esc; *p; p++) {
      if (*p == 0x1B)
	esc += charset.execToDesc("ESC");
      else {
	static const char digits[] = "0123456789";
	int c = (unsigned char)*p >> 4;
	if (c >= 10)
	  esc += charset.execToDesc('1');
	esc += charset.execToDesc(digits[c % 10]);
	esc += charset.execToDesc('/');
	c = (*p & 0xf);
	if (c >= 10)
	  esc += charset.execToDesc('1');
	esc += charset.execToDesc(digits[c % 10]);
      }
      if (p[1])
	esc += charset.execToDesc(' ');
    }
    if (s == esc)
      return escTable[i].number;
  }
  return UNREGISTERED;
}

CharsetRegistry::Iter *CharsetRegistry::makeIter(ISORegistrationNumber number)
{
  for (size_t i = 0; i < SIZEOF(rangeTable); i++) {
    if (number == rangeTable[i].number)
      return new CharsetRegistryRangeIter(rangeTable[i].ranges, rangeTable[i].nRanges);
  }
  for (size_t i = 0; i < SIZEOF(descTable); i++) {
    if (number == descTable[i].number)
      return new CharsetRegistryDescIter(descTable[i].desc);
  }
  return 0;
}

#ifdef SP_NAMESPACE
}
#endif