xref: /aoo41x/main/basic/source/comp/io.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <tools/stream.hxx>
33*cdf0e10cSrcweir #include "sbcomp.hxx"
34*cdf0e10cSrcweir #include "iosys.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir // Test, ob ein I/O-Channel angegeben wurde
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir sal_Bool SbiParser::Channel( sal_Bool bAlways )
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
41*cdf0e10cSrcweir 	Peek();
42*cdf0e10cSrcweir 	if( IsHash() )
43*cdf0e10cSrcweir 	{
44*cdf0e10cSrcweir 		SbiExpression aExpr( this );
45*cdf0e10cSrcweir 		while( Peek() == COMMA || Peek() == SEMICOLON )
46*cdf0e10cSrcweir 			Next();
47*cdf0e10cSrcweir 		aExpr.Gen();
48*cdf0e10cSrcweir 		aGen.Gen( _CHANNEL );
49*cdf0e10cSrcweir 		bRes = sal_True;
50*cdf0e10cSrcweir 	}
51*cdf0e10cSrcweir 	else if( bAlways )
52*cdf0e10cSrcweir 		Error( SbERR_EXPECTED, "#" );
53*cdf0e10cSrcweir 	return bRes;
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir // Fuer PRINT und WRITE wird bei Objektvariablen versucht,
57*cdf0e10cSrcweir // die Default-Property anzusprechen.
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // PRINT
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir void SbiParser::Print()
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	sal_Bool bChan = Channel();
64*cdf0e10cSrcweir 	// Die Ausdruecke zum Drucken:
65*cdf0e10cSrcweir 	while( !bAbort )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		if( !IsEoln( Peek() ) )
68*cdf0e10cSrcweir 		{
69*cdf0e10cSrcweir 			SbiExpression* pExpr = new SbiExpression( this );
70*cdf0e10cSrcweir 			pExpr->Gen();
71*cdf0e10cSrcweir 			delete pExpr;
72*cdf0e10cSrcweir 			Peek();
73*cdf0e10cSrcweir 			aGen.Gen( eCurTok == COMMA ? _PRINTF : _BPRINT );
74*cdf0e10cSrcweir 		}
75*cdf0e10cSrcweir 		if( eCurTok == COMMA || eCurTok == SEMICOLON )
76*cdf0e10cSrcweir 		{
77*cdf0e10cSrcweir 			Next();
78*cdf0e10cSrcweir 			if( IsEoln( Peek() ) ) break;
79*cdf0e10cSrcweir 		}
80*cdf0e10cSrcweir 		else
81*cdf0e10cSrcweir 		{
82*cdf0e10cSrcweir 			aGen.Gen( _PRCHAR, '\n' );
83*cdf0e10cSrcweir 			break;
84*cdf0e10cSrcweir 		}
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir 	if( bChan )
87*cdf0e10cSrcweir 		aGen.Gen( _CHAN0 );
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir // WRITE #chan, expr, ...
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir void SbiParser::Write()
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir 	sal_Bool bChan = Channel();
95*cdf0e10cSrcweir 	// Die Ausdruecke zum Drucken:
96*cdf0e10cSrcweir 	while( !bAbort )
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		SbiExpression* pExpr = new SbiExpression( this );
99*cdf0e10cSrcweir 		pExpr->Gen();
100*cdf0e10cSrcweir 		delete pExpr;
101*cdf0e10cSrcweir 		aGen.Gen( _BWRITE );
102*cdf0e10cSrcweir 		if( Peek() == COMMA )
103*cdf0e10cSrcweir 		{
104*cdf0e10cSrcweir 			aGen.Gen( _PRCHAR, ',' );
105*cdf0e10cSrcweir 			Next();
106*cdf0e10cSrcweir 			if( IsEoln( Peek() ) ) break;
107*cdf0e10cSrcweir 		}
108*cdf0e10cSrcweir 		else
109*cdf0e10cSrcweir 		{
110*cdf0e10cSrcweir 			aGen.Gen( _PRCHAR, '\n' );
111*cdf0e10cSrcweir 			break;
112*cdf0e10cSrcweir 		}
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 	if( bChan )
115*cdf0e10cSrcweir 		aGen.Gen( _CHAN0 );
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir // #i92642 Handle LINE keyword outside ::Next()
120*cdf0e10cSrcweir void SbiParser::Line()
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	// #i92642: Special handling to allow name as symbol
123*cdf0e10cSrcweir 	if( Peek() == INPUT )
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		Next();
126*cdf0e10cSrcweir 		LineInput();
127*cdf0e10cSrcweir 	}
128*cdf0e10cSrcweir 	else
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		aGen.Statement();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		KeywordSymbolInfo aInfo;
133*cdf0e10cSrcweir 		aInfo.m_aKeywordSymbol = String( RTL_CONSTASCII_USTRINGPARAM( "line" ) );
134*cdf0e10cSrcweir 		aInfo.m_eSbxDataType = GetType();
135*cdf0e10cSrcweir 		aInfo.m_eTok = SYMBOL;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		Symbol( &aInfo );
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir // LINE INPUT [prompt], var$
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir void SbiParser::LineInput()
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	Channel( sal_True );
147*cdf0e10cSrcweir 	// sal_Bool bChan = Channel( sal_True );
148*cdf0e10cSrcweir 	SbiExpression* pExpr = new SbiExpression( this, SbOPERAND );
149*cdf0e10cSrcweir 	/* AB 15.1.96: Keinen allgemeinen Ausdruck mehr zulassen
150*cdf0e10cSrcweir 	SbiExpression* pExpr = new SbiExpression( this );
151*cdf0e10cSrcweir 	if( !pExpr->IsVariable() )
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		SbiToken eTok = Peek();
154*cdf0e10cSrcweir 		if( eTok == COMMA || eTok == SEMICOLON ) Next();
155*cdf0e10cSrcweir 		else Error( SbERR_EXPECTED, COMMA );
156*cdf0e10cSrcweir 		// mit Prompt
157*cdf0e10cSrcweir 		if( !bChan )
158*cdf0e10cSrcweir 		{
159*cdf0e10cSrcweir 			pExpr->Gen();
160*cdf0e10cSrcweir 			aGen.Gen( _PROMPT );
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		else
163*cdf0e10cSrcweir 			Error( SbERR_VAR_EXPECTED );
164*cdf0e10cSrcweir 		delete pExpr;
165*cdf0e10cSrcweir 		pExpr = new SbiExpression( this, SbOPERAND );
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 	*/
168*cdf0e10cSrcweir 	if( !pExpr->IsVariable() )
169*cdf0e10cSrcweir 		Error( SbERR_VAR_EXPECTED );
170*cdf0e10cSrcweir 	if( pExpr->GetType() != SbxVARIANT && pExpr->GetType() != SbxSTRING )
171*cdf0e10cSrcweir 		Error( SbERR_CONVERSION );
172*cdf0e10cSrcweir 	pExpr->Gen();
173*cdf0e10cSrcweir 	aGen.Gen( _LINPUT );
174*cdf0e10cSrcweir 	delete pExpr;
175*cdf0e10cSrcweir 	aGen.Gen( _CHAN0 );		// ResetChannel() nicht mehr in StepLINPUT()
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir // INPUT
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir void SbiParser::Input()
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	aGen.Gen( _RESTART );
183*cdf0e10cSrcweir 	Channel( sal_True );
184*cdf0e10cSrcweir 	// sal_Bool bChan = Channel( sal_True );
185*cdf0e10cSrcweir 	SbiExpression* pExpr = new SbiExpression( this, SbOPERAND );
186*cdf0e10cSrcweir 	/* ALT: Jetzt keinen allgemeinen Ausdruck mehr zulassen
187*cdf0e10cSrcweir 	SbiExpression* pExpr = new SbiExpression( this );
188*cdf0e10cSrcweir 	...
189*cdf0e10cSrcweir 	siehe LineInput
190*cdf0e10cSrcweir 	*/
191*cdf0e10cSrcweir 	while( !bAbort )
192*cdf0e10cSrcweir 	{
193*cdf0e10cSrcweir 		if( !pExpr->IsVariable() )
194*cdf0e10cSrcweir 			Error( SbERR_VAR_EXPECTED );
195*cdf0e10cSrcweir 		pExpr->Gen();
196*cdf0e10cSrcweir 		aGen.Gen( _INPUT );
197*cdf0e10cSrcweir 		if( Peek() == COMMA )
198*cdf0e10cSrcweir 		{
199*cdf0e10cSrcweir 			Next();
200*cdf0e10cSrcweir 			delete pExpr;
201*cdf0e10cSrcweir 			pExpr = new SbiExpression( this, SbOPERAND );
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 		else break;
204*cdf0e10cSrcweir 	}
205*cdf0e10cSrcweir 	delete pExpr;
206*cdf0e10cSrcweir 	aGen.Gen( _CHAN0 );		// ResetChannel() nicht mehr in StepINPUT()
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // OPEN stringexpr FOR mode ACCCESS access mode AS Channel [Len=n]
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void SbiParser::Open()
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir 	SbiExpression aFileName( this );
214*cdf0e10cSrcweir 	SbiToken eTok;
215*cdf0e10cSrcweir 	TestToken( FOR );
216*cdf0e10cSrcweir 	short nMode = 0;
217*cdf0e10cSrcweir 	short nFlags = 0;
218*cdf0e10cSrcweir 	switch( Next() )
219*cdf0e10cSrcweir 	{
220*cdf0e10cSrcweir 		case INPUT:
221*cdf0e10cSrcweir 			nMode = STREAM_READ;  nFlags |= SBSTRM_INPUT; break;
222*cdf0e10cSrcweir 		case OUTPUT:
223*cdf0e10cSrcweir 			nMode = STREAM_WRITE | STREAM_TRUNC; nFlags |= SBSTRM_OUTPUT; break;
224*cdf0e10cSrcweir 		case APPEND:
225*cdf0e10cSrcweir 			nMode = STREAM_WRITE; nFlags |= SBSTRM_APPEND; break;
226*cdf0e10cSrcweir 		case RANDOM:
227*cdf0e10cSrcweir 			nMode = STREAM_READ | STREAM_WRITE; nFlags |= SBSTRM_RANDOM; break;
228*cdf0e10cSrcweir 		case BINARY:
229*cdf0e10cSrcweir 			nMode = STREAM_READ | STREAM_WRITE; nFlags |= SBSTRM_BINARY; break;
230*cdf0e10cSrcweir 		default:
231*cdf0e10cSrcweir 			Error( SbERR_SYNTAX );
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir 	if( Peek() == ACCESS )
234*cdf0e10cSrcweir 	{
235*cdf0e10cSrcweir 		Next();
236*cdf0e10cSrcweir 		eTok = Next();
237*cdf0e10cSrcweir 		// #27964# Nur STREAM_READ,STREAM_WRITE-Flags in nMode beeinflussen
238*cdf0e10cSrcweir 		nMode &= ~(STREAM_READ | STREAM_WRITE);		// loeschen
239*cdf0e10cSrcweir 		if( eTok == READ )
240*cdf0e10cSrcweir 		{
241*cdf0e10cSrcweir 			if( Peek() == WRITE )
242*cdf0e10cSrcweir 			{
243*cdf0e10cSrcweir 				Next();
244*cdf0e10cSrcweir 				nMode |= (STREAM_READ | STREAM_WRITE);
245*cdf0e10cSrcweir 			}
246*cdf0e10cSrcweir 			else
247*cdf0e10cSrcweir 				nMode |= STREAM_READ;
248*cdf0e10cSrcweir 		}
249*cdf0e10cSrcweir 		else if( eTok == WRITE )
250*cdf0e10cSrcweir 			nMode |= STREAM_WRITE;
251*cdf0e10cSrcweir 		else
252*cdf0e10cSrcweir 			Error( SbERR_SYNTAX );
253*cdf0e10cSrcweir 	}
254*cdf0e10cSrcweir 	switch( Peek() )
255*cdf0e10cSrcweir 	{
256*cdf0e10cSrcweir #ifdef SHARED
257*cdf0e10cSrcweir #undef SHARED
258*cdf0e10cSrcweir #define tmpSHARED
259*cdf0e10cSrcweir #endif
260*cdf0e10cSrcweir 		case SHARED:
261*cdf0e10cSrcweir 			Next(); nMode |= STREAM_SHARE_DENYNONE; break;
262*cdf0e10cSrcweir #ifdef tmpSHARED
263*cdf0e10cSrcweir #define SHARED
264*cdf0e10cSrcweir #undef tmpSHARED
265*cdf0e10cSrcweir #endif
266*cdf0e10cSrcweir 		case LOCK:
267*cdf0e10cSrcweir 			Next();
268*cdf0e10cSrcweir 			eTok = Next();
269*cdf0e10cSrcweir 			if( eTok == READ )
270*cdf0e10cSrcweir 			{
271*cdf0e10cSrcweir 				if( Peek() == WRITE ) Next(), nMode |= STREAM_SHARE_DENYALL;
272*cdf0e10cSrcweir 				else nMode |= STREAM_SHARE_DENYREAD;
273*cdf0e10cSrcweir 			}
274*cdf0e10cSrcweir 			else if( eTok == WRITE )
275*cdf0e10cSrcweir 				nMode |= STREAM_SHARE_DENYWRITE;
276*cdf0e10cSrcweir 			else
277*cdf0e10cSrcweir 				Error( SbERR_SYNTAX );
278*cdf0e10cSrcweir 			break;
279*cdf0e10cSrcweir 		default: break;
280*cdf0e10cSrcweir 	}
281*cdf0e10cSrcweir 	TestToken( AS );
282*cdf0e10cSrcweir 	// Die Kanalnummer
283*cdf0e10cSrcweir 	SbiExpression* pChan = new SbiExpression( this );
284*cdf0e10cSrcweir 	if( !pChan )
285*cdf0e10cSrcweir 		Error( SbERR_SYNTAX );
286*cdf0e10cSrcweir 	SbiExpression* pLen = NULL;
287*cdf0e10cSrcweir 	if( Peek() == SYMBOL )
288*cdf0e10cSrcweir 	{
289*cdf0e10cSrcweir 		Next();
290*cdf0e10cSrcweir 		String aLen( aSym );
291*cdf0e10cSrcweir 		if( aLen.EqualsIgnoreCaseAscii( "LEN" ) )
292*cdf0e10cSrcweir 		{
293*cdf0e10cSrcweir 			TestToken( EQ );
294*cdf0e10cSrcweir 			pLen = new SbiExpression( this );
295*cdf0e10cSrcweir 		}
296*cdf0e10cSrcweir 	}
297*cdf0e10cSrcweir 	if( !pLen ) pLen = new SbiExpression( this, 128, SbxINTEGER );
298*cdf0e10cSrcweir 	// Der Stack fuer den OPEN-Befehl sieht wie folgt aus:
299*cdf0e10cSrcweir 	// Blocklaenge
300*cdf0e10cSrcweir 	// Kanalnummer
301*cdf0e10cSrcweir 	// Dateiname
302*cdf0e10cSrcweir 	pLen->Gen();
303*cdf0e10cSrcweir 	if( pChan )
304*cdf0e10cSrcweir 		pChan->Gen();
305*cdf0e10cSrcweir 	aFileName.Gen();
306*cdf0e10cSrcweir 	aGen.Gen( _OPEN, nMode, nFlags );
307*cdf0e10cSrcweir 	delete pLen;
308*cdf0e10cSrcweir 	delete pChan;
309*cdf0e10cSrcweir }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir // NAME file AS file
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir void SbiParser::Name()
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	// #i92642: Special handling to allow name as symbol
316*cdf0e10cSrcweir 	if( Peek() == EQ )
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir 		aGen.Statement();
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 		KeywordSymbolInfo aInfo;
321*cdf0e10cSrcweir 		aInfo.m_aKeywordSymbol = String( RTL_CONSTASCII_USTRINGPARAM( "name" ) );
322*cdf0e10cSrcweir 		aInfo.m_eSbxDataType = GetType();
323*cdf0e10cSrcweir 		aInfo.m_eTok = SYMBOL;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 		Symbol( &aInfo );
326*cdf0e10cSrcweir 		return;
327*cdf0e10cSrcweir 	}
328*cdf0e10cSrcweir 	SbiExpression aExpr1( this );
329*cdf0e10cSrcweir 	TestToken( AS );
330*cdf0e10cSrcweir 	SbiExpression aExpr2( this );
331*cdf0e10cSrcweir 	aExpr1.Gen();
332*cdf0e10cSrcweir 	aExpr2.Gen();
333*cdf0e10cSrcweir 	aGen.Gen( _RENAME );
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir // CLOSE [n,...]
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir void SbiParser::Close()
339*cdf0e10cSrcweir {
340*cdf0e10cSrcweir 	Peek();
341*cdf0e10cSrcweir 	if( IsEoln( eCurTok ) )
342*cdf0e10cSrcweir 		aGen.Gen( _CLOSE, 0 );
343*cdf0e10cSrcweir 	else
344*cdf0e10cSrcweir 	for( ;; )
345*cdf0e10cSrcweir 	{
346*cdf0e10cSrcweir 		SbiExpression aExpr( this );
347*cdf0e10cSrcweir 		while( Peek() == COMMA || Peek() == SEMICOLON )
348*cdf0e10cSrcweir 			Next();
349*cdf0e10cSrcweir 		aExpr.Gen();
350*cdf0e10cSrcweir 		aGen.Gen( _CHANNEL );
351*cdf0e10cSrcweir 		aGen.Gen( _CLOSE, 1 );
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 		if( IsEoln( Peek() ) )
354*cdf0e10cSrcweir 			break;
355*cdf0e10cSrcweir 	}
356*cdf0e10cSrcweir }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 
359