Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / CharsetInfo.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: CharsetInfo.C /main/1 1996/07/29 16:47:17 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 "CharsetInfo.h"
32 #include "ISet.h"
33 #include "constant.h"
34
35 #ifdef SP_NAMESPACE
36 namespace SP_NAMESPACE {
37 #endif
38
39 CharsetInfo::CharsetInfo(const UnivCharsetDesc &desc)
40 : desc_(desc)
41 {
42   // FIXME remove mappings from desc for characters greater charMax
43   init();
44 }
45
46 CharsetInfo::CharsetInfo()
47 {
48   for (size_t i = 0; i < nSmall; i++) {
49     smallUnivValid_[i] = 0;
50     smallDescValid_[i] = 0;
51   }
52 }
53
54 void CharsetInfo::set(const UnivCharsetDesc &desc)
55 {
56   desc_ = desc;
57   init();
58 }
59
60 void CharsetInfo::init()
61 {
62   size_t i;
63   for (i = 0; i < nSmall; i++) {
64     smallUnivValid_[i] = 0;
65     smallDescValid_[i] = 0;
66   }
67
68   UnivCharsetDescIter iter(desc_);
69   
70   WideChar descMin, descMax;
71   UnivChar univMin;
72   while (iter.next(descMin, descMax, univMin)) {
73     WideChar j = descMin;
74     do {
75       UnivChar k = univMin + (j - descMin);
76       if (k >= nSmall)
77         break;
78       if (!smallUnivValid_[k]) {
79         smallUnivValid_[k] = 1;
80         smallUnivToDesc_[k] = j;
81       }
82       else
83         smallUnivValid_[k] = 2;
84     } while (j++ != descMax);
85     j = descMin;
86     do {
87       if (j >= nSmall)
88         break;
89       if (!smallDescValid_[j]) {
90         smallDescValid_[j] = 1;
91         smallDescToUniv_[j] = univMin + (j - descMin);
92       }
93     } while (j++ != descMax);
94   }
95   // These are the characters that the ANSI C
96   // standard guarantees will be in the basic execution
97   // character set.
98   static char execChars[] =
99     "\t\n\r "
100     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
101     "abcdefghijklmnopqrstuvwxyz"
102     "0123456789"
103     "!\"#%&'()*+,-./:"
104     ";<=>?[\\]^_{|}~";
105   // These are the corresponding ISO 646 codes.
106   static char univCodes[] = {
107     9, 10, 13, 32,
108     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
109     78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
110     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
111     110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
112     48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
113     33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58,
114     59, 60, 61, 62, 63, 91, 92, 93, 94, 95, 123, 124, 125, 126,
115   };
116   for (i = 0; execChars[i] != '\0'; i++) {
117     WideChar c;
118     ISet<WideChar> set;
119     if (univToDesc(univCodes[i], c, set) > 0 && c <= Char(-1))
120       execToDesc_[(unsigned char)execChars[i]] = Char(c);
121   }
122 }
123
124 void CharsetInfo::getDescSet(ISet<Char> &set) const
125 {
126   UnivCharsetDescIter iter(desc_);
127   WideChar descMin, descMax;
128   UnivChar univMin;
129   while (iter.next(descMin, descMax, univMin)) {
130     if (descMin > charMax)
131       break;
132     if (descMax > charMax)
133       descMax = charMax;
134     set.addRange(Char(descMin), Char(descMax));
135   }
136 }
137
138 int CharsetInfo::digitWeight(Char c) const
139 {
140   for (int i = 0; i < 10; i++)
141     if (c == execToDesc('0' + i))
142       return i;
143   return -1;
144 }
145
146 StringC CharsetInfo::execToDesc(const char *s) const
147 {
148   StringC result;
149   while (*s != '\0')
150     result += execToDesc(*s++);
151   return result;
152 }
153
154 #ifdef SP_NAMESPACE
155 }
156 #endif