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