1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 #include <precomp.h>
23 #include <tokens/tkpstam2.hxx>
24
25 // NOT FULLY DECLARED SERVICES
26 #include <tokens/stmstar2.hxx>
27 #include <tools/tkpchars.hxx>
28
29
30 const intt C_nStatuslistResizeValue = 32;
31 const intt C_nTopStatus = 0;
32
StateMachin2(intt in_nStatusSize,intt in_nInitial_StatusListSize)33 StateMachin2::StateMachin2( intt in_nStatusSize,
34 intt in_nInitial_StatusListSize )
35 : pStati(new StmStatu2*[in_nInitial_StatusListSize]),
36 nCurrentStatus(C_nTopStatus),
37 nPeekedStatus(C_nTopStatus),
38 nStatusSize(in_nStatusSize),
39 nNrofStati(0),
40 nStatiSpace(in_nInitial_StatusListSize)
41 {
42 csv_assert(in_nStatusSize > 0);
43 csv_assert(in_nInitial_StatusListSize > 0);
44
45 memset(pStati, 0, sizeof(StmStatu2*) * nStatiSpace);
46 }
47
48 intt
AddStatus(StmStatu2 * let_dpStatus)49 StateMachin2::AddStatus(StmStatu2 * let_dpStatus)
50 {
51 if (nNrofStati == nStatiSpace)
52 {
53 ResizeStati();
54 }
55 pStati[nNrofStati] = let_dpStatus;
56 return nNrofStati++;
57 }
58
59 void
AddToken(const char * in_sToken,UINT16 in_nTokenId,const INT16 * in_aBranches,INT16 in_nBoundsStatus)60 StateMachin2::AddToken( const char * in_sToken,
61 UINT16 in_nTokenId,
62 const INT16 * in_aBranches,
63 INT16 in_nBoundsStatus )
64 {
65 if (csv::no_str(in_sToken))
66 return;
67
68 // Durch existierende Stati durchhangeln:
69 nCurrentStatus = 0;
70 nPeekedStatus = 0;
71
72 for ( const char * pChar = in_sToken;
73 *pChar != NULCH;
74 ++pChar )
75 {
76 Peek(*pChar);
77 StmStatu2 & rPst = Status(nPeekedStatus);
78 if ( rPst.IsADefault() OR rPst.AsBounds() != 0 )
79 {
80 nPeekedStatus = AddStatus( new StmArrayStatu2(nStatusSize, in_aBranches, 0, false ) );
81 CurrentStatus().SetBranch( *pChar, nPeekedStatus );
82 }
83 nCurrentStatus = nPeekedStatus;
84 } // end for
85 StmArrayStatu2 & rLastStatus = CurrentStatus();
86 rLastStatus.SetTokenId(in_nTokenId);
87 for (intt i = 0; i < nStatusSize; i++)
88 {
89 if (Status(rLastStatus.NextBy(i)).AsBounds() != 0)
90 rLastStatus.SetBranch(i,in_nBoundsStatus);
91 } // end for
92 }
93
~StateMachin2()94 StateMachin2::~StateMachin2()
95 {
96 for (intt i = 0; i < nNrofStati; i++)
97 {
98 delete pStati[i];
99 }
100 delete [] pStati;
101 }
102
103 StmBoundsStatu2 &
GetCharChain(UINT16 & o_nTokenId,CharacterSource & io_rText)104 StateMachin2::GetCharChain( UINT16 & o_nTokenId,
105 CharacterSource & io_rText )
106 {
107 nCurrentStatus = C_nTopStatus;
108 Peek(io_rText.CurChar());
109 while (BoundsStatus() == 0)
110 {
111 nCurrentStatus = nPeekedStatus;
112 Peek(io_rText.MoveOn());
113 }
114 o_nTokenId = CurrentStatus().TokenId();
115
116 return *BoundsStatus();
117 }
118
119 void
ResizeStati()120 StateMachin2::ResizeStati()
121 {
122 intt nNewSize = nStatiSpace + C_nStatuslistResizeValue;
123 intt i = 0;
124 StatusList pNewStati = new StmStatu2*[nNewSize];
125
126 for ( ; i < nNrofStati; i++)
127 {
128 pNewStati[i] = pStati[i];
129 }
130 memset( pNewStati+i,
131 0,
132 (nNewSize-i) * sizeof(StmStatu2*) );
133
134 delete [] pStati;
135 pStati = pNewStati;
136 nStatiSpace = nNewSize;
137 }
138
139 StmStatu2 &
Status(intt in_nStatusNr) const140 StateMachin2::Status(intt in_nStatusNr) const
141 {
142 csv_assert( csv::in_range(intt(0), in_nStatusNr, intt(nNrofStati)) );
143 return *pStati[in_nStatusNr];
144 }
145
146 StmArrayStatu2 &
CurrentStatus() const147 StateMachin2::CurrentStatus() const
148 {
149 StmArrayStatu2 * pCurSt = Status(nCurrentStatus).AsArray();
150 if (pCurSt == 0)
151 {
152 csv_assert(false);
153 }
154 return *pCurSt;
155 }
156
157 StmBoundsStatu2 *
BoundsStatus() const158 StateMachin2::BoundsStatus() const
159 {
160 return Status(nPeekedStatus).AsBounds();
161 }
162
163 void
Peek(intt in_nBranch)164 StateMachin2::Peek(intt in_nBranch)
165 {
166 StmArrayStatu2 & rSt = CurrentStatus();
167 nPeekedStatus = rSt.NextBy(in_nBranch);
168 }
169