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 );
241cdf0e10cSrcweir if( !bRet )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir // No valid security! -> Secure mode!
244cdf0e10cSrcweir return sal_True;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
247cdf0e10cSrcweir Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
248cdf0e10cSrcweir if( !xSMgr.is() )
249cdf0e10cSrcweir return sal_True;
250cdf0e10cSrcweir Reference< XBridgeFactory > xBridgeFac( xSMgr->createInstance
251cdf0e10cSrcweir ( ::rtl::OUString::createFromAscii( "com.sun.star.bridge.BridgeFactory" ) ), UNO_QUERY );
252cdf0e10cSrcweir
253cdf0e10cSrcweir Sequence< Reference< XBridge > > aBridgeSeq;
254cdf0e10cSrcweir sal_Int32 nBridgeCount = 0;
255cdf0e10cSrcweir if( xBridgeFac.is() )
256cdf0e10cSrcweir {
257cdf0e10cSrcweir aBridgeSeq = xBridgeFac->getExistingBridges();
258cdf0e10cSrcweir nBridgeCount = aBridgeSeq.getLength();
259cdf0e10cSrcweir }
260cdf0e10cSrcweir
261cdf0e10cSrcweir if( nBridgeCount == 0 )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir // No bridges -> local
264cdf0e10cSrcweir bRetVal = sal_False;
265cdf0e10cSrcweir return bRetVal;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
268cdf0e10cSrcweir // Iterate through all bridges to find (portal) user property
269cdf0e10cSrcweir const Reference< XBridge >* pBridges = aBridgeSeq.getConstArray();
270cdf0e10cSrcweir bRetVal = sal_False; // Now only sal_True if user different from portal user is found
271cdf0e10cSrcweir sal_Int32 i;
272cdf0e10cSrcweir for( i = 0 ; i < nBridgeCount ; i++ )
273cdf0e10cSrcweir {
274cdf0e10cSrcweir const Reference< XBridge >& rxBridge = pBridges[ i ];
275cdf0e10cSrcweir ::rtl::OUString aDescription = rxBridge->getDescription();
276cdf0e10cSrcweir ::rtl::OUString aPortalUser = findUserInDescription( aDescription );
277*0848378bSHerbert Dürr if( !aPortalUser.isEmpty() )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir // User Found, compare to system user
280cdf0e10cSrcweir if( aPortalUser == aSystemUser )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir // Same user -> system security is ok, bRetVal stays FALSE
283cdf0e10cSrcweir break;
284cdf0e10cSrcweir }
285cdf0e10cSrcweir else
286cdf0e10cSrcweir {
287cdf0e10cSrcweir // Different user -> Secure mode!
288cdf0e10cSrcweir bRetVal = sal_True;
289cdf0e10cSrcweir break;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir }
292cdf0e10cSrcweir }
293cdf0e10cSrcweir // No user found or PortalUser != SystemUser -> Secure mode! (Keep default value)
294cdf0e10cSrcweir }
295cdf0e10cSrcweir
296cdf0e10cSrcweir return bRetVal;
297cdf0e10cSrcweir #else
298cdf0e10cSrcweir return sal_False;
299cdf0e10cSrcweir #endif
300cdf0e10cSrcweir }
301cdf0e10cSrcweir
302cdf0e10cSrcweir // Returns sal_True if UNO is available, otherwise the old file
303cdf0e10cSrcweir // system implementation has to be used
304cdf0e10cSrcweir // #89378 New semantic: Don't just ask for UNO but for UCB
hasUno(void)305cdf0e10cSrcweir sal_Bool hasUno( void )
306cdf0e10cSrcweir {
307cdf0e10cSrcweir #ifdef _USE_UNO
308cdf0e10cSrcweir static sal_Bool bNeedInit = sal_True;
309cdf0e10cSrcweir static sal_Bool bRetVal = sal_True;
310cdf0e10cSrcweir
311cdf0e10cSrcweir if( bNeedInit )
312cdf0e10cSrcweir {
313cdf0e10cSrcweir bNeedInit = sal_False;
314cdf0e10cSrcweir Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
315cdf0e10cSrcweir if( !xSMgr.is() )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir // No service manager at all
318cdf0e10cSrcweir bRetVal = sal_False;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir else
321cdf0e10cSrcweir {
322cdf0e10cSrcweir Reference< XContentProviderManager > xManager( xSMgr->createInstance( ::rtl::OUString::createFromAscii
323cdf0e10cSrcweir ( "com.sun.star.ucb.UniversalContentBroker" ) ), UNO_QUERY );
324cdf0e10cSrcweir
325cdf0e10cSrcweir if ( !( xManager.is() && xManager->queryContentProvider( ::rtl::OUString::createFromAscii( "file:///" ) ).is() ) )
326cdf0e10cSrcweir {
327cdf0e10cSrcweir // No UCB
328cdf0e10cSrcweir bRetVal = sal_False;
329cdf0e10cSrcweir }
330cdf0e10cSrcweir }
331cdf0e10cSrcweir }
332cdf0e10cSrcweir return bRetVal;
333cdf0e10cSrcweir #else
334cdf0e10cSrcweir return sal_False;
335cdf0e10cSrcweir #endif
336cdf0e10cSrcweir }
337cdf0e10cSrcweir
338cdf0e10cSrcweir
339cdf0e10cSrcweir
340cdf0e10cSrcweir #ifndef _OLD_FILE_IMPL
341cdf0e10cSrcweir
342cdf0e10cSrcweir class OslStream : public SvStream
343cdf0e10cSrcweir {
344cdf0e10cSrcweir File maFile;
345cdf0e10cSrcweir short mnStrmMode;
346cdf0e10cSrcweir
347cdf0e10cSrcweir public:
348cdf0e10cSrcweir OslStream( const String& rName, short nStrmMode );
349cdf0e10cSrcweir ~OslStream();
350cdf0e10cSrcweir virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
351cdf0e10cSrcweir virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
352cdf0e10cSrcweir virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
353cdf0e10cSrcweir virtual void FlushData();
354cdf0e10cSrcweir virtual void SetSize( sal_uIntPtr nSize );
355cdf0e10cSrcweir };
356cdf0e10cSrcweir
OslStream(const String & rName,short nStrmMode)357cdf0e10cSrcweir OslStream::OslStream( const String& rName, short nStrmMode )
358cdf0e10cSrcweir : maFile( rName )
359cdf0e10cSrcweir , mnStrmMode( nStrmMode )
360cdf0e10cSrcweir {
361cdf0e10cSrcweir sal_uInt32 nFlags;
362cdf0e10cSrcweir
363cdf0e10cSrcweir if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
364cdf0e10cSrcweir {
365cdf0e10cSrcweir nFlags = OpenFlag_Read | OpenFlag_Write;
366cdf0e10cSrcweir }
367cdf0e10cSrcweir else if( nStrmMode & STREAM_WRITE )
368cdf0e10cSrcweir {
369cdf0e10cSrcweir nFlags = OpenFlag_Write;
370cdf0e10cSrcweir }
371cdf0e10cSrcweir else //if( nStrmMode & STREAM_READ )
372cdf0e10cSrcweir {
373cdf0e10cSrcweir nFlags = OpenFlag_Read;
374cdf0e10cSrcweir }
375cdf0e10cSrcweir
376cdf0e10cSrcweir FileBase::RC nRet = maFile.open( nFlags );
377cdf0e10cSrcweir if( nRet == FileBase::E_NOENT && nFlags != OpenFlag_Read )
378cdf0e10cSrcweir {
379cdf0e10cSrcweir nFlags |= OpenFlag_Create;
380cdf0e10cSrcweir nRet = maFile.open( nFlags );
381cdf0e10cSrcweir }
382cdf0e10cSrcweir
383cdf0e10cSrcweir if( nRet != FileBase::E_None )
384cdf0e10cSrcweir {
385cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
386cdf0e10cSrcweir }
387cdf0e10cSrcweir }
388cdf0e10cSrcweir
389cdf0e10cSrcweir
~OslStream()390cdf0e10cSrcweir OslStream::~OslStream()
391cdf0e10cSrcweir {
392cdf0e10cSrcweir maFile.close();
393cdf0e10cSrcweir }
394cdf0e10cSrcweir
GetData(void * pData,sal_uIntPtr nSize)395cdf0e10cSrcweir sal_uIntPtr OslStream::GetData( void* pData, sal_uIntPtr nSize )
396cdf0e10cSrcweir {
397cdf0e10cSrcweir sal_uInt64 nBytesRead = nSize;
398cdf0e10cSrcweir FileBase::RC nRet = FileBase::E_None;
399cdf0e10cSrcweir nRet = maFile.read( pData, nBytesRead, nBytesRead );
400cdf0e10cSrcweir return (sal_uIntPtr)nBytesRead;
401cdf0e10cSrcweir }
402cdf0e10cSrcweir
PutData(const void * pData,sal_uIntPtr nSize)403cdf0e10cSrcweir sal_uIntPtr OslStream::PutData( const void* pData, sal_uIntPtr nSize )
404cdf0e10cSrcweir {
405cdf0e10cSrcweir sal_uInt64 nBytesWritten;
406cdf0e10cSrcweir FileBase::RC nRet = FileBase::E_None;
407cdf0e10cSrcweir nRet = maFile.write( pData, (sal_uInt64)nSize, nBytesWritten );
408cdf0e10cSrcweir return (sal_uIntPtr)nBytesWritten;
409cdf0e10cSrcweir }
410cdf0e10cSrcweir
SeekPos(sal_uIntPtr nPos)411cdf0e10cSrcweir sal_uIntPtr OslStream::SeekPos( sal_uIntPtr nPos )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir FileBase::RC nRet;
414cdf0e10cSrcweir if( nPos == STREAM_SEEK_TO_END )
415cdf0e10cSrcweir {
416cdf0e10cSrcweir nRet = maFile.setPos( Pos_End, 0 );
417cdf0e10cSrcweir }
418cdf0e10cSrcweir else
419cdf0e10cSrcweir {
420cdf0e10cSrcweir nRet = maFile.setPos( Pos_Absolut, (sal_uInt64)nPos );
421cdf0e10cSrcweir }
422cdf0e10cSrcweir sal_uInt64 nRealPos;
423cdf0e10cSrcweir nRet = maFile.getPos( nRealPos );
424cdf0e10cSrcweir return sal::static_int_cast<sal_uIntPtr>(nRealPos);
425cdf0e10cSrcweir }
426cdf0e10cSrcweir
FlushData()427cdf0e10cSrcweir void OslStream::FlushData()
428cdf0e10cSrcweir {
429cdf0e10cSrcweir }
430cdf0e10cSrcweir
SetSize(sal_uIntPtr nSize)431cdf0e10cSrcweir void OslStream::SetSize( sal_uIntPtr nSize )
432cdf0e10cSrcweir {
433cdf0e10cSrcweir FileBase::RC nRet = FileBase::E_None;
434cdf0e10cSrcweir nRet = maFile.setSize( (sal_uInt64)nSize );
435cdf0e10cSrcweir }
436cdf0e10cSrcweir
437cdf0e10cSrcweir #endif
438cdf0e10cSrcweir
439cdf0e10cSrcweir
440cdf0e10cSrcweir #ifdef _USE_UNO
441cdf0e10cSrcweir
442cdf0e10cSrcweir class UCBStream : public SvStream
443cdf0e10cSrcweir {
444cdf0e10cSrcweir Reference< XInputStream > xIS;
445cdf0e10cSrcweir Reference< XOutputStream > xOS;
446cdf0e10cSrcweir Reference< XStream > xS;
447cdf0e10cSrcweir Reference< XSeekable > xSeek;
448cdf0e10cSrcweir public:
449cdf0e10cSrcweir UCBStream( Reference< XInputStream > & xIS );
450cdf0e10cSrcweir UCBStream( Reference< XOutputStream > & xOS );
451cdf0e10cSrcweir UCBStream( Reference< XStream > & xS );
452cdf0e10cSrcweir ~UCBStream();
453cdf0e10cSrcweir virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
454cdf0e10cSrcweir virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
455cdf0e10cSrcweir virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
456cdf0e10cSrcweir virtual void FlushData();
457cdf0e10cSrcweir virtual void SetSize( sal_uIntPtr nSize );
458cdf0e10cSrcweir };
459cdf0e10cSrcweir
460cdf0e10cSrcweir /*
461cdf0e10cSrcweir sal_uIntPtr UCBErrorToSvStramError( ucb::IOErrorCode nError )
462cdf0e10cSrcweir {
463cdf0e10cSrcweir sal_uIntPtr eReturn = ERRCODE_IO_GENERAL;
464cdf0e10cSrcweir switch( nError )
465cdf0e10cSrcweir {
466cdf0e10cSrcweir case ucb::IOErrorCode_ABORT: eReturn = SVSTREAM_GENERALERROR; break;
467cdf0e10cSrcweir case ucb::IOErrorCode_NOT_EXISTING: eReturn = SVSTREAM_FILE_NOT_FOUND; break;
468cdf0e10cSrcweir case ucb::IOErrorCode_NOT_EXISTING_PATH: eReturn = SVSTREAM_PATH_NOT_FOUND; break;
469cdf0e10cSrcweir case ucb::IOErrorCode_OUT_OF_FILE_HANDLES: eReturn = SVSTREAM_TOO_MANY_OPEN_FILES; break;
470cdf0e10cSrcweir case ucb::IOErrorCode_ACCESS_DENIED: eReturn = SVSTREAM_ACCESS_DENIED; break;
471cdf0e10cSrcweir case ucb::IOErrorCode_LOCKING_VIOLATION: eReturn = SVSTREAM_SHARING_VIOLATION; break;
472cdf0e10cSrcweir
473cdf0e10cSrcweir case ucb::IOErrorCode_INVALID_ACCESS: eReturn = SVSTREAM_INVALID_ACCESS; break;
474cdf0e10cSrcweir case ucb::IOErrorCode_CANT_CREATE: eReturn = SVSTREAM_CANNOT_MAKE; break;
475cdf0e10cSrcweir case ucb::IOErrorCode_INVALID_PARAMETER: eReturn = SVSTREAM_INVALID_PARAMETER; break;
476cdf0e10cSrcweir
477cdf0e10cSrcweir case ucb::IOErrorCode_CANT_READ: eReturn = SVSTREAM_READ_ERROR; break;
478cdf0e10cSrcweir case ucb::IOErrorCode_CANT_WRITE: eReturn = SVSTREAM_WRITE_ERROR; break;
479cdf0e10cSrcweir case ucb::IOErrorCode_CANT_SEEK: eReturn = SVSTREAM_SEEK_ERROR; break;
480cdf0e10cSrcweir case ucb::IOErrorCode_CANT_TELL: eReturn = SVSTREAM_TELL_ERROR; break;
481cdf0e10cSrcweir
482cdf0e10cSrcweir case ucb::IOErrorCode_OUT_OF_MEMORY: eReturn = SVSTREAM_OUTOFMEMORY; break;
483cdf0e10cSrcweir
484cdf0e10cSrcweir case SVSTREAM_FILEFORMAT_ERROR: eReturn = SVSTREAM_FILEFORMAT_ERROR; break;
485cdf0e10cSrcweir case ucb::IOErrorCode_WRONG_VERSION: eReturn = SVSTREAM_WRONGVERSION;
486cdf0e10cSrcweir case ucb::IOErrorCode_OUT_OF_DISK_SPACE: eReturn = SVSTREAM_DISK_FULL; break;
487cdf0e10cSrcweir
488cdf0e10cSrcweir case ucb::IOErrorCode_BAD_CRC: eReturn = ERRCODE_IO_BADCRC; break;
489cdf0e10cSrcweir }
490cdf0e10cSrcweir return eReturn;
491cdf0e10cSrcweir }
492cdf0e10cSrcweir */
493cdf0e10cSrcweir
UCBStream(Reference<XInputStream> & rStm)494cdf0e10cSrcweir UCBStream::UCBStream( Reference< XInputStream > & rStm )
495cdf0e10cSrcweir : xIS( rStm )
496cdf0e10cSrcweir , xSeek( rStm, UNO_QUERY )
497cdf0e10cSrcweir {
498cdf0e10cSrcweir }
499cdf0e10cSrcweir
UCBStream(Reference<XOutputStream> & rStm)500cdf0e10cSrcweir UCBStream::UCBStream( Reference< XOutputStream > & rStm )
501cdf0e10cSrcweir : xOS( rStm )
502cdf0e10cSrcweir , xSeek( rStm, UNO_QUERY )
503cdf0e10cSrcweir {
504cdf0e10cSrcweir }
505cdf0e10cSrcweir
UCBStream(Reference<XStream> & rStm)506cdf0e10cSrcweir UCBStream::UCBStream( Reference< XStream > & rStm )
507cdf0e10cSrcweir : xS( rStm )
508cdf0e10cSrcweir , xSeek( rStm, UNO_QUERY )
509cdf0e10cSrcweir {
510cdf0e10cSrcweir }
511cdf0e10cSrcweir
512cdf0e10cSrcweir
~UCBStream()513cdf0e10cSrcweir UCBStream::~UCBStream()
514cdf0e10cSrcweir {
515cdf0e10cSrcweir try
516cdf0e10cSrcweir {
517cdf0e10cSrcweir if( xIS.is() )
518cdf0e10cSrcweir xIS->closeInput();
519cdf0e10cSrcweir else if( xOS.is() )
520cdf0e10cSrcweir xOS->closeOutput();
521cdf0e10cSrcweir else if( xS.is() )
522cdf0e10cSrcweir {
523cdf0e10cSrcweir Reference< XInputStream > xIS_ = xS->getInputStream();
524cdf0e10cSrcweir if( xIS_.is() )
525cdf0e10cSrcweir xIS_->closeInput();
526cdf0e10cSrcweir }
527cdf0e10cSrcweir }
528cdf0e10cSrcweir catch( Exception & )
529cdf0e10cSrcweir {
530cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
531cdf0e10cSrcweir }
532cdf0e10cSrcweir }
533cdf0e10cSrcweir
GetData(void * pData,sal_uIntPtr nSize)534cdf0e10cSrcweir sal_uIntPtr UCBStream::GetData( void* pData, sal_uIntPtr nSize )
535cdf0e10cSrcweir {
536cdf0e10cSrcweir try
537cdf0e10cSrcweir {
538cdf0e10cSrcweir Reference< XInputStream > xISFromS;
539cdf0e10cSrcweir if( xIS.is() )
540cdf0e10cSrcweir {
541cdf0e10cSrcweir Sequence<sal_Int8> aData;
542cdf0e10cSrcweir nSize = xIS->readBytes( aData, nSize );
543cdf0e10cSrcweir rtl_copyMemory( pData, aData.getConstArray(), nSize );
544cdf0e10cSrcweir return nSize;
545cdf0e10cSrcweir }
546cdf0e10cSrcweir else if( xS.is() && (xISFromS = xS->getInputStream()).is() )
547cdf0e10cSrcweir {
548cdf0e10cSrcweir Sequence<sal_Int8> aData;
549cdf0e10cSrcweir nSize = xISFromS->readBytes( aData, nSize );
550cdf0e10cSrcweir rtl_copyMemory( pData, aData.getConstArray(), nSize );
551cdf0e10cSrcweir return nSize;
552cdf0e10cSrcweir }
553cdf0e10cSrcweir else
554cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
555cdf0e10cSrcweir }
556cdf0e10cSrcweir catch( Exception & )
557cdf0e10cSrcweir {
558cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
559cdf0e10cSrcweir }
560cdf0e10cSrcweir return 0;
561cdf0e10cSrcweir }
562cdf0e10cSrcweir
PutData(const void * pData,sal_uIntPtr nSize)563cdf0e10cSrcweir sal_uIntPtr UCBStream::PutData( const void* pData, sal_uIntPtr nSize )
564cdf0e10cSrcweir {
565cdf0e10cSrcweir try
566cdf0e10cSrcweir {
567cdf0e10cSrcweir Reference< XOutputStream > xOSFromS;
568cdf0e10cSrcweir if( xOS.is() )
569cdf0e10cSrcweir {
570cdf0e10cSrcweir Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
571cdf0e10cSrcweir xOS->writeBytes( aData );
572cdf0e10cSrcweir return nSize;
573cdf0e10cSrcweir }
574cdf0e10cSrcweir else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
575cdf0e10cSrcweir {
576cdf0e10cSrcweir Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
577cdf0e10cSrcweir xOSFromS->writeBytes( aData );
578cdf0e10cSrcweir return nSize;
579cdf0e10cSrcweir }
580cdf0e10cSrcweir else
581cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
582cdf0e10cSrcweir }
583cdf0e10cSrcweir catch( Exception & )
584cdf0e10cSrcweir {
585cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
586cdf0e10cSrcweir }
587cdf0e10cSrcweir return 0;
588cdf0e10cSrcweir }
589cdf0e10cSrcweir
SeekPos(sal_uIntPtr nPos)590cdf0e10cSrcweir sal_uIntPtr UCBStream::SeekPos( sal_uIntPtr nPos )
591cdf0e10cSrcweir {
592cdf0e10cSrcweir try
593cdf0e10cSrcweir {
594cdf0e10cSrcweir if( xSeek.is() )
595cdf0e10cSrcweir {
596cdf0e10cSrcweir sal_uIntPtr nLen = sal::static_int_cast<sal_uIntPtr>( xSeek->getLength() );
597cdf0e10cSrcweir if( nPos > nLen )
598cdf0e10cSrcweir nPos = nLen;
599cdf0e10cSrcweir xSeek->seek( nPos );
600cdf0e10cSrcweir return nPos;
601cdf0e10cSrcweir }
602cdf0e10cSrcweir else
603cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
604cdf0e10cSrcweir }
605cdf0e10cSrcweir catch( Exception & )
606cdf0e10cSrcweir {
607cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
608cdf0e10cSrcweir }
609cdf0e10cSrcweir return 0;
610cdf0e10cSrcweir }
611cdf0e10cSrcweir
FlushData()612cdf0e10cSrcweir void UCBStream::FlushData()
613cdf0e10cSrcweir {
614cdf0e10cSrcweir try
615cdf0e10cSrcweir {
616cdf0e10cSrcweir Reference< XOutputStream > xOSFromS;
617cdf0e10cSrcweir if( xOS.is() )
618cdf0e10cSrcweir xOS->flush();
619cdf0e10cSrcweir else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
620cdf0e10cSrcweir xOSFromS->flush();
621cdf0e10cSrcweir else
622cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
623cdf0e10cSrcweir }
624cdf0e10cSrcweir catch( Exception & )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
627cdf0e10cSrcweir }
628cdf0e10cSrcweir }
629cdf0e10cSrcweir
SetSize(sal_uIntPtr nSize)630cdf0e10cSrcweir void UCBStream::SetSize( sal_uIntPtr nSize )
631cdf0e10cSrcweir {
632cdf0e10cSrcweir (void)nSize;
633cdf0e10cSrcweir
634cdf0e10cSrcweir DBG_ERROR( "not allowed to call from basic" );
635cdf0e10cSrcweir SetError( ERRCODE_IO_GENERAL );
636cdf0e10cSrcweir }
637cdf0e10cSrcweir
638cdf0e10cSrcweir #endif
639cdf0e10cSrcweir
640cdf0e10cSrcweir // Oeffnen eines Streams
Open(short nCh,const ByteString & rName,short nStrmMode,short nFlags,short nL)641cdf0e10cSrcweir SbError SbiStream::Open
642cdf0e10cSrcweir ( short nCh, const ByteString& rName, short nStrmMode, short nFlags, short nL )
643cdf0e10cSrcweir {
644cdf0e10cSrcweir nMode = nFlags;
645cdf0e10cSrcweir nLen = nL;
646cdf0e10cSrcweir nChan = nCh;
647cdf0e10cSrcweir nLine = 0;
648cdf0e10cSrcweir nExpandOnWriteTo = 0;
649cdf0e10cSrcweir if( ( nStrmMode & ( STREAM_READ|STREAM_WRITE ) ) == STREAM_READ )
650cdf0e10cSrcweir nStrmMode |= STREAM_NOCREATE;
651cdf0e10cSrcweir String aStr( rName, gsl_getSystemTextEncoding() );
652cdf0e10cSrcweir String aNameStr = getFullPath( aStr );
653cdf0e10cSrcweir
654cdf0e10cSrcweir #ifdef _USE_UNO
655cdf0e10cSrcweir if( hasUno() )
656cdf0e10cSrcweir {
657cdf0e10cSrcweir Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
658cdf0e10cSrcweir if( xSMgr.is() )
659cdf0e10cSrcweir {
660cdf0e10cSrcweir Reference< XSimpleFileAccess >
661cdf0e10cSrcweir xSFI( xSMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );
662cdf0e10cSrcweir if( xSFI.is() )
663cdf0e10cSrcweir {
664cdf0e10cSrcweir try
665cdf0e10cSrcweir {
666cdf0e10cSrcweir
667cdf0e10cSrcweir // #??? For write access delete file if it already exists (not for appending)
668cdf0e10cSrcweir if( (nStrmMode & STREAM_WRITE) != 0 && !IsAppend() && !IsBinary() &&
669cdf0e10cSrcweir xSFI->exists( aNameStr ) && !xSFI->isFolder( aNameStr ) )
670cdf0e10cSrcweir {
671cdf0e10cSrcweir xSFI->kill( aNameStr );
672cdf0e10cSrcweir }
673cdf0e10cSrcweir
674cdf0e10cSrcweir if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
675cdf0e10cSrcweir {
676cdf0e10cSrcweir Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
677cdf0e10cSrcweir pStrm = new UCBStream( xIS );
678cdf0e10cSrcweir }
679cdf0e10cSrcweir else if( nStrmMode & STREAM_WRITE )
680cdf0e10cSrcweir {
681cdf0e10cSrcweir Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
682cdf0e10cSrcweir pStrm = new UCBStream( xIS );
683cdf0e10cSrcweir // Open for writing is not implemented in ucb yet!!!
684cdf0e10cSrcweir //Reference< XOutputStream > xIS = xSFI->openFileWrite( aNameStr );
685cdf0e10cSrcweir //pStrm = new UCBStream( xIS );
686cdf0e10cSrcweir }
687cdf0e10cSrcweir else //if( nStrmMode & STREAM_READ )
688cdf0e10cSrcweir {
689cdf0e10cSrcweir Reference< XInputStream > xIS = xSFI->openFileRead( aNameStr );
690cdf0e10cSrcweir pStrm = new UCBStream( xIS );
691cdf0e10cSrcweir }
692cdf0e10cSrcweir
693cdf0e10cSrcweir }
694cdf0e10cSrcweir catch( Exception & )
695cdf0e10cSrcweir {
696cdf0e10cSrcweir nError = ERRCODE_IO_GENERAL;
697cdf0e10cSrcweir }
698cdf0e10cSrcweir }
699cdf0e10cSrcweir }
700cdf0e10cSrcweir }
701cdf0e10cSrcweir
702cdf0e10cSrcweir #endif
703cdf0e10cSrcweir if( !pStrm )
704cdf0e10cSrcweir {
705cdf0e10cSrcweir #ifdef _OLD_FILE_IMPL
706cdf0e10cSrcweir pStrm = new SvFileStream( aNameStr, nStrmMode );
707cdf0e10cSrcweir #else
708cdf0e10cSrcweir pStrm = new OslStream( aNameStr, nStrmMode );
709cdf0e10cSrcweir #endif
710cdf0e10cSrcweir }
711cdf0e10cSrcweir if( IsAppend() )
712cdf0e10cSrcweir pStrm->Seek( STREAM_SEEK_TO_END );
713cdf0e10cSrcweir MapError();
714cdf0e10cSrcweir if( nError )
715cdf0e10cSrcweir delete pStrm, pStrm = NULL;
716cdf0e10cSrcweir return nError;
717cdf0e10cSrcweir }
718cdf0e10cSrcweir
Close()719cdf0e10cSrcweir SbError SbiStream::Close()
720cdf0e10cSrcweir {
721cdf0e10cSrcweir if( pStrm )
722cdf0e10cSrcweir {
723cdf0e10cSrcweir if( !hasUno() )
724cdf0e10cSrcweir {
725cdf0e10cSrcweir #ifdef _OLD_FILE_IMPL
726cdf0e10cSrcweir ((SvFileStream *)pStrm)->Close();
727cdf0e10cSrcweir #endif
728cdf0e10cSrcweir }
729cdf0e10cSrcweir MapError();
730cdf0e10cSrcweir delete pStrm;
731cdf0e10cSrcweir pStrm = NULL;
732cdf0e10cSrcweir }
733cdf0e10cSrcweir nChan = 0;
734cdf0e10cSrcweir return nError;
735cdf0e10cSrcweir }
736cdf0e10cSrcweir
Read(ByteString & rBuf,sal_uInt16 n,bool bForceReadingPerByte)737cdf0e10cSrcweir SbError SbiStream::Read( ByteString& rBuf, sal_uInt16 n, bool bForceReadingPerByte )
738cdf0e10cSrcweir {
739cdf0e10cSrcweir nExpandOnWriteTo = 0;
740cdf0e10cSrcweir if( !bForceReadingPerByte && IsText() )
741cdf0e10cSrcweir {
742cdf0e10cSrcweir pStrm->ReadLine( rBuf );
743cdf0e10cSrcweir nLine++;
744cdf0e10cSrcweir }
745cdf0e10cSrcweir else
746cdf0e10cSrcweir {
747cdf0e10cSrcweir if( !n ) n = nLen;
748cdf0e10cSrcweir if( !n )
749cdf0e10cSrcweir return nError = SbERR_BAD_RECORD_LENGTH;
750cdf0e10cSrcweir rBuf.Fill( n, ' ' );
751cdf0e10cSrcweir pStrm->Read( (void*)rBuf.GetBuffer(), n );
752cdf0e10cSrcweir }
753cdf0e10cSrcweir MapError();
754cdf0e10cSrcweir if( !nError && pStrm->IsEof() )
755cdf0e10cSrcweir nError = SbERR_READ_PAST_EOF;
756cdf0e10cSrcweir return nError;
757cdf0e10cSrcweir }
758cdf0e10cSrcweir
Read(char & ch)759cdf0e10cSrcweir SbError SbiStream::Read( char& ch )
760cdf0e10cSrcweir {
761cdf0e10cSrcweir nExpandOnWriteTo = 0;
762cdf0e10cSrcweir if( !aLine.Len() )
763cdf0e10cSrcweir {
764cdf0e10cSrcweir Read( aLine, 0 );
765cdf0e10cSrcweir aLine += '\n';
766cdf0e10cSrcweir }
767cdf0e10cSrcweir ch = aLine.GetBuffer()[0];
768cdf0e10cSrcweir aLine.Erase( 0, 1 );
769cdf0e10cSrcweir return nError;
770cdf0e10cSrcweir }
771cdf0e10cSrcweir
ExpandFile()772cdf0e10cSrcweir void SbiStream::ExpandFile()
773cdf0e10cSrcweir {
774cdf0e10cSrcweir if ( nExpandOnWriteTo )
775cdf0e10cSrcweir {
776cdf0e10cSrcweir sal_uIntPtr nCur = pStrm->Seek(STREAM_SEEK_TO_END);
777cdf0e10cSrcweir if( nCur < nExpandOnWriteTo )
778cdf0e10cSrcweir {
779cdf0e10cSrcweir sal_uIntPtr nDiff = nExpandOnWriteTo - nCur;
780cdf0e10cSrcweir char c = 0;
781cdf0e10cSrcweir while( nDiff-- )
782cdf0e10cSrcweir *pStrm << c;
783cdf0e10cSrcweir }
784cdf0e10cSrcweir else
785cdf0e10cSrcweir {
786cdf0e10cSrcweir pStrm->Seek( nExpandOnWriteTo );
787cdf0e10cSrcweir }
788cdf0e10cSrcweir nExpandOnWriteTo = 0;
789cdf0e10cSrcweir }
790cdf0e10cSrcweir }
791cdf0e10cSrcweir
Write(const ByteString & rBuf,sal_uInt16 n)792cdf0e10cSrcweir SbError SbiStream::Write( const ByteString& rBuf, sal_uInt16 n )
793cdf0e10cSrcweir {
794cdf0e10cSrcweir ExpandFile();
795cdf0e10cSrcweir if( IsAppend() )
796cdf0e10cSrcweir pStrm->Seek( STREAM_SEEK_TO_END );
797cdf0e10cSrcweir
798cdf0e10cSrcweir if( IsText() )
799cdf0e10cSrcweir {
800cdf0e10cSrcweir aLine += rBuf;
801cdf0e10cSrcweir // Raus damit, wenn das Ende ein LF ist, aber CRLF vorher
802cdf0e10cSrcweir // strippen, da der SvStrm ein CRLF anfuegt!
803cdf0e10cSrcweir sal_uInt16 nLineLen = aLine.Len();
804cdf0e10cSrcweir if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0A )
805cdf0e10cSrcweir {
806cdf0e10cSrcweir aLine.Erase( nLineLen );
807cdf0e10cSrcweir if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0D )
808cdf0e10cSrcweir aLine.Erase( nLineLen );
809cdf0e10cSrcweir pStrm->WriteLines( aLine );
810cdf0e10cSrcweir aLine.Erase();
811cdf0e10cSrcweir }
812cdf0e10cSrcweir }
813cdf0e10cSrcweir else
814cdf0e10cSrcweir {
815cdf0e10cSrcweir if( !n ) n = nLen;
816cdf0e10cSrcweir if( !n )
817cdf0e10cSrcweir return nError = SbERR_BAD_RECORD_LENGTH;
818cdf0e10cSrcweir pStrm->Write( rBuf.GetBuffer(), n );
819cdf0e10cSrcweir MapError();
820cdf0e10cSrcweir }
821cdf0e10cSrcweir return nError;
822cdf0e10cSrcweir }
823cdf0e10cSrcweir
824cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
825cdf0e10cSrcweir
826cdf0e10cSrcweir // Zugriff auf das aktuelle I/O-System:
827cdf0e10cSrcweir
SbGetIoSystem()828cdf0e10cSrcweir SbiIoSystem* SbGetIoSystem()
829cdf0e10cSrcweir {
830cdf0e10cSrcweir SbiInstance* pInst = pINST;
831cdf0e10cSrcweir return pInst ? pInst->GetIoSystem() : NULL;
832cdf0e10cSrcweir }
833cdf0e10cSrcweir
834cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
835cdf0e10cSrcweir
SbiIoSystem()836cdf0e10cSrcweir SbiIoSystem::SbiIoSystem()
837cdf0e10cSrcweir {
838cdf0e10cSrcweir for( short i = 0; i < CHANNELS; i++ )
839cdf0e10cSrcweir pChan[ i ] = NULL;
840cdf0e10cSrcweir nChan = 0;
841cdf0e10cSrcweir nError = 0;
842cdf0e10cSrcweir }
843cdf0e10cSrcweir
~SbiIoSystem()844cdf0e10cSrcweir SbiIoSystem::~SbiIoSystem()
845cdf0e10cSrcweir {
846cdf0e10cSrcweir Shutdown();
847cdf0e10cSrcweir }
848cdf0e10cSrcweir
GetError()849cdf0e10cSrcweir SbError SbiIoSystem::GetError()
850cdf0e10cSrcweir {
851cdf0e10cSrcweir SbError n = nError; nError = 0;
852cdf0e10cSrcweir return n;
853cdf0e10cSrcweir }
854cdf0e10cSrcweir
Open(short nCh,const ByteString & rName,short nMode,short nFlags,short nLen)855cdf0e10cSrcweir void SbiIoSystem::Open
856cdf0e10cSrcweir ( short nCh, const ByteString& rName, short nMode, short nFlags, short nLen )
857cdf0e10cSrcweir {
858cdf0e10cSrcweir nError = 0;
859cdf0e10cSrcweir if( nCh >= CHANNELS || !nCh )
860cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
861cdf0e10cSrcweir else if( pChan[ nCh ] )
862cdf0e10cSrcweir nError = SbERR_FILE_ALREADY_OPEN;
863cdf0e10cSrcweir else
864cdf0e10cSrcweir {
865cdf0e10cSrcweir pChan[ nCh ] = new SbiStream;
866cdf0e10cSrcweir nError = pChan[ nCh ]->Open( nCh, rName, nMode, nFlags, nLen );
867cdf0e10cSrcweir if( nError )
868cdf0e10cSrcweir delete pChan[ nCh ], pChan[ nCh ] = NULL;
869cdf0e10cSrcweir }
870cdf0e10cSrcweir nChan = 0;
871cdf0e10cSrcweir }
872cdf0e10cSrcweir
873cdf0e10cSrcweir // Aktuellen Kanal schliessen
874cdf0e10cSrcweir
Close()875cdf0e10cSrcweir void SbiIoSystem::Close()
876cdf0e10cSrcweir {
877cdf0e10cSrcweir if( !nChan )
878cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
879cdf0e10cSrcweir else if( !pChan[ nChan ] )
880cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
881cdf0e10cSrcweir else
882cdf0e10cSrcweir {
883cdf0e10cSrcweir nError = pChan[ nChan ]->Close();
884cdf0e10cSrcweir delete pChan[ nChan ];
885cdf0e10cSrcweir pChan[ nChan ] = NULL;
886cdf0e10cSrcweir }
887cdf0e10cSrcweir nChan = 0;
888cdf0e10cSrcweir }
889cdf0e10cSrcweir
890cdf0e10cSrcweir // Shutdown nach Programmlauf
891cdf0e10cSrcweir
Shutdown()892cdf0e10cSrcweir void SbiIoSystem::Shutdown()
893cdf0e10cSrcweir {
894cdf0e10cSrcweir for( short i = 1; i < CHANNELS; i++ )
895cdf0e10cSrcweir {
896cdf0e10cSrcweir if( pChan[ i ] )
897cdf0e10cSrcweir {
898cdf0e10cSrcweir SbError n = pChan[ i ]->Close();
899cdf0e10cSrcweir delete pChan[ i ];
900cdf0e10cSrcweir pChan[ i ] = NULL;
901cdf0e10cSrcweir if( n && !nError )
902cdf0e10cSrcweir nError = n;
903cdf0e10cSrcweir }
904cdf0e10cSrcweir }
905cdf0e10cSrcweir nChan = 0;
906cdf0e10cSrcweir // Noch was zu PRINTen?
907cdf0e10cSrcweir if( aOut.Len() )
908cdf0e10cSrcweir {
909cdf0e10cSrcweir String aOutStr( aOut, gsl_getSystemTextEncoding() );
910cdf0e10cSrcweir #if defined GCC
911cdf0e10cSrcweir Window* pParent = Application::GetDefDialogParent();
912cdf0e10cSrcweir MessBox( pParent, WinBits( WB_OK ), String(), aOutStr ).Execute();
913cdf0e10cSrcweir #else
914cdf0e10cSrcweir MessBox( GetpApp()->GetDefDialogParent(), WinBits( WB_OK ), String(), aOutStr ).Execute();
915cdf0e10cSrcweir #endif
916cdf0e10cSrcweir }
917cdf0e10cSrcweir aOut.Erase();
918cdf0e10cSrcweir }
919cdf0e10cSrcweir
920cdf0e10cSrcweir // Aus aktuellem Kanal lesen
921cdf0e10cSrcweir
Read(ByteString & rBuf,short n)922cdf0e10cSrcweir void SbiIoSystem::Read( ByteString& rBuf, short n )
923cdf0e10cSrcweir {
924cdf0e10cSrcweir if( !nChan )
925cdf0e10cSrcweir ReadCon( rBuf );
926cdf0e10cSrcweir else if( !pChan[ nChan ] )
927cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
928cdf0e10cSrcweir else
929cdf0e10cSrcweir nError = pChan[ nChan ]->Read( rBuf, n );
930cdf0e10cSrcweir }
931cdf0e10cSrcweir
Read()932cdf0e10cSrcweir char SbiIoSystem::Read()
933cdf0e10cSrcweir {
934cdf0e10cSrcweir char ch = ' ';
935cdf0e10cSrcweir if( !nChan )
936cdf0e10cSrcweir {
937cdf0e10cSrcweir if( !aIn.Len() )
938cdf0e10cSrcweir {
939cdf0e10cSrcweir ReadCon( aIn );
940cdf0e10cSrcweir aIn += '\n';
941cdf0e10cSrcweir }
942cdf0e10cSrcweir ch = aIn.GetBuffer()[0];
943cdf0e10cSrcweir aIn.Erase( 0, 1 );
944cdf0e10cSrcweir }
945cdf0e10cSrcweir else if( !pChan[ nChan ] )
946cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
947cdf0e10cSrcweir else
948cdf0e10cSrcweir nError = pChan[ nChan ]->Read( ch );
949cdf0e10cSrcweir return ch;
950cdf0e10cSrcweir }
951cdf0e10cSrcweir
Write(const ByteString & rBuf,short n)952cdf0e10cSrcweir void SbiIoSystem::Write( const ByteString& rBuf, short n )
953cdf0e10cSrcweir {
954cdf0e10cSrcweir if( !nChan )
955cdf0e10cSrcweir WriteCon( rBuf );
956cdf0e10cSrcweir else if( !pChan[ nChan ] )
957cdf0e10cSrcweir nError = SbERR_BAD_CHANNEL;
958cdf0e10cSrcweir else
959cdf0e10cSrcweir nError = pChan[ nChan ]->Write( rBuf, n );
960cdf0e10cSrcweir }
961cdf0e10cSrcweir
NextChannel()962cdf0e10cSrcweir short SbiIoSystem::NextChannel()
963cdf0e10cSrcweir {
964cdf0e10cSrcweir for( short i = 1; i < CHANNELS; i++ )
965cdf0e10cSrcweir {
966cdf0e10cSrcweir if( !pChan[ i ] )
967cdf0e10cSrcweir return i;
968cdf0e10cSrcweir }
969cdf0e10cSrcweir nError = SbERR_TOO_MANY_FILES;
970cdf0e10cSrcweir return CHANNELS;
971cdf0e10cSrcweir }
972cdf0e10cSrcweir
973cdf0e10cSrcweir // nChannel == 0..CHANNELS-1
974cdf0e10cSrcweir
GetStream(short nChannel) const975cdf0e10cSrcweir SbiStream* SbiIoSystem::GetStream( short nChannel ) const
976cdf0e10cSrcweir {
977cdf0e10cSrcweir SbiStream* pRet = 0;
978cdf0e10cSrcweir if( nChannel >= 0 && nChannel < CHANNELS )
979cdf0e10cSrcweir pRet = pChan[ nChannel ];
980cdf0e10cSrcweir return pRet;
981cdf0e10cSrcweir }
982cdf0e10cSrcweir
CloseAll(void)983cdf0e10cSrcweir void SbiIoSystem::CloseAll(void)
984cdf0e10cSrcweir {
985cdf0e10cSrcweir for( short i = 1; i < CHANNELS; i++ )
986cdf0e10cSrcweir {
987cdf0e10cSrcweir if( pChan[ i ] )
988cdf0e10cSrcweir {
989cdf0e10cSrcweir SbError n = pChan[ i ]->Close();
990cdf0e10cSrcweir delete pChan[ i ];
991cdf0e10cSrcweir pChan[ i ] = NULL;
992cdf0e10cSrcweir if( n && !nError )
993cdf0e10cSrcweir nError = n;
994cdf0e10cSrcweir }
995cdf0e10cSrcweir }
996cdf0e10cSrcweir }
997cdf0e10cSrcweir
998cdf0e10cSrcweir /***************************************************************************
999cdf0e10cSrcweir *
1000cdf0e10cSrcweir * Console Support
1001cdf0e10cSrcweir *
1002cdf0e10cSrcweir ***************************************************************************/
1003cdf0e10cSrcweir
1004cdf0e10cSrcweir // Einlesen einer Zeile von der Console
1005cdf0e10cSrcweir
ReadCon(ByteString & rIn)1006cdf0e10cSrcweir void SbiIoSystem::ReadCon( ByteString& rIn )
1007cdf0e10cSrcweir {
1008cdf0e10cSrcweir String aPromptStr( aPrompt, gsl_getSystemTextEncoding() );
1009cdf0e10cSrcweir SbiInputDialog aDlg( NULL, aPromptStr );
1010cdf0e10cSrcweir if( aDlg.Execute() )
1011cdf0e10cSrcweir rIn = ByteString( aDlg.GetInput(), gsl_getSystemTextEncoding() );
1012cdf0e10cSrcweir else
1013cdf0e10cSrcweir nError = SbERR_USER_ABORT;
1014cdf0e10cSrcweir aPrompt.Erase();
1015cdf0e10cSrcweir }
1016cdf0e10cSrcweir
1017cdf0e10cSrcweir // Ausgabe einer MessageBox, wenn im Console-Puffer ein CR ist
1018cdf0e10cSrcweir
WriteCon(const ByteString & rText)1019cdf0e10cSrcweir void SbiIoSystem::WriteCon( const ByteString& rText )
1020cdf0e10cSrcweir {
1021cdf0e10cSrcweir aOut += rText;
1022cdf0e10cSrcweir sal_uInt16 n1 = aOut.Search( '\n' );
1023cdf0e10cSrcweir sal_uInt16 n2 = aOut.Search( '\r' );
1024cdf0e10cSrcweir if( n1 != STRING_NOTFOUND || n2 != STRING_NOTFOUND )
1025cdf0e10cSrcweir {
1026cdf0e10cSrcweir if( n1 == STRING_NOTFOUND ) n1 = n2;
1027cdf0e10cSrcweir else
1028cdf0e10cSrcweir if( n2 == STRING_NOTFOUND ) n2 = n1;
1029cdf0e10cSrcweir if( n1 > n2 ) n1 = n2;
1030cdf0e10cSrcweir ByteString s( aOut.Copy( 0, n1 ) );
1031cdf0e10cSrcweir aOut.Erase( 0, n1 );
1032cdf0e10cSrcweir while( aOut.GetBuffer()[0] == '\n' || aOut.GetBuffer()[0] == '\r' )
1033cdf0e10cSrcweir aOut.Erase( 0, 1 );
1034cdf0e10cSrcweir String aStr( s, gsl_getSystemTextEncoding() );
1035cdf0e10cSrcweir {
1036cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1037cdf0e10cSrcweir if( !MessBox( GetpApp()->GetDefDialogParent(),
1038cdf0e10cSrcweir WinBits( WB_OK_CANCEL | WB_DEF_OK ),
1039cdf0e10cSrcweir String(), aStr ).Execute() )
1040cdf0e10cSrcweir nError = SbERR_USER_ABORT;
1041cdf0e10cSrcweir }
1042cdf0e10cSrcweir }
1043cdf0e10cSrcweir }
1044cdf0e10cSrcweir
1045