Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / Recognizer.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: Recognizer.C /main/1 1996/07/29 17:02:40 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 "Resource.h"
32 #include "Trie.h"
33 #include "Owner.h"
34 #include "XcharMap.h"
35 #include "Recognizer.h"
36 #include "InputSource.h"
37
38 #ifdef SP_NAMESPACE
39 namespace SP_NAMESPACE {
40 #endif
41
42 Recognizer::Recognizer(Trie *trie, const XcharMap<EquivCode> &map)
43 : trie_(trie), map_(map), multicode_(0)
44 {
45 }
46
47 Recognizer::Recognizer(Trie *trie, const XcharMap<EquivCode> &map,
48                        Vector<Token> &suppressTokens)
49 : trie_(trie), map_(map), multicode_(1)
50 {
51   suppressTokens.swap(suppressTokens_);
52 }
53
54 Token Recognizer::recognize(InputSource *in, Messenger &mgr) const
55 {
56   if (multicode_) {
57     in->startToken();
58     if (in->scanSuppress())
59       return suppressTokens_[map_[in->tokenChar(mgr)]];
60   }
61   else
62     in->startTokenNoMulticode();
63   register const Trie *pos = trie_.pointer();
64   do {
65     pos = pos->next(map_[in->tokenChar(mgr)]);
66   } while (pos->hasNext());
67   if (!pos->blank()) {
68     in->endToken(pos->tokenLength());
69     return pos->token();
70   }
71   const BlankTrie *b = pos->blank();
72   const Trie *newPos = b;
73   size_t maxBlanks = b->maxBlanksToScan();
74   size_t nBlanks;
75   for (nBlanks = 0; nBlanks < maxBlanks; nBlanks++) {
76     EquivCode code = map_[in->tokenChar(mgr)];
77     if (!b->codeIsBlank(code)) {
78       if (newPos->hasNext())
79         newPos = newPos->next(code);
80       break;
81     }
82   }
83   while (newPos->hasNext())
84     newPos = newPos->next(map_[in->tokenChar(mgr)]);
85   if (newPos->token() != 0) {
86     in->endToken(newPos->tokenLength() + b->additionalLength() + nBlanks);
87     return newPos->token();
88   }
89   else {
90     in->endToken(pos->tokenLength() + (pos->includeBlanks() ? nBlanks : 0));
91     return pos->token();
92   }
93 }
94
95
96 #ifdef SP_NAMESPACE
97 }
98 #endif