Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "reffind.hxx"
21 : #include "global.hxx"
22 : #include "compiler.hxx"
23 : #include "document.hxx"
24 :
25 : // STATIC DATA
26 : namespace {
27 :
28 : // Include colon; addresses in range reference are handled individually.
29 : const sal_Unicode pDelimiters[] = {
30 : '=','(',')','+','-','*','/','^','&',' ','{','}','<','>',':', 0
31 : };
32 :
33 0 : inline bool IsText( sal_Unicode c )
34 : {
35 0 : bool bFound = ScGlobal::UnicodeStrChr( pDelimiters, c );
36 0 : if (bFound)
37 : // This is one of delimiters, therefore not text.
38 0 : return false;
39 :
40 : // argument separator is configurable.
41 0 : const sal_Unicode sep = ScCompiler::GetNativeSymbolChar(ocSep);
42 0 : return c != sep;
43 : }
44 :
45 0 : inline bool IsText( bool& bQuote, sal_Unicode c )
46 : {
47 0 : if (c == '\'')
48 : {
49 0 : bQuote = !bQuote;
50 0 : return true;
51 : }
52 0 : if (bQuote)
53 0 : return true;
54 :
55 0 : return IsText(c);
56 : }
57 :
58 : /**
59 : * Find first character position that is considered text. A character is
60 : * considered a text when it's within the ascii range and when it's not a
61 : * delimiter.
62 : */
63 0 : sal_Int32 FindStartPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
64 : {
65 0 : while (nStartPos <= nEndPos && !IsText(p[nStartPos]))
66 0 : ++nStartPos;
67 :
68 0 : return nStartPos;
69 : }
70 :
71 0 : sal_Int32 FindEndPosA1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
72 : {
73 0 : bool bQuote = false;
74 0 : sal_Int32 nNewEnd = nStartPos;
75 0 : while (nNewEnd <= nEndPos && IsText(bQuote, p[nNewEnd]))
76 0 : ++nNewEnd;
77 :
78 0 : return nNewEnd;
79 : }
80 :
81 0 : sal_Int32 FindEndPosR1C1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
82 : {
83 0 : sal_Int32 nNewEnd = nStartPos;
84 0 : p = &p[nStartPos];
85 0 : for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
86 : {
87 0 : if (*p == '\'')
88 : {
89 : // Skip until the closing quote.
90 0 : for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
91 0 : if (*p == '\'')
92 0 : break;
93 0 : if (nNewEnd > nEndPos)
94 0 : break;
95 : }
96 0 : else if (*p == '[')
97 : {
98 : // Skip until the closing braket.
99 0 : for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
100 0 : if (*p == ']')
101 0 : break;
102 0 : if (nNewEnd > nEndPos)
103 0 : break;
104 : }
105 0 : else if (!IsText(*p))
106 0 : break;
107 : }
108 :
109 0 : return nNewEnd;
110 : }
111 :
112 : /**
113 : * Find last character position that is considred text, from the specified
114 : * start position.
115 : */
116 0 : sal_Int32 FindEndPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos,
117 : formula::FormulaGrammar::AddressConvention eConv)
118 : {
119 0 : switch (eConv)
120 : {
121 : case formula::FormulaGrammar::CONV_XL_R1C1:
122 0 : return FindEndPosR1C1(p, nStartPos, nEndPos);
123 : case formula::FormulaGrammar::CONV_OOO:
124 : case formula::FormulaGrammar::CONV_XL_A1:
125 : default:
126 0 : return FindEndPosA1(p, nStartPos, nEndPos);
127 : }
128 : }
129 :
130 0 : void ExpandToTextA1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
131 : {
132 0 : while (rStartPos > 0 && IsText(p[rStartPos - 1]) )
133 0 : --rStartPos;
134 0 : if (rEndPos)
135 0 : --rEndPos;
136 0 : while (rEndPos+1 < nLen && IsText(p[rEndPos + 1]) )
137 0 : ++rEndPos;
138 0 : }
139 :
140 0 : void ExpandToTextR1C1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
141 : {
142 : // move back the start position to the first text character.
143 0 : if (rStartPos > 0)
144 : {
145 0 : for (--rStartPos; rStartPos > 0; --rStartPos)
146 : {
147 0 : sal_Unicode c = p[rStartPos];
148 0 : if (c == '\'')
149 : {
150 : // Skip until the opening quote.
151 0 : for (--rStartPos; rStartPos > 0; --rStartPos)
152 : {
153 0 : c = p[rStartPos];
154 0 : if (c == '\'')
155 0 : break;
156 : }
157 0 : if (rStartPos == 0)
158 0 : break;
159 : }
160 0 : else if (c == ']')
161 : {
162 : // Skip until the opening braket.
163 0 : for (--rStartPos; rStartPos > 0; --rStartPos)
164 : {
165 0 : c = p[rStartPos];
166 0 : if (c == '[')
167 0 : break;
168 : }
169 0 : if (rStartPos == 0)
170 0 : break;
171 : }
172 0 : else if (!IsText(c))
173 : {
174 0 : ++rStartPos;
175 0 : break;
176 : }
177 : }
178 : }
179 :
180 : // move forward the end position to the last text character.
181 0 : rEndPos = FindEndPosR1C1(p, rEndPos, nLen-1);
182 0 : }
183 :
184 0 : void ExpandToText(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos,
185 : formula::FormulaGrammar::AddressConvention eConv)
186 : {
187 0 : switch (eConv)
188 : {
189 : case formula::FormulaGrammar::CONV_XL_R1C1:
190 0 : ExpandToTextR1C1(p, nLen, rStartPos, rEndPos);
191 0 : break;
192 : case formula::FormulaGrammar::CONV_OOO:
193 : case formula::FormulaGrammar::CONV_XL_A1:
194 : default:
195 0 : ExpandToTextA1(p, nLen, rStartPos, rEndPos);
196 : }
197 0 : }
198 :
199 : }
200 :
201 0 : ScRefFinder::ScRefFinder(
202 : const OUString& rFormula, const ScAddress& rPos,
203 : ScDocument* pDoc, formula::FormulaGrammar::AddressConvention eConvP) :
204 : maFormula(rFormula),
205 : meConv(eConvP),
206 : mpDoc(pDoc),
207 : maPos(rPos),
208 : mnFound(0),
209 : mnSelStart(0),
210 0 : mnSelEnd(0)
211 : {
212 0 : }
213 :
214 0 : ScRefFinder::~ScRefFinder()
215 : {
216 0 : }
217 :
218 0 : static sal_uInt16 lcl_NextFlags( sal_uInt16 nOld )
219 : {
220 0 : sal_uInt16 nNew = nOld & 7; // die drei Abs-Flags
221 0 : nNew = ( nNew - 1 ) & 7; // weiterzaehlen
222 :
223 0 : if (!(nOld & SCA_TAB_3D))
224 0 : nNew &= ~SCA_TAB_ABSOLUTE; // not 3D -> never absolute!
225 :
226 0 : return ( nOld & 0xfff8 ) | nNew;
227 : }
228 :
229 0 : void ScRefFinder::ToggleRel( sal_Int32 nStartPos, sal_Int32 nEndPos )
230 : {
231 0 : sal_Int32 nLen = maFormula.getLength();
232 0 : if (nLen <= 0)
233 0 : return;
234 0 : const sal_Unicode* pSource = maFormula.getStr(); // for quick access
235 :
236 : // expand selection, and instead of selection start- and end-index
237 :
238 0 : if ( nEndPos < nStartPos )
239 0 : ::std::swap(nEndPos, nStartPos);
240 :
241 0 : ExpandToText(pSource, nLen, nStartPos, nEndPos, meConv);
242 :
243 0 : OUString aResult;
244 0 : OUString aExpr;
245 0 : OUString aSep;
246 0 : ScAddress aAddr;
247 0 : mnFound = 0;
248 :
249 0 : sal_Int32 nLoopStart = nStartPos;
250 0 : while ( nLoopStart <= nEndPos )
251 : {
252 : // Determine the stard and end positions of a text segment. Note that
253 : // the end position returned from FindEndPos may be one position after
254 : // the last character position in case of the last segment.
255 0 : sal_Int32 nEStart = FindStartPos(pSource, nLoopStart, nEndPos);
256 0 : sal_Int32 nEEnd = FindEndPos(pSource, nEStart, nEndPos, meConv);
257 :
258 0 : aSep = maFormula.copy(nLoopStart, nEStart-nLoopStart);
259 0 : if (nEEnd < maFormula.getLength())
260 0 : aExpr = maFormula.copy(nEStart, nEEnd-nEStart);
261 : else
262 0 : aExpr = maFormula.copy(nEStart);
263 :
264 : // Check the validity of the expression, and toggle the relative flag.
265 0 : ScAddress::Details aDetails(meConv, maPos.Row(), maPos.Col());
266 0 : sal_uInt16 nResult = aAddr.Parse(aExpr, mpDoc, aDetails);
267 0 : if ( nResult & SCA_VALID )
268 : {
269 0 : sal_uInt16 nFlags = lcl_NextFlags( nResult );
270 0 : aExpr = aAddr.Format(nFlags, mpDoc, aDetails);
271 :
272 0 : sal_Int32 nAbsStart = nStartPos+aResult.getLength()+aSep.getLength();
273 :
274 0 : if (!mnFound) // first reference ?
275 0 : mnSelStart = nAbsStart;
276 0 : mnSelEnd = nAbsStart + aExpr.getLength(); // selection, no indices
277 0 : ++mnFound;
278 : }
279 :
280 : // assemble
281 :
282 0 : aResult += aSep;
283 0 : aResult += aExpr;
284 :
285 0 : nLoopStart = nEEnd;
286 : }
287 :
288 0 : OUString aTotal = maFormula.copy(0, nStartPos);
289 0 : aTotal += aResult;
290 0 : if (nEndPos < maFormula.getLength()-1)
291 0 : aTotal += maFormula.copy(nEndPos+1);
292 :
293 0 : maFormula = aTotal;
294 : }
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|