Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / nsgmls / CharsetRegistry.C
1 /* $XConsortium: CharsetRegistry.C /main/1 1996/07/29 16:47:29 cde-hp $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8 #include "splib.h"
9 #include "CharsetRegistry.h"
10 #include "ExternalId.h"
11 #include "CharsetInfo.h"
12 #include "UnivCharsetDesc.h"
13 #include "StringC.h"
14 #include "types.h"
15 #include "macros.h"
16
17 #ifdef SP_NAMESPACE
18 namespace SP_NAMESPACE {
19 #endif
20
21 static UnivCharsetDesc::Range iso646_irv[] = {
22   { 0, 128, 0 }
23 };
24
25 static UnivCharsetDesc::Range iso646_C0[] = {
26   { 0, 32, 0 },
27   { 127, 1, 127 },
28 };
29
30 static struct {
31   const char *sequence;
32   const UnivCharsetDesc::Range *ranges;
33   size_t nRanges;
34 } table[] = {
35   { "ESC 2/5 4/0", iso646_irv, SIZEOF(iso646_irv) },
36   { "ESC 2/8 4/0", iso646_irv, SIZEOF(iso646_irv) },
37   { "ESC 2/8 4/2", iso646_irv, SIZEOF(iso646_irv) }, // ASCII
38   { "ESC 2/1 4/0", iso646_C0, SIZEOF(iso646_C0) },
39 };
40
41 Boolean CharsetRegistry::findCharset(const PublicId &id,
42                                      const CharsetInfo &charset,
43                                      UnivCharsetDesc &desc)
44 {
45   PublicId::OwnerType ownerType;
46   if (!id.getOwnerType(ownerType) || ownerType != PublicId::ISO)
47     return 0;
48   StringC sequence;
49   if (!id.getDesignatingSequence(sequence))
50     return 0;
51   // Canonicalize the escape sequence by mapping esc -> ESC,
52   // removing leading zeros from escape sequences, and removing
53   // initial spaces.
54   StringC s;
55   size_t i;
56   for (i = 0; i < sequence.size(); i++) {
57     Char c = sequence[i];
58     if (c == charset.execToDesc('e'))
59       s += charset.execToDesc('E');
60     else if (c == charset.execToDesc('s'))
61       s += charset.execToDesc('S');
62     else if (c == charset.execToDesc('c'))
63       s += charset.execToDesc('C');
64     else if (charset.digitWeight(c) >= 0
65              && s.size() > 0
66              && s[s.size() - 1] == charset.execToDesc('0')
67              && (s.size() == 1
68                  || charset.digitWeight(s[s.size() - 2]) >= 0))
69       s[s.size() - 1] = c;
70     else if (c != charset.execToDesc(' ') || s.size() > 0)
71       s += c;
72   }
73   for (i = 0; i < SIZEOF(table); i++)
74     if (s == charset.execToDesc(table[i].sequence)) {
75       desc.set(table[i].ranges, table[i].nRanges);
76       return 1;
77     }
78   return 0;
79 }
80
81 #ifdef SP_NAMESPACE
82 }
83 #endif