xref: /aoo41x/main/autodoc/source/tools/tkpchars.cxx (revision d291ea28)
1*d291ea28SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d291ea28SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d291ea28SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d291ea28SAndrew Rist  * distributed with this work for additional information
6*d291ea28SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d291ea28SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d291ea28SAndrew Rist  * "License"); you may not use this file except in compliance
9*d291ea28SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d291ea28SAndrew Rist  *
11*d291ea28SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d291ea28SAndrew Rist  *
13*d291ea28SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d291ea28SAndrew Rist  * software distributed under the License is distributed on an
15*d291ea28SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d291ea28SAndrew Rist  * KIND, either express or implied.  See the License for the
17*d291ea28SAndrew Rist  * specific language governing permissions and limitations
18*d291ea28SAndrew Rist  * under the License.
19*d291ea28SAndrew Rist  *
20*d291ea28SAndrew Rist  *************************************************************/
21cdf0e10cSrcweir 
22cdf0e10cSrcweir #include <precomp.h>
23cdf0e10cSrcweir #include <tools/tkpchars.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
26cdf0e10cSrcweir #include <cosv/bstream.hxx>
27cdf0e10cSrcweir #include <cosv/x.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
CharacterSource()31cdf0e10cSrcweir CharacterSource::CharacterSource()
32cdf0e10cSrcweir 	:	dpSource(new char[2]),
33cdf0e10cSrcweir 		nSourceSize(0),
34cdf0e10cSrcweir 		nCurPos(0),
35cdf0e10cSrcweir 		nLastCut(0),
36cdf0e10cSrcweir 		nLastTokenStart(0),
37cdf0e10cSrcweir 		cCharAtLastCut(0)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	dpSource[nSourceSize] = NULCH;
40cdf0e10cSrcweir 	dpSource[nSourceSize+1] = NULCH;
41cdf0e10cSrcweir }
42cdf0e10cSrcweir 
~CharacterSource()43cdf0e10cSrcweir CharacterSource::~CharacterSource()
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 	delete [] dpSource;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir void
LoadText(csv::bstream & io_rSource)49cdf0e10cSrcweir CharacterSource::LoadText(csv::bstream & io_rSource)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	if (dpSource != 0)
52cdf0e10cSrcweir 		delete [] dpSource;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	io_rSource.seek(0, csv::end);
55cdf0e10cSrcweir 	nSourceSize = intt(io_rSource.position());
56cdf0e10cSrcweir 	io_rSource.seek(0);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	dpSource = new char[nSourceSize+1];
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	intt nCount = (intt) io_rSource.read(dpSource,nSourceSize);
61cdf0e10cSrcweir     if (nCount != nSourceSize)
62cdf0e10cSrcweir         throw csv::X_Default("IO-Error: Could not load file completely.");
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	dpSource[nSourceSize] = NULCH;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     BeginSource();
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir ///  KORR_FUTURE:  So far, this works only when tokens do not cross inserted text boundaries.
70cdf0e10cSrcweir void
InsertTextAtCurPos(const char * i_sText2Insert)71cdf0e10cSrcweir CharacterSource::InsertTextAtCurPos( const char * i_sText2Insert )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     if ( i_sText2Insert == 0 ? true : strlen(i_sText2Insert) == 0 )
74cdf0e10cSrcweir         return;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     aSourcesStack.push( S_SourceState(
77cdf0e10cSrcweir                             dpSource,
78cdf0e10cSrcweir 		                    nSourceSize,
79cdf0e10cSrcweir                             nCurPos,
80cdf0e10cSrcweir 		                    nLastCut,
81cdf0e10cSrcweir 		                    nLastTokenStart,
82cdf0e10cSrcweir 		                    cCharAtLastCut ) );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	nSourceSize = strlen(i_sText2Insert);
85cdf0e10cSrcweir 	dpSource = new char[nSourceSize+1];
86cdf0e10cSrcweir 	strcpy( dpSource,  i_sText2Insert);     // SAFE STRCPY (#100211# - checked)
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     BeginSource();
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir const char *
CutToken()92cdf0e10cSrcweir CharacterSource::CutToken()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	dpSource[nLastCut] = cCharAtLastCut;
95cdf0e10cSrcweir 	nLastTokenStart = nLastCut;
96cdf0e10cSrcweir 	nLastCut = CurPos();
97cdf0e10cSrcweir 	cCharAtLastCut = dpSource[nLastCut];
98cdf0e10cSrcweir 	dpSource[nLastCut] = NULCH;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	return &dpSource[nLastTokenStart];
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir void
BeginSource()104cdf0e10cSrcweir CharacterSource::BeginSource()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	nCurPos = 0;
107cdf0e10cSrcweir 	nLastCut = 0;
108cdf0e10cSrcweir 	nLastTokenStart = 0;
109cdf0e10cSrcweir 	cCharAtLastCut = dpSource[nLastCut];
110cdf0e10cSrcweir 	dpSource[nLastCut] = NULCH;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //  KORR_FUTURE:  So far, this works only when tokens do not cross inserted text boundaries.
114cdf0e10cSrcweir char
MoveOn_OverStack()115cdf0e10cSrcweir CharacterSource::MoveOn_OverStack()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     while ( aSourcesStack.size() > 0 AND nCurPos >= nSourceSize-1 )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         S_SourceState & aState = aSourcesStack.top();
120cdf0e10cSrcweir         delete [] dpSource;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         dpSource = aState.dpSource;
123cdf0e10cSrcweir         nSourceSize = aState.nSourceSize;
124cdf0e10cSrcweir         nCurPos = aState.nCurPos;
125cdf0e10cSrcweir         nLastCut = aState.nLastCut;
126cdf0e10cSrcweir         nLastTokenStart = aState.nLastTokenStart;
127cdf0e10cSrcweir         cCharAtLastCut = aState.cCharAtLastCut;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         aSourcesStack.pop();
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     if ( nLastCut < nCurPos )
133cdf0e10cSrcweir         CutToken();
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     return CurChar();
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir CharacterSource::
S_SourceState(DYN char * dpSource_,intt nSourceSize_,intt nCurPos_,intt nLastCut_,intt nLastTokenStart_,char cCharAtLastCut_)139cdf0e10cSrcweir S_SourceState::S_SourceState( DYN char *	dpSource_,
140cdf0e10cSrcweir 		                      intt			nSourceSize_,
141cdf0e10cSrcweir 		                      intt			nCurPos_,
142cdf0e10cSrcweir 		                      intt			nLastCut_,
143cdf0e10cSrcweir 		                      intt			nLastTokenStart_,
144cdf0e10cSrcweir 		                      char 			cCharAtLastCut_ )
145cdf0e10cSrcweir     :   dpSource(dpSource_),
146cdf0e10cSrcweir         nSourceSize(nSourceSize_),
147cdf0e10cSrcweir         nCurPos(nCurPos_),
148cdf0e10cSrcweir 		nLastCut(nLastCut_),
149cdf0e10cSrcweir 		nLastTokenStart(nLastTokenStart_),
150cdf0e10cSrcweir 		cCharAtLastCut(cCharAtLastCut_)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154