1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <vcl/dialog.hxx>
#include <vcl/edit.hxx>
#include <vcl/button.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/svapp.hxx>
#include <osl/security.h>
#include <osl/file.hxx>
#include <tools/urlobj.hxx>
#include <osl/mutex.hxx>
#include "runtime.hxx"
#include <sal/alloca.h>
#include <ctype.h>
#include <rtl/byteseq.hxx>
#include <rtl/textenc.h>
#include <rtl/strbuf.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/textenc.h>
#include <rtl/ustrbuf.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <com/sun/star/ucb/XContentProvider.hpp>
#include <com/sun/star/ucb/XContentProviderManager.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/bridge/XBridge.hpp>
#include <com/sun/star/bridge/XBridgeFactory.hpp>
using namespace comphelper;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::ucb;
using namespace com::sun::star::io;
using namespace com::sun::star::bridge;
#include "iosys.hxx"
#include "sbintern.hxx"
class SbiInputDialog : public ModalDialog {
Edit aInput;
OKButton aOk;
CancelButton aCancel;
String aText;
DECL_LINK( Ok, Window * );
DECL_LINK( Cancel, Window * );
public:
SbiInputDialog( Window*, const String& );
const String& GetInput() { return aText; }
};
SbiInputDialog::SbiInputDialog( Window* pParent, const String& rPrompt )
:ModalDialog( pParent, WB_3DLOOK | WB_MOVEABLE | WB_CLOSEABLE ),
aInput( this, WB_3DLOOK | WB_LEFT | WB_BORDER ),
aOk( this ), aCancel( this )
{
SetText( rPrompt );
aOk.SetClickHdl( LINK( this, SbiInputDialog, Ok ) );
aCancel.SetClickHdl( LINK( this, SbiInputDialog, Cancel ) );
SetMapMode( MapMode( MAP_APPFONT ) );
Point aPt = LogicToPixel( Point( 50, 50 ) );
Size aSz = LogicToPixel( Size( 145, 65 ) );
SetPosSizePixel( aPt, aSz );
aPt = LogicToPixel( Point( 10, 10 ) );
aSz = LogicToPixel( Size( 120, 12 ) );
aInput.SetPosSizePixel( aPt, aSz );
aPt = LogicToPixel( Point( 15, 30 ) );
aSz = LogicToPixel( Size( 45, 15) );
aOk.SetPosSizePixel( aPt, aSz );
aPt = LogicToPixel( Point( 80, 30 ) );
aSz = LogicToPixel( Size( 45, 15) );
aCancel.SetPosSizePixel( aPt, aSz );
aInput.Show();
aOk.Show();
aCancel.Show();
}
IMPL_LINK_INLINE_START( SbiInputDialog, Ok, Window *, pWindow )
{
(void)pWindow;
aText = aInput.GetText();
EndDialog( 1 );
return 0;
}
IMPL_LINK_INLINE_END( SbiInputDialog, Ok, Window *, pWindow )
IMPL_LINK_INLINE_START( SbiInputDialog, Cancel, Window *, pWindow )
{
(void)pWindow;
EndDialog( 0 );
return 0;
}
IMPL_LINK_INLINE_END( SbiInputDialog, Cancel, Window *, pWindow )
SbiStream::SbiStream()
: pStrm( 0 )
{
}
SbiStream::~SbiStream()
{
delete pStrm;
}
// map an SvStream-error to StarBASIC-code
void SbiStream::MapError()
{
if( pStrm )
switch( pStrm->GetError() )
{
case SVSTREAM_OK:
nError = 0; break;
case SVSTREAM_FILE_NOT_FOUND:
nError = SbERR_FILE_NOT_FOUND; break;
case SVSTREAM_PATH_NOT_FOUND:
nError = SbERR_PATH_NOT_FOUND; break;
case SVSTREAM_TOO_MANY_OPEN_FILES:
nError = SbERR_TOO_MANY_FILES; break;
case SVSTREAM_ACCESS_DENIED:
nError = SbERR_ACCESS_DENIED; break;
case SVSTREAM_INVALID_PARAMETER:
nError = SbERR_BAD_ARGUMENT; break;
case SVSTREAM_OUTOFMEMORY:
nError = SbERR_NO_MEMORY; break;
default:
nError = SbERR_IO_ERROR; break;
}
}
// TODO: Code is copied from daemons2/source/uno/asciiEncoder.cxx
::rtl::OUString findUserInDescription( const ::rtl::OUString& aDescription )
{
::rtl::OUString user;
sal_Int32 index;
sal_Int32 lastIndex = 0;
do
{
index = aDescription.indexOf((sal_Unicode) ',', lastIndex);
::rtl::OUString token = (index == -1) ? aDescription.copy(lastIndex) : aDescription.copy(lastIndex, index - lastIndex);
lastIndex = index + 1;
sal_Int32 eindex = token.indexOf((sal_Unicode)'=');
::rtl::OUString left = token.copy(0, eindex).toAsciiLowerCase().trim();
::rtl::OUString right = INetURLObject::decode( token.copy(eindex + 1).trim(), '%',
INetURLObject::DECODE_WITH_CHARSET );
if(left.equals(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user"))))
{
user = right;
break;
}
}
while(index != -1);
return user;
}
sal_Bool needSecurityRestrictions( void )
{
static sal_Bool bNeedInit = sal_True;
static sal_Bool bRetVal = sal_True;
if( bNeedInit )
{
bNeedInit = sal_False;
// Get system user to compare to portal user
oslSecurity aSecurity = osl_getCurrentSecurity();
::rtl::OUString aSystemUser;
sal_Bool bRet = osl_getUserName( aSecurity, &aSystemUser.pData );
if( !bRet )
{
// No valid security! -> Secure mode!
return sal_True;
}
Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
if( !xSMgr.is() )
return sal_True;
Reference< XBridgeFactory > xBridgeFac( xSMgr->createInstance
( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.bridge.BridgeFactory" )) ), UNO_QUERY );
Sequence< Reference< XBridge > > aBridgeSeq;
sal_Int32 nBridgeCount = 0;
if( xBridgeFac.is() )
{
aBridgeSeq = xBridgeFac->getExistingBridges();
nBridgeCount = aBridgeSeq.getLength();
}
if( nBridgeCount == 0 )
{
// No bridges -> local
bRetVal = sal_False;
return bRetVal;
}
// Iterate through all bridges to find (portal) user property
const Reference< XBridge >* pBridges = aBridgeSeq.getConstArray();
bRetVal = sal_False; // Now only sal_True if user different from portal user is found
sal_Int32 i;
for( i = 0 ; i < nBridgeCount ; i++ )
{
const Reference< XBridge >& rxBridge = pBridges[ i ];
::rtl::OUString aDescription = rxBridge->getDescription();
::rtl::OUString aPortalUser = findUserInDescription( aDescription );
if( !aPortalUser.isEmpty() )
{
// User Found, compare to system user
if( aPortalUser == aSystemUser )
{
// Same user -> system security is ok, bRetVal stays FALSE
break;
}
else
{
// Different user -> Secure mode!
bRetVal = sal_True;
break;
}
}
}
// No user found or PortalUser != SystemUser -> Secure mode! (Keep default value)
}
return bRetVal;
}
// Returns sal_True if UNO is available, otherwise the old file
// system implementation has to be used
// #89378 New semantic: Don't just ask for UNO but for UCB
sal_Bool hasUno( void )
{
static sal_Bool bNeedInit = sal_True;
static sal_Bool bRetVal = sal_True;
if( bNeedInit )
{
bNeedInit = sal_False;
Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
if( !xSMgr.is() )
{
// No service manager at all
bRetVal = sal_False;
}
else
{
Reference< XContentProviderManager > xManager( xSMgr->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
( "com.sun.star.ucb.UniversalContentBroker" )) ), UNO_QUERY );
if ( !( xManager.is() && xManager->queryContentProvider( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "file:///" )) ).is() ) )
{
// No UCB
bRetVal = sal_False;
}
}
}
return bRetVal;
}
class OslStream : public SvStream
{
osl::File maFile;
short mnStrmMode;
public:
OslStream( const String& rName, short nStrmMode );
~OslStream();
virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
virtual void FlushData();
virtual void SetSize( sal_uIntPtr nSize );
};
OslStream::OslStream( const String& rName, short nStrmMode )
: maFile( rName )
, mnStrmMode( nStrmMode )
{
sal_uInt32 nFlags;
if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
{
nFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
}
else if( nStrmMode & STREAM_WRITE )
{
nFlags = osl_File_OpenFlag_Write;
}
else //if( nStrmMode & STREAM_READ )
{
nFlags = osl_File_OpenFlag_Read;
}
osl::FileBase::RC nRet = maFile.open( nFlags );
if( nRet == osl::FileBase::E_NOENT && nFlags != osl_File_OpenFlag_Read )
{
nFlags |= osl_File_OpenFlag_Create;
nRet = maFile.open( nFlags );
}
if( nRet != osl::FileBase::E_None )
{
SetError( ERRCODE_IO_GENERAL );
}
}
OslStream::~OslStream()
{
maFile.close();
}
sal_uIntPtr OslStream::GetData( void* pData, sal_uIntPtr nSize )
{
sal_uInt64 nBytesRead = nSize;
maFile.read( pData, nBytesRead, nBytesRead );
return (sal_uIntPtr)nBytesRead;
}
sal_uIntPtr OslStream::PutData( const void* pData, sal_uIntPtr nSize )
{
sal_uInt64 nBytesWritten;
maFile.write( pData, (sal_uInt64)nSize, nBytesWritten );
return (sal_uIntPtr)nBytesWritten;
}
sal_uIntPtr OslStream::SeekPos( sal_uIntPtr nPos )
{
::osl::FileBase::RC rc = ::osl::FileBase::E_None;
if( nPos == STREAM_SEEK_TO_END )
rc = maFile.setPos( osl_Pos_End, 0 );
else
rc = maFile.setPos( osl_Pos_Absolut, (sal_uInt64)nPos );
OSL_VERIFY(rc == ::osl::FileBase::E_None);
sal_uInt64 nRealPos(0);
maFile.getPos( nRealPos );
return sal::static_int_cast<sal_uIntPtr>(nRealPos);
}
void OslStream::FlushData()
{
}
void OslStream::SetSize( sal_uIntPtr nSize )
{
maFile.setSize( (sal_uInt64)nSize );
}
class UCBStream : public SvStream
{
Reference< XInputStream > xIS;
Reference< XOutputStream > xOS;
Reference< XStream > xS;
Reference< XSeekable > xSeek;
public:
UCBStream( Reference< XInputStream > & xIS );
UCBStream( Reference< XOutputStream > & xOS );
UCBStream( Reference< XStream > & xS );
~UCBStream();
virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize );
virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
virtual void FlushData();
virtual void SetSize( sal_uIntPtr nSize );
};
UCBStream::UCBStream( Reference< XInputStream > & rStm )
: xIS( rStm )
, xSeek( rStm, UNO_QUERY )
{
}
UCBStream::UCBStream( Reference< XOutputStream > & rStm )
: xOS( rStm )
, xSeek( rStm, UNO_QUERY )
{
}
UCBStream::UCBStream( Reference< XStream > & rStm )
: xS( rStm )
, xSeek( rStm, UNO_QUERY )
{
}
UCBStream::~UCBStream()
{
try
{
if( xIS.is() )
xIS->closeInput();
else if( xOS.is() )
xOS->closeOutput();
else if( xS.is() )
{
Reference< XInputStream > xIS_ = xS->getInputStream();
if( xIS_.is() )
xIS_->closeInput();
}
}
catch(const Exception & )
{
SetError( ERRCODE_IO_GENERAL );
}
}
sal_uIntPtr UCBStream::GetData( void* pData, sal_uIntPtr nSize )
{
try
{
Reference< XInputStream > xISFromS;
if( xIS.is() )
{
Sequence<sal_Int8> aData;
nSize = xIS->readBytes( aData, nSize );
rtl_copyMemory( pData, aData.getConstArray(), nSize );
return nSize;
}
else if( xS.is() && (xISFromS = xS->getInputStream()).is() )
{
Sequence<sal_Int8> aData;
nSize = xISFromS->readBytes( aData, nSize );
rtl_copyMemory( pData, aData.getConstArray(), nSize );
return nSize;
}
else
SetError( ERRCODE_IO_GENERAL );
}
catch(const Exception & )
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
sal_uIntPtr UCBStream::PutData( const void* pData, sal_uIntPtr nSize )
{
try
{
Reference< XOutputStream > xOSFromS;
if( xOS.is() )
{
Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
xOS->writeBytes( aData );
return nSize;
}
else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
{
Sequence<sal_Int8> aData( (const sal_Int8 *)pData, nSize );
xOSFromS->writeBytes( aData );
return nSize;
}
else
SetError( ERRCODE_IO_GENERAL );
}
catch(const Exception & )
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
sal_uIntPtr UCBStream::SeekPos( sal_uIntPtr nPos )
{
try
{
if( xSeek.is() )
{
sal_uIntPtr nLen = sal::static_int_cast<sal_uIntPtr>( xSeek->getLength() );
if( nPos > nLen )
nPos = nLen;
xSeek->seek( nPos );
return nPos;
}
else
SetError( ERRCODE_IO_GENERAL );
}
catch(const Exception & )
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
void UCBStream::FlushData()
{
try
{
Reference< XOutputStream > xOSFromS;
if( xOS.is() )
xOS->flush();
else if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
xOSFromS->flush();
else
SetError( ERRCODE_IO_GENERAL );
}
catch(const Exception & )
{
SetError( ERRCODE_IO_GENERAL );
}
}
void UCBStream::SetSize( sal_uIntPtr nSize )
{
(void)nSize;
OSL_FAIL( "not allowed to call from basic" );
SetError( ERRCODE_IO_GENERAL );
}
SbError SbiStream::Open
( short nCh, const ByteString& rName, short nStrmMode, short nFlags, short nL )
{
nMode = nFlags;
nLen = nL;
nChan = nCh;
nLine = 0;
nExpandOnWriteTo = 0;
if( ( nStrmMode & ( STREAM_READ|STREAM_WRITE ) ) == STREAM_READ )
nStrmMode |= STREAM_NOCREATE;
String aStr( rName, osl_getThreadTextEncoding() );
String aNameStr = getFullPath( aStr );
if( hasUno() )
{
Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
if( xSMgr.is() )
{
Reference< XSimpleFileAccess >
xSFI( xSMgr->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" )) ), UNO_QUERY );
if( xSFI.is() )
{
try
{
// #??? For write access delete file if it already exists (not for appending)
if( (nStrmMode & STREAM_WRITE) != 0 && !IsAppend() && !IsBinary() &&
xSFI->exists( aNameStr ) && !xSFI->isFolder( aNameStr ) )
{
xSFI->kill( aNameStr );
}
if( (nStrmMode & (STREAM_READ | STREAM_WRITE)) == (STREAM_READ | STREAM_WRITE) )
{
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
pStrm = new UCBStream( xIS );
}
else if( nStrmMode & STREAM_WRITE )
{
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
pStrm = new UCBStream( xIS );
}
else //if( nStrmMode & STREAM_READ )
{
Reference< XInputStream > xIS = xSFI->openFileRead( aNameStr );
pStrm = new UCBStream( xIS );
}
}
catch(const Exception & )
{
nError = ERRCODE_IO_GENERAL;
}
}
}
}
if( !pStrm )
{
pStrm = new OslStream( aNameStr, nStrmMode );
}
if( IsAppend() )
pStrm->Seek( STREAM_SEEK_TO_END );
MapError();
if( nError )
delete pStrm, pStrm = NULL;
return nError;
}
SbError SbiStream::Close()
{
if( pStrm )
{
MapError();
delete pStrm;
pStrm = NULL;
}
nChan = 0;
return nError;
}
SbError SbiStream::Read( ByteString& rBuf, sal_uInt16 n, bool bForceReadingPerByte )
{
nExpandOnWriteTo = 0;
if( !bForceReadingPerByte && IsText() )
{
pStrm->ReadLine( rBuf );
nLine++;
}
else
{
if( !n )
n = nLen;
if( !n )
return nError = SbERR_BAD_RECORD_LENGTH;
rtl::OStringBuffer aBuffer(read_uInt8s_ToOString(*pStrm, n));
//Pad it out with ' ' to the requested length on short read
sal_Int32 nRequested = sal::static_int_cast<sal_Int32>(n);
comphelper::string::padToLength(aBuffer, nRequested, ' ');
rBuf = aBuffer.makeStringAndClear();
}
MapError();
if( !nError && pStrm->IsEof() )
nError = SbERR_READ_PAST_EOF;
return nError;
}
SbError SbiStream::Read( char& ch )
{
nExpandOnWriteTo = 0;
if( !aLine.Len() )
{
Read( aLine, 0 );
aLine += '\n';
}
ch = aLine.GetBuffer()[0];
aLine.Erase( 0, 1 );
return nError;
}
void SbiStream::ExpandFile()
{
if ( nExpandOnWriteTo )
{
sal_uIntPtr nCur = pStrm->Seek(STREAM_SEEK_TO_END);
if( nCur < nExpandOnWriteTo )
{
sal_uIntPtr nDiff = nExpandOnWriteTo - nCur;
char c = 0;
while( nDiff-- )
*pStrm << c;
}
else
{
pStrm->Seek( nExpandOnWriteTo );
}
nExpandOnWriteTo = 0;
}
}
SbError SbiStream::Write( const ByteString& rBuf, sal_uInt16 n )
{
ExpandFile();
if( IsAppend() )
pStrm->Seek( STREAM_SEEK_TO_END );
if( IsText() )
{
aLine += rBuf;
// Get it out, if the end is an LF, but strip CRLF before,
// because the SvStrm adds a CRLF!
sal_uInt16 nLineLen = aLine.Len();
if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0A )
{
aLine.Erase( nLineLen );
if( nLineLen && aLine.GetBuffer()[ --nLineLen ] == 0x0D )
aLine.Erase( nLineLen );
pStrm->WriteLines( aLine );
aLine.Erase();
}
}
else
{
if( !n ) n = nLen;
if( !n )
return nError = SbERR_BAD_RECORD_LENGTH;
pStrm->Write( rBuf.GetBuffer(), n );
MapError();
}
return nError;
}
SbiIoSystem* SbGetIoSystem()
{
SbiInstance* pInst = pINST;
return pInst ? pInst->GetIoSystem() : NULL;
}
SbiIoSystem::SbiIoSystem()
{
for( short i = 0; i < CHANNELS; i++ )
pChan[ i ] = NULL;
nChan = 0;
nError = 0;
}
SbiIoSystem::~SbiIoSystem()
{
Shutdown();
}
SbError SbiIoSystem::GetError()
{
SbError n = nError; nError = 0;
return n;
}
void SbiIoSystem::Open
( short nCh, const ByteString& rName, short nMode, short nFlags, short nLen )
{
nError = 0;
if( nCh >= CHANNELS || !nCh )
nError = SbERR_BAD_CHANNEL;
else if( pChan[ nCh ] )
nError = SbERR_FILE_ALREADY_OPEN;
else
{
pChan[ nCh ] = new SbiStream;
nError = pChan[ nCh ]->Open( nCh, rName, nMode, nFlags, nLen );
if( nError )
delete pChan[ nCh ], pChan[ nCh ] = NULL;
}
nChan = 0;
}
void SbiIoSystem::Close()
{
if( !nChan )
nError = SbERR_BAD_CHANNEL;
else if( !pChan[ nChan ] )
nError = SbERR_BAD_CHANNEL;
else
{
nError = pChan[ nChan ]->Close();
delete pChan[ nChan ];
pChan[ nChan ] = NULL;
}
nChan = 0;
}
void SbiIoSystem::Shutdown()
{
for( short i = 1; i < CHANNELS; i++ )
{
if( pChan[ i ] )
{
SbError n = pChan[ i ]->Close();
delete pChan[ i ];
pChan[ i ] = NULL;
if( n && !nError )
nError = n;
}
}
nChan = 0;
// anything left to PRINT?
if( aOut.Len() )
{
String aOutStr( aOut, osl_getThreadTextEncoding() );
#if defined GCC
Window* pParent = Application::GetDefDialogParent();
MessBox( pParent, WinBits( WB_OK ), String(), aOutStr ).Execute();
#else
MessBox( GetpApp()->GetDefDialogParent(), WinBits( WB_OK ), String(), aOutStr ).Execute();
#endif
}
aOut.Erase();
}
void SbiIoSystem::Read( ByteString& rBuf, short n )
{
if( !nChan )
ReadCon( rBuf );
else if( !pChan[ nChan ] )
nError = SbERR_BAD_CHANNEL;
else
nError = pChan[ nChan ]->Read( rBuf, n );
}
char SbiIoSystem::Read()
{
char ch = ' ';
if( !nChan )
{
if( !aIn.Len() )
{
ReadCon( aIn );
aIn += '\n';
}
ch = aIn.GetBuffer()[0];
aIn.Erase( 0, 1 );
}
else if( !pChan[ nChan ] )
nError = SbERR_BAD_CHANNEL;
else
nError = pChan[ nChan ]->Read( ch );
return ch;
}
void SbiIoSystem::Write( const ByteString& rBuf, short n )
{
if( !nChan )
WriteCon( rBuf );
else if( !pChan[ nChan ] )
nError = SbERR_BAD_CHANNEL;
else
nError = pChan[ nChan ]->Write( rBuf, n );
}
short SbiIoSystem::NextChannel()
{
for( short i = 1; i < CHANNELS; i++ )
{
if( !pChan[ i ] )
return i;
}
nError = SbERR_TOO_MANY_FILES;
return CHANNELS;
}
// nChannel == 0..CHANNELS-1
SbiStream* SbiIoSystem::GetStream( short nChannel ) const
{
SbiStream* pRet = 0;
if( nChannel >= 0 && nChannel < CHANNELS )
pRet = pChan[ nChannel ];
return pRet;
}
void SbiIoSystem::CloseAll(void)
{
for( short i = 1; i < CHANNELS; i++ )
{
if( pChan[ i ] )
{
SbError n = pChan[ i ]->Close();
delete pChan[ i ];
pChan[ i ] = NULL;
if( n && !nError )
nError = n;
}
}
}
/***************************************************************************
*
* Console Support
*
***************************************************************************/
void SbiIoSystem::ReadCon( ByteString& rIn )
{
String aPromptStr( aPrompt, osl_getThreadTextEncoding() );
SbiInputDialog aDlg( NULL, aPromptStr );
if( aDlg.Execute() )
rIn = rtl::OUStringToOString(aDlg.GetInput(), osl_getThreadTextEncoding());
else
nError = SbERR_USER_ABORT;
aPrompt.Erase();
}
// output of a MessageBox, if theres a CR in the console-buffer
void SbiIoSystem::WriteCon( const ByteString& rText )
{
aOut += rText;
sal_uInt16 n1 = aOut.Search( '\n' );
sal_uInt16 n2 = aOut.Search( '\r' );
if( n1 != STRING_NOTFOUND || n2 != STRING_NOTFOUND )
{
if( n1 == STRING_NOTFOUND ) n1 = n2;
else
if( n2 == STRING_NOTFOUND ) n2 = n1;
if( n1 > n2 ) n1 = n2;
ByteString s( aOut.Copy( 0, n1 ) );
aOut.Erase( 0, n1 );
while( aOut.GetBuffer()[0] == '\n' || aOut.GetBuffer()[0] == '\r' )
aOut.Erase( 0, 1 );
String aStr( s, osl_getThreadTextEncoding() );
{
SolarMutexGuard aSolarGuard;
if( !MessBox( GetpApp()->GetDefDialogParent(),
WinBits( WB_OK_CANCEL | WB_DEF_OK ),
String(), aStr ).Execute() )
nError = SbERR_USER_ABORT;
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|