1*78bc99aaSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*78bc99aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*78bc99aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*78bc99aaSAndrew Rist  * distributed with this work for additional information
6*78bc99aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*78bc99aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*78bc99aaSAndrew Rist  * "License"); you may not use this file except in compliance
9*78bc99aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*78bc99aaSAndrew Rist  *
11*78bc99aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*78bc99aaSAndrew Rist  *
13*78bc99aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*78bc99aaSAndrew Rist  * software distributed under the License is distributed on an
15*78bc99aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*78bc99aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*78bc99aaSAndrew Rist  * specific language governing permissions and limitations
18*78bc99aaSAndrew Rist  * under the License.
19*78bc99aaSAndrew Rist  *
20*78bc99aaSAndrew Rist  *************************************************************/
21cdf0e10cSrcweir 
22cdf0e10cSrcweir #include <precomp.h>
23cdf0e10cSrcweir #include <tokens/tkpstam2.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
26cdf0e10cSrcweir #include <tokens/stmstar2.hxx>
27cdf0e10cSrcweir #include <tools/tkpchars.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir const intt	C_nStatuslistResizeValue = 32;
31cdf0e10cSrcweir const intt	C_nTopStatus = 0;
32cdf0e10cSrcweir 
StateMachin2(intt in_nStatusSize,intt in_nInitial_StatusListSize)33cdf0e10cSrcweir StateMachin2::StateMachin2( intt			in_nStatusSize,
34cdf0e10cSrcweir 							intt			in_nInitial_StatusListSize )
35cdf0e10cSrcweir 	:	pStati(new StmStatu2*[in_nInitial_StatusListSize]),
36cdf0e10cSrcweir 		nCurrentStatus(C_nTopStatus),
37cdf0e10cSrcweir 		nPeekedStatus(C_nTopStatus),
38cdf0e10cSrcweir 		nStatusSize(in_nStatusSize),
39cdf0e10cSrcweir 		nNrofStati(0),
40cdf0e10cSrcweir 		nStatiSpace(in_nInitial_StatusListSize)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	csv_assert(in_nStatusSize > 0);
43cdf0e10cSrcweir 	csv_assert(in_nInitial_StatusListSize > 0);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	memset(pStati, 0, sizeof(StmStatu2*) * nStatiSpace);
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir intt
AddStatus(StmStatu2 * let_dpStatus)49cdf0e10cSrcweir StateMachin2::AddStatus(StmStatu2 * let_dpStatus)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	if (nNrofStati == nStatiSpace)
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		ResizeStati();
54cdf0e10cSrcweir 	}
55cdf0e10cSrcweir 	pStati[nNrofStati] = let_dpStatus;
56cdf0e10cSrcweir 	return nNrofStati++;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir void
AddToken(const char * in_sToken,UINT16 in_nTokenId,const INT16 * in_aBranches,INT16 in_nBoundsStatus)60cdf0e10cSrcweir StateMachin2::AddToken( const char *		in_sToken,
61cdf0e10cSrcweir 						UINT16				in_nTokenId,
62cdf0e10cSrcweir 						const INT16 *		in_aBranches,
63cdf0e10cSrcweir 						INT16				in_nBoundsStatus )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	if (csv::no_str(in_sToken))
66cdf0e10cSrcweir 		return;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	// Durch existierende Stati durchhangeln:
69cdf0e10cSrcweir 	nCurrentStatus = 0;
70cdf0e10cSrcweir 	nPeekedStatus = 0;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	for ( const char * pChar = in_sToken;
73cdf0e10cSrcweir 		  *pChar != NULCH;
74cdf0e10cSrcweir 		  ++pChar )
75cdf0e10cSrcweir 	{
76cdf0e10cSrcweir 		Peek(*pChar);
77cdf0e10cSrcweir 		StmStatu2 & rPst = Status(nPeekedStatus);
78cdf0e10cSrcweir 		if ( rPst.IsADefault() OR rPst.AsBounds() != 0 )
79cdf0e10cSrcweir 		{
80cdf0e10cSrcweir 			nPeekedStatus = AddStatus( new StmArrayStatu2(nStatusSize, in_aBranches, 0, false ) );
81cdf0e10cSrcweir 			CurrentStatus().SetBranch( *pChar, nPeekedStatus );
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
84cdf0e10cSrcweir 	}	// end for
85cdf0e10cSrcweir 	StmArrayStatu2 & rLastStatus = CurrentStatus();
86cdf0e10cSrcweir 	rLastStatus.SetTokenId(in_nTokenId);
87cdf0e10cSrcweir 	for (intt i = 0; i < nStatusSize; i++)
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		if (Status(rLastStatus.NextBy(i)).AsBounds() != 0)
90cdf0e10cSrcweir 			rLastStatus.SetBranch(i,in_nBoundsStatus);
91cdf0e10cSrcweir 	}	// end for
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
~StateMachin2()94cdf0e10cSrcweir StateMachin2::~StateMachin2()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	for (intt i = 0; i < nNrofStati; i++)
97cdf0e10cSrcweir 	{
98cdf0e10cSrcweir 		delete pStati[i];
99cdf0e10cSrcweir 	}
100cdf0e10cSrcweir 	delete [] pStati;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir StmBoundsStatu2 &
GetCharChain(UINT16 & o_nTokenId,CharacterSource & io_rText)104cdf0e10cSrcweir StateMachin2::GetCharChain( UINT16 &  		   o_nTokenId,
105cdf0e10cSrcweir 							CharacterSource &  io_rText )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	nCurrentStatus = C_nTopStatus;
108cdf0e10cSrcweir 	Peek(io_rText.CurChar());
109cdf0e10cSrcweir 	while (BoundsStatus() == 0)
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
112cdf0e10cSrcweir 		Peek(io_rText.MoveOn());
113cdf0e10cSrcweir 	}
114cdf0e10cSrcweir     o_nTokenId = CurrentStatus().TokenId();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	return *BoundsStatus();
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir void
ResizeStati()120cdf0e10cSrcweir StateMachin2::ResizeStati()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	intt nNewSize = nStatiSpace + C_nStatuslistResizeValue;
123cdf0e10cSrcweir 	intt i = 0;
124cdf0e10cSrcweir 	StatusList pNewStati = new StmStatu2*[nNewSize];
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	for ( ; i < nNrofStati; i++)
127cdf0e10cSrcweir 	{
128cdf0e10cSrcweir 		pNewStati[i] = pStati[i];
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 	memset( pNewStati+i,
131cdf0e10cSrcweir 			0,
132cdf0e10cSrcweir 			(nNewSize-i) * sizeof(StmStatu2*) );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	delete [] pStati;
135cdf0e10cSrcweir 	pStati = pNewStati;
136cdf0e10cSrcweir 	nStatiSpace = nNewSize;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir StmStatu2 &
Status(intt in_nStatusNr) const140cdf0e10cSrcweir StateMachin2::Status(intt in_nStatusNr) const
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	csv_assert( csv::in_range(intt(0), in_nStatusNr, intt(nNrofStati)) );
143cdf0e10cSrcweir 	return *pStati[in_nStatusNr];
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir StmArrayStatu2 &
CurrentStatus() const147cdf0e10cSrcweir StateMachin2::CurrentStatus() const
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	StmArrayStatu2 * pCurSt = Status(nCurrentStatus).AsArray();
150cdf0e10cSrcweir 	if (pCurSt == 0)
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir 		csv_assert(false);
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 	return *pCurSt;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir StmBoundsStatu2 *
BoundsStatus() const158cdf0e10cSrcweir StateMachin2::BoundsStatus() const
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	return Status(nPeekedStatus).AsBounds();
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir void
Peek(intt in_nBranch)164cdf0e10cSrcweir StateMachin2::Peek(intt	in_nBranch)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	StmArrayStatu2 & rSt = CurrentStatus();
167cdf0e10cSrcweir 	nPeekedStatus = rSt.NextBy(in_nBranch);
168cdf0e10cSrcweir }
169