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 <tools/errcode.hxx>
21 : #include <basic/sbx.hxx>
22 : #include "sbxconv.hxx"
23 : #include "sbxres.hxx"
24 : #include "runtime.hxx"
25 : #include <rtl/ustrbuf.hxx>
26 :
27 : // The conversion of an item onto String was handled via the Put-Methods
28 : // of the several data types to avoid double code.
29 :
30 14 : ::rtl::OUString ImpGetString( const SbxValues* p )
31 : {
32 14 : SbxValues aTmp;
33 14 : ::rtl::OUString aRes;
34 14 : aTmp.eType = SbxSTRING;
35 14 : aTmp.pOUString = &aRes;
36 14 : switch( +p->eType )
37 : {
38 : case SbxNULL:
39 0 : SbxBase::SetError( SbxERR_CONVERSION );
40 : case SbxEMPTY:
41 0 : break;
42 : case SbxCHAR:
43 0 : ImpPutChar( &aTmp, p->nChar ); break;
44 : case SbxBYTE:
45 0 : ImpPutByte( &aTmp, p->nByte ); break;
46 : case SbxINTEGER:
47 0 : ImpPutInteger( &aTmp, p->nInteger ); break;
48 : case SbxBOOL:
49 0 : ImpPutBool( &aTmp, p->nUShort ); break;
50 : case SbxUSHORT:
51 0 : ImpPutUShort( &aTmp, p->nUShort ); break;
52 : case SbxLONG:
53 0 : ImpPutLong( &aTmp, p->nLong ); break;
54 : case SbxULONG:
55 0 : ImpPutULong( &aTmp, p->nULong ); break;
56 : case SbxSINGLE:
57 0 : ImpPutSingle( &aTmp, p->nSingle ); break;
58 : case SbxDOUBLE:
59 0 : ImpPutDouble( &aTmp, p->nDouble ); break;
60 : case SbxCURRENCY:
61 0 : ImpPutCurrency( &aTmp, p->nInt64 ); break;
62 : case SbxDECIMAL:
63 : case SbxBYREF | SbxDECIMAL:
64 0 : ImpPutDecimal( &aTmp, p->pDecimal ); break;
65 : case SbxSALINT64:
66 0 : ImpPutInt64( &aTmp, p->nInt64 ); break;
67 : case SbxSALUINT64:
68 0 : ImpPutUInt64( &aTmp, p->uInt64 ); break;
69 : case SbxBYREF | SbxSTRING:
70 : case SbxSTRING:
71 : case SbxLPSTR:
72 14 : if ( p->pOUString )
73 : {
74 14 : *aTmp.pOUString = *p->pOUString;
75 : }
76 14 : break;
77 : case SbxOBJECT:
78 : {
79 0 : SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
80 0 : if( pVal )
81 : {
82 0 : aRes = pVal->GetOUString();
83 : }
84 0 : else if( p->pObj && p->pObj->IsFixed()
85 0 : && (p->pObj->GetType() == (SbxARRAY | SbxBYTE )) )
86 : {
87 : // convert byte array to string
88 0 : SbxArray* pArr = PTR_CAST(SbxArray, p->pObj);
89 0 : if( pArr )
90 : {
91 0 : aRes = ByteArrayToString( pArr );
92 : }
93 : }
94 : else
95 : {
96 0 : SbxBase::SetError( SbxERR_NO_OBJECT );
97 : }
98 0 : break;
99 : }
100 : case SbxERROR:
101 : // Here will be created the String "Error n"
102 0 : aRes = SbxRes( STRING_ERRORMSG );
103 0 : aRes += ::rtl::OUString( p->nUShort ); break;
104 : case SbxDATE:
105 0 : ImpPutDate( &aTmp, p->nDouble ); break;
106 :
107 : case SbxBYREF | SbxCHAR:
108 0 : ImpPutChar( &aTmp, *p->pChar ); break;
109 : case SbxBYREF | SbxBYTE:
110 0 : ImpPutByte( &aTmp, *p->pByte ); break;
111 : case SbxBYREF | SbxINTEGER:
112 : case SbxBYREF | SbxBOOL:
113 0 : ImpPutInteger( &aTmp, *p->pInteger ); break;
114 : case SbxBYREF | SbxLONG:
115 0 : ImpPutLong( &aTmp, *p->pLong ); break;
116 : case SbxBYREF | SbxULONG:
117 0 : ImpPutULong( &aTmp, *p->pULong ); break;
118 : case SbxBYREF | SbxERROR:
119 : case SbxBYREF | SbxUSHORT:
120 0 : ImpPutUShort( &aTmp, *p->pUShort ); break;
121 : case SbxBYREF | SbxSINGLE:
122 0 : ImpPutSingle( &aTmp, *p->pSingle ); break;
123 : case SbxBYREF | SbxDATE:
124 : case SbxBYREF | SbxDOUBLE:
125 0 : ImpPutDouble( &aTmp, *p->pDouble ); break;
126 : case SbxBYREF | SbxCURRENCY:
127 0 : ImpPutCurrency( &aTmp, *p->pnInt64 ); break;
128 : case SbxBYREF | SbxSALINT64:
129 0 : ImpPutInt64( &aTmp, *p->pnInt64 ); break;
130 : case SbxBYREF | SbxSALUINT64:
131 0 : ImpPutUInt64( &aTmp, *p->puInt64 ); break;
132 : default:
133 0 : SbxBase::SetError( SbxERR_CONVERSION );
134 : }
135 14 : return aRes;
136 : }
137 :
138 : // From 1997-04-10, new function for SbxValue::GetCoreString()
139 0 : ::rtl::OUString ImpGetCoreString( const SbxValues* p )
140 : {
141 : // For now only for double
142 0 : if( ( p->eType & (~SbxBYREF) ) == SbxDOUBLE )
143 : {
144 0 : SbxValues aTmp;
145 0 : rtl::OUString aRes;
146 0 : aTmp.eType = SbxSTRING;
147 0 : aTmp.pOUString = &aRes;
148 0 : if( p->eType == SbxDOUBLE )
149 0 : ImpPutDouble( &aTmp, p->nDouble, true ); // true = bCoreString
150 : else
151 0 : ImpPutDouble( &aTmp, *p->pDouble, true ); // true = bCoreString
152 0 : return aRes;
153 : }
154 : else
155 0 : return ImpGetString( p );
156 : }
157 :
158 73 : void ImpPutString( SbxValues* p, const ::rtl::OUString* n )
159 : {
160 73 : SbxValues aTmp;
161 73 : aTmp.eType = SbxSTRING;
162 73 : ::rtl::OUString* pTmp = NULL;
163 : // as a precaution, if a NULL-Ptr appears
164 73 : if( !n )
165 0 : n = pTmp = new ::rtl::OUString;
166 73 : aTmp.pOUString = (::rtl::OUString*)n;
167 73 : switch( +p->eType )
168 : {
169 : case SbxCHAR:
170 0 : p->nChar = ImpGetChar( &aTmp ); break;
171 : case SbxBYTE:
172 0 : p->nByte = ImpGetByte( &aTmp ); break;
173 : case SbxINTEGER:
174 : case SbxBOOL:
175 0 : p->nInteger = ImpGetInteger( &aTmp ); break;
176 : case SbxLONG:
177 0 : p->nLong = ImpGetLong( &aTmp ); break;
178 : case SbxULONG:
179 0 : p->nULong = ImpGetULong( &aTmp ); break;
180 : case SbxERROR:
181 : case SbxUSHORT:
182 0 : p->nUShort = ImpGetUShort( &aTmp ); break;
183 : case SbxSINGLE:
184 0 : p->nSingle = ImpGetSingle( &aTmp ); break;
185 : case SbxDATE:
186 0 : p->nDouble = ImpGetDate( &aTmp ); break;
187 : case SbxDOUBLE:
188 0 : p->nDouble = ImpGetDouble( &aTmp ); break;
189 : case SbxCURRENCY:
190 0 : p->nInt64 = ImpGetCurrency( &aTmp ); break;
191 : case SbxDECIMAL:
192 : case SbxBYREF | SbxDECIMAL:
193 0 : releaseDecimalPtr( p->pDecimal );
194 0 : p->pDecimal = ImpGetDecimal( &aTmp ); break;
195 : case SbxSALINT64:
196 0 : p->nInt64 = ImpGetInt64( &aTmp ); break;
197 : case SbxSALUINT64:
198 0 : p->uInt64 = ImpGetUInt64( &aTmp ); break;
199 :
200 : case SbxBYREF | SbxSTRING:
201 : case SbxSTRING:
202 : case SbxLPSTR:
203 73 : if( !n->isEmpty() )
204 : {
205 43 : if( !p->pOUString )
206 43 : p->pOUString = new ::rtl::OUString( *n );
207 : else
208 0 : *p->pOUString = *n;
209 : }
210 : else
211 30 : delete p->pOUString, p->pOUString = NULL;
212 73 : break;
213 : case SbxOBJECT:
214 : {
215 0 : SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
216 0 : if( pVal )
217 0 : pVal->PutString( *n );
218 : else
219 0 : SbxBase::SetError( SbxERR_NO_OBJECT );
220 0 : break;
221 : }
222 : case SbxBYREF | SbxCHAR:
223 0 : *p->pChar = ImpGetChar( p ); break;
224 : case SbxBYREF | SbxBYTE:
225 0 : *p->pByte = ImpGetByte( p ); break;
226 : case SbxBYREF | SbxINTEGER:
227 0 : *p->pInteger = ImpGetInteger( p ); break;
228 : case SbxBYREF | SbxBOOL:
229 0 : *p->pUShort = sal::static_int_cast< sal_uInt16 >( ImpGetBool( p ) );
230 0 : break;
231 : case SbxBYREF | SbxERROR:
232 : case SbxBYREF | SbxUSHORT:
233 0 : *p->pUShort = ImpGetUShort( p ); break;
234 : case SbxBYREF | SbxLONG:
235 0 : *p->pLong = ImpGetLong( p ); break;
236 : case SbxBYREF | SbxULONG:
237 0 : *p->pULong = ImpGetULong( p ); break;
238 : case SbxBYREF | SbxSINGLE:
239 0 : *p->pSingle = ImpGetSingle( p ); break;
240 : case SbxBYREF | SbxDATE:
241 0 : *p->pDouble = ImpGetDate( p ); break;
242 : case SbxBYREF | SbxDOUBLE:
243 0 : *p->pDouble = ImpGetDouble( p ); break;
244 : case SbxBYREF | SbxCURRENCY:
245 0 : *p->pnInt64 = ImpGetCurrency( p ); break;
246 : case SbxBYREF | SbxSALINT64:
247 0 : *p->pnInt64 = ImpGetInt64( p ); break;
248 : case SbxBYREF | SbxSALUINT64:
249 0 : *p->puInt64 = ImpGetUInt64( p ); break;
250 : default:
251 0 : SbxBase::SetError( SbxERR_CONVERSION );
252 : }
253 73 : delete pTmp;
254 73 : }
255 :
256 :
257 : // Convert string to an array of bytes, preserving unicode (2bytes per character)
258 0 : SbxArray* StringToByteArray(const ::rtl::OUString& rStr)
259 : {
260 0 : sal_Int32 nArraySize = rStr.getLength() * 2;
261 0 : const sal_Unicode* pSrc = rStr.getStr();
262 0 : SbxDimArray* pArray = new SbxDimArray(SbxBYTE);
263 : #ifdef DISABLE_SCRIPTING
264 : bool bIncIndex = false;
265 : #else
266 0 : bool bIncIndex = ( IsBaseIndexOne() && SbiRuntime::isVBAEnabled() );
267 : #endif
268 0 : if( nArraySize )
269 : {
270 0 : if( bIncIndex )
271 0 : pArray->AddDim32( 1, nArraySize );
272 : else
273 0 : pArray->AddDim32( 0, nArraySize-1 );
274 : }
275 : else
276 : {
277 0 : pArray->unoAddDim( 0, -1 );
278 : }
279 :
280 0 : for( sal_uInt16 i=0; i< nArraySize; i++)
281 : {
282 0 : SbxVariable* pNew = new SbxVariable( SbxBYTE );
283 0 : sal_uInt8 aByte = static_cast< sal_uInt8 >( (i%2) ? ((*pSrc) >> 8) & 0xff : (*pSrc) & 0xff );
284 0 : pNew->PutByte( aByte );
285 0 : pNew->SetFlag( SBX_WRITE );
286 0 : pArray->Put( pNew, i );
287 0 : if( i%2 )
288 0 : pSrc++;
289 : }
290 0 : return pArray;
291 : }
292 :
293 : // Convert an array of bytes to string (2bytes per character)
294 0 : ::rtl::OUString ByteArrayToString(SbxArray* pArr)
295 : {
296 0 : sal_uInt16 nCount = pArr->Count();
297 0 : ::rtl::OUStringBuffer aStrBuf;
298 0 : sal_Unicode aChar = 0;
299 0 : for( sal_uInt16 i = 0 ; i < nCount ; i++ )
300 : {
301 0 : sal_Unicode aTempChar = pArr->Get(i)->GetByte();
302 0 : if( i%2 )
303 : {
304 0 : aChar = (aTempChar << 8 ) | aChar;
305 0 : aStrBuf.append(aChar);
306 0 : aChar = 0;
307 : }
308 : else
309 : {
310 0 : aChar = aTempChar;
311 : }
312 : }
313 :
314 0 : if( nCount%2 )
315 : {
316 0 : aStrBuf.append(aChar);
317 : }
318 :
319 0 : return aStrBuf.makeStringAndClear();
320 : }
321 :
322 :
323 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|