update dev300-m58
[ooovba.git] / basic / source / comp / io.cxx
blob3d979656f04c951188ffe6160beac068bb41e7dd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: io.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
35 #include <tools/stream.hxx>
36 #include "sbcomp.hxx"
37 #include "iosys.hxx"
39 // Test, ob ein I/O-Channel angegeben wurde
41 BOOL SbiParser::Channel( BOOL bAlways )
43 BOOL bRes = FALSE;
44 Peek();
45 if( IsHash() )
47 SbiExpression aExpr( this );
48 while( Peek() == COMMA || Peek() == SEMICOLON )
49 Next();
50 aExpr.Gen();
51 aGen.Gen( _CHANNEL );
52 bRes = TRUE;
54 else if( bAlways )
55 Error( SbERR_EXPECTED, "#" );
56 return bRes;
59 // Fuer PRINT und WRITE wird bei Objektvariablen versucht,
60 // die Default-Property anzusprechen.
62 // PRINT
64 void SbiParser::Print()
66 BOOL bChan = Channel();
67 // Die Ausdruecke zum Drucken:
68 while( !bAbort )
70 if( !IsEoln( Peek() ) )
72 SbiExpression* pExpr = new SbiExpression( this );
73 pExpr->Gen();
74 delete pExpr;
75 Peek();
76 aGen.Gen( eCurTok == COMMA ? _PRINTF : _BPRINT );
78 if( eCurTok == COMMA || eCurTok == SEMICOLON )
80 Next();
81 if( IsEoln( Peek() ) ) break;
83 else
85 aGen.Gen( _PRCHAR, '\n' );
86 break;
89 if( bChan )
90 aGen.Gen( _CHAN0 );
93 // WRITE #chan, expr, ...
95 void SbiParser::Write()
97 BOOL bChan = Channel();
98 // Die Ausdruecke zum Drucken:
99 while( !bAbort )
101 SbiExpression* pExpr = new SbiExpression( this );
102 pExpr->Gen();
103 delete pExpr;
104 aGen.Gen( _BWRITE );
105 if( Peek() == COMMA )
107 aGen.Gen( _PRCHAR, ',' );
108 Next();
109 if( IsEoln( Peek() ) ) break;
111 else
113 aGen.Gen( _PRCHAR, '\n' );
114 break;
117 if( bChan )
118 aGen.Gen( _CHAN0 );
121 // LINE INPUT [prompt], var$
123 void SbiParser::LineInput()
125 Channel( TRUE );
126 // BOOL bChan = Channel( TRUE );
127 SbiExpression* pExpr = new SbiExpression( this, SbOPERAND );
128 /* AB 15.1.96: Keinen allgemeinen Ausdruck mehr zulassen
129 SbiExpression* pExpr = new SbiExpression( this );
130 if( !pExpr->IsVariable() )
132 SbiToken eTok = Peek();
133 if( eTok == COMMA || eTok == SEMICOLON ) Next();
134 else Error( SbERR_EXPECTED, COMMA );
135 // mit Prompt
136 if( !bChan )
138 pExpr->Gen();
139 aGen.Gen( _PROMPT );
141 else
142 Error( SbERR_VAR_EXPECTED );
143 delete pExpr;
144 pExpr = new SbiExpression( this, SbOPERAND );
147 if( !pExpr->IsVariable() )
148 Error( SbERR_VAR_EXPECTED );
149 if( pExpr->GetType() != SbxVARIANT && pExpr->GetType() != SbxSTRING )
150 Error( SbERR_CONVERSION );
151 pExpr->Gen();
152 aGen.Gen( _LINPUT );
153 delete pExpr;
154 aGen.Gen( _CHAN0 ); // ResetChannel() nicht mehr in StepLINPUT()
157 // INPUT
159 void SbiParser::Input()
161 aGen.Gen( _RESTART );
162 Channel( TRUE );
163 // BOOL bChan = Channel( TRUE );
164 SbiExpression* pExpr = new SbiExpression( this, SbOPERAND );
165 /* ALT: Jetzt keinen allgemeinen Ausdruck mehr zulassen
166 SbiExpression* pExpr = new SbiExpression( this );
168 siehe LineInput
170 while( !bAbort )
172 if( !pExpr->IsVariable() )
173 Error( SbERR_VAR_EXPECTED );
174 pExpr->Gen();
175 aGen.Gen( _INPUT );
176 if( Peek() == COMMA )
178 Next();
179 delete pExpr;
180 pExpr = new SbiExpression( this, SbOPERAND );
182 else break;
184 delete pExpr;
185 aGen.Gen( _CHAN0 ); // ResetChannel() nicht mehr in StepINPUT()
188 // OPEN stringexpr FOR mode ACCCESS access mode AS Channel [Len=n]
190 void SbiParser::Open()
192 SbiExpression aFileName( this );
193 SbiToken eTok;
194 TestToken( FOR );
195 short nMode = 0;
196 short nFlags = 0;
197 switch( Next() )
199 case INPUT:
200 nMode = STREAM_READ; nFlags |= SBSTRM_INPUT; break;
201 case OUTPUT:
202 nMode = STREAM_WRITE | STREAM_TRUNC; nFlags |= SBSTRM_OUTPUT; break;
203 case APPEND:
204 nMode = STREAM_WRITE; nFlags |= SBSTRM_APPEND; break;
205 case RANDOM:
206 nMode = STREAM_READ | STREAM_WRITE; nFlags |= SBSTRM_RANDOM; break;
207 case BINARY:
208 nMode = STREAM_READ | STREAM_WRITE; nFlags |= SBSTRM_BINARY; break;
209 default:
210 Error( SbERR_SYNTAX );
212 if( Peek() == ACCESS )
214 Next();
215 eTok = Next();
216 // #27964# Nur STREAM_READ,STREAM_WRITE-Flags in nMode beeinflussen
217 nMode &= ~(STREAM_READ | STREAM_WRITE); // loeschen
218 if( eTok == READ )
220 if( Peek() == WRITE )
222 Next();
223 nMode |= (STREAM_READ | STREAM_WRITE);
225 else
226 nMode |= STREAM_READ;
228 else if( eTok == WRITE )
229 nMode |= STREAM_WRITE;
230 else
231 Error( SbERR_SYNTAX );
233 switch( Peek() )
235 #ifdef SHARED
236 #undef SHARED
237 #define tmpSHARED
238 #endif
239 case SHARED:
240 Next(); nMode |= STREAM_SHARE_DENYNONE; break;
241 #ifdef tmpSHARED
242 #define SHARED
243 #undef tmpSHARED
244 #endif
245 case LOCK:
246 Next();
247 eTok = Next();
248 if( eTok == READ )
250 if( Peek() == WRITE ) Next(), nMode |= STREAM_SHARE_DENYALL;
251 else nMode |= STREAM_SHARE_DENYREAD;
253 else if( eTok == WRITE )
254 nMode |= STREAM_SHARE_DENYWRITE;
255 else
256 Error( SbERR_SYNTAX );
257 break;
258 default: break;
260 TestToken( AS );
261 // Die Kanalnummer
262 SbiExpression* pChan = new SbiExpression( this );
263 if( !pChan )
264 Error( SbERR_SYNTAX );
265 SbiExpression* pLen = NULL;
266 if( Peek() == SYMBOL )
268 Next();
269 String aLen( aSym );
270 if( aLen.EqualsIgnoreCaseAscii( "LEN" ) )
272 TestToken( EQ );
273 pLen = new SbiExpression( this );
276 if( !pLen ) pLen = new SbiExpression( this, 128, SbxINTEGER );
277 // Der Stack fuer den OPEN-Befehl sieht wie folgt aus:
278 // Blocklaenge
279 // Kanalnummer
280 // Dateiname
281 pLen->Gen();
282 if( pChan )
283 pChan->Gen();
284 aFileName.Gen();
285 aGen.Gen( _OPEN, nMode, nFlags );
286 delete pLen;
287 delete pChan;
290 // NAME file AS file
292 void SbiParser::Name()
294 SbiExpression aExpr1( this );
295 TestToken( AS );
296 SbiExpression aExpr2( this );
297 aExpr1.Gen();
298 aExpr2.Gen();
299 aGen.Gen( _RENAME );
302 // CLOSE [n,...]
304 void SbiParser::Close()
306 Peek();
307 if( IsEoln( eCurTok ) )
308 aGen.Gen( _CLOSE, 0 );
309 else
310 for( ;; )
312 SbiExpression aExpr( this );
313 while( Peek() == COMMA || Peek() == SEMICOLON )
314 Next();
315 aExpr.Gen();
316 aGen.Gen( _CHANNEL );
317 aGen.Gen( _CLOSE, 1 );
319 if( IsEoln( Peek() ) )
320 break;