1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <precomp.h>
29*cdf0e10cSrcweir #include <tokens/tkpstama.hxx>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
32*cdf0e10cSrcweir // #include <srcfind.hxx>
33*cdf0e10cSrcweir #include <tokens/stmstarr.hxx>
34*cdf0e10cSrcweir //#include <parseinc.hxx>
35*cdf0e10cSrcweir #include <tools/tkpchars.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir const intt	C_nStatuslistResizeValue = 32;
39*cdf0e10cSrcweir const intt	C_nTopStatus = 0;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir StateMachine::StateMachine( intt			in_nStatusSize,
42*cdf0e10cSrcweir 							intt			in_nInitial_StatusListSize )
43*cdf0e10cSrcweir 	:	pStati(new StmStatus*[in_nInitial_StatusListSize]),
44*cdf0e10cSrcweir 		nCurrentStatus(C_nTopStatus),
45*cdf0e10cSrcweir 		nPeekedStatus(C_nTopStatus),
46*cdf0e10cSrcweir 		nStatusSize(in_nStatusSize),
47*cdf0e10cSrcweir 		nNrofStati(0),
48*cdf0e10cSrcweir 		nStatiSpace(in_nInitial_StatusListSize)
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	csv_assert(in_nStatusSize > 0);
51*cdf0e10cSrcweir 	csv_assert(in_nInitial_StatusListSize > 0);
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	memset(pStati, 0, sizeof(StmStatus*) * nStatiSpace);
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir intt
57*cdf0e10cSrcweir StateMachine::AddStatus(StmStatus * let_dpStatus)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir 	if (nNrofStati == nStatiSpace)
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir 		ResizeStati();
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 	pStati[nNrofStati] = let_dpStatus;
64*cdf0e10cSrcweir 	return nNrofStati++;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir void
68*cdf0e10cSrcweir StateMachine::AddToken( const char *		in_sToken,
69*cdf0e10cSrcweir 						TextToken::F_CRTOK	in_fTokenCreateFunction,
70*cdf0e10cSrcweir 						const INT16 *		in_aBranches,
71*cdf0e10cSrcweir 						INT16				in_nBoundsStatus )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	if (csv::no_str(in_sToken))
74*cdf0e10cSrcweir 		return;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	// Durch existierende Stati durchhangeln:
77*cdf0e10cSrcweir 	nCurrentStatus = 0;
78*cdf0e10cSrcweir 	nPeekedStatus = 0;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	for ( const char * pChar = in_sToken;
81*cdf0e10cSrcweir 		  *pChar != NULCH;
82*cdf0e10cSrcweir 		  ++pChar )
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		Peek(*pChar);
85*cdf0e10cSrcweir 		StmStatus & rPst = Status(nPeekedStatus);
86*cdf0e10cSrcweir 		if ( rPst.IsADefault() OR rPst.AsBounds() != 0 )
87*cdf0e10cSrcweir 		{
88*cdf0e10cSrcweir 			nPeekedStatus = AddStatus( new StmArrayStatus(nStatusSize, in_aBranches, 0, false ) );
89*cdf0e10cSrcweir 			CurrentStatus().SetBranch( *pChar, nPeekedStatus );
90*cdf0e10cSrcweir 		}
91*cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
92*cdf0e10cSrcweir 	}	// end for
93*cdf0e10cSrcweir 	StmArrayStatus & rLastStatus = CurrentStatus();
94*cdf0e10cSrcweir 	rLastStatus.SetTokenCreateFunction(in_fTokenCreateFunction);
95*cdf0e10cSrcweir 	for (intt i = 0; i < nStatusSize; i++)
96*cdf0e10cSrcweir 	{
97*cdf0e10cSrcweir 		if (Status(rLastStatus.NextBy(i)).AsBounds() != 0)
98*cdf0e10cSrcweir 			rLastStatus.SetBranch(i,in_nBoundsStatus);
99*cdf0e10cSrcweir 	}	// end for
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir StateMachine::~StateMachine()
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	for (intt i = 0; i < nNrofStati; i++)
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		delete pStati[i];
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 	delete [] pStati;
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir StmBoundsStatus &
112*cdf0e10cSrcweir StateMachine::GetCharChain( TextToken::F_CRTOK &	o_nTokenCreateFunction,
113*cdf0e10cSrcweir 							CharacterSource &       io_rText )
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir 	nCurrentStatus = C_nTopStatus;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	Peek(io_rText.CurChar());
118*cdf0e10cSrcweir 	while (BoundsStatus() == 0)
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		nCurrentStatus = nPeekedStatus;
121*cdf0e10cSrcweir 		Peek(io_rText.MoveOn());
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir     o_nTokenCreateFunction = CurrentStatus().TokenCreateFunction();
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	return *BoundsStatus();
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir void
129*cdf0e10cSrcweir StateMachine::ResizeStati()
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	intt nNewSize = nStatiSpace + C_nStatuslistResizeValue;
132*cdf0e10cSrcweir 	intt i = 0;
133*cdf0e10cSrcweir 	StatusList pNewStati = new StmStatus*[nNewSize];
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	for ( ; i < nNrofStati; i++)
136*cdf0e10cSrcweir 	{
137*cdf0e10cSrcweir 		pNewStati[i] = pStati[i];
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 	memset( pNewStati+i,
140*cdf0e10cSrcweir 			0,
141*cdf0e10cSrcweir 			(nNewSize-i) * sizeof(StmStatus*) );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	delete [] pStati;
144*cdf0e10cSrcweir 	pStati = pNewStati;
145*cdf0e10cSrcweir 	nStatiSpace = nNewSize;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir StmStatus &
149*cdf0e10cSrcweir StateMachine::Status(intt in_nStatusNr) const
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	csv_assert( csv::in_range(intt(0), in_nStatusNr, intt(nNrofStati)) );
152*cdf0e10cSrcweir 	return *pStati[in_nStatusNr];
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir StmArrayStatus &
156*cdf0e10cSrcweir StateMachine::CurrentStatus() const
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	StmArrayStatus * pCurSt = Status(nCurrentStatus).AsArray();
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	csv_assert(pCurSt != 0);
161*cdf0e10cSrcweir //	if(pCurSt == 0)
162*cdf0e10cSrcweir //		csv_assert(false);
163*cdf0e10cSrcweir 	return *pCurSt;
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir StmBoundsStatus *
167*cdf0e10cSrcweir StateMachine::BoundsStatus() const
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir 	return Status(nPeekedStatus).AsBounds();
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir void
173*cdf0e10cSrcweir StateMachine::Peek(intt	in_nBranch)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	StmArrayStatus & rSt = CurrentStatus();
176*cdf0e10cSrcweir 	nPeekedStatus = rSt.NextBy(in_nBranch);
177*cdf0e10cSrcweir }
178