xref: /trunk/main/basic/source/sbx/sbxbool.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basic.hxx"
30 #include <tools/errcode.hxx>
31 #include <basic/sbx.hxx>
32 #include "sbxconv.hxx"
33 #include "sbxres.hxx"
34 
35 // AB 29.10.99 Unicode
36 #ifndef _USE_NO_NAMESPACE
37 using namespace rtl;
38 #endif
39 
40 enum SbxBOOL ImpGetBool( const SbxValues* p )
41 {
42 	enum SbxBOOL nRes;
43 	switch( +p->eType )
44 	{
45 		case SbxNULL:
46 			SbxBase::SetError( SbxERR_CONVERSION );
47 		case SbxEMPTY:
48 			nRes = SbxFALSE; break;
49 		case SbxCHAR:
50 			nRes = p->nChar ? SbxTRUE : SbxFALSE; break;
51 		case SbxBYTE:
52 			nRes = p->nByte ? SbxTRUE : SbxFALSE; break;
53 		case SbxINTEGER:
54 		case SbxBOOL:
55 			nRes = p->nInteger ? SbxTRUE : SbxFALSE; break;
56 		case SbxERROR:
57 		case SbxUSHORT:
58 			nRes = p->nUShort ? SbxTRUE : SbxFALSE; break;
59 		case SbxLONG:
60 			nRes = p->nLong ? SbxTRUE : SbxFALSE; break;
61 		case SbxULONG:
62 			nRes = p->nULong ? SbxTRUE : SbxFALSE; break;
63 		case SbxSINGLE:
64 			nRes = p->nSingle ? SbxTRUE : SbxFALSE; break;
65 		case SbxDATE:
66 		case SbxDOUBLE:
67 			nRes = p->nDouble ? SbxTRUE : SbxFALSE; break;
68 		case SbxDECIMAL:
69 		case SbxBYREF | SbxDECIMAL:
70 			{
71 			double dVal = 0.0;
72 			if( p->pDecimal )
73 				p->pDecimal->getDouble( dVal );
74 			nRes = dVal ? SbxTRUE : SbxFALSE;
75 			}
76 			break;
77 		case SbxSALINT64:
78             nRes = p->nInt64 ? SbxTRUE : SbxFALSE; break;
79 		case SbxSALUINT64:
80             nRes = p->uInt64 ? SbxTRUE : SbxFALSE; break;
81 		case SbxULONG64:
82 			nRes = !!p->nULong64 ? SbxTRUE : SbxFALSE; break;
83 		case SbxLONG64:
84 		case SbxCURRENCY:
85 			nRes = !!p->nLong64 ? SbxTRUE : SbxFALSE; break;
86 		case SbxBYREF | SbxSTRING:
87 		case SbxSTRING:
88 		case SbxLPSTR:
89 			nRes = SbxFALSE;
90             if ( p->pOUString )
91             {
92                 if( p->pOUString->equalsIgnoreAsciiCase( SbxRes( STRING_TRUE ) ) )
93                     nRes = SbxTRUE;
94                 else if( !p->pOUString->equalsIgnoreAsciiCase( SbxRes( STRING_FALSE ) ) )
95                 {
96                     // Jetzt kann es noch in eine Zahl konvertierbar sein
97                     sal_Bool bError = sal_True;
98                     double n;
99                     SbxDataType t;
100                     sal_uInt16 nLen = 0;
101                     if( ImpScan( *p->pOUString, n, t, &nLen ) == SbxERR_OK )
102                     {
103                         if( nLen == p->pOUString->getLength() )
104                         {
105                             bError = sal_False;
106                             if( n != 0.0 )
107                                 nRes = SbxTRUE;
108                         }
109                     }
110                     if( bError )
111                         SbxBase::SetError( SbxERR_CONVERSION );
112                 }
113             }
114 			break;
115 		case SbxOBJECT:
116 		{
117 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
118 			if( pVal )
119 				nRes = pVal->GetBool() ? SbxTRUE : SbxFALSE;
120 			else
121 			{
122 				SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = SbxFALSE;
123 			}
124 			break;
125 		}
126 
127 		case SbxBYREF | SbxCHAR:
128 			nRes = *p->pChar ? SbxTRUE : SbxFALSE; break;
129 		case SbxBYREF | SbxBYTE:
130 			nRes = *p->pByte ? SbxTRUE : SbxFALSE; break;
131 		case SbxBYREF | SbxINTEGER:
132 		case SbxBYREF | SbxBOOL:
133 			nRes = *p->pInteger ? SbxTRUE : SbxFALSE; break;
134 		case SbxBYREF | SbxLONG:
135 			nRes = *p->pLong ? SbxTRUE : SbxFALSE; break;
136 		case SbxBYREF | SbxULONG:
137 			nRes = *p->pULong ? SbxTRUE : SbxFALSE; break;
138 		case SbxBYREF | SbxERROR:
139 		case SbxBYREF | SbxUSHORT:
140 			nRes = *p->pUShort ? SbxTRUE : SbxFALSE; break;
141 		case SbxBYREF | SbxSINGLE:
142 			nRes = ( *p->pSingle != 0 ) ? SbxTRUE : SbxFALSE; break;
143 		case SbxBYREF | SbxDATE:
144 		case SbxBYREF | SbxDOUBLE:
145 			nRes = ( *p->pDouble != 0 ) ? SbxTRUE : SbxFALSE; break;
146 		case SbxBYREF | SbxSALINT64:
147             nRes = ( *p->pnInt64 ) ? SbxTRUE : SbxFALSE; break;
148 		case SbxBYREF | SbxSALUINT64:
149             nRes = ( *p->puInt64 ) ? SbxTRUE : SbxFALSE; break;
150 		case SbxBYREF | SbxULONG64:
151 			nRes = !!*p->pULong64 ? SbxTRUE : SbxFALSE; break;
152 		case SbxBYREF | SbxLONG64:
153 		case SbxBYREF | SbxCURRENCY:
154 			nRes = !!*p->pLong64 ? SbxTRUE : SbxFALSE; break;
155 
156 		default:
157 			SbxBase::SetError( SbxERR_CONVERSION ); nRes = SbxFALSE;
158 	}
159 	return nRes;
160 }
161 
162 void ImpPutBool( SbxValues* p, sal_Int16 n )
163 {
164 	if( n )
165 		n = SbxTRUE;
166 	switch( +p->eType )
167 	{
168 		case SbxCHAR:
169 			p->nChar = (xub_Unicode) n; break;
170 		case SbxUINT:
171 			p->nByte = (sal_uInt8) n; break;
172 		case SbxINTEGER:
173 		case SbxBOOL:
174 			p->nInteger = n; break;
175 		case SbxLONG:
176 			p->nLong = n; break;
177 		case SbxULONG:
178 			p->nULong = (sal_uInt32) n; break;
179 		case SbxERROR:
180 		case SbxUSHORT:
181 			p->nUShort = (sal_uInt16) n; break;
182 		case SbxSINGLE:
183 			p->nSingle = n; break;
184 		case SbxDATE:
185 		case SbxDOUBLE:
186 			p->nDouble = n; break;
187 		case SbxSALINT64:
188 			p->nInt64 = n; break;
189 		case SbxSALUINT64:
190 			p->uInt64 = n; break;
191 		case SbxULONG64:
192 			p->nULong64.Set( (sal_uInt32)n ); break;
193 		case SbxLONG64:
194 		case SbxCURRENCY:
195 			p->nLong64.Set( (sal_Int32)n ); break;
196 		case SbxDECIMAL:
197 		case SbxBYREF | SbxDECIMAL:
198 			ImpCreateDecimal( p )->setInt( (sal_Int16)n );
199 			break;
200 
201 		case SbxBYREF | SbxSTRING:
202 		case SbxSTRING:
203 		case SbxLPSTR:
204             if ( !p->pOUString )
205                 p->pOUString = new ::rtl::OUString( SbxRes( n ? STRING_TRUE : STRING_FALSE ) );
206             else
207                 *p->pOUString = SbxRes( n ? STRING_TRUE : STRING_FALSE );
208 			break;
209 
210 		case SbxOBJECT:
211 		{
212 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
213 			if( pVal )
214 				pVal->PutBool( sal_Bool( n != 0 ) );
215 			else
216 				SbxBase::SetError( SbxERR_NO_OBJECT );
217 			break;
218 		}
219 		case SbxBYREF | SbxCHAR:
220 			*p->pChar = (xub_Unicode) n; break;
221 		case SbxBYREF | SbxBYTE:
222 			*p->pByte = (sal_uInt8) n; break;
223 		case SbxBYREF | SbxINTEGER:
224 		case SbxBYREF | SbxBOOL:
225 			*p->pInteger = (sal_Int16) n; break;
226 		case SbxBYREF | SbxERROR:
227 		case SbxBYREF | SbxUSHORT:
228 			*p->pUShort = (sal_uInt16) n; break;
229 		case SbxBYREF | SbxLONG:
230 			*p->pLong = n; break;
231 		case SbxBYREF | SbxULONG:
232 			*p->pULong = (sal_uInt32) n; break;
233 		case SbxBYREF | SbxSINGLE:
234 			*p->pSingle = n; break;
235 		case SbxBYREF | SbxDATE:
236 		case SbxBYREF | SbxDOUBLE:
237 			*p->pDouble = n; break;
238 		case SbxBYREF | SbxSALINT64:
239 			*p->pnInt64 = n; break;
240 		case SbxBYREF | SbxSALUINT64:
241 			*p->puInt64 = n; break;
242 		case SbxBYREF | SbxULONG64:
243 			p->pULong64->Set( (sal_uInt32)n ); break;
244 		case SbxBYREF | SbxLONG64:
245 		case SbxBYREF | SbxCURRENCY:
246 			p->pLong64->Set( (sal_Int32)n ); break;
247 
248 		default:
249 			SbxBase::SetError( SbxERR_CONVERSION );
250 	}
251 }
252 
253