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