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