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 "xiname.hxx"
21 : #include "rangenam.hxx"
22 : #include "xistream.hxx"
23 : #include "excform.hxx"
24 : #include "excimp8.hxx"
25 : #include "scextopt.hxx"
26 : #include "document.hxx"
27 :
28 : // *** Implementation ***
29 :
30 132 : XclImpName::TokenStrmData::TokenStrmData( XclImpStream& rStrm ) :
31 132 : mrStrm(rStrm), mnStrmPos(0), mnStrmSize(0) {}
32 :
33 165 : XclImpName::XclImpName( XclImpStream& rStrm, sal_uInt16 nXclNameIdx ) :
34 165 : XclImpRoot( rStrm.GetRoot() ),
35 : mpScData( 0 ),
36 : mcBuiltIn( EXC_BUILTIN_UNKNOWN ),
37 : mnScTab( SCTAB_MAX ),
38 : meNameType( RT_NAME ),
39 : mnXclTab( EXC_NAME_GLOBAL ),
40 : mnNameIndex( nXclNameIdx ),
41 : mbVBName( false ),
42 : mbMacro( false ),
43 165 : mpTokensData( NULL )
44 : {
45 165 : ExcelToSc& rFmlaConv = GetOldFmlaConverter();
46 :
47 : // 1) *** read data from stream *** ---------------------------------------
48 :
49 165 : sal_uInt16 nFlags = 0, nFmlaSize = 0, nExtSheet = EXC_NAME_GLOBAL;
50 165 : sal_uInt8 nNameLen = 0;
51 :
52 165 : switch( GetBiff() )
53 : {
54 : case EXC_BIFF2:
55 : {
56 : sal_uInt8 nFlagsBiff2;
57 0 : nFlagsBiff2 = rStrm.ReaduInt8();
58 0 : rStrm.Ignore( 1 );
59 0 : rStrm.Ignore( 1 ); //nShortCut
60 0 : nNameLen = rStrm.ReaduInt8();
61 0 : nFmlaSize = rStrm.ReaduInt8();
62 0 : ::set_flag( nFlags, EXC_NAME_FUNC, ::get_flag( nFlagsBiff2, EXC_NAME2_FUNC ) );
63 : }
64 0 : break;
65 :
66 : case EXC_BIFF3:
67 : case EXC_BIFF4:
68 : {
69 0 : nFlags = rStrm.ReaduInt16();
70 0 : rStrm.Ignore( 1 ); //nShortCut
71 0 : nNameLen = rStrm.ReaduInt8();
72 0 : nFmlaSize = rStrm.ReaduInt16();
73 : }
74 0 : break;
75 :
76 : case EXC_BIFF5:
77 : case EXC_BIFF8:
78 : {
79 165 : nFlags = rStrm.ReaduInt16();
80 165 : rStrm.Ignore( 1 ); //nShortCut
81 165 : nNameLen = rStrm.ReaduInt8();
82 165 : nFmlaSize = rStrm.ReaduInt16();
83 165 : nExtSheet = rStrm.ReaduInt16();
84 165 : mnXclTab = rStrm.ReaduInt16();
85 165 : rStrm.Ignore( 4 );
86 : }
87 165 : break;
88 :
89 : default: DBG_ERROR_BIFF();
90 : }
91 :
92 165 : if( GetBiff() <= EXC_BIFF5 )
93 1 : maXclName = rStrm.ReadRawByteString( nNameLen );
94 : else
95 164 : maXclName = rStrm.ReadUniString( nNameLen );
96 :
97 : // 2) *** convert sheet index and name *** --------------------------------
98 :
99 : // functions and VBA
100 165 : bool bFunction = ::get_flag( nFlags, EXC_NAME_FUNC );
101 165 : mbVBName = ::get_flag( nFlags, EXC_NAME_VB );
102 165 : mbMacro = ::get_flag( nFlags, EXC_NAME_PROC );
103 :
104 : // get built-in name, or convert characters invalid in Calc
105 165 : bool bBuiltIn = ::get_flag( nFlags, EXC_NAME_BUILTIN );
106 :
107 : // special case for BIFF5 filter range - name appears as plain text without built-in flag
108 165 : if( (GetBiff() == EXC_BIFF5) && (maXclName == XclTools::GetXclBuiltInDefName(EXC_BUILTIN_FILTERDATABASE)) )
109 : {
110 1 : bBuiltIn = true;
111 1 : maXclName = OUStringLiteral1<EXC_BUILTIN_FILTERDATABASE>();
112 : }
113 :
114 : // convert Excel name to Calc name
115 165 : if( mbVBName )
116 : {
117 : // VB macro name
118 1 : maScName = maXclName;
119 : }
120 164 : else if( bBuiltIn )
121 : {
122 : // built-in name
123 32 : if( !maXclName.isEmpty() )
124 32 : mcBuiltIn = maXclName[0];
125 32 : if( mcBuiltIn == '?' ) // NUL character is imported as '?'
126 0 : mcBuiltIn = '\0';
127 32 : maScName = XclTools::GetBuiltInDefName( mcBuiltIn );
128 : }
129 : else
130 : {
131 : // any other name
132 132 : maScName = ScfTools::ConvertToScDefinedName( maXclName );
133 : }
134 :
135 : // add index for local names
136 165 : if( mnXclTab != EXC_NAME_GLOBAL )
137 : {
138 42 : sal_uInt16 nUsedTab = (GetBiff() == EXC_BIFF8) ? mnXclTab : nExtSheet;
139 : // TODO: may not work for BIFF5, handle skipped sheets (all BIFF)
140 42 : mnScTab = static_cast< SCTAB >( nUsedTab - 1 );
141 : }
142 :
143 : // 3) *** convert the name definition formula *** -------------------------
144 :
145 165 : rFmlaConv.Reset();
146 165 : const ScTokenArray* pTokArr = 0; // pointer to token array, owned by rFmlaConv
147 :
148 165 : if( ::get_flag( nFlags, EXC_NAME_BIG ) )
149 : {
150 : // special, unsupported name
151 0 : rFmlaConv.GetDummy( pTokArr );
152 : }
153 165 : else if( bBuiltIn )
154 : {
155 32 : SCsTAB const nLocalTab = (mnXclTab == EXC_NAME_GLOBAL) ? SCTAB_MAX : (mnXclTab - 1);
156 :
157 : // --- print ranges or title ranges ---
158 32 : rStrm.PushPosition();
159 32 : switch( mcBuiltIn )
160 : {
161 : case EXC_BUILTIN_PRINTAREA:
162 5 : if( rFmlaConv.Convert( GetPrintAreaBuffer(), rStrm, nFmlaSize, nLocalTab, FT_RangeName ) == ConvOK )
163 5 : meNameType |= RT_PRINTAREA;
164 5 : break;
165 : case EXC_BUILTIN_PRINTTITLES:
166 0 : if( rFmlaConv.Convert( GetTitleAreaBuffer(), rStrm, nFmlaSize, nLocalTab, FT_RangeName ) == ConvOK )
167 0 : meNameType |= RT_COLHEADER | RT_ROWHEADER;
168 0 : break;
169 : }
170 32 : rStrm.PopPosition();
171 :
172 : // --- name formula ---
173 : // JEG : double check this. It is clearly false for normal names
174 : // but some of the builtins (sheettitle?) might be able to handle arrays
175 32 : rFmlaConv.Convert( pTokArr, rStrm, nFmlaSize, false, FT_RangeName );
176 :
177 : // --- auto or advanced filter ---
178 32 : if( (GetBiff() == EXC_BIFF8) && pTokArr && bBuiltIn )
179 : {
180 31 : ScRange aRange;
181 31 : if (pTokArr->IsReference(aRange, ScAddress()))
182 : {
183 28 : switch( mcBuiltIn )
184 : {
185 : case EXC_BUILTIN_FILTERDATABASE:
186 26 : GetFilterManager().Insert( &GetOldRoot(), aRange);
187 26 : break;
188 : case EXC_BUILTIN_CRITERIA:
189 0 : GetFilterManager().AddAdvancedRange( aRange );
190 0 : meNameType |= RT_CRITERIA;
191 0 : break;
192 : case EXC_BUILTIN_EXTRACT:
193 0 : if (pTokArr->IsValidReference(aRange, ScAddress()))
194 0 : GetFilterManager().AddExtractPos( aRange );
195 0 : break;
196 : }
197 : }
198 : }
199 : }
200 133 : else if( nFmlaSize > 0 )
201 : {
202 : // Regular defined name. We need to convert the tokens after all the
203 : // names have been registered (for cross-referenced names).
204 132 : mpTokensData.reset(new TokenStrmData(rStrm));
205 132 : mpTokensData->mnStrmPos = rStrm.GetSvStreamPos();
206 132 : rStrm.StorePosition(mpTokensData->maStrmPos);
207 132 : mpTokensData->mnStrmSize = nFmlaSize;
208 : }
209 :
210 165 : if (pTokArr && !bFunction && !mbVBName)
211 32 : InsertName(pTokArr);
212 165 : }
213 :
214 165 : void XclImpName::ConvertTokens()
215 : {
216 165 : if (!mpTokensData)
217 198 : return;
218 :
219 132 : ExcelToSc& rFmlaConv = GetOldFmlaConverter();
220 132 : rFmlaConv.Reset();
221 132 : const ScTokenArray* pArray = NULL;
222 :
223 132 : XclImpStreamPos aOldPos;
224 132 : XclImpStream& rStrm = mpTokensData->mrStrm;
225 132 : rStrm.StorePosition(aOldPos);
226 132 : rStrm.RestorePosition(mpTokensData->maStrmPos);
227 132 : rFmlaConv.Convert(pArray, rStrm, mpTokensData->mnStrmSize, true, FT_RangeName);
228 132 : rStrm.RestorePosition(aOldPos);
229 :
230 132 : if (pArray)
231 132 : InsertName(pArray);
232 :
233 132 : mpTokensData.reset();
234 : }
235 :
236 164 : void XclImpName::InsertName(const ScTokenArray* pArray)
237 : {
238 : // create the Calc name data
239 164 : ScRangeData* pData = new ScRangeData(GetDocPtr(), maScName, *pArray, ScAddress(), meNameType);
240 164 : pData->GuessPosition(); // calculate base position for relative refs
241 164 : pData->SetIndex( mnNameIndex ); // used as unique identifier in formulas
242 164 : if (mnXclTab == EXC_NAME_GLOBAL)
243 : {
244 122 : if (!GetDoc().GetRangeName()->insert(pData))
245 0 : pData = NULL;
246 : }
247 : else
248 : {
249 42 : ScRangeName* pLocalNames = GetDoc().GetRangeName(mnScTab);
250 42 : if (pLocalNames)
251 : {
252 42 : if (!pLocalNames->insert(pData))
253 0 : pData = NULL;
254 : }
255 :
256 42 : if (GetBiff() == EXC_BIFF8 && pData)
257 : {
258 41 : ScRange aRange;
259 : // discard deleted ranges ( for the moment at least )
260 41 : if ( pData->IsValidReference( aRange ) )
261 : {
262 38 : GetExtDocOptions().GetOrCreateTabSettings( mnXclTab );
263 : }
264 : }
265 : }
266 164 : if (pData)
267 164 : mpScData = pData; // cache for later use
268 164 : }
269 :
270 84 : XclImpNameManager::XclImpNameManager( const XclImpRoot& rRoot ) :
271 84 : XclImpRoot( rRoot )
272 : {
273 84 : }
274 :
275 165 : void XclImpNameManager::ReadName( XclImpStream& rStrm )
276 : {
277 165 : sal_uLong nCount = maNameList.size();
278 165 : if( nCount < 0xFFFF )
279 165 : maNameList.push_back( new XclImpName( rStrm, static_cast< sal_uInt16 >( nCount + 1 ) ) );
280 165 : }
281 :
282 0 : const XclImpName* XclImpNameManager::FindName( const OUString& rXclName, SCTAB nScTab ) const
283 : {
284 0 : const XclImpName* pGlobalName = 0; // a found global name
285 0 : const XclImpName* pLocalName = 0; // a found local name
286 0 : for( XclImpNameList::const_iterator itName = maNameList.begin(); itName != maNameList.end() && !pLocalName; ++itName )
287 : {
288 0 : if( itName->GetXclName() == rXclName )
289 : {
290 0 : if( itName->GetScTab() == nScTab )
291 0 : pLocalName = &(*itName);
292 0 : else if( itName->IsGlobal() )
293 0 : pGlobalName = &(*itName);
294 : }
295 : }
296 0 : return pLocalName ? pLocalName : pGlobalName;
297 : }
298 :
299 199 : const XclImpName* XclImpNameManager::GetName( sal_uInt16 nXclNameIdx ) const
300 : {
301 : OSL_ENSURE( nXclNameIdx > 0, "XclImpNameManager::GetName - index must be >0" );
302 199 : return ( nXclNameIdx <= 0 || nXclNameIdx > maNameList.size() ) ? NULL : &(maNameList.at( nXclNameIdx - 1 ));
303 : }
304 :
305 83 : void XclImpNameManager::ConvertAllTokens()
306 : {
307 83 : XclImpNameList::iterator it = maNameList.begin(), itEnd = maNameList.end();
308 248 : for (; it != itEnd; ++it)
309 165 : it->ConvertTokens();
310 113 : }
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|