xref: /trunk/main/autodoc/source/tools/tkpchars.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 <tools/tkpchars.hxx>
30 
31 // NOT FULLY DECLARED SERVICES
32 #include <cosv/bstream.hxx>
33 #include <cosv/x.hxx>
34 
35 
36 
37 CharacterSource::CharacterSource()
38     :   dpSource(new char[2]),
39         nSourceSize(0),
40         nCurPos(0),
41         nLastCut(0),
42         nLastTokenStart(0),
43         cCharAtLastCut(0)
44 {
45     dpSource[nSourceSize] = NULCH;
46     dpSource[nSourceSize+1] = NULCH;
47 }
48 
49 CharacterSource::~CharacterSource()
50 {
51     delete [] dpSource;
52 }
53 
54 void
55 CharacterSource::LoadText(csv::bstream & io_rSource)
56 {
57     if (dpSource != 0)
58         delete [] dpSource;
59 
60     io_rSource.seek(0, csv::end);
61     nSourceSize = intt(io_rSource.position());
62     io_rSource.seek(0);
63 
64     dpSource = new char[nSourceSize+1];
65 
66     intt nCount = (intt) io_rSource.read(dpSource,nSourceSize);
67     if (nCount != nSourceSize)
68         throw csv::X_Default("IO-Error: Could not load file completely.");
69 
70     dpSource[nSourceSize] = NULCH;
71 
72     BeginSource();
73 }
74 
75 ///  KORR_FUTURE:  So far, this works only when tokens do not cross inserted text boundaries.
76 void
77 CharacterSource::InsertTextAtCurPos( const char * i_sText2Insert )
78 {
79     if ( i_sText2Insert == 0 ? true : strlen(i_sText2Insert) == 0 )
80         return;
81 
82     aSourcesStack.push( S_SourceState(
83                             dpSource,
84                             nSourceSize,
85                             nCurPos,
86                             nLastCut,
87                             nLastTokenStart,
88                             cCharAtLastCut ) );
89 
90     nSourceSize = strlen(i_sText2Insert);
91     dpSource = new char[nSourceSize+1];
92     strcpy( dpSource,  i_sText2Insert);     // SAFE STRCPY (#100211# - checked)
93 
94     BeginSource();
95 }
96 
97 const char *
98 CharacterSource::CutToken()
99 {
100     dpSource[nLastCut] = cCharAtLastCut;
101     nLastTokenStart = nLastCut;
102     nLastCut = CurPos();
103     cCharAtLastCut = dpSource[nLastCut];
104     dpSource[nLastCut] = NULCH;
105 
106     return &dpSource[nLastTokenStart];
107 }
108 
109 void
110 CharacterSource::BeginSource()
111 {
112     nCurPos = 0;
113     nLastCut = 0;
114     nLastTokenStart = 0;
115     cCharAtLastCut = dpSource[nLastCut];
116     dpSource[nLastCut] = NULCH;
117 }
118 
119 //  KORR_FUTURE:  So far, this works only when tokens do not cross inserted text boundaries.
120 char
121 CharacterSource::MoveOn_OverStack()
122 {
123     while ( aSourcesStack.size() > 0 AND nCurPos >= nSourceSize-1 )
124     {
125         S_SourceState & aState = aSourcesStack.top();
126         delete [] dpSource;
127 
128         dpSource = aState.dpSource;
129         nSourceSize = aState.nSourceSize;
130         nCurPos = aState.nCurPos;
131         nLastCut = aState.nLastCut;
132         nLastTokenStart = aState.nLastTokenStart;
133         cCharAtLastCut = aState.cCharAtLastCut;
134 
135         aSourcesStack.pop();
136     }
137 
138     if ( nLastCut < nCurPos )
139         CutToken();
140 
141     return CurChar();
142 }
143 
144 CharacterSource::
145 S_SourceState::S_SourceState( DYN char *    dpSource_,
146                               intt          nSourceSize_,
147                               intt          nCurPos_,
148                               intt          nLastCut_,
149                               intt          nLastTokenStart_,
150                               char          cCharAtLastCut_ )
151     :   dpSource(dpSource_),
152         nSourceSize(nSourceSize_),
153         nCurPos(nCurPos_),
154         nLastCut(nLastCut_),
155         nLastTokenStart(nLastTokenStart_),
156         cCharAtLastCut(cCharAtLastCut_)
157 {
158 }
159 
160