nsgmls: resolve coverity warnings related to uninitialed members in C++ classes
[oweals/cde.git] / cde / programs / nsgmls / Syntax.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: Syntax.C /main/2 1996/08/12 13:22:19 mgreess $ */
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 "Syntax.h"
32 #include "Sd.h"
33 #include "CharsetInfo.h"
34 #include "ISetIter.h"
35 #include "macros.h"
36 #include "MarkupScan.h"
37 #include "constant.h"
38
39 #ifdef SP_NAMESPACE
40 namespace SP_NAMESPACE {
41 #endif
42
43 const int Syntax::referenceQuantity_[] = {
44   40,
45   960,
46   960,
47   16,
48   16,
49   16,
50   32,
51   96,
52   16,
53   240,
54   8,
55   2,
56   240,
57   960,
58   24
59 };
60
61 Syntax::Syntax(const Sd &sd)
62 : generalSubst_(0),
63   entitySubst_(0),
64   categoryTable_(otherCategory),
65   shuncharControls_(0),
66   multicode_(0),
67   markupScanTable_(MarkupScan::normal),
68   namecaseGeneral_(false),
69   namecaseEntity_(false)
70 {
71   static const char lcletter[] = "abcdefghijklmnopqrstuvwxyz";
72   static const char ucletter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
73   int i;
74   for (i = 0; i < 26; i++) {
75     Char lc = sd.execToDoc(lcletter[i]);
76     Char uc = sd.execToDoc(ucletter[i]); 
77     set_[nameStart] += lc;
78     set_[nameStart] += uc;
79     set_[minimumData] += lc;
80     set_[minimumData] += uc;
81     set_[significant] += lc;
82     set_[significant] += uc;
83     categoryTable_.setChar(lc, nameStartCategory);
84     categoryTable_.setChar(uc, nameStartCategory);
85     subst(lc, uc);
86   }
87   static const char digits[] = "0123456789";
88   for (i = 0; i < 10; i++) {
89     Char c = sd.execToDoc(digits[i]);
90     set_[digit] += c;
91     set_[minimumData] += c;
92     set_[significant] += c;
93     categoryTable_.setChar(c, digitCategory);
94   }
95   static const char special[] = "'()+,-./:=?";
96   for (i = 0; special[i] != '\0'; i++) {
97     Char c = sd.execToDoc(special[i]);
98     set_[minimumData] += c;
99     set_[significant] += c;
100   }
101   for (i = 0; i < nQuantity; i++)
102     quantity_[i] = referenceQuantity_[i];
103   for (i = 0; i < 3; i++)
104     standardFunctionValid_[i] = 0;
105 }
106
107
108 Syntax::Syntax(const Syntax & syn)
109         : generalSubst_(syn.generalSubst_),
110           entitySubst_(syn.entitySubst_),
111           shunchar_(syn.shunchar_),
112           shuncharControls_(syn.shuncharControls_),
113           namecaseGeneral_(syn.namecaseGeneral_),
114           namecaseEntity_(syn.namecaseEntity_),
115           delimShortrefComplex_(syn.delimShortrefComplex_),
116           delimShortrefSimple_(syn.delimShortrefSimple_),
117           nameTable_(syn.nameTable_),
118           functionTable_(syn.functionTable_),
119           upperSubst_(syn.upperSubst_),
120           identitySubst_(syn.identitySubst_),
121           categoryTable_(syn.categoryTable_),
122           multicode_(syn.multicode_),
123           markupScanTable_(syn.markupScanTable_)
124 {
125   int i;
126
127   for (i = 0; i < nSet; i++)
128     set_[i] = syn.set_[i];
129
130   for (i = 0; i < 3; i++) {
131     standardFunction_[i] = syn.standardFunction_[i];
132     standardFunctionValid_[i] = syn.standardFunctionValid_[i];
133   }
134
135   for (i = 0; i < nDelimGeneral; i++)
136     delimGeneral_[i] = syn.delimGeneral_[i];
137
138   for (i = 0; i < nNames; i++)
139     names_[i] = syn.names_[i];
140
141   for (i = 0; i < nQuantity; i++)
142     quantity_[i] = syn.quantity_[i];
143 }
144
145 void Syntax::addNameCharacters(const ISet<Char> &set)
146 {
147   ISetIter<Char> iter(set);
148   Char min, max;
149   while (iter.next(min, max)) {
150     set_[nmchar].addRange(min, max);
151     set_[significant].addRange(min, max);
152     categoryTable_.setRange(min, max, otherNameCategory);
153   }
154 }
155
156 void Syntax::addNameStartCharacters(const ISet<Char> &set)
157 {
158   ISetIter<Char> iter(set);
159   Char min, max;
160   while (iter.next(min, max)) {
161     set_[nameStart].addRange(min, max);
162     set_[significant].addRange(min, max);
163     categoryTable_.setRange(min, max, nameStartCategory);
164   }
165 }
166
167 void Syntax::addSubst(Char lc, Char uc)
168 {
169   subst(lc, uc);
170 }
171
172 void Syntax::setStandardFunction(StandardFunction f, Char c)
173 {
174   standardFunction_[f] = c;
175   standardFunctionValid_[f] = 1;
176   set_[minimumData] += c;
177   set_[s] += c;
178   categoryTable_.setChar(c, sCategory);
179   set_[functionChar] += c;
180   set_[significant] += c;
181   switch (f) {
182   case fSPACE:
183     set_[blank] += c;
184     break;
185   case fRE:
186   case fRS:
187     break;
188   }
189 }
190
191 void Syntax::enterStandardFunctionNames()
192 {
193   static ReservedName name[3] = {
194     rRE, rRS, rSPACE
195   };
196   for (int i = 0; i < 3; i++)
197     if (standardFunctionValid_[i])
198       functionTable_.insert(reservedName(name[i]), standardFunction_[i]);
199 }
200
201 void Syntax::setDelimGeneral(int i, const StringC &str)
202 {
203   delimGeneral_[i] = str;
204   for (size_t j = 0; j < str.size(); j++)
205     set_[significant] += str[j];
206 }
207
208 void Syntax::addDelimShortref(const StringC &str, const CharsetInfo &charset)
209 {
210   if (str.size() == 1 && str[0] != charset.execToDesc('B') && !isB(str[0]))
211     delimShortrefSimple_.add(str[0]);
212   else
213     delimShortrefComplex_.push_back(str);
214   for (size_t i = 0; i < str.size(); i++)
215     set_[significant] += str[i];
216 }
217
218 void Syntax::addDelimShortrefs(const ISet<Char> &shortrefChars,
219                                const CharsetInfo &charset)
220 {
221   ISetIter<Char> blankIter(set_[blank]);
222   Char min, max;
223   StringC specialChars;
224   while (blankIter.next(min, max)) {
225     do {
226       specialChars += min;
227     } while (min++ != max);
228   }
229   specialChars += charset.execToDesc('B');
230   const ISet<Char> *simpleCharsPtr = &shortrefChars;
231   ISet<Char> simpleChars;
232   for (size_t i = 0; i < specialChars.size(); i++)
233     if (shortrefChars.contains(specialChars[i])) {
234       if (simpleCharsPtr != &simpleChars) {
235         simpleChars = shortrefChars;
236         simpleCharsPtr = &simpleChars;
237       }
238       simpleChars.remove(specialChars[i]);
239     }
240   ISetIter<Char> iter(*simpleCharsPtr);
241   while (iter.next(min, max)) {
242     delimShortrefSimple_.addRange(min, max);
243     set_[significant].addRange(min, max);
244   }
245 }
246
247 void Syntax::addFunctionChar(const StringC &str, FunctionClass fun, Char c)
248 {
249   switch (fun) {
250   case cFUNCHAR:
251     break;
252   case cSEPCHAR:
253     set_[s] += c;
254     categoryTable_.setChar(c, sCategory);
255     set_[blank] += c;
256     set_[sepchar] += c;
257     break;
258   case cMSOCHAR:
259     multicode_ = 1;
260     markupScanTable_.setChar(c, MarkupScan::out);
261     break;
262   case cMSICHAR:
263     // don't need to do anything special if we just have MSICHARs
264     markupScanTable_.setChar(c, MarkupScan::in);
265     break;
266   case cMSSCHAR:
267     multicode_ = 1;
268     markupScanTable_.setChar(c, MarkupScan::suppress);
269     break;
270   }
271   set_[functionChar] += c;
272   set_[significant] += c;
273   functionTable_.insert(str, c);
274 }
275
276 void Syntax::setName(int i, const StringC &str)
277 {
278   names_[i] = str;
279   nameTable_.insert(str, i);
280 }
281
282 void Syntax::setNamecaseGeneral(Boolean b)
283 {
284   namecaseGeneral_ = b;
285   generalSubst_ = b ? &upperSubst_ : &identitySubst_;
286 }
287
288 void Syntax::setNamecaseEntity(Boolean b)
289 {
290   namecaseEntity_ = b;
291   entitySubst_ = b ? &upperSubst_ : &identitySubst_;
292 }
293
294 void Syntax::subst(Char from, Char to)
295 {
296   upperSubst_.addSubst(from, to);
297 }
298
299 void Syntax::addShunchar(Char c)
300 {
301   shunchar_.add(c);
302 }
303
304 Boolean Syntax::lookupReservedName(const StringC &str,
305                                    ReservedName *result) const
306 {
307   const int *tem = nameTable_.lookup(str);
308   if (tem) {
309     *result = ReservedName(*tem);
310     return 1;
311   }
312   else
313     return 0;
314 }
315
316 Boolean Syntax::lookupFunctionChar(const StringC &name, Char *result) const
317 {
318   const Char *p = functionTable_.lookup(name);
319   if (p) {
320     *result = *p;
321     return 1;
322   }
323   else
324     return 0;
325 }
326
327 #ifdef __GNUG__
328 typedef HashTableIter<StringC,Char> Dummy_HashTableIter_StringC_Char;
329 #endif
330
331 Boolean Syntax::charFunctionName(Char c, const StringC *&name) const
332 {
333   HashTableIter<StringC,Char> iter(functionTable_);
334   const Char *cp;
335   while (iter.next(name, cp))
336     if (*cp == c)
337       return 1;
338   return 0;
339 }
340
341 Boolean Syntax::isValidShortref(const StringC &str) const
342 {
343   if (str.size() == 1 && delimShortrefSimple_.contains(str[0]))
344     return 1;
345   for (size_t i = 0; i < delimShortrefComplex_.size(); i++)
346     if (str == delimShortrefComplex_[i])
347       return 1;
348   return 0;
349 }
350
351 void Syntax::implySgmlChar(const CharsetInfo &docCharset)
352 {
353   docCharset.getDescSet(set_[sgmlChar]);
354   ISet<WideChar> invalid;
355   checkSgmlChar(docCharset, 0, invalid);
356   ISetIter<WideChar> iter(invalid);
357   WideChar min, max;
358   while (iter.next(min, max)) {
359     do {
360       if (min <= charMax)
361         set_[sgmlChar].remove(Char(min));
362     } while (min++ != max);
363   }
364 }
365
366 void Syntax::checkSgmlChar(const CharsetInfo &docCharset,
367                            const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
368                            ISet<WideChar> &invalid) const
369 {
370   ISetIter<Char> iter(shunchar_);
371   Char min, max;
372   while (iter.next(min, max)) {
373     if (min <= max) {
374       do {
375         if (!set_[significant].contains(min)
376             && (!otherSyntax || !otherSyntax->set_[significant].contains(min))
377             && set_[sgmlChar].contains(min))
378           invalid += min;
379       } while (min++ != max);
380     }
381   }
382   if (shuncharControls_) {
383     UnivChar i;
384     for (i = 0; i < 32; i++)
385       checkUnivControlChar(i, docCharset, otherSyntax, invalid);
386     for (i = 127; i < 160; i++)
387       checkUnivControlChar(i, docCharset, otherSyntax, invalid);
388   }
389 }
390
391 void Syntax::checkUnivControlChar(UnivChar univChar,
392                                   const CharsetInfo &docCharset,
393                                   const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
394                                   ISet<WideChar> &invalid) const
395 {
396   WideChar c;
397   ISet<WideChar> set;
398   switch (docCharset.univToDesc(univChar, c, set)) {
399   case 0:
400     break;
401   case 1:
402     set += c;
403     // fall through
404   default:
405     {
406       ISetIter<WideChar> iter(set);
407       WideChar min, max;
408       while (iter.next(min, max)) {
409         do {
410           if (min > charMax)
411             break;
412           Char ch = Char(min);
413           if (!set_[significant].contains(ch)
414               && (!otherSyntax
415                   || !otherSyntax->set_[significant].contains(ch))
416               && set_[sgmlChar].contains(ch))
417             invalid += ch;
418         } while (min++ != max);
419       }
420     }
421   }
422 }
423
424 StringC Syntax::rniReservedName(ReservedName i) const
425 {
426   StringC result = delimGeneral(dRNI);
427   result += reservedName(i);
428   return result;
429 }
430
431 const SubstTable<Char> &Syntax::upperSubstTable() const
432 {
433   return upperSubst_;
434 }
435
436 const StringC &Syntax::peroDelim() const
437 {
438   return delimGeneral(dPERO);
439 }
440
441
442 #ifdef SP_NAMESPACE
443 }
444 #endif