xref: /trunk/main/sc/source/core/tool/unitconv.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 <com/sun/star/uno/Any.hxx>
30cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "unitconv.hxx"
33cdf0e10cSrcweir #include "global.hxx"
34cdf0e10cSrcweir #include "viewopti.hxx"			//! move ScLinkConfigItem to separate header!
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace utl;
37cdf0e10cSrcweir using namespace rtl;
38cdf0e10cSrcweir using namespace com::sun::star::uno;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // --------------------------------------------------------------------
41cdf0e10cSrcweir 
42cdf0e10cSrcweir const sal_Unicode cDelim = 0x01;		// Delimiter zwischen From und To
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // --- ScUnitConverterData --------------------------------------------
46cdf0e10cSrcweir 
ScUnitConverterData(const String & rFromUnit,const String & rToUnit,double fVal)47cdf0e10cSrcweir ScUnitConverterData::ScUnitConverterData( const String& rFromUnit,
48cdf0e10cSrcweir 			const String& rToUnit, double fVal )
49cdf0e10cSrcweir 		:
50cdf0e10cSrcweir 		StrData( rFromUnit ),
51cdf0e10cSrcweir 		fValue( fVal )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	String aTmp;
54cdf0e10cSrcweir 	ScUnitConverterData::BuildIndexString( aTmp, rFromUnit, rToUnit );
55cdf0e10cSrcweir 	SetString( aTmp );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
ScUnitConverterData(const ScUnitConverterData & r)59cdf0e10cSrcweir ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r )
60cdf0e10cSrcweir 		:
61cdf0e10cSrcweir 		StrData( r ),
62cdf0e10cSrcweir 		fValue( r.fValue )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
Clone() const67cdf0e10cSrcweir ScDataObject* ScUnitConverterData::Clone() const
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	return new ScUnitConverterData( *this );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // static
BuildIndexString(String & rStr,const String & rFromUnit,const String & rToUnit)74cdf0e10cSrcweir void ScUnitConverterData::BuildIndexString( String& rStr,
75cdf0e10cSrcweir 			const String& rFromUnit, const String& rToUnit )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir #if 1
78cdf0e10cSrcweir // case sensitive
79cdf0e10cSrcweir 	rStr = rFromUnit;
80cdf0e10cSrcweir 	rStr += cDelim;
81cdf0e10cSrcweir 	rStr += rToUnit;
82cdf0e10cSrcweir #else
83cdf0e10cSrcweir // not case sensitive
84cdf0e10cSrcweir 	rStr = rFromUnit;
85cdf0e10cSrcweir 	String aTo( rToUnit );
86cdf0e10cSrcweir 	ScGlobal::pCharClass->toUpper( rStr );
87cdf0e10cSrcweir 	ScGlobal::pCharClass->toUpper( aTo );
88cdf0e10cSrcweir 	rStr += cDelim;
89cdf0e10cSrcweir 	rStr += aTo;
90cdf0e10cSrcweir #endif
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir // --- ScUnitConverter ------------------------------------------------
95cdf0e10cSrcweir 
96cdf0e10cSrcweir #define CFGPATH_UNIT		"Office.Calc/UnitConversion"
97cdf0e10cSrcweir #define CFGSTR_UNIT_FROM	"FromUnit"
98cdf0e10cSrcweir #define CFGSTR_UNIT_TO		"ToUnit"
99cdf0e10cSrcweir #define CFGSTR_UNIT_FACTOR	"Factor"
100cdf0e10cSrcweir 
ScUnitConverter(sal_uInt16 nInit,sal_uInt16 nDeltaP)101cdf0e10cSrcweir ScUnitConverter::ScUnitConverter( sal_uInt16 nInit, sal_uInt16 nDeltaP ) :
102cdf0e10cSrcweir         ScStrCollection( nInit, nDeltaP, sal_False )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	//	read from configuration - "convert.ini" is no longer used
105cdf0e10cSrcweir 	//!	config item as member to allow change of values
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	ScLinkConfigItem aConfigItem( OUString::createFromAscii( CFGPATH_UNIT ) );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	// empty node name -> use the config item's path itself
110cdf0e10cSrcweir     OUString aEmptyString;
111cdf0e10cSrcweir 	Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	long nNodeCount = aNodeNames.getLength();
114cdf0e10cSrcweir 	if ( nNodeCount )
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		const OUString* pNodeArray = aNodeNames.getConstArray();
117cdf0e10cSrcweir 		Sequence<OUString> aValNames( nNodeCount * 3 );
118cdf0e10cSrcweir 		OUString* pValNameArray = aValNames.getArray();
119cdf0e10cSrcweir 		const OUString sSlash('/');
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		long nIndex = 0;
122cdf0e10cSrcweir 		for (long i=0; i<nNodeCount; i++)
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			OUString sPrefix = pNodeArray[i];
125cdf0e10cSrcweir 			sPrefix += sSlash;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			pValNameArray[nIndex] = sPrefix;
128cdf0e10cSrcweir 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FROM );
129cdf0e10cSrcweir 			pValNameArray[nIndex] = sPrefix;
130cdf0e10cSrcweir 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_TO );
131cdf0e10cSrcweir 			pValNameArray[nIndex] = sPrefix;
132cdf0e10cSrcweir 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FACTOR );
133cdf0e10cSrcweir 		}
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 		if (aProperties.getLength() == aValNames.getLength())
138cdf0e10cSrcweir 		{
139cdf0e10cSrcweir 			const Any* pProperties = aProperties.getConstArray();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 			OUString sFromUnit;
142cdf0e10cSrcweir 			OUString sToUnit;
143cdf0e10cSrcweir 			double fFactor = 0;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 			nIndex = 0;
146cdf0e10cSrcweir 			for (long i=0; i<nNodeCount; i++)
147cdf0e10cSrcweir 			{
148cdf0e10cSrcweir 				pProperties[nIndex++] >>= sFromUnit;
149cdf0e10cSrcweir 				pProperties[nIndex++] >>= sToUnit;
150cdf0e10cSrcweir 				pProperties[nIndex++] >>= fFactor;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 				ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
153cdf0e10cSrcweir 				if ( !Insert( pNew ) )
154cdf0e10cSrcweir 					delete pNew;
155cdf0e10cSrcweir 			}
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
GetValue(double & fValue,const String & rFromUnit,const String & rToUnit) const160cdf0e10cSrcweir sal_Bool ScUnitConverter::GetValue( double& fValue, const String& rFromUnit,
161cdf0e10cSrcweir 				const String& rToUnit ) const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	ScUnitConverterData aSearch( rFromUnit, rToUnit );
164cdf0e10cSrcweir 	sal_uInt16 nIndex;
165cdf0e10cSrcweir 	if ( Search( &aSearch, nIndex ) )
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir 		fValue = ((const ScUnitConverterData*)(At( nIndex )))->GetValue();
168cdf0e10cSrcweir 		return sal_True;
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 	fValue = 1.0;
171cdf0e10cSrcweir 	return sal_False;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175