Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / CharsetRegistry.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: CharsetRegistry.C /main/1 1996/07/29 16:47:29 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30 #include "splib.h"
31 #include "CharsetRegistry.h"
32 #include "ExternalId.h"
33 #include "CharsetInfo.h"
34 #include "UnivCharsetDesc.h"
35 #include "StringC.h"
36 #include "types.h"
37 #include "macros.h"
38
39 #ifdef SP_NAMESPACE
40 namespace SP_NAMESPACE {
41 #endif
42
43 static UnivCharsetDesc::Range iso646_irv[] = {
44   { 0, 128, 0 }
45 };
46
47 static UnivCharsetDesc::Range iso646_C0[] = {
48   { 0, 32, 0 },
49   { 127, 1, 127 },
50 };
51
52 static struct {
53   const char *sequence;
54   const UnivCharsetDesc::Range *ranges;
55   size_t nRanges;
56 } table[] = {
57   { "ESC 2/5 4/0", iso646_irv, SIZEOF(iso646_irv) },
58   { "ESC 2/8 4/0", iso646_irv, SIZEOF(iso646_irv) },
59   { "ESC 2/8 4/2", iso646_irv, SIZEOF(iso646_irv) }, // ASCII
60   { "ESC 2/1 4/0", iso646_C0, SIZEOF(iso646_C0) },
61 };
62
63 Boolean CharsetRegistry::findCharset(const PublicId &id,
64                                      const CharsetInfo &charset,
65                                      UnivCharsetDesc &desc)
66 {
67   PublicId::OwnerType ownerType;
68   if (!id.getOwnerType(ownerType) || ownerType != PublicId::ISO)
69     return 0;
70   StringC sequence;
71   if (!id.getDesignatingSequence(sequence))
72     return 0;
73   // Canonicalize the escape sequence by mapping esc -> ESC,
74   // removing leading zeros from escape sequences, and removing
75   // initial spaces.
76   StringC s;
77   size_t i;
78   for (i = 0; i < sequence.size(); i++) {
79     Char c = sequence[i];
80     if (c == charset.execToDesc('e'))
81       s += charset.execToDesc('E');
82     else if (c == charset.execToDesc('s'))
83       s += charset.execToDesc('S');
84     else if (c == charset.execToDesc('c'))
85       s += charset.execToDesc('C');
86     else if (charset.digitWeight(c) >= 0
87              && s.size() > 0
88              && s[s.size() - 1] == charset.execToDesc('0')
89              && (s.size() == 1
90                  || charset.digitWeight(s[s.size() - 2]) >= 0))
91       s[s.size() - 1] = c;
92     else if (c != charset.execToDesc(' ') || s.size() > 0)
93       s += c;
94   }
95   for (i = 0; i < SIZEOF(table); i++)
96     if (s == charset.execToDesc(table[i].sequence)) {
97       desc.set(table[i].ranges, table[i].nRanges);
98       return 1;
99     }
100   return 0;
101 }
102
103 #ifdef SP_NAMESPACE
104 }
105 #endif