xref: /aoo41x/main/sc/source/core/tool/reffind.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <string.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "reffind.hxx"
34cdf0e10cSrcweir #include "global.hxx"
35cdf0e10cSrcweir #include "compiler.hxx"
36cdf0e10cSrcweir #include "document.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //	incl. Doppelpunkt -> Doppelte Referenzen werden einzeln behandelt
41cdf0e10cSrcweir const sal_Unicode __FAR_DATA ScRefFinder::pDelimiters[] = {
42cdf0e10cSrcweir 	'=','(',')',';','+','-','*','/','^','&',' ','{','}','<','>',':', 0
43cdf0e10cSrcweir };
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // =======================================================================
46cdf0e10cSrcweir 
IsText(sal_Unicode c)47cdf0e10cSrcweir inline sal_Bool IsText( sal_Unicode c )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	return !ScGlobal::UnicodeStrChr( ScRefFinder::pDelimiters, c );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
IsText(sal_Bool & bQuote,sal_Unicode c)52cdf0e10cSrcweir inline sal_Bool IsText( sal_Bool& bQuote, sal_Unicode c )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	if ( c == '\'' )
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		bQuote = !bQuote;
57cdf0e10cSrcweir 		return sal_True;
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 	if ( bQuote )
60cdf0e10cSrcweir 		return sal_True;
61cdf0e10cSrcweir 	return IsText( c );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
ScRefFinder(const String & rFormula,ScDocument * pDocument,formula::FormulaGrammar::AddressConvention eConvP)64cdf0e10cSrcweir ScRefFinder::ScRefFinder(const String& rFormula, ScDocument* pDocument,
65cdf0e10cSrcweir 			 formula::FormulaGrammar::AddressConvention eConvP) :
66cdf0e10cSrcweir 	aFormula( rFormula ),
67cdf0e10cSrcweir 	eConv( eConvP ),
68cdf0e10cSrcweir 	pDoc( pDocument )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	nSelStart = nSelEnd = nFound = 0;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
~ScRefFinder()73cdf0e10cSrcweir ScRefFinder::~ScRefFinder()
74cdf0e10cSrcweir {
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
lcl_NextFlags(sal_uInt16 nOld)77cdf0e10cSrcweir sal_uInt16 lcl_NextFlags( sal_uInt16 nOld )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	sal_uInt16 nNew = nOld & 7;					// die drei Abs-Flags
80cdf0e10cSrcweir 	nNew = ( nNew - 1 ) & 7;				// weiterzaehlen
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	if (!(nOld & SCA_TAB_3D))
83cdf0e10cSrcweir 		nNew &= ~SCA_TAB_ABSOLUTE;			// nicht 3D -> nie absolut!
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	return ( nOld & 0xfff8 ) | nNew;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
ToggleRel(xub_StrLen nStartPos,xub_StrLen nEndPos)88cdf0e10cSrcweir void ScRefFinder::ToggleRel( xub_StrLen nStartPos, xub_StrLen nEndPos )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	xub_StrLen nLen = aFormula.Len();
91cdf0e10cSrcweir 	if (!nLen)
92cdf0e10cSrcweir 		return;
93cdf0e10cSrcweir 	const sal_Unicode* pSource = aFormula.GetBuffer();		// fuer schnellen Zugriff
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	//	Selektion erweitern, und statt Selektion Start- und Endindex
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	if ( nEndPos < nStartPos )
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		xub_StrLen nTemp = nStartPos; nStartPos = nEndPos; nEndPos = nTemp;
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir 	while (nStartPos > 0 && IsText(pSource[nStartPos - 1]) )
102cdf0e10cSrcweir 		--nStartPos;
103cdf0e10cSrcweir 	if (nEndPos)
104cdf0e10cSrcweir 		--nEndPos;
105cdf0e10cSrcweir 	while (nEndPos+1 < nLen && IsText(pSource[nEndPos + 1]) )
106cdf0e10cSrcweir 		++nEndPos;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	String aResult;
109cdf0e10cSrcweir 	String aExpr;
110cdf0e10cSrcweir 	String aSep;
111cdf0e10cSrcweir 	ScAddress aAddr;
112cdf0e10cSrcweir 	nFound = 0;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	xub_StrLen nLoopStart = nStartPos;
115cdf0e10cSrcweir 	while ( nLoopStart <= nEndPos )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		//	Formel zerlegen
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		xub_StrLen nEStart = nLoopStart;
120cdf0e10cSrcweir 		while ( nEStart <= nEndPos && !IsText(pSource[nEStart]) )
121cdf0e10cSrcweir 			++nEStart;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 		sal_Bool bQuote = sal_False;
124cdf0e10cSrcweir 		xub_StrLen nEEnd = nEStart;
125cdf0e10cSrcweir 		while ( nEEnd <= nEndPos && IsText(bQuote,pSource[nEEnd]) )
126cdf0e10cSrcweir 			++nEEnd;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		aSep  = aFormula.Copy( nLoopStart, nEStart-nLoopStart );
129cdf0e10cSrcweir 		aExpr = aFormula.Copy( nEStart, nEEnd-nEStart );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 		//	Test, ob aExpr eine Referenz ist
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		sal_uInt16 nResult = aAddr.Parse( aExpr, pDoc, pDoc->GetAddressConvention() );
134cdf0e10cSrcweir 		if ( nResult & SCA_VALID )
135cdf0e10cSrcweir 		{
136cdf0e10cSrcweir 			sal_uInt16 nFlags = lcl_NextFlags( nResult );
137cdf0e10cSrcweir 			aAddr.Format( aExpr, nFlags, pDoc, pDoc->GetAddressConvention() );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 			xub_StrLen nAbsStart = nStartPos+aResult.Len()+aSep.Len();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 			if (!nFound)							// erste Referenz ?
142cdf0e10cSrcweir 				nSelStart = nAbsStart;
143cdf0e10cSrcweir 			nSelEnd = nAbsStart+aExpr.Len();		// Selektion, keine Indizes
144cdf0e10cSrcweir 			++nFound;
145cdf0e10cSrcweir 		}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 		//	zusammenbauen
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		aResult += aSep;
150cdf0e10cSrcweir 		aResult += aExpr;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		nLoopStart = nEEnd;
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	String aTotal = aFormula.Copy( 0, nStartPos );
156cdf0e10cSrcweir 	aTotal += aResult;
157cdf0e10cSrcweir 	aTotal += aFormula.Copy( nEndPos+1 );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	aFormula = aTotal;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165