xref: /trunk/main/basic/source/runtime/iosys.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #include <vcl/dialog.hxx>
31*cdf0e10cSrcweir #include <vcl/edit.hxx>
32*cdf0e10cSrcweir #ifndef _SV_BUTTON_HXX //autogen
33*cdf0e10cSrcweir #include <vcl/button.hxx>
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
36*cdf0e10cSrcweir #include <vcl/svapp.hxx>
37*cdf0e10cSrcweir #include <osl/security.h>
38*cdf0e10cSrcweir #include <osl/file.hxx>
39*cdf0e10cSrcweir #include <tools/urlobj.hxx>
40*cdf0e10cSrcweir #include <vos/mutex.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "runtime.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #ifdef _USE_UNO
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // <-- encoding
47*cdf0e10cSrcweir #include <sal/alloca.h>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <ctype.h>
50*cdf0e10cSrcweir #include <rtl/byteseq.hxx>
51*cdf0e10cSrcweir #include <rtl/textenc.h>
52*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
53*cdf0e10cSrcweir #include <rtl/textenc.h>
54*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
55*cdf0e10cSrcweir // encoding -->
56*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
59*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProviderManager.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
64*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
66*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
67*cdf0e10cSrcweir #include <com/sun/star/bridge/XBridge.hpp>
68*cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir using namespace comphelper;
71*cdf0e10cSrcweir using namespace osl;
72*cdf0e10cSrcweir using namespace com::sun::star::uno;
73*cdf0e10cSrcweir using namespace com::sun::star::lang;
74*cdf0e10cSrcweir using namespace com::sun::star::ucb;
75*cdf0e10cSrcweir using namespace com::sun::star::io;
76*cdf0e10cSrcweir using namespace com::sun::star::bridge;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir #endif /* _USE_UNO */
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir #include "iosys.hxx"
81*cdf0e10cSrcweir #include "sbintern.hxx"
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir // Der Input-Dialog:
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir class SbiInputDialog : public ModalDialog {
86*cdf0e10cSrcweir     Edit aInput;
87*cdf0e10cSrcweir     OKButton aOk;
88*cdf0e10cSrcweir     CancelButton aCancel;
89*cdf0e10cSrcweir     String aText;
90*cdf0e10cSrcweir     DECL_LINK( Ok, Window * );
91*cdf0e10cSrcweir     DECL_LINK( Cancel, Window * );
92*cdf0e10cSrcweir public:
93*cdf0e10cSrcweir     SbiInputDialog( Window*, const String& );
94*cdf0e10cSrcweir     const String& GetInput() { return aText; }
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir SbiInputDialog::SbiInputDialog( Window* pParent, const String& rPrompt )
98*cdf0e10cSrcweir             :ModalDialog( pParent, WB_3DLOOK | WB_MOVEABLE | WB_CLOSEABLE ),
99*cdf0e10cSrcweir              aInput( this, WB_3DLOOK | WB_LEFT | WB_BORDER ),
100*cdf0e10cSrcweir              aOk( this ), aCancel( this )
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     SetText( rPrompt );
103*cdf0e10cSrcweir     aOk.SetClickHdl( LINK( this, SbiInputDialog, Ok ) );
104*cdf0e10cSrcweir     aCancel.SetClickHdl( LINK( this, SbiInputDialog, Cancel ) );
105*cdf0e10cSrcweir     SetMapMode( MapMode( MAP_APPFONT ) );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     Point aPt = LogicToPixel( Point( 50, 50 ) );
108*cdf0e10cSrcweir     Size  aSz = LogicToPixel( Size( 145, 65 ) );
109*cdf0e10cSrcweir     SetPosSizePixel( aPt, aSz );
110*cdf0e10cSrcweir     aPt = LogicToPixel( Point( 10, 10 ) );
111*cdf0e10cSrcweir     aSz = LogicToPixel( Size( 120, 12 ) );
112*cdf0e10cSrcweir     aInput.SetPosSizePixel( aPt, aSz );
113*cdf0e10cSrcweir     aPt = LogicToPixel( Point( 15, 30 ) );
114*cdf0e10cSrcweir     aSz = LogicToPixel( Size( 45, 15) );
115*cdf0e10cSrcweir     aOk.SetPosSizePixel( aPt, aSz );
116*cdf0e10cSrcweir     aPt = LogicToPixel( Point( 80, 30 ) );
117*cdf0e10cSrcweir     aSz = LogicToPixel( Size( 45, 15) );
118*cdf0e10cSrcweir     aCancel.SetPosSizePixel( aPt, aSz );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     aInput.Show();
121*cdf0e10cSrcweir     aOk.Show();
122*cdf0e10cSrcweir     aCancel.Show();
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir IMPL_LINK_INLINE_START( SbiInputDialog, Ok, Window *, pWindow )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir     (void)pWindow;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     aText = aInput.GetText();
130*cdf0e10cSrcweir     EndDialog( 1 );
131*cdf0e10cSrcweir     return 0;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir IMPL_LINK_INLINE_END( SbiInputDialog, Ok, Window *, pWindow )
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir IMPL_LINK_INLINE_START( SbiInputDialog, Cancel, Window *, pWindow )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     (void)pWindow;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     EndDialog( 0 );
140*cdf0e10cSrcweir     return 0;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir IMPL_LINK_INLINE_END( SbiInputDialog, Cancel, Window *, pWindow )
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir SbiStream::SbiStream()
147*cdf0e10cSrcweir     : pStrm( 0 )
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir SbiStream::~SbiStream()
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir     delete pStrm;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir // Ummappen eines SvStream-Fehlers auf einen StarBASIC-Code
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir void SbiStream::MapError()
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     if( pStrm )
161*cdf0e10cSrcweir      switch( pStrm->GetError() )
162*cdf0e10cSrcweir      {
163*cdf0e10cSrcweir         case SVSTREAM_OK:
164*cdf0e10cSrcweir             nError = 0; break;
165*cdf0e10cSrcweir         case SVSTREAM_FILE_NOT_FOUND:
166*cdf0e10cSrcweir             nError = SbERR_FILE_NOT_FOUND; break;
167*cdf0e10cSrcweir         case SVSTREAM_PATH_NOT_FOUND:
168*cdf0e10cSrcweir             nError = SbERR_PATH_NOT_FOUND; break;
169*cdf0e10cSrcweir         case SVSTREAM_TOO_MANY_OPEN_FILES:
170*cdf0e10cSrcweir             nError = SbERR_TOO_MANY_FILES; break;
171*cdf0e10cSrcweir         case SVSTREAM_ACCESS_DENIED:
172*cdf0e10cSrcweir             nError = SbERR_ACCESS_DENIED; break;
173*cdf0e10cSrcweir         case SVSTREAM_INVALID_PARAMETER:
174*cdf0e10cSrcweir             nError = SbERR_BAD_ARGUMENT; break;
175*cdf0e10cSrcweir         case SVSTREAM_OUTOFMEMORY:
176*cdf0e10cSrcweir             nError = SbERR_NO_MEMORY; break;
177*cdf0e10cSrcweir         default:
178*cdf0e10cSrcweir             nError = SbERR_IO_ERROR; break;
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir #ifdef _USE_UNO
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir // TODO: Code is copied from daemons2/source/uno/asciiEncoder.cxx
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir ::rtl::OUString findUserInDescription( const ::rtl::OUString& aDescription )
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir     ::rtl::OUString user;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     sal_Int32 index;
191*cdf0e10cSrcweir     sal_Int32 lastIndex = 0;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     do
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         index = aDescription.indexOf((sal_Unicode) ',', lastIndex);
196*cdf0e10cSrcweir         ::rtl::OUString token = (index == -1) ? aDescription.copy(lastIndex) : aDescription.copy(lastIndex, index - lastIndex);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         lastIndex = index + 1;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         sal_Int32 eindex = token.indexOf((sal_Unicode)'=');
201*cdf0e10cSrcweir         ::rtl::OUString left = token.copy(0, eindex).toAsciiLowerCase().trim();
202*cdf0e10cSrcweir         ::rtl::OUString right = INetURLObject::decode( token.copy(eindex + 1).trim(), '%',
203*cdf0e10cSrcweir                             INetURLObject::DECODE_WITH_CHARSET );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         if(left.equals(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user"))))
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir             user = right;
208*cdf0e10cSrcweir             break;
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir     while(index != -1);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     return user;
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir #endif
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir // Hack for #83750
220*cdf0e10cSrcweir sal_Bool runsInSetup( void );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir sal_Bool needSecurityRestrictions( void )
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir #ifdef _USE_UNO
225*cdf0e10cSrcweir     static sal_Bool bNeedInit = sal_True;
226*cdf0e10cSrcweir     static sal_Bool bRetVal = sal_True;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     if( bNeedInit )
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         // Hack for #83750, use internal flag until
231*cdf0e10cSrcweir         // setup provides own service manager
232*cdf0e10cSrcweir         if( runsInSetup() )
233*cdf0e10cSrcweir         {
234*cdf0e10cSrcweir             // Setup is not critical
235*cdf0e10cSrcweir             bRetVal = sal_False;
236*cdf0e10cSrcweir             return bRetVal;
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         bNeedInit = sal_False;
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir         // Get system user to compare to portal user
242*cdf0e10cSrcweir         oslSecurity aSecurity = osl_getCurrentSecurity();
243*cdf0e10cSrcweir         ::rtl::OUString aSystemUser;
244*cdf0e10cSrcweir         sal_Bool bRet = osl_getUserName( aSecurity, &aSystemUser.pData );
245*cdf0e10cSrcweir         if( !bRet )
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             // No valid security! -> Secure mode!
248*cdf0e10cSrcweir             return sal_True;
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
252*cdf0e10cSrcweir         if( !xSMgr.is() )
253*cdf0e10cSrcweir             return sal_True;
254*cdf0e10cSrcweir         Reference< XBridgeFactory > xBridgeFac( xSMgr->createInstance
255*cdf0e10cSrcweir             ( ::rtl::OUString::createFromAscii( "com.sun.star.bridge.BridgeFactory" ) ), UNO_QUERY );
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         Sequence< Reference< XBridge > > aBridgeSeq;
258*cdf0e10cSrcweir         sal_Int32 nBridgeCount = 0;
259*cdf0e10cSrcweir         if( xBridgeFac.is() )
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             aBridgeSeq = xBridgeFac->getExistingBridges();
262*cdf0e10cSrcweir             nBridgeCount = aBridgeSeq.getLength();
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         if( nBridgeCount == 0 )
266*cdf0e10cSrcweir         {
267*cdf0e10cSrcweir             // No bridges -> local
268*cdf0e10cSrcweir             bRetVal = sal_False;
269*cdf0e10cSrcweir             return bRetVal;
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         // Iterate through all bridges to find (portal) user property
273*cdf0e10cSrcweir         const Reference< XBridge >* pBridges = aBridgeSeq.getConstArray();
274*cdf0e10cSrcweir         bRetVal = sal_False;    // Now only sal_True if user different from portal user is found
275*cdf0e10cSrcweir         sal_Int32 i;
276*cdf0e10cSrcweir         for( i = 0 ; i < nBridgeCount ; i++ )
277*cdf0e10cSrcweir         {
278*cdf0e10cSrcweir             const Reference< XBridge >& rxBridge = pBridges[ i ];
279*cdf0e10cSrcweir             ::rtl::OUString aDescription = rxBridge->getDescription();
280*cdf0e10cSrcweir             ::rtl::OUString aPortalUser = findUserInDescription( aDescription );
281*cdf0e10cSrcweir             if( aPortalUser.getLength() > 0 )
282*cdf0e10cSrcweir             {
283*cdf0e10cSrcweir                 // User Found, compare to system user
284*cdf0e10cSrcweir                 if( aPortalUser == aSystemUser )
285*cdf0e10cSrcweir                 {
286*cdf0e10cSrcweir                     // Same user -> system security is ok, bRetVal stays FALSE
287*cdf0e10cSrcweir                     break;
288*cdf0e10cSrcweir                 }
289*cdf0e10cSrcweir                 else
290*cdf0e10cSrcweir                 {
291*cdf0e10cSrcweir                     // Different user -> Secure mode!
292*cdf0e10cSrcweir                     bRetVal = sal_True;
293*cdf0e10cSrcweir                     break;
294*cdf0e10cSrcweir                 }
295*cdf0e10cSrcweir             }
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir         // No user found or PortalUser != SystemUser -> Secure mode! (Keep default value)
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     return bRetVal;
301*cdf0e10cSrcweir #else
302*cdf0e10cSrcweir     return sal_False;
303*cdf0e10cSrcweir #endif
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir // Returns sal_True if UNO is available, otherwise the old file
307*cdf0e10cSrcweir // system implementation has to be used
308*cdf0e10cSrcweir // #89378 New semantic: Don't just ask for UNO but for UCB
309*cdf0e10cSrcweir sal_Bool hasUno( void )
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir #ifdef _USE_UNO
312*cdf0e10cSrcweir     static sal_Bool bNeedInit = sal_True;
313*cdf0e10cSrcweir     static sal_Bool bRetVal = sal_True;
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     if( bNeedInit )
316*cdf0e10cSrcweir     {
317*cdf0e10cSrcweir         bNeedInit = sal_False;
318*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
319*cdf0e10cSrcweir         if( !xSMgr.is() )
320*cdf0e10cSrcweir         {
321*cdf0e10cSrcweir             // No service manager at all
322*cdf0e10cSrcweir             bRetVal = sal_False;
323*cdf0e10cSrcweir         }
324*cdf0e10cSrcweir         else
325*cdf0e10cSrcweir         {
326*cdf0e10cSrcweir             Reference< XContentProviderManager > xManager( xSMgr->createInstance( ::rtl::OUString::createFromAscii
327*cdf0e10cSrcweir                     ( "com.sun.star.ucb.UniversalContentBroker" ) ), UNO_QUERY );
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir             if ( !( xManager.is() && xManager->queryContentProvider( ::rtl::OUString::createFromAscii( "file:///" ) ).is() ) )
330*cdf0e10cSrcweir             {
331*cdf0e10cSrcweir                 // No UCB
332*cdf0e10cSrcweir                 bRetVal = sal_False;
333*cdf0e10cSrcweir             }
334*cdf0e10cSrcweir         }
335*cdf0e10cSrcweir     }
336*cdf0e10cSrcweir     return bRetVal;
337*cdf0e10cSrcweir #else
338*cdf0e10cSrcweir     return sal_False;
339*cdf0e10cSrcweir #endif
340*cdf0e10cSrcweir }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir #ifndef _OLD_FILE_IMPL
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir class OslStream : public SvStream
347*cdf0e10cSrcweir {
348*cdf0e10cSrcweir     File maFile;
349*cdf0e10cSrcweir     short mnStrmMode;
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir public:
352*cdf0e10cSrcweir                     OslStream( const String& rName, short nStrmMode );
353*cdf0e10cSrcweir                     ~OslStream();
354*cdf0e10cSrcweir     virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
355*cdf0e10cSrcweir     virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
356*cdf0e10cSrcweir     virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
357*cdf0e10cSrcweir     virtual void    FlushData();
358*cdf0e10cSrcweir     virtual void    SetSize( sal_uIntPtr nSize );
359*cdf0e10cSrcweir };
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir OslStream::OslStream( const String& rName, short nStrmMode )
362*cdf0e10cSrcweir     : maFile( rName )
363*cdf0e10cSrcweir     , mnStrmMode( nStrmMode )
364*cdf0e10cSrcweir {
365*cdf0e10cSrcweir     sal_uInt32 nFlags;
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir     if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
368*cdf0e10cSrcweir     {
369*cdf0e10cSrcweir         nFlags = OpenFlag_Read | OpenFlag_Write;
370*cdf0e10cSrcweir     }
371*cdf0e10cSrcweir     else if( nStrmMode & STREAM_WRITE )
372*cdf0e10cSrcweir     {
373*cdf0e10cSrcweir         nFlags = OpenFlag_Write;
374*cdf0e10cSrcweir     }
375*cdf0e10cSrcweir     else //if( nStrmMode & STREAM_READ )
376*cdf0e10cSrcweir     {
377*cdf0e10cSrcweir         nFlags = OpenFlag_Read;
378*cdf0e10cSrcweir     }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir     FileBase::RC nRet = maFile.open( nFlags );
381*cdf0e10cSrcweir     if( nRet == FileBase::E_NOENT && nFlags != OpenFlag_Read )
382*cdf0e10cSrcweir     {
383*cdf0e10cSrcweir         nFlags |= OpenFlag_Create;
384*cdf0e10cSrcweir         nRet = maFile.open( nFlags );
385*cdf0e10cSrcweir     }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir     if( nRet != FileBase::E_None )
388*cdf0e10cSrcweir     {
389*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
390*cdf0e10cSrcweir     }
391*cdf0e10cSrcweir }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir OslStream::~OslStream()
395*cdf0e10cSrcweir {
396*cdf0e10cSrcweir     maFile.close();
397*cdf0e10cSrcweir }
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir sal_uIntPtr OslStream::GetData( void* pData, sal_uIntPtr nSize )
400*cdf0e10cSrcweir {
401*cdf0e10cSrcweir     sal_uInt64 nBytesRead = nSize;
402*cdf0e10cSrcweir     FileBase::RC nRet = FileBase::E_None;
403*cdf0e10cSrcweir     nRet = maFile.read( pData, nBytesRead, nBytesRead );
404*cdf0e10cSrcweir     return (sal_uIntPtr)nBytesRead;
405*cdf0e10cSrcweir }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir sal_uIntPtr OslStream::PutData( const void* pData, sal_uIntPtr nSize )
408*cdf0e10cSrcweir {
409*cdf0e10cSrcweir     sal_uInt64 nBytesWritten;
410*cdf0e10cSrcweir     FileBase::RC nRet = FileBase::E_None;
411*cdf0e10cSrcweir     nRet = maFile.write( pData, (sal_uInt64)nSize, nBytesWritten );
412*cdf0e10cSrcweir     return (sal_uIntPtr)nBytesWritten;
413*cdf0e10cSrcweir }
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir sal_uIntPtr OslStream::SeekPos( sal_uIntPtr nPos )
416*cdf0e10cSrcweir {
417*cdf0e10cSrcweir     FileBase::RC nRet;
418*cdf0e10cSrcweir     if( nPos == STREAM_SEEK_TO_END )
419*cdf0e10cSrcweir     {
420*cdf0e10cSrcweir         nRet = maFile.setPos( Pos_End, 0 );
421*cdf0e10cSrcweir     }
422*cdf0e10cSrcweir     else
423*cdf0e10cSrcweir     {
424*cdf0e10cSrcweir         nRet = maFile.setPos( Pos_Absolut, (sal_uInt64)nPos );
425*cdf0e10cSrcweir     }
426*cdf0e10cSrcweir     sal_uInt64 nRealPos;
427*cdf0e10cSrcweir     nRet = maFile.getPos( nRealPos );
428*cdf0e10cSrcweir     return sal::static_int_cast<sal_uIntPtr>(nRealPos);
429*cdf0e10cSrcweir }
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir void OslStream::FlushData()
432*cdf0e10cSrcweir {
433*cdf0e10cSrcweir }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir void OslStream::SetSize( sal_uIntPtr nSize )
436*cdf0e10cSrcweir {
437*cdf0e10cSrcweir     FileBase::RC nRet = FileBase::E_None;
438*cdf0e10cSrcweir     nRet = maFile.setSize( (sal_uInt64)nSize );
439*cdf0e10cSrcweir }
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir #endif
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir #ifdef _USE_UNO
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir class UCBStream : public SvStream
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     Reference< XInputStream >   xIS;
449*cdf0e10cSrcweir     Reference< XOutputStream >  xOS;
450*cdf0e10cSrcweir     Reference< XStream >        xS;
451*cdf0e10cSrcweir     Reference< XSeekable >      xSeek;
452*cdf0e10cSrcweir public:
453*cdf0e10cSrcweir                     UCBStream( Reference< XInputStream > & xIS );
454*cdf0e10cSrcweir                     UCBStream( Reference< XOutputStream > & xOS );
455*cdf0e10cSrcweir                     UCBStream( Reference< XStream > & xS );
456*cdf0e10cSrcweir                     ~UCBStream();
457*cdf0e10cSrcweir     virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
458*cdf0e10cSrcweir     virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
459*cdf0e10cSrcweir     virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
460*cdf0e10cSrcweir     virtual void    FlushData();
461*cdf0e10cSrcweir     virtual void    SetSize( sal_uIntPtr nSize );
462*cdf0e10cSrcweir };
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir /*
465*cdf0e10cSrcweir sal_uIntPtr UCBErrorToSvStramError( ucb::IOErrorCode nError )
466*cdf0e10cSrcweir {
467*cdf0e10cSrcweir     sal_uIntPtr eReturn = ERRCODE_IO_GENERAL;
468*cdf0e10cSrcweir     switch( nError )
469*cdf0e10cSrcweir     {
470*cdf0e10cSrcweir         case ucb::IOErrorCode_ABORT:                eReturn = SVSTREAM_GENERALERROR; break;
471*cdf0e10cSrcweir         case ucb::IOErrorCode_NOT_EXISTING:         eReturn = SVSTREAM_FILE_NOT_FOUND; break;
472*cdf0e10cSrcweir         case ucb::IOErrorCode_NOT_EXISTING_PATH:    eReturn = SVSTREAM_PATH_NOT_FOUND; break;
473*cdf0e10cSrcweir         case ucb::IOErrorCode_OUT_OF_FILE_HANDLES:  eReturn = SVSTREAM_TOO_MANY_OPEN_FILES; break;
474*cdf0e10cSrcweir         case ucb::IOErrorCode_ACCESS_DENIED:        eReturn = SVSTREAM_ACCESS_DENIED; break;
475*cdf0e10cSrcweir         case ucb::IOErrorCode_LOCKING_VIOLATION:    eReturn = SVSTREAM_SHARING_VIOLATION; break;
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir         case ucb::IOErrorCode_INVALID_ACCESS:       eReturn = SVSTREAM_INVALID_ACCESS; break;
478*cdf0e10cSrcweir         case ucb::IOErrorCode_CANT_CREATE:          eReturn = SVSTREAM_CANNOT_MAKE; break;
479*cdf0e10cSrcweir         case ucb::IOErrorCode_INVALID_PARAMETER:    eReturn = SVSTREAM_INVALID_PARAMETER; break;
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir         case ucb::IOErrorCode_CANT_READ:            eReturn = SVSTREAM_READ_ERROR; break;
482*cdf0e10cSrcweir         case ucb::IOErrorCode_CANT_WRITE:           eReturn = SVSTREAM_WRITE_ERROR; break;
483*cdf0e10cSrcweir         case ucb::IOErrorCode_CANT_SEEK:            eReturn = SVSTREAM_SEEK_ERROR; break;
484*cdf0e10cSrcweir         case ucb::IOErrorCode_CANT_TELL:            eReturn = SVSTREAM_TELL_ERROR; break;
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir         case ucb::IOErrorCode_OUT_OF_MEMORY:        eReturn = SVSTREAM_OUTOFMEMORY; break;
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir         case SVSTREAM_FILEFORMAT_ERROR:             eReturn = SVSTREAM_FILEFORMAT_ERROR; break;
489*cdf0e10cSrcweir         case ucb::IOErrorCode_WRONG_VERSION:        eReturn = SVSTREAM_WRONGVERSION;
490*cdf0e10cSrcweir         case ucb::IOErrorCode_OUT_OF_DISK_SPACE:    eReturn = SVSTREAM_DISK_FULL; break;
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir         case ucb::IOErrorCode_BAD_CRC:              eReturn = ERRCODE_IO_BADCRC; break;
493*cdf0e10cSrcweir     }
494*cdf0e10cSrcweir     return eReturn;
495*cdf0e10cSrcweir }
496*cdf0e10cSrcweir */
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir UCBStream::UCBStream( Reference< XInputStream > & rStm )
499*cdf0e10cSrcweir     : xIS( rStm )
500*cdf0e10cSrcweir     , xSeek( rStm, UNO_QUERY )
501*cdf0e10cSrcweir {
502*cdf0e10cSrcweir }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir UCBStream::UCBStream( Reference< XOutputStream > & rStm )
505*cdf0e10cSrcweir     : xOS( rStm )
506*cdf0e10cSrcweir     , xSeek( rStm, UNO_QUERY )
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir }
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir UCBStream::UCBStream( Reference< XStream > & rStm )
511*cdf0e10cSrcweir     : xS( rStm )
512*cdf0e10cSrcweir     , xSeek( rStm, UNO_QUERY )
513*cdf0e10cSrcweir {
514*cdf0e10cSrcweir }
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir UCBStream::~UCBStream()
518*cdf0e10cSrcweir {
519*cdf0e10cSrcweir     try
520*cdf0e10cSrcweir     {
521*cdf0e10cSrcweir         if( xIS.is() )
522*cdf0e10cSrcweir             xIS->closeInput();
523*cdf0e10cSrcweir         else if( xOS.is() )
524*cdf0e10cSrcweir             xOS->closeOutput();
525*cdf0e10cSrcweir         else if( xS.is() )
526*cdf0e10cSrcweir         {
527*cdf0e10cSrcweir             Reference< XInputStream > xIS_ = xS->getInputStream();
528*cdf0e10cSrcweir             if( xIS_.is() )
529*cdf0e10cSrcweir                 xIS_->closeInput();
530*cdf0e10cSrcweir         }
531*cdf0e10cSrcweir     }
532*cdf0e10cSrcweir     catch( Exception & )
533*cdf0e10cSrcweir     {
534*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
535*cdf0e10cSrcweir     }
536*cdf0e10cSrcweir }
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir sal_uIntPtr UCBStream::GetData( void* pData, sal_uIntPtr nSize )
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir     try
541*cdf0e10cSrcweir     {
542*cdf0e10cSrcweir         Reference< XInputStream > xISFromS;
543*cdf0e10cSrcweir         if( xIS.is() )
544*cdf0e10cSrcweir         {
545*cdf0e10cSrcweir             Sequence<sal_Int8> aData;
546*cdf0e10cSrcweir             nSize = xIS->readBytes( aData, nSize );
547*cdf0e10cSrcweir             rtl_copyMemory( pData, aData.getConstArray(), nSize );
548*cdf0e10cSrcweir             return nSize;
549*cdf0e10cSrcweir         }
550*cdf0e10cSrcweir         else if( xS.is() && (xISFromS = xS->getInputStream()).is() )
551*cdf0e10cSrcweir         {
552*cdf0e10cSrcweir             Sequence<sal_Int8> aData;
553*cdf0e10cSrcweir             nSize = xISFromS->readBytes( aData, nSize );
554*cdf0e10cSrcweir             rtl_copyMemory( pData, aData.getConstArray(), nSize );
555*cdf0e10cSrcweir             return nSize;
556*cdf0e10cSrcweir         }
557*cdf0e10cSrcweir         else
558*cdf0e10cSrcweir             SetError( ERRCODE_IO_GENERAL );
559*cdf0e10cSrcweir     }
560*cdf0e10cSrcweir     catch( Exception & )
561*cdf0e10cSrcweir     {
562*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
563*cdf0e10cSrcweir     }
564*cdf0e10cSrcweir     return 0;
565*cdf0e10cSrcweir }
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir sal_uIntPtr UCBStream::PutData( const void* pData, sal_uIntPtr nSize )
568*cdf0e10cSrcweir {
569*cdf0e10cSrcweir     try
570*cdf0e10cSrcweir     {
571*cdf0e10cSrcweir         Reference< XOutputStream > xOSFromS;
572*cdf0e10cSrcweir         if( xOS.is() )
573*cdf0e10cSrcweir         {
574*cdf0e10cSrcweir             Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
575*cdf0e10cSrcweir             xOS->writeBytes( aData );
576*cdf0e10cSrcweir             return nSize;
577*cdf0e10cSrcweir         }
578*cdf0e10cSrcweir         else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
579*cdf0e10cSrcweir         {
580*cdf0e10cSrcweir             Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
581*cdf0e10cSrcweir             xOSFromS->writeBytes( aData );
582*cdf0e10cSrcweir             return nSize;
583*cdf0e10cSrcweir         }
584*cdf0e10cSrcweir         else
585*cdf0e10cSrcweir             SetError( ERRCODE_IO_GENERAL );
586*cdf0e10cSrcweir     }
587*cdf0e10cSrcweir     catch( Exception & )
588*cdf0e10cSrcweir     {
589*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir     return 0;
592*cdf0e10cSrcweir }
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir sal_uIntPtr UCBStream::SeekPos( sal_uIntPtr nPos )
595*cdf0e10cSrcweir {
596*cdf0e10cSrcweir     try
597*cdf0e10cSrcweir     {
598*cdf0e10cSrcweir         if( xSeek.is() )
599*cdf0e10cSrcweir         {
600*cdf0e10cSrcweir             sal_uIntPtr nLen = sal::static_int_cast<sal_uIntPtr>( xSeek->getLength() );
601*cdf0e10cSrcweir             if( nPos > nLen )
602*cdf0e10cSrcweir                 nPos = nLen;
603*cdf0e10cSrcweir             xSeek->seek( nPos );
604*cdf0e10cSrcweir             return nPos;
605*cdf0e10cSrcweir         }
606*cdf0e10cSrcweir         else
607*cdf0e10cSrcweir             SetError( ERRCODE_IO_GENERAL );
608*cdf0e10cSrcweir     }
609*cdf0e10cSrcweir     catch( Exception & )
610*cdf0e10cSrcweir     {
611*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
612*cdf0e10cSrcweir     }
613*cdf0e10cSrcweir     return 0;
614*cdf0e10cSrcweir }
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir void    UCBStream::FlushData()
617*cdf0e10cSrcweir {
618*cdf0e10cSrcweir     try
619*cdf0e10cSrcweir     {
620*cdf0e10cSrcweir         Reference< XOutputStream > xOSFromS;
621*cdf0e10cSrcweir         if( xOS.is() )
622*cdf0e10cSrcweir             xOS->flush();
623*cdf0e10cSrcweir         else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
624*cdf0e10cSrcweir             xOSFromS->flush();
625*cdf0e10cSrcweir         else
626*cdf0e10cSrcweir             SetError( ERRCODE_IO_GENERAL );
627*cdf0e10cSrcweir     }
628*cdf0e10cSrcweir     catch( Exception & )
629*cdf0e10cSrcweir     {
630*cdf0e10cSrcweir         SetError( ERRCODE_IO_GENERAL );
631*cdf0e10cSrcweir     }
632*cdf0e10cSrcweir }
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir void    UCBStream::SetSize( sal_uIntPtr nSize )
635*cdf0e10cSrcweir {
636*cdf0e10cSrcweir     (void)nSize;
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir     DBG_ERROR( "not allowed to call from basic" );
639*cdf0e10cSrcweir     SetError( ERRCODE_IO_GENERAL );
640*cdf0e10cSrcweir }
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir #endif
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir // Oeffnen eines Streams
645*cdf0e10cSrcweir SbError SbiStream::Open
646*cdf0e10cSrcweir ( short nCh, const ByteString& rName, short nStrmMode, short nFlags, short nL )
647*cdf0e10cSrcweir {
648*cdf0e10cSrcweir     nMode   = nFlags;
649*cdf0e10cSrcweir     nLen    = nL;
650*cdf0e10cSrcweir     nChan   = nCh;
651*cdf0e10cSrcweir     nLine   = 0;
652*cdf0e10cSrcweir     nExpandOnWriteTo = 0;
653*cdf0e10cSrcweir     if( ( nStrmMode & ( STREAM_READ|STREAM_WRITE ) ) == STREAM_READ )
654*cdf0e10cSrcweir         nStrmMode |= STREAM_NOCREATE;
655*cdf0e10cSrcweir     String aStr( rName, gsl_getSystemTextEncoding() );
656*cdf0e10cSrcweir     String aNameStr = getFullPath( aStr );
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir #ifdef _USE_UNO
659*cdf0e10cSrcweir     if( hasUno() )
660*cdf0e10cSrcweir     {
661*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
662*cdf0e10cSrcweir         if( xSMgr.is() )
663*cdf0e10cSrcweir         {
664*cdf0e10cSrcweir             Reference< XSimpleFileAccess >
665*cdf0e10cSrcweir                 xSFI( xSMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );
666*cdf0e10cSrcweir             if( xSFI.is() )
667*cdf0e10cSrcweir             {
668*cdf0e10cSrcweir                 try
669*cdf0e10cSrcweir                 {
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir                 // #??? For write access delete file if it already exists (not for appending)
672*cdf0e10cSrcweir                 if( (nStrmMode & STREAM_WRITE) != 0 && !IsAppend() && !IsBinary() &&
673*cdf0e10cSrcweir                     xSFI->exists( aNameStr ) && !xSFI->isFolder( aNameStr ) )
674*cdf0e10cSrcweir                 {
675*cdf0e10cSrcweir                     xSFI->kill( aNameStr );
676*cdf0e10cSrcweir                 }
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir                 if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
679*cdf0e10cSrcweir                 {
680*cdf0e10cSrcweir                     Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
681*cdf0e10cSrcweir                     pStrm = new UCBStream( xIS );
682*cdf0e10cSrcweir                 }
683*cdf0e10cSrcweir                 else if( nStrmMode & STREAM_WRITE )
684*cdf0e10cSrcweir                 {
685*cdf0e10cSrcweir                     Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
686*cdf0e10cSrcweir                     pStrm = new UCBStream( xIS );
687*cdf0e10cSrcweir                     // Open for writing is not implemented in ucb yet!!!
688*cdf0e10cSrcweir                     //Reference< XOutputStream > xIS = xSFI->openFileWrite( aNameStr );
689*cdf0e10cSrcweir                     //pStrm = new UCBStream( xIS );
690*cdf0e10cSrcweir                 }
691*cdf0e10cSrcweir                 else //if( nStrmMode & STREAM_READ )
692*cdf0e10cSrcweir                 {
693*cdf0e10cSrcweir                     Reference< XInputStream > xIS = xSFI->openFileRead( aNameStr );
694*cdf0e10cSrcweir                     pStrm = new UCBStream( xIS );
695*cdf0e10cSrcweir                 }
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir                 }
698*cdf0e10cSrcweir                 catch( Exception & )
699*cdf0e10cSrcweir                 {
700*cdf0e10cSrcweir                     nError = ERRCODE_IO_GENERAL;
701*cdf0e10cSrcweir                 }
702*cdf0e10cSrcweir             }
703*cdf0e10cSrcweir         }
704*cdf0e10cSrcweir     }
705*cdf0e10cSrcweir 
706*cdf0e10cSrcweir #endif
707*cdf0e10cSrcweir     if( !pStrm )
708*cdf0e10cSrcweir     {
709*cdf0e10cSrcweir #ifdef _OLD_FILE_IMPL
710*cdf0e10cSrcweir         pStrm = new SvFileStream( aNameStr, nStrmMode );
711*cdf0e10cSrcweir #else
712*cdf0e10cSrcweir         pStrm = new OslStream( aNameStr, nStrmMode );
713*cdf0e10cSrcweir #endif
714*cdf0e10cSrcweir     }
715*cdf0e10cSrcweir     if( IsAppend() )
716*cdf0e10cSrcweir         pStrm->Seek( STREAM_SEEK_TO_END );
717*cdf0e10cSrcweir     MapError();
718*cdf0e10cSrcweir     if( nError )
719*cdf0e10cSrcweir         delete pStrm, pStrm = NULL;
720*cdf0e10cSrcweir     return nError;
721*cdf0e10cSrcweir }
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir SbError SbiStream::Close()
724*cdf0e10cSrcweir {
725*cdf0e10cSrcweir     if( pStrm )
726*cdf0e10cSrcweir     {
727*cdf0e10cSrcweir         if( !hasUno() )
728*cdf0e10cSrcweir         {
729*cdf0e10cSrcweir #ifdef _OLD_FILE_IMPL
730*cdf0e10cSrcweir             ((SvFileStream *)pStrm)->Close();
731*cdf0e10cSrcweir #endif
732*cdf0e10cSrcweir         }
733*cdf0e10cSrcweir         MapError();
734*cdf0e10cSrcweir         delete pStrm;
735*cdf0e10cSrcweir         pStrm = NULL;
736*cdf0e10cSrcweir     }
737*cdf0e10cSrcweir     nChan = 0;
738*cdf0e10cSrcweir     return nError;
739*cdf0e10cSrcweir }
740*cdf0e10cSrcweir 
741*cdf0e10cSrcweir SbError SbiStream::Read( ByteString& rBuf, sal_uInt16 n, bool bForceReadingPerByte )
742*cdf0e10cSrcweir {
743*cdf0e10cSrcweir     nExpandOnWriteTo = 0;
744*cdf0e10cSrcweir     if( !bForceReadingPerByte && IsText() )
745*cdf0e10cSrcweir     {
746*cdf0e10cSrcweir         pStrm->ReadLine( rBuf );
747*cdf0e10cSrcweir         nLine++;
748*cdf0e10cSrcweir     }
749*cdf0e10cSrcweir     else
750*cdf0e10cSrcweir     {
751*cdf0e10cSrcweir         if( !n ) n = nLen;
752*cdf0e10cSrcweir         if( !n )
753*cdf0e10cSrcweir             return nError = SbERR_BAD_RECORD_LENGTH;
754*cdf0e10cSrcweir         rBuf.Fill( n, ' ' );
755*cdf0e10cSrcweir         pStrm->Read( (void*)rBuf.GetBuffer(), n );
756*cdf0e10cSrcweir     }
757*cdf0e10cSrcweir     MapError();
758*cdf0e10cSrcweir     if( !nError && pStrm->IsEof() )
759*cdf0e10cSrcweir         nError = SbERR_READ_PAST_EOF;
760*cdf0e10cSrcweir     return nError;
761*cdf0e10cSrcweir }
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir SbError SbiStream::Read( char& ch )
764*cdf0e10cSrcweir {
765*cdf0e10cSrcweir     nExpandOnWriteTo = 0;
766*cdf0e10cSrcweir     if( !aLine.Len() )
767*cdf0e10cSrcweir     {
768*cdf0e10cSrcweir         Read( aLine, 0 );
769*cdf0e10cSrcweir         aLine += '\n';
770*cdf0e10cSrcweir     }
771*cdf0e10cSrcweir     ch = aLine.GetBuffer()[0];
772*cdf0e10cSrcweir     aLine.Erase( 0, 1 );
773*cdf0e10cSrcweir     return nError;
774*cdf0e10cSrcweir }
775*cdf0e10cSrcweir 
776*cdf0e10cSrcweir void SbiStream::ExpandFile()
777*cdf0e10cSrcweir {
778*cdf0e10cSrcweir     if ( nExpandOnWriteTo )
779*cdf0e10cSrcweir     {
780*cdf0e10cSrcweir         sal_uIntPtr nCur = pStrm->Seek(STREAM_SEEK_TO_END);
781*cdf0e10cSrcweir         if( nCur < nExpandOnWriteTo )
782*cdf0e10cSrcweir         {
783*cdf0e10cSrcweir             sal_uIntPtr nDiff = nExpandOnWriteTo - nCur;
784*cdf0e10cSrcweir             char c = 0;
785*cdf0e10cSrcweir             while( nDiff-- )
786*cdf0e10cSrcweir                 *pStrm << c;
787*cdf0e10cSrcweir         }
788*cdf0e10cSrcweir         else
789*cdf0e10cSrcweir         {
790*cdf0e10cSrcweir             pStrm->Seek( nExpandOnWriteTo );
791*cdf0e10cSrcweir         }
792*cdf0e10cSrcweir         nExpandOnWriteTo = 0;
793*cdf0e10cSrcweir     }
794*cdf0e10cSrcweir }
795*cdf0e10cSrcweir 
796*cdf0e10cSrcweir SbError SbiStream::Write( const ByteString& rBuf, sal_uInt16 n )
797*cdf0e10cSrcweir {
798*cdf0e10cSrcweir     ExpandFile();
799*cdf0e10cSrcweir     if( IsAppend() )
800*cdf0e10cSrcweir         pStrm->Seek( STREAM_SEEK_TO_END );
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir     if( IsText() )
803*cdf0e10cSrcweir     {
804*cdf0e10cSrcweir         aLine += rBuf;
805*cdf0e10cSrcweir         // Raus damit, wenn das Ende ein LF ist, aber CRLF vorher
806*cdf0e10cSrcweir         // strippen, da der SvStrm ein CRLF anfuegt!
807*cdf0e10cSrcweir         sal_uInt16 nLineLen = aLine.Len();
808*cdf0e10cSrcweir         if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0A )
809*cdf0e10cSrcweir         {
810*cdf0e10cSrcweir             aLine.Erase( nLineLen );
811*cdf0e10cSrcweir             if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0D )
812*cdf0e10cSrcweir                 aLine.Erase( nLineLen );
813*cdf0e10cSrcweir             pStrm->WriteLines( aLine );
814*cdf0e10cSrcweir             aLine.Erase();
815*cdf0e10cSrcweir         }
816*cdf0e10cSrcweir     }
817*cdf0e10cSrcweir     else
818*cdf0e10cSrcweir     {
819*cdf0e10cSrcweir         if( !n ) n = nLen;
820*cdf0e10cSrcweir         if( !n )
821*cdf0e10cSrcweir             return nError = SbERR_BAD_RECORD_LENGTH;
822*cdf0e10cSrcweir         pStrm->Write( rBuf.GetBuffer(), n );
823*cdf0e10cSrcweir         MapError();
824*cdf0e10cSrcweir     }
825*cdf0e10cSrcweir     return nError;
826*cdf0e10cSrcweir }
827*cdf0e10cSrcweir 
828*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
829*cdf0e10cSrcweir 
830*cdf0e10cSrcweir // Zugriff auf das aktuelle I/O-System:
831*cdf0e10cSrcweir 
832*cdf0e10cSrcweir SbiIoSystem* SbGetIoSystem()
833*cdf0e10cSrcweir {
834*cdf0e10cSrcweir     SbiInstance* pInst = pINST;
835*cdf0e10cSrcweir     return pInst ? pInst->GetIoSystem() : NULL;
836*cdf0e10cSrcweir }
837*cdf0e10cSrcweir 
838*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
839*cdf0e10cSrcweir 
840*cdf0e10cSrcweir SbiIoSystem::SbiIoSystem()
841*cdf0e10cSrcweir {
842*cdf0e10cSrcweir     for( short i = 0; i < CHANNELS; i++ )
843*cdf0e10cSrcweir         pChan[ i ] = NULL;
844*cdf0e10cSrcweir     nChan  = 0;
845*cdf0e10cSrcweir     nError = 0;
846*cdf0e10cSrcweir }
847*cdf0e10cSrcweir 
848*cdf0e10cSrcweir SbiIoSystem::~SbiIoSystem()
849*cdf0e10cSrcweir {
850*cdf0e10cSrcweir     Shutdown();
851*cdf0e10cSrcweir }
852*cdf0e10cSrcweir 
853*cdf0e10cSrcweir SbError SbiIoSystem::GetError()
854*cdf0e10cSrcweir {
855*cdf0e10cSrcweir     SbError n = nError; nError = 0;
856*cdf0e10cSrcweir     return n;
857*cdf0e10cSrcweir }
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir void SbiIoSystem::Open
860*cdf0e10cSrcweir     ( short nCh, const ByteString& rName, short nMode, short nFlags, short nLen )
861*cdf0e10cSrcweir {
862*cdf0e10cSrcweir     nError = 0;
863*cdf0e10cSrcweir     if( nCh >= CHANNELS || !nCh )
864*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
865*cdf0e10cSrcweir     else if( pChan[ nCh ] )
866*cdf0e10cSrcweir         nError = SbERR_FILE_ALREADY_OPEN;
867*cdf0e10cSrcweir     else
868*cdf0e10cSrcweir     {
869*cdf0e10cSrcweir         pChan[ nCh ] = new SbiStream;
870*cdf0e10cSrcweir         nError = pChan[ nCh ]->Open( nCh, rName, nMode, nFlags, nLen );
871*cdf0e10cSrcweir         if( nError )
872*cdf0e10cSrcweir             delete pChan[ nCh ], pChan[ nCh ] = NULL;
873*cdf0e10cSrcweir     }
874*cdf0e10cSrcweir     nChan = 0;
875*cdf0e10cSrcweir }
876*cdf0e10cSrcweir 
877*cdf0e10cSrcweir // Aktuellen Kanal schliessen
878*cdf0e10cSrcweir 
879*cdf0e10cSrcweir void SbiIoSystem::Close()
880*cdf0e10cSrcweir {
881*cdf0e10cSrcweir     if( !nChan )
882*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
883*cdf0e10cSrcweir     else if( !pChan[ nChan ] )
884*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
885*cdf0e10cSrcweir     else
886*cdf0e10cSrcweir     {
887*cdf0e10cSrcweir         nError = pChan[ nChan ]->Close();
888*cdf0e10cSrcweir         delete pChan[ nChan ];
889*cdf0e10cSrcweir         pChan[ nChan ] = NULL;
890*cdf0e10cSrcweir     }
891*cdf0e10cSrcweir     nChan = 0;
892*cdf0e10cSrcweir }
893*cdf0e10cSrcweir 
894*cdf0e10cSrcweir // Shutdown nach Programmlauf
895*cdf0e10cSrcweir 
896*cdf0e10cSrcweir void SbiIoSystem::Shutdown()
897*cdf0e10cSrcweir {
898*cdf0e10cSrcweir     for( short i = 1; i < CHANNELS; i++ )
899*cdf0e10cSrcweir     {
900*cdf0e10cSrcweir         if( pChan[ i ] )
901*cdf0e10cSrcweir         {
902*cdf0e10cSrcweir             SbError n = pChan[ i ]->Close();
903*cdf0e10cSrcweir             delete pChan[ i ];
904*cdf0e10cSrcweir             pChan[ i ] = NULL;
905*cdf0e10cSrcweir             if( n && !nError )
906*cdf0e10cSrcweir                 nError = n;
907*cdf0e10cSrcweir         }
908*cdf0e10cSrcweir     }
909*cdf0e10cSrcweir     nChan = 0;
910*cdf0e10cSrcweir     // Noch was zu PRINTen?
911*cdf0e10cSrcweir     if( aOut.Len() )
912*cdf0e10cSrcweir     {
913*cdf0e10cSrcweir         String aOutStr( aOut, gsl_getSystemTextEncoding() );
914*cdf0e10cSrcweir #if defined GCC
915*cdf0e10cSrcweir         Window* pParent = Application::GetDefDialogParent();
916*cdf0e10cSrcweir         MessBox( pParent, WinBits( WB_OK ), String(), aOutStr ).Execute();
917*cdf0e10cSrcweir #else
918*cdf0e10cSrcweir         MessBox( GetpApp()->GetDefDialogParent(), WinBits( WB_OK ), String(), aOutStr ).Execute();
919*cdf0e10cSrcweir #endif
920*cdf0e10cSrcweir     }
921*cdf0e10cSrcweir     aOut.Erase();
922*cdf0e10cSrcweir }
923*cdf0e10cSrcweir 
924*cdf0e10cSrcweir // Aus aktuellem Kanal lesen
925*cdf0e10cSrcweir 
926*cdf0e10cSrcweir void SbiIoSystem::Read( ByteString& rBuf, short n )
927*cdf0e10cSrcweir {
928*cdf0e10cSrcweir     if( !nChan )
929*cdf0e10cSrcweir         ReadCon( rBuf );
930*cdf0e10cSrcweir     else if( !pChan[ nChan ] )
931*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
932*cdf0e10cSrcweir     else
933*cdf0e10cSrcweir         nError = pChan[ nChan ]->Read( rBuf, n );
934*cdf0e10cSrcweir }
935*cdf0e10cSrcweir 
936*cdf0e10cSrcweir char SbiIoSystem::Read()
937*cdf0e10cSrcweir {
938*cdf0e10cSrcweir     char ch = ' ';
939*cdf0e10cSrcweir     if( !nChan )
940*cdf0e10cSrcweir     {
941*cdf0e10cSrcweir         if( !aIn.Len() )
942*cdf0e10cSrcweir         {
943*cdf0e10cSrcweir             ReadCon( aIn );
944*cdf0e10cSrcweir             aIn += '\n';
945*cdf0e10cSrcweir         }
946*cdf0e10cSrcweir         ch = aIn.GetBuffer()[0];
947*cdf0e10cSrcweir         aIn.Erase( 0, 1 );
948*cdf0e10cSrcweir     }
949*cdf0e10cSrcweir     else if( !pChan[ nChan ] )
950*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
951*cdf0e10cSrcweir     else
952*cdf0e10cSrcweir         nError = pChan[ nChan ]->Read( ch );
953*cdf0e10cSrcweir     return ch;
954*cdf0e10cSrcweir }
955*cdf0e10cSrcweir 
956*cdf0e10cSrcweir void SbiIoSystem::Write( const ByteString& rBuf, short n )
957*cdf0e10cSrcweir {
958*cdf0e10cSrcweir     if( !nChan )
959*cdf0e10cSrcweir         WriteCon( rBuf );
960*cdf0e10cSrcweir     else if( !pChan[ nChan ] )
961*cdf0e10cSrcweir         nError = SbERR_BAD_CHANNEL;
962*cdf0e10cSrcweir     else
963*cdf0e10cSrcweir         nError = pChan[ nChan ]->Write( rBuf, n );
964*cdf0e10cSrcweir }
965*cdf0e10cSrcweir 
966*cdf0e10cSrcweir short SbiIoSystem::NextChannel()
967*cdf0e10cSrcweir {
968*cdf0e10cSrcweir     for( short i = 1; i < CHANNELS; i++ )
969*cdf0e10cSrcweir     {
970*cdf0e10cSrcweir         if( !pChan[ i ] )
971*cdf0e10cSrcweir             return i;
972*cdf0e10cSrcweir     }
973*cdf0e10cSrcweir     nError = SbERR_TOO_MANY_FILES;
974*cdf0e10cSrcweir     return CHANNELS;
975*cdf0e10cSrcweir }
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir // nChannel == 0..CHANNELS-1
978*cdf0e10cSrcweir 
979*cdf0e10cSrcweir SbiStream* SbiIoSystem::GetStream( short nChannel ) const
980*cdf0e10cSrcweir {
981*cdf0e10cSrcweir     SbiStream* pRet = 0;
982*cdf0e10cSrcweir     if( nChannel >= 0 && nChannel < CHANNELS )
983*cdf0e10cSrcweir         pRet = pChan[ nChannel ];
984*cdf0e10cSrcweir     return pRet;
985*cdf0e10cSrcweir }
986*cdf0e10cSrcweir 
987*cdf0e10cSrcweir void SbiIoSystem::CloseAll(void)
988*cdf0e10cSrcweir {
989*cdf0e10cSrcweir     for( short i = 1; i < CHANNELS; i++ )
990*cdf0e10cSrcweir     {
991*cdf0e10cSrcweir         if( pChan[ i ] )
992*cdf0e10cSrcweir         {
993*cdf0e10cSrcweir             SbError n = pChan[ i ]->Close();
994*cdf0e10cSrcweir             delete pChan[ i ];
995*cdf0e10cSrcweir             pChan[ i ] = NULL;
996*cdf0e10cSrcweir             if( n && !nError )
997*cdf0e10cSrcweir                 nError = n;
998*cdf0e10cSrcweir         }
999*cdf0e10cSrcweir     }
1000*cdf0e10cSrcweir }
1001*cdf0e10cSrcweir 
1002*cdf0e10cSrcweir /***************************************************************************
1003*cdf0e10cSrcweir *
1004*cdf0e10cSrcweir *   Console Support
1005*cdf0e10cSrcweir *
1006*cdf0e10cSrcweir ***************************************************************************/
1007*cdf0e10cSrcweir 
1008*cdf0e10cSrcweir // Einlesen einer Zeile von der Console
1009*cdf0e10cSrcweir 
1010*cdf0e10cSrcweir void SbiIoSystem::ReadCon( ByteString& rIn )
1011*cdf0e10cSrcweir {
1012*cdf0e10cSrcweir     String aPromptStr( aPrompt, gsl_getSystemTextEncoding() );
1013*cdf0e10cSrcweir     SbiInputDialog aDlg( NULL, aPromptStr );
1014*cdf0e10cSrcweir     if( aDlg.Execute() )
1015*cdf0e10cSrcweir         rIn = ByteString( aDlg.GetInput(), gsl_getSystemTextEncoding() );
1016*cdf0e10cSrcweir     else
1017*cdf0e10cSrcweir         nError = SbERR_USER_ABORT;
1018*cdf0e10cSrcweir     aPrompt.Erase();
1019*cdf0e10cSrcweir }
1020*cdf0e10cSrcweir 
1021*cdf0e10cSrcweir // Ausgabe einer MessageBox, wenn im Console-Puffer ein CR ist
1022*cdf0e10cSrcweir 
1023*cdf0e10cSrcweir void SbiIoSystem::WriteCon( const ByteString& rText )
1024*cdf0e10cSrcweir {
1025*cdf0e10cSrcweir     aOut += rText;
1026*cdf0e10cSrcweir     sal_uInt16 n1 = aOut.Search( '\n' );
1027*cdf0e10cSrcweir     sal_uInt16 n2 = aOut.Search( '\r' );
1028*cdf0e10cSrcweir     if( n1 != STRING_NOTFOUND || n2 != STRING_NOTFOUND )
1029*cdf0e10cSrcweir     {
1030*cdf0e10cSrcweir         if( n1 == STRING_NOTFOUND ) n1 = n2;
1031*cdf0e10cSrcweir         else
1032*cdf0e10cSrcweir         if( n2 == STRING_NOTFOUND ) n2 = n1;
1033*cdf0e10cSrcweir         if( n1 > n2 ) n1 = n2;
1034*cdf0e10cSrcweir         ByteString s( aOut.Copy( 0, n1 ) );
1035*cdf0e10cSrcweir         aOut.Erase( 0, n1 );
1036*cdf0e10cSrcweir         while( aOut.GetBuffer()[0] == '\n' || aOut.GetBuffer()[0] == '\r' )
1037*cdf0e10cSrcweir             aOut.Erase( 0, 1 );
1038*cdf0e10cSrcweir         String aStr( s, gsl_getSystemTextEncoding() );
1039*cdf0e10cSrcweir         {
1040*cdf0e10cSrcweir             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1041*cdf0e10cSrcweir             if( !MessBox( GetpApp()->GetDefDialogParent(),
1042*cdf0e10cSrcweir                         WinBits( WB_OK_CANCEL | WB_DEF_OK ),
1043*cdf0e10cSrcweir                         String(), aStr ).Execute() )
1044*cdf0e10cSrcweir                 nError = SbERR_USER_ABORT;
1045*cdf0e10cSrcweir         }
1046*cdf0e10cSrcweir     }
1047*cdf0e10cSrcweir }
1048*cdf0e10cSrcweir 
1049