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