1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/errcode.hxx>
21 #include <basic/sbx.hxx>
22 #include "sbxconv.hxx"
24 #include "runtime.hxx"
25 #include <rtl/ustrbuf.hxx>
27 // The conversion of an item onto String was handled via the Put-Methods
28 // of the several data types to avoid double code.
30 OUString
ImpGetString( const SbxValues
* p
)
34 aTmp
.eType
= SbxSTRING
;
35 aTmp
.pOUString
= &aRes
;
39 SbxBase::SetError( SbxERR_CONVERSION
);
43 ImpPutChar( &aTmp
, p
->nChar
); break;
45 ImpPutByte( &aTmp
, p
->nByte
); break;
47 ImpPutInteger( &aTmp
, p
->nInteger
); break;
49 ImpPutBool( &aTmp
, p
->nUShort
); break;
51 ImpPutUShort( &aTmp
, p
->nUShort
); break;
53 ImpPutLong( &aTmp
, p
->nLong
); break;
55 ImpPutULong( &aTmp
, p
->nULong
); break;
57 ImpPutSingle( &aTmp
, p
->nSingle
); break;
59 ImpPutDouble( &aTmp
, p
->nDouble
); break;
61 ImpPutCurrency( &aTmp
, p
->nInt64
); break;
63 case SbxBYREF
| SbxDECIMAL
:
64 ImpPutDecimal( &aTmp
, p
->pDecimal
); break;
66 ImpPutInt64( &aTmp
, p
->nInt64
); break;
68 ImpPutUInt64( &aTmp
, p
->uInt64
); break;
69 case SbxBYREF
| SbxSTRING
:
74 *aTmp
.pOUString
= *p
->pOUString
;
79 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
82 aRes
= pVal
->GetOUString();
84 else if( p
->pObj
&& p
->pObj
->IsFixed()
85 && (p
->pObj
->GetType() == (SbxARRAY
| SbxBYTE
)) )
87 // convert byte array to string
88 SbxArray
* pArr
= PTR_CAST(SbxArray
, p
->pObj
);
91 aRes
= ByteArrayToString( pArr
);
96 SbxBase::SetError( SbxERR_NO_OBJECT
);
101 // Here will be created the String "Error n"
102 aRes
= SbxRes( STRING_ERRORMSG
);
103 aRes
+= OUString( p
->nUShort
); break;
105 ImpPutDate( &aTmp
, p
->nDouble
); break;
107 case SbxBYREF
| SbxCHAR
:
108 ImpPutChar( &aTmp
, *p
->pChar
); break;
109 case SbxBYREF
| SbxBYTE
:
110 ImpPutByte( &aTmp
, *p
->pByte
); break;
111 case SbxBYREF
| SbxINTEGER
:
112 case SbxBYREF
| SbxBOOL
:
113 ImpPutInteger( &aTmp
, *p
->pInteger
); break;
114 case SbxBYREF
| SbxLONG
:
115 ImpPutLong( &aTmp
, *p
->pLong
); break;
116 case SbxBYREF
| SbxULONG
:
117 ImpPutULong( &aTmp
, *p
->pULong
); break;
118 case SbxBYREF
| SbxERROR
:
119 case SbxBYREF
| SbxUSHORT
:
120 ImpPutUShort( &aTmp
, *p
->pUShort
); break;
121 case SbxBYREF
| SbxSINGLE
:
122 ImpPutSingle( &aTmp
, *p
->pSingle
); break;
123 case SbxBYREF
| SbxDATE
:
124 case SbxBYREF
| SbxDOUBLE
:
125 ImpPutDouble( &aTmp
, *p
->pDouble
); break;
126 case SbxBYREF
| SbxCURRENCY
:
127 ImpPutCurrency( &aTmp
, *p
->pnInt64
); break;
128 case SbxBYREF
| SbxSALINT64
:
129 ImpPutInt64( &aTmp
, *p
->pnInt64
); break;
130 case SbxBYREF
| SbxSALUINT64
:
131 ImpPutUInt64( &aTmp
, *p
->puInt64
); break;
133 SbxBase::SetError( SbxERR_CONVERSION
);
138 // From 1997-04-10, new function for SbxValue::GetCoreString()
139 OUString
ImpGetCoreString( const SbxValues
* p
)
141 // For now only for double
142 if( ( p
->eType
& (~SbxBYREF
) ) == SbxDOUBLE
)
146 aTmp
.eType
= SbxSTRING
;
147 aTmp
.pOUString
= &aRes
;
148 if( p
->eType
== SbxDOUBLE
)
149 ImpPutDouble( &aTmp
, p
->nDouble
, true ); // true = bCoreString
151 ImpPutDouble( &aTmp
, *p
->pDouble
, true ); // true = bCoreString
155 return ImpGetString( p
);
158 void ImpPutString( SbxValues
* p
, const OUString
* n
)
161 aTmp
.eType
= SbxSTRING
;
162 OUString
* pTmp
= NULL
;
163 // as a precaution, if a NULL-Ptr appears
165 n
= pTmp
= new OUString
;
166 aTmp
.pOUString
= (OUString
*)n
;
170 p
->nChar
= ImpGetChar( &aTmp
); break;
172 p
->nByte
= ImpGetByte( &aTmp
); break;
175 p
->nInteger
= ImpGetInteger( &aTmp
); break;
177 p
->nLong
= ImpGetLong( &aTmp
); break;
179 p
->nULong
= ImpGetULong( &aTmp
); break;
182 p
->nUShort
= ImpGetUShort( &aTmp
); break;
184 p
->nSingle
= ImpGetSingle( &aTmp
); break;
186 p
->nDouble
= ImpGetDate( &aTmp
); break;
188 p
->nDouble
= ImpGetDouble( &aTmp
); break;
190 p
->nInt64
= ImpGetCurrency( &aTmp
); break;
192 case SbxBYREF
| SbxDECIMAL
:
193 releaseDecimalPtr( p
->pDecimal
);
194 p
->pDecimal
= ImpGetDecimal( &aTmp
); break;
196 p
->nInt64
= ImpGetInt64( &aTmp
); break;
198 p
->uInt64
= ImpGetUInt64( &aTmp
); break;
200 case SbxBYREF
| SbxSTRING
:
206 p
->pOUString
= new OUString( *n
);
211 delete p
->pOUString
, p
->pOUString
= NULL
;
215 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
217 pVal
->PutString( *n
);
219 SbxBase::SetError( SbxERR_NO_OBJECT
);
222 case SbxBYREF
| SbxCHAR
:
223 *p
->pChar
= ImpGetChar( p
); break;
224 case SbxBYREF
| SbxBYTE
:
225 *p
->pByte
= ImpGetByte( p
); break;
226 case SbxBYREF
| SbxINTEGER
:
227 *p
->pInteger
= ImpGetInteger( p
); break;
228 case SbxBYREF
| SbxBOOL
:
229 *p
->pUShort
= sal::static_int_cast
< sal_uInt16
>( ImpGetBool( p
) );
231 case SbxBYREF
| SbxERROR
:
232 case SbxBYREF
| SbxUSHORT
:
233 *p
->pUShort
= ImpGetUShort( p
); break;
234 case SbxBYREF
| SbxLONG
:
235 *p
->pLong
= ImpGetLong( p
); break;
236 case SbxBYREF
| SbxULONG
:
237 *p
->pULong
= ImpGetULong( p
); break;
238 case SbxBYREF
| SbxSINGLE
:
239 *p
->pSingle
= ImpGetSingle( p
); break;
240 case SbxBYREF
| SbxDATE
:
241 *p
->pDouble
= ImpGetDate( p
); break;
242 case SbxBYREF
| SbxDOUBLE
:
243 *p
->pDouble
= ImpGetDouble( p
); break;
244 case SbxBYREF
| SbxCURRENCY
:
245 *p
->pnInt64
= ImpGetCurrency( p
); break;
246 case SbxBYREF
| SbxSALINT64
:
247 *p
->pnInt64
= ImpGetInt64( p
); break;
248 case SbxBYREF
| SbxSALUINT64
:
249 *p
->puInt64
= ImpGetUInt64( p
); break;
251 SbxBase::SetError( SbxERR_CONVERSION
);
257 // Convert string to an array of bytes, preserving unicode (2bytes per character)
258 SbxArray
* StringToByteArray(const OUString
& rStr
)
260 sal_Int32 nArraySize
= rStr
.getLength() * 2;
261 const sal_Unicode
* pSrc
= rStr
.getStr();
262 SbxDimArray
* pArray
= new SbxDimArray(SbxBYTE
);
263 #ifdef DISABLE_SCRIPTING
264 bool bIncIndex
= false;
266 bool bIncIndex
= ( IsBaseIndexOne() && SbiRuntime::isVBAEnabled() );
271 pArray
->AddDim32( 1, nArraySize
);
273 pArray
->AddDim32( 0, nArraySize
-1 );
277 pArray
->unoAddDim( 0, -1 );
280 for( sal_uInt16 i
=0; i
< nArraySize
; i
++)
282 SbxVariable
* pNew
= new SbxVariable( SbxBYTE
);
283 sal_uInt8 aByte
= static_cast< sal_uInt8
>( (i
%2) ? ((*pSrc
) >> 8) & 0xff : (*pSrc
) & 0xff );
284 pNew
->PutByte( aByte
);
285 pNew
->SetFlag( SBX_WRITE
);
286 pArray
->Put( pNew
, i
);
293 // Convert an array of bytes to string (2bytes per character)
294 OUString
ByteArrayToString(SbxArray
* pArr
)
296 sal_uInt16 nCount
= pArr
->Count();
297 OUStringBuffer aStrBuf
;
298 sal_Unicode aChar
= 0;
299 for( sal_uInt16 i
= 0 ; i
< nCount
; i
++ )
301 sal_Unicode aTempChar
= pArr
->Get(i
)->GetByte();
304 aChar
= (aTempChar
<< 8 ) | aChar
;
305 aStrBuf
.append(aChar
);
316 aStrBuf
.append(aChar
);
319 return aStrBuf
.makeStringAndClear();
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */