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/tkpstama.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
26cdf0e10cSrcweir // #include <srcfind.hxx>
27cdf0e10cSrcweir #include <tokens/stmstarr.hxx>
28cdf0e10cSrcweir //#include <parseinc.hxx>
29cdf0e10cSrcweir #include <tools/tkpchars.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir const intt	C_nStatuslistResizeValue = 32;
33cdf0e10cSrcweir const intt	C_nTopStatus = 0;
34cdf0e10cSrcweir 
StateMachine(intt in_nStatusSize,intt in_nInitial_StatusListSize)35cdf0e10cSrcweir StateMachine::StateMachine( intt			in_nStatusSize,
36cdf0e10cSrcweir 							intt			in_nInitial_StatusListSize )
37cdf0e10cSrcweir 	:	pStati(new StmStatus*[in_nInitial_StatusListSize]),
38cdf0e10cSrcweir 		nCurrentStatus(C_nTopStatus),
39cdf0e10cSrcweir 		nPeekedStatus(C_nTopStatus),
40cdf0e10cSrcweir 		nStatusSize(in_nStatusSize),
41cdf0e10cSrcweir 		nNrofStati(0),
42cdf0e10cSrcweir 		nStatiSpace(in_nInitial_StatusListSize)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	csv_assert(in_nStatusSize > 0);
45cdf0e10cSrcweir 	csv_assert(in_nInitial_StatusListSize > 0);
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	memset(pStati, 0, sizeof(StmStatus*) * nStatiSpace);
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir intt
AddStatus(StmStatus * let_dpStatus)51cdf0e10cSrcweir StateMachine::AddStatus(StmStatus * let_dpStatus)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	if (nNrofStati == nStatiSpace)
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		ResizeStati();
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 	pStati[nNrofStati] = let_dpStatus;
58cdf0e10cSrcweir 	return nNrofStati++;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir void
AddToken(const char * in_sToken,TextToken::F_CRTOK in_fTokenCreateFunction,const INT16 * in_aBranches,INT16 in_nBoundsStatus)62cdf0e10cSrcweir StateMachine::AddToken( const char *		in_sToken,
63cdf0e10cSrcweir 						TextToken::F_CRTOK	in_fTokenCreateFunction,
64cdf0e10cSrcweir 						const INT16 *		in_aBranches,
65cdf0e10cSrcweir 						INT16				in_nBoundsStatus )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	if (csv::no_str(in_sToken))
68cdf0e10cSrcweir 		return;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	// Durch existierende Stati durchhangeln:
71cdf0e10cSrcweir 	nCurrentStatus = 0;
72cdf0e10cSrcweir 	nPeekedStatus = 0;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	for ( const char * pChar = in_sToken;
75cdf0e10cSrcweir 		  *pChar != NULCH;
76cdf0e10cSrcweir 		  ++pChar )
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir 		Peek(*pChar);
79cdf0e10cSrcweir 		StmStatus & rPst = Status(nPeekedStatus);
80cdf0e10cSrcweir 		if ( rPst.IsADefault() OR rPst.AsBounds() != 0 )
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			nPeekedStatus = AddStatus( new StmArrayStatus(nStatusSize, in_aBranches, 0, false ) );
83cdf0e10cSrcweir 			CurrentStatus().SetBranch( *pChar, nPeekedStatus );
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
86cdf0e10cSrcweir 	}	// end for
87cdf0e10cSrcweir 	StmArrayStatus & rLastStatus = CurrentStatus();
88cdf0e10cSrcweir 	rLastStatus.SetTokenCreateFunction(in_fTokenCreateFunction);
89cdf0e10cSrcweir 	for (intt i = 0; i < nStatusSize; i++)
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		if (Status(rLastStatus.NextBy(i)).AsBounds() != 0)
92cdf0e10cSrcweir 			rLastStatus.SetBranch(i,in_nBoundsStatus);
93cdf0e10cSrcweir 	}	// end for
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
~StateMachine()96cdf0e10cSrcweir StateMachine::~StateMachine()
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	for (intt i = 0; i < nNrofStati; i++)
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir 		delete pStati[i];
101cdf0e10cSrcweir 	}
102cdf0e10cSrcweir 	delete [] pStati;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir StmBoundsStatus &
GetCharChain(TextToken::F_CRTOK & o_nTokenCreateFunction,CharacterSource & io_rText)106cdf0e10cSrcweir StateMachine::GetCharChain( TextToken::F_CRTOK &	o_nTokenCreateFunction,
107cdf0e10cSrcweir 							CharacterSource &       io_rText )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	nCurrentStatus = C_nTopStatus;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	Peek(io_rText.CurChar());
112cdf0e10cSrcweir 	while (BoundsStatus() == 0)
113cdf0e10cSrcweir 	{
114cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
115cdf0e10cSrcweir 		Peek(io_rText.MoveOn());
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir     o_nTokenCreateFunction = CurrentStatus().TokenCreateFunction();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	return *BoundsStatus();
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir void
ResizeStati()123cdf0e10cSrcweir StateMachine::ResizeStati()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	intt nNewSize = nStatiSpace + C_nStatuslistResizeValue;
126cdf0e10cSrcweir 	intt i = 0;
127cdf0e10cSrcweir 	StatusList pNewStati = new StmStatus*[nNewSize];
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	for ( ; i < nNrofStati; i++)
130cdf0e10cSrcweir 	{
131cdf0e10cSrcweir 		pNewStati[i] = pStati[i];
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir 	memset( pNewStati+i,
134cdf0e10cSrcweir 			0,
135cdf0e10cSrcweir 			(nNewSize-i) * sizeof(StmStatus*) );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	delete [] pStati;
138cdf0e10cSrcweir 	pStati = pNewStati;
139cdf0e10cSrcweir 	nStatiSpace = nNewSize;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir StmStatus &
Status(intt in_nStatusNr) const143cdf0e10cSrcweir StateMachine::Status(intt in_nStatusNr) const
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	csv_assert( csv::in_range(intt(0), in_nStatusNr, intt(nNrofStati)) );
146cdf0e10cSrcweir 	return *pStati[in_nStatusNr];
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir StmArrayStatus &
CurrentStatus() const150cdf0e10cSrcweir StateMachine::CurrentStatus() const
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	StmArrayStatus * pCurSt = Status(nCurrentStatus).AsArray();
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	csv_assert(pCurSt != 0);
155cdf0e10cSrcweir //	if(pCurSt == 0)
156cdf0e10cSrcweir //		csv_assert(false);
157cdf0e10cSrcweir 	return *pCurSt;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir StmBoundsStatus *
BoundsStatus() const161cdf0e10cSrcweir StateMachine::BoundsStatus() const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	return Status(nPeekedStatus).AsBounds();
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir void
Peek(intt in_nBranch)167cdf0e10cSrcweir StateMachine::Peek(intt	in_nBranch)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	StmArrayStatus & rSt = CurrentStatus();
170cdf0e10cSrcweir 	nPeekedStatus = rSt.NextBy(in_nBranch);
171cdf0e10cSrcweir }
172