dsdm: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / nsgmls / CharsetDecl.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 libraries 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: CharsetDecl.C /main/1 1996/07/29 16:47:07 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 "CharsetDecl.h"
32 #include "macros.h"
33 #include "ISet.h"
34 #include "constant.h"
35
36 #ifdef SP_NAMESPACE
37 namespace SP_NAMESPACE {
38 #endif
39
40 CharsetDeclRange::CharsetDeclRange()
41 : descMin_(0),
42   count_(0),
43   type_(unused),
44   baseMin_(0)
45 {
46 }
47
48 CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count,
49                                    WideChar baseMin)
50 : descMin_(descMin),
51   count_(count),
52   type_(number),
53   baseMin_(baseMin)
54 {
55 }
56
57 CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count)
58 : descMin_(descMin),
59   count_(count),
60   type_(unused),
61   baseMin_(0)
62 {
63 }
64
65 CharsetDeclRange::CharsetDeclRange(WideChar descMin, Number count,
66                                    const StringC &str)
67 : descMin_(descMin),
68   count_(count),
69   type_(string),
70   str_(str),
71   baseMin_(0)
72 {
73 }
74
75 void CharsetDeclRange::rangeDeclared(WideChar min, Number count,
76                                      ISet<WideChar> &declared) const
77 {
78   if (count > 0 && min + count > descMin_ && min < descMin_ + count_) {
79     WideChar commMin = (descMin_ > min) ? descMin_ : min;
80     WideChar commMax = min + ((min + count < descMin_ + count_
81                                ? count
82                                : descMin_ + count_ - min) - 1);
83     ASSERT(commMin <= commMax);
84     declared.addRange(commMin, commMax);
85   }
86 }
87
88 void CharsetDeclRange::usedSet(ISet<Char> &set) const
89 {
90   if (type_ != unused && count_ > 0 && descMin_ <= charMax) {
91     Char max;
92     if (charMax - descMin_ < count_ - 1)
93       max = charMax;
94     else
95       max = Char(descMin_ + (count_ - 1));
96     set.addRange(Char(descMin_), max);
97   }
98 }
99
100 void CharsetDeclRange::stringToChar(const StringC &str, ISet<WideChar> &to)
101      const
102 {
103   if (type_ == string && str_ == str && count_ > 0)
104     to.addRange(descMin_, descMin_ + (count_ - 1));
105 }
106
107 void CharsetDeclRange::numberToChar(Number n, ISet<WideChar> &to,
108                                     Number &count)
109      const
110 {
111   if (type_ == number && n >= baseMin_ && n - baseMin_ < count_) {
112     Number thisCount = count_ - (n - baseMin_);
113     if (to.isEmpty() || thisCount < count)
114       count = thisCount;
115     to.add(descMin_ + (n - baseMin_));
116   }
117 }
118
119 Boolean CharsetDeclRange::getCharInfo(WideChar fromChar,
120                                       CharsetDeclRange::Type &type,
121                                       Number &n,
122                                       StringC &str,
123                                       Number &count) const
124 {
125   if (fromChar >= descMin_ && fromChar - descMin_ < count_) {
126     type = type_;
127     if (type == number)
128       n = baseMin_ + (fromChar - descMin_);
129     else if (type == string)
130       str = str_;
131     count = count_ - (fromChar - descMin_);
132     return 1;
133   }
134   else
135     return 0;
136 }
137
138 CharsetDeclSection::CharsetDeclSection()
139 {
140 }
141
142 void CharsetDeclSection::setPublicId(const PublicId &id)
143 {
144   baseset_ = id;
145 }
146
147 void CharsetDeclSection::addRange(const CharsetDeclRange &range)
148 {
149   ranges_.push_back(range);
150 }
151
152 void CharsetDeclSection::rangeDeclared(WideChar min, Number count,
153                                        ISet<WideChar> &declared) const
154 {
155   for (size_t i = 0; i < ranges_.size(); i++)
156     ranges_[i].rangeDeclared(min, count, declared);
157 }
158
159 void CharsetDeclSection::usedSet(ISet<Char> &set) const
160 {
161   for (size_t i = 0; i < ranges_.size(); i++)
162     ranges_[i].usedSet(set);
163 }
164
165 void CharsetDeclSection::stringToChar(const StringC &str, ISet<WideChar> &to)
166      const
167 {
168   for (size_t i = 0; i < ranges_.size(); i++)
169     ranges_[i].stringToChar(str, to);
170 }
171
172 void CharsetDeclSection::numberToChar(const PublicId *id, Number n,
173                                       ISet<WideChar> &to, Number &count) const
174 {
175   PublicId::OwnerType ownerType;
176   StringC seq1, seq2;
177   if (id->string() == baseset_.string()
178       // Assume that 2 ISO character sets are the same if
179       // their designating sequences are the same.
180       || (id->getOwnerType(ownerType)
181           && ownerType == PublicId::ISO
182           && baseset_.getOwnerType(ownerType)
183           && ownerType == PublicId::ISO
184           && id->getDesignatingSequence(seq1)
185           && baseset_.getDesignatingSequence(seq2)
186           && seq1 == seq2)) {
187     for (size_t i = 0; i < ranges_.size(); i++)
188       ranges_[i].numberToChar(n, to, count);
189   }
190 }
191
192 Boolean CharsetDeclSection::getCharInfo(WideChar fromChar,
193                                         const PublicId *&id,
194                                         CharsetDeclRange::Type &type,
195                                         Number &n,
196                                         StringC &str,
197                                         Number &count) const
198 {
199   for (size_t i = 0; i < ranges_.size(); i++)
200     if (ranges_[i].getCharInfo(fromChar, type, n, str, count)) {
201       id = &baseset_;
202       return 1;
203     }
204   return 0;
205 }
206
207 CharsetDecl::CharsetDecl()
208 {
209 }
210
211 void CharsetDecl::addSection(const PublicId &id)
212 {
213   sections_.resize(sections_.size() + 1);
214   sections_.back().setPublicId(id);
215 }
216
217 void CharsetDecl::swap(CharsetDecl &to)
218 {
219   sections_.swap(to.sections_);
220   declaredSet_.swap(to.declaredSet_);
221 }
222
223 void CharsetDecl::clear()
224 {
225   sections_.clear();
226 }
227
228 void CharsetDecl::addRange(WideChar min, Number count, WideChar baseMin)
229 {
230   if (count > 0)
231     declaredSet_.addRange(min, min + (count - 1));
232   CharsetDeclRange range(min, count, baseMin);
233   sections_.back().addRange(range);
234 }
235
236 void CharsetDecl::addRange(WideChar min, Number count)
237 {
238   if (count > 0)
239     declaredSet_.addRange(min, min + (count - 1));
240   CharsetDeclRange range(min, count);
241   sections_.back().addRange(range);
242 }
243
244 void CharsetDecl::addRange(WideChar min, Number count, const StringC &str)
245 {
246   if (count > 0)
247     declaredSet_.addRange(min, min + (count - 1));
248   CharsetDeclRange range(min, count, str);
249   sections_.back().addRange(range);
250 }
251
252 void CharsetDecl::rangeDeclared(WideChar min, Number count,
253                                 ISet<WideChar> &declared) const
254 {
255   for (size_t i = 0; i < sections_.size(); i++)
256     sections_[i].rangeDeclared(min, count, declared);
257 }
258
259 void CharsetDecl::usedSet(ISet<Char> &set) const
260 {
261   for (size_t i = 0; i < sections_.size(); i++)
262     sections_[i].usedSet(set);
263 }
264
265 Boolean CharsetDecl::getCharInfo(WideChar fromChar,
266                                  const PublicId *&id,
267                                  CharsetDeclRange::Type &type,
268                                  Number &n,
269                                  StringC &str,
270                                  Number &count) const
271 {
272   for (size_t i = 0; i < sections_.size(); i++)
273     if (sections_[i].getCharInfo(fromChar, id, type, n, str, count))
274       return 1;
275   return 0;
276 }
277
278 void CharsetDecl::stringToChar(const StringC &str, ISet<WideChar> &to) const
279 {
280   for (size_t i = 0; i < sections_.size(); i++)
281     sections_[i].stringToChar(str, to);
282 }
283
284 void CharsetDecl::numberToChar(const PublicId *id, Number n,
285                                ISet<WideChar> &to, Number &count) const
286 {
287   for (size_t i = 0; i < sections_.size(); i++)
288     sections_[i].numberToChar(id, n, to, count);
289 }
290
291 #ifdef SP_NAMESPACE
292 }
293 #endif