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