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 <vcl/svapp.hxx>
21 : #include <svl/zforlist.hxx>
22 : #include <tools/errcode.hxx>
23 : #include <tools/color.hxx>
24 : #include <i18nlangtag/lang.h>
25 : #include <basic/sbx.hxx>
26 : #include "sbxconv.hxx"
27 : #include "math.h"
28 : #include <comphelper/processfactory.hxx>
29 :
30 :
31 3 : double ImpGetDate( const SbxValues* p )
32 : {
33 : double nRes;
34 : SbxValue* pVal;
35 :
36 3 : switch( +p->eType )
37 : {
38 : case SbxNULL:
39 0 : SbxBase::SetError( SbxERR_CONVERSION );
40 : case SbxEMPTY:
41 0 : nRes = 0;
42 0 : break;
43 : case SbxCHAR:
44 0 : nRes = p->nChar;
45 0 : break;
46 : case SbxBYTE:
47 0 : nRes = p->nByte;
48 0 : break;
49 : case SbxINTEGER:
50 : case SbxBOOL:
51 1 : nRes = p->nInteger;
52 1 : break;
53 : case SbxERROR:
54 : case SbxUSHORT:
55 0 : nRes = p->nUShort;
56 0 : break;
57 : case SbxLONG:
58 0 : nRes = (double) p->nLong;
59 0 : break;
60 : case SbxULONG:
61 0 : nRes = (double) p->nULong;
62 0 : break;
63 : case SbxSINGLE:
64 0 : nRes = p->nSingle;
65 0 : break;
66 : case SbxDATE:
67 : case SbxDOUBLE:
68 2 : nRes = p->nDouble;
69 2 : break;
70 : case SbxCURRENCY:
71 0 : nRes = ImpCurrencyToDouble( p->nInt64 );
72 0 : break;
73 : case SbxSALINT64:
74 0 : nRes = static_cast< double >(p->nInt64);
75 0 : break;
76 : case SbxSALUINT64:
77 0 : nRes = ImpSalUInt64ToDouble( p->uInt64 );
78 0 : break;
79 : case SbxDECIMAL:
80 : case SbxBYREF | SbxDECIMAL:
81 0 : if( p->pDecimal )
82 : {
83 0 : p->pDecimal->getDouble( nRes );
84 : }
85 : else
86 : {
87 0 : nRes = 0.0;
88 : }
89 0 : break;
90 : case SbxBYREF | SbxSTRING:
91 : case SbxSTRING:
92 : case SbxLPSTR:
93 0 : if( !p->pOUString )
94 : {
95 0 : nRes = 0;
96 : }
97 : else
98 : {
99 0 : LanguageType eLangType = GetpApp()->GetSettings().GetLanguageTag().getLanguageType();
100 :
101 0 : SvNumberFormatter* pFormatter = new SvNumberFormatter( comphelper::getProcessComponentContext(), eLangType );
102 :
103 : sal_uInt32 nIndex;
104 0 : sal_Int32 nCheckPos = 0;
105 0 : short nType = 127;
106 :
107 : // Default templates of the formatter have only two-digit
108 : // date. Therefore register an own format.
109 :
110 : // HACK, because the number formatter in PutandConvertEntry replace the wildcard
111 : // for month, day, year not according to the configuration.
112 : // Problem: Print Year(Date) under Engl. OS
113 : // quod vide basic/source/runtime/runtime.cxx
114 :
115 0 : SvtSysLocale aSysLocale;
116 0 : DateFormat eDate = aSysLocale.GetLocaleData().getDateFormat();
117 0 : OUString aDateStr;
118 0 : switch( eDate )
119 : {
120 : default:
121 0 : case MDY: aDateStr = "MM/DD/YYYY"; break;
122 0 : case DMY: aDateStr = "DD/MM/YYYY"; break;
123 0 : case YMD: aDateStr = "YYYY/MM/DD"; break;
124 : }
125 :
126 0 : OUString aStr( aDateStr );
127 0 : aStr += " HH:MM:SS";
128 :
129 : pFormatter->PutandConvertEntry( aStr, nCheckPos, nType,
130 0 : nIndex, LANGUAGE_ENGLISH_US, eLangType );
131 0 : sal_Bool bSuccess = pFormatter->IsNumberFormat( *p->pOUString, nIndex, nRes );
132 0 : if ( bSuccess )
133 : {
134 0 : short nType_ = pFormatter->GetType( nIndex );
135 0 : if(!(nType_ & ( NUMBERFORMAT_DATETIME | NUMBERFORMAT_DATE |
136 : NUMBERFORMAT_TIME | NUMBERFORMAT_DEFINED )))
137 : {
138 0 : bSuccess = sal_False;
139 : }
140 : }
141 :
142 0 : if ( !bSuccess )
143 : {
144 0 : SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
145 : }
146 :
147 0 : delete pFormatter;
148 : }
149 0 : break;
150 : case SbxOBJECT:
151 0 : pVal = PTR_CAST(SbxValue,p->pObj);
152 0 : if( pVal )
153 : {
154 0 : nRes = pVal->GetDate();
155 : }
156 : else
157 : {
158 0 : SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
159 : }
160 0 : break;
161 : case SbxBYREF | SbxCHAR:
162 0 : nRes = *p->pChar;
163 0 : break;
164 : case SbxBYREF | SbxBYTE:
165 0 : nRes = *p->pByte;
166 0 : break;
167 : case SbxBYREF | SbxINTEGER:
168 : case SbxBYREF | SbxBOOL:
169 0 : nRes = *p->pInteger;
170 0 : break;
171 : case SbxBYREF | SbxLONG:
172 0 : nRes = *p->pLong;
173 0 : break;
174 : case SbxBYREF | SbxULONG:
175 0 : nRes = *p->pULong;
176 0 : break;
177 : case SbxBYREF | SbxERROR:
178 : case SbxBYREF | SbxUSHORT:
179 0 : nRes = *p->pUShort;
180 0 : break;
181 : case SbxBYREF | SbxSINGLE:
182 0 : nRes = *p->pSingle;
183 0 : break;
184 : case SbxBYREF | SbxDATE:
185 : case SbxBYREF | SbxDOUBLE:
186 0 : nRes = *p->pDouble;
187 0 : break;
188 : case SbxBYREF | SbxCURRENCY:
189 0 : nRes = ImpCurrencyToDouble( *p->pnInt64 );
190 0 : break;
191 : case SbxBYREF | SbxSALINT64:
192 0 : nRes = static_cast< double >(*p->pnInt64);
193 0 : break;
194 : case SbxBYREF | SbxSALUINT64:
195 0 : nRes = ImpSalUInt64ToDouble( *p->puInt64 );
196 0 : break;
197 : default:
198 0 : SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
199 0 : break;
200 : }
201 3 : return nRes;
202 : }
203 :
204 36 : void ImpPutDate( SbxValues* p, double n )
205 : {
206 36 : SbxValues aTmp;
207 : SbxDecimal* pDec;
208 : SbxValue* pVal;
209 :
210 : start:
211 36 : switch( +p->eType )
212 : {
213 : case SbxDATE:
214 : case SbxDOUBLE:
215 31 : p->nDouble = n;
216 31 : break;
217 : // from here will be tested
218 : case SbxCHAR:
219 0 : aTmp.pChar = &p->nChar;
220 0 : goto direct;
221 : case SbxBYTE:
222 0 : aTmp.pByte = &p->nByte;
223 0 : goto direct;
224 : case SbxINTEGER:
225 : case SbxBOOL:
226 0 : aTmp.pInteger = &p->nInteger;
227 0 : goto direct;
228 : case SbxLONG:
229 0 : aTmp.pLong = &p->nLong;
230 0 : goto direct;
231 : case SbxULONG:
232 0 : aTmp.pULong = &p->nULong;
233 0 : goto direct;
234 : case SbxERROR:
235 : case SbxUSHORT:
236 0 : aTmp.pUShort = &p->nUShort;
237 0 : goto direct;
238 : case SbxSINGLE:
239 0 : aTmp.pSingle = &p->nSingle;
240 0 : goto direct;
241 : case SbxCURRENCY:
242 : case SbxSALINT64:
243 0 : aTmp.pnInt64 = &p->nInt64;
244 0 : goto direct;
245 : case SbxSALUINT64:
246 0 : aTmp.puInt64 = &p->uInt64;
247 0 : goto direct;
248 : case SbxDECIMAL:
249 : case SbxBYREF | SbxDECIMAL:
250 0 : pDec = ImpCreateDecimal( p );
251 0 : if( !pDec->setDouble( n ) )
252 : {
253 0 : SbxBase::SetError( SbxERR_OVERFLOW );
254 : }
255 0 : break;
256 : direct:
257 0 : aTmp.eType = SbxDataType( p->eType | SbxBYREF );
258 0 : p = &aTmp; goto start;
259 :
260 : case SbxBYREF | SbxSTRING:
261 : case SbxSTRING:
262 : case SbxLPSTR:
263 : {
264 5 : if( !p->pOUString )
265 : {
266 0 : p->pOUString = new OUString;
267 : }
268 : Color* pColor;
269 :
270 5 : LanguageType eLangType = GetpApp()->GetSettings().GetLanguageTag().getLanguageType();
271 5 : SvNumberFormatter* pFormatter = new SvNumberFormatter( comphelper::getProcessComponentContext(), eLangType );
272 :
273 : sal_uInt32 nIndex;
274 5 : sal_Int32 nCheckPos = 0;
275 : short nType;
276 :
277 5 : SvtSysLocale aSysLocale;
278 5 : DateFormat eDate = aSysLocale.GetLocaleData().getDateFormat();
279 10 : OUString aStr;
280 : // if the whole-number part is 0, we want no year!
281 5 : if( n <= -1.0 || n >= 1.0 )
282 : {
283 : // Time only if != 00:00:00
284 10 : if( floor( n ) == n )
285 : {
286 5 : switch( eDate )
287 : {
288 5 : case MDY: aStr = "MM.TT.JJJJ"; break;
289 0 : case DMY: aStr = "TT.MM.JJJJ"; break;
290 0 : case YMD: aStr = "JJJJ.MM.TT"; break;
291 0 : default: aStr = "MM.TT.JJJJ";
292 : }
293 : }
294 : else
295 : {
296 0 : switch( eDate )
297 : {
298 0 : case MDY: aStr = "MM.TT.JJJJ HH:MM:SS"; break;
299 0 : case DMY: aStr = "TT.MM.JJJJ HH:MM:SS"; break;
300 0 : case YMD: aStr = "JJJJ.MM.TT HH:MM:SS"; break;
301 0 : default: aStr = "MM.TT.JJJJ HH:MM:SS";
302 : }
303 : }
304 : }
305 : else
306 : {
307 0 : aStr = "HH:MM:SS";
308 : }
309 : pFormatter->PutandConvertEntry( aStr,
310 : nCheckPos,
311 : nType,
312 : nIndex,
313 : LANGUAGE_GERMAN,
314 5 : eLangType );
315 5 : OUString aTmpString;
316 5 : pFormatter->GetOutputString( n, nIndex, aTmpString, &pColor );
317 5 : *p->pOUString = aTmpString;
318 5 : delete pFormatter;
319 10 : break;
320 : }
321 : case SbxOBJECT:
322 0 : pVal = PTR_CAST(SbxValue,p->pObj);
323 0 : if( pVal )
324 : {
325 0 : pVal->PutDate( n );
326 : }
327 : else
328 : {
329 0 : SbxBase::SetError( SbxERR_NO_OBJECT );
330 : }
331 0 : break;
332 : case SbxBYREF | SbxCHAR:
333 0 : if( n > SbxMAXCHAR )
334 : {
335 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
336 : }
337 0 : else if( n < SbxMINCHAR )
338 : {
339 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
340 : }
341 0 : *p->pChar = (sal_Unicode) n;
342 0 : break;
343 : case SbxBYREF | SbxBYTE:
344 0 : if( n > SbxMAXBYTE )
345 : {
346 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
347 : }
348 0 : else if( n < 0 )
349 : {
350 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
351 : }
352 0 : *p->pByte = (sal_uInt8) n;
353 0 : break;
354 : case SbxBYREF | SbxINTEGER:
355 : case SbxBYREF | SbxBOOL:
356 0 : if( n > SbxMAXINT )
357 : {
358 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
359 : }
360 0 : else if( n < SbxMININT )
361 : {
362 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
363 : }
364 0 : *p->pInteger = (sal_Int16) n;
365 0 : break;
366 : case SbxBYREF | SbxERROR:
367 : case SbxBYREF | SbxUSHORT:
368 0 : if( n > SbxMAXUINT )
369 : {
370 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
371 : }
372 0 : else if( n < 0 )
373 : {
374 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
375 : }
376 0 : *p->pUShort = (sal_uInt16) n;
377 0 : break;
378 : case SbxBYREF | SbxLONG:
379 0 : if( n > SbxMAXLNG )
380 : {
381 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXLNG;
382 : }
383 0 : else if( n < SbxMINLNG )
384 : {
385 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINLNG;
386 : }
387 0 : *p->pLong = (sal_Int32) n;
388 0 : break;
389 : case SbxBYREF | SbxULONG:
390 0 : if( n > SbxMAXULNG )
391 : {
392 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXULNG;
393 : }
394 0 : else if( n < 0 )
395 : {
396 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
397 : }
398 0 : *p->pULong = (sal_uInt32) n;
399 0 : break;
400 : case SbxBYREF | SbxSINGLE:
401 0 : if( n > SbxMAXSNG )
402 : {
403 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG;
404 : }
405 0 : else if( n < SbxMINSNG )
406 : {
407 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG;
408 : }
409 0 : *p->pSingle = (float) n;
410 0 : break;
411 : case SbxBYREF | SbxSALINT64:
412 0 : *p->pnInt64 = ImpDoubleToSalInt64( n );
413 0 : break;
414 : case SbxBYREF | SbxSALUINT64:
415 0 : *p->puInt64 = ImpDoubleToSalUInt64( n );
416 0 : break;
417 : case SbxBYREF | SbxDATE:
418 : case SbxBYREF | SbxDOUBLE:
419 0 : *p->pDouble = (double) n;
420 0 : break;
421 : case SbxBYREF | SbxCURRENCY:
422 0 : if( n > SbxMAXCURR )
423 : {
424 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
425 : }
426 0 : else if( n < SbxMINCURR )
427 : {
428 0 : SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
429 : }
430 0 : *p->pnInt64 = ImpDoubleToCurrency( n );
431 0 : break;
432 : default:
433 0 : SbxBase::SetError( SbxERR_CONVERSION );
434 0 : break;
435 : }
436 465 : }
437 :
438 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|