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