Files
libreoffice/basic/source/runtime/iosys.cxx

850 lines
20 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 15:18:56 +00:00
#include <string.h>
2000-09-18 15:18:56 +00:00
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <vcl/window.hxx>
2000-09-26 08:02:02 +00:00
#include <osl/file.hxx>
#include <tools/urlobj.hxx>
#include <runtime.hxx>
2000-09-18 15:18:56 +00:00
#include <rtl/byteseq.hxx>
#include <rtl/textenc.h>
2011-09-15 09:44:06 +01:00
#include <rtl/strbuf.hxx>
2000-09-18 15:18:56 +00:00
#include <rtl/ustrbuf.hxx>
#include <sal/log.hxx>
2011-03-22 12:23:36 +01:00
2000-11-02 11:03:49 +00:00
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
2000-09-18 15:18:56 +00:00
#include <com/sun/star/bridge/BridgeFactory.hpp>
#include <com/sun/star/bridge/XBridge.hpp>
2000-09-18 15:18:56 +00:00
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/ucb/UniversalContentBroker.hpp>
#include <com/sun/star/ucb/XContentProvider.hpp>
#include <com/sun/star/ucb/XContentProviderManager.hpp>
2000-09-18 15:18:56 +00:00
#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>
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>
2000-09-18 15:18:56 +00:00
class SbiInputDialog : public weld::GenericDialogController
{
std::unique_ptr<weld::Entry> m_xInput;
std::unique_ptr<weld::Button> m_xOk;
std::unique_ptr<weld::Button> m_xCancel;
std::unique_ptr<weld::Label> m_xPromptText;
OUString m_aText;
DECL_LINK(Ok, weld::Button&, void);
DECL_LINK(Cancel, weld::Button&, void);
2000-09-18 15:18:56 +00:00
public:
SbiInputDialog(weld::Window*, const OUString&);
const OUString& GetInput() { return m_aText; }
2000-09-18 15:18:56 +00:00
};
SbiInputDialog::SbiInputDialog(weld::Window* pParent, const OUString& rPrompt)
: GenericDialogController(pParent, "svt/ui/inputbox.ui", "InputBox")
, m_xInput(m_xBuilder->weld_entry("entry"))
, m_xOk(m_xBuilder->weld_button("ok"))
, m_xCancel(m_xBuilder->weld_button("cancel"))
, m_xPromptText(m_xBuilder->weld_label("prompt"))
{
m_xDialog->set_title(rPrompt);
m_xPromptText->set_label(rPrompt);
m_xOk->connect_clicked( LINK( this, SbiInputDialog, Ok ) );
m_xCancel->connect_clicked( LINK( this, SbiInputDialog, Cancel ) );
2000-09-18 15:18:56 +00:00
}
IMPL_LINK_NOARG( SbiInputDialog, Ok, weld::Button&, void )
2000-09-18 15:18:56 +00:00
{
m_aText = m_xInput->get_text();
m_xDialog->response(RET_OK);
2000-09-18 15:18:56 +00:00
}
IMPL_LINK_NOARG( SbiInputDialog, Cancel, weld::Button&, void )
2000-09-18 15:18:56 +00:00
{
m_xDialog->response(RET_CANCEL);
2000-09-18 15:18:56 +00:00
}
SbiStream::SbiStream()
: nExpandOnWriteTo(0)
, nLine(0)
, nLen(0)
, nMode(SbiStreamFlags::NONE)
, nError(0)
2000-09-18 15:18:56 +00:00
{
}
SbiStream::~SbiStream()
{
}
// map an SvStream-error to StarBASIC-code
2000-09-18 15:18:56 +00:00
void SbiStream::MapError()
{
if( pStrm )
{
ErrCode nEC = pStrm->GetError();
if (nEC == ERRCODE_NONE)
nError = ERRCODE_NONE;
else if (nEC == SVSTREAM_FILE_NOT_FOUND)
nError = ERRCODE_BASIC_FILE_NOT_FOUND;
else if (nEC ==SVSTREAM_PATH_NOT_FOUND)
nError = ERRCODE_BASIC_PATH_NOT_FOUND;
else if (nEC ==SVSTREAM_TOO_MANY_OPEN_FILES)
nError = ERRCODE_BASIC_TOO_MANY_FILES;
else if (nEC ==SVSTREAM_ACCESS_DENIED)
nError = ERRCODE_BASIC_ACCESS_DENIED;
else if (nEC ==SVSTREAM_INVALID_PARAMETER)
nError = ERRCODE_BASIC_BAD_ARGUMENT;
else if (nEC ==SVSTREAM_OUTOFMEMORY)
nError = ERRCODE_BASIC_NO_MEMORY;
else
nError = ERRCODE_BASIC_IO_ERROR;
2000-09-18 15:18:56 +00:00
}
}
// 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
bool hasUno()
2000-09-18 15:18:56 +00:00
{
static bool bNeedInit = true;
static bool bRetVal = true;
2000-09-18 15:18:56 +00:00
if( bNeedInit )
{
bNeedInit = false;
Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
if( !xContext.is() )
{
// No service manager at all
bRetVal = false;
}
else
{
Reference< XUniversalContentBroker > xManager = UniversalContentBroker::create(xContext);
if ( !( xManager->queryContentProvider( "file:///" ).is() ) )
{
// No UCB
bRetVal = false;
}
}
2000-09-18 15:18:56 +00:00
}
return bRetVal;
}
2000-09-26 08:02:02 +00:00
class OslStream : public SvStream
{
osl::File maFile;
2000-09-26 08:02:02 +00:00
public:
OslStream( const OUString& rName, StreamMode nStrmMode );
virtual ~OslStream() override;
virtual std::size_t GetData(void* pData, std::size_t nSize) override;
virtual std::size_t PutData(const void* pData, std::size_t nSize) override;
virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
virtual void FlushData() override;
virtual void SetSize( sal_uInt64 nSize) override;
2000-09-26 08:02:02 +00:00
};
OslStream::OslStream( const OUString& rName, StreamMode nStrmMode )
2000-09-26 08:02:02 +00:00
: maFile( rName )
{
sal_uInt32 nFlags;
if( (nStrmMode & (StreamMode::READ | StreamMode::WRITE)) == (StreamMode::READ | StreamMode::WRITE) )
2000-09-26 08:02:02 +00:00
{
2010-12-11 18:02:43 +00:00
nFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
2000-09-26 08:02:02 +00:00
}
else if( nStrmMode & StreamMode::WRITE )
2000-09-26 08:02:02 +00:00
{
2010-12-11 18:02:43 +00:00
nFlags = osl_File_OpenFlag_Write;
2000-09-26 08:02:02 +00:00
}
else //if( nStrmMode & StreamMode::READ )
2000-09-26 08:02:02 +00:00
{
2010-12-11 18:02:43 +00:00
nFlags = osl_File_OpenFlag_Read;
2000-09-26 08:02:02 +00:00
}
osl::FileBase::RC nRet = maFile.open( nFlags );
2010-12-11 18:02:43 +00:00
if( nRet == osl::FileBase::E_NOENT && nFlags != osl_File_OpenFlag_Read )
2000-09-26 08:02:02 +00:00
{
2010-12-11 18:02:43 +00:00
nFlags |= osl_File_OpenFlag_Create;
2000-09-26 08:02:02 +00:00
nRet = maFile.open( nFlags );
}
if( nRet != osl::FileBase::E_None )
2000-09-26 08:02:02 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
}
OslStream::~OslStream()
{
maFile.close();
2000-09-26 08:02:02 +00:00
}
std::size_t OslStream::GetData(void* pData, std::size_t nSize)
2000-09-26 08:02:02 +00:00
{
sal_uInt64 nBytesRead = nSize;
2011-01-23 18:29:24 +00:00
maFile.read( pData, nBytesRead, nBytesRead );
return nBytesRead;
2000-09-26 08:02:02 +00:00
}
std::size_t OslStream::PutData(const void* pData, std::size_t nSize)
2000-09-26 08:02:02 +00:00
{
sal_uInt64 nBytesWritten;
maFile.write( pData, nSize, nBytesWritten );
return nBytesWritten;
2000-09-26 08:02:02 +00:00
}
sal_uInt64 OslStream::SeekPos( sal_uInt64 nPos )
2000-09-26 08:02:02 +00:00
{
2011-07-21 13:14:21 +01:00
::osl::FileBase::RC rc = ::osl::FileBase::E_None;
// check if a truncated STREAM_SEEK_TO_END was passed
assert(nPos != SAL_MAX_UINT32);
2000-09-26 08:02:02 +00:00
if( nPos == STREAM_SEEK_TO_END )
{
2011-07-21 13:14:21 +01:00
rc = maFile.setPos( osl_Pos_End, 0 );
}
2000-09-26 08:02:02 +00:00
else
{
rc = maFile.setPos( osl_Pos_Absolut, nPos );
}
2011-07-31 22:39:22 +01:00
OSL_VERIFY(rc == ::osl::FileBase::E_None);
2011-01-23 18:29:24 +00:00
sal_uInt64 nRealPos(0);
maFile.getPos( nRealPos );
return nRealPos;
2000-09-26 08:02:02 +00:00
}
void OslStream::FlushData()
{
}
void OslStream::SetSize( sal_uInt64 nSize )
2000-09-26 08:02:02 +00:00
{
maFile.setSize( nSize );
2000-09-26 08:02:02 +00:00
}
2000-09-18 15:18:56 +00:00
class UCBStream : public SvStream
{
Reference< XInputStream > xIS;
Reference< XStream > xS;
Reference< XSeekable > xSeek;
public:
explicit UCBStream( Reference< XInputStream > const & xIS );
explicit UCBStream( Reference< XStream > const & xS );
virtual ~UCBStream() override;
virtual std::size_t GetData( void* pData, std::size_t nSize ) override;
virtual std::size_t PutData( const void* pData, std::size_t nSize ) override;
virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
virtual void FlushData() override;
virtual void SetSize( sal_uInt64 nSize ) override;
2000-09-18 15:18:56 +00:00
};
UCBStream::UCBStream( Reference< XInputStream > const & rStm )
2000-09-18 15:18:56 +00:00
: xIS( rStm )
, xSeek( rStm, UNO_QUERY )
{
}
UCBStream::UCBStream( Reference< XStream > const & rStm )
2000-09-18 15:18:56 +00:00
: xS( rStm )
, xSeek( rStm, UNO_QUERY )
{
}
UCBStream::~UCBStream()
{
try
{
if( xIS.is() )
{
2000-09-18 15:18:56 +00:00
xIS->closeInput();
}
2000-09-18 15:18:56 +00:00
else if( xS.is() )
2000-10-19 07:36:50 +00:00
{
Reference< XInputStream > xIS_ = xS->getInputStream();
if( xIS_.is() )
{
xIS_->closeInput();
}
2000-10-19 07:36:50 +00:00
}
2000-09-18 15:18:56 +00:00
}
catch(const Exception & )
2000-09-18 15:18:56 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
}
std::size_t UCBStream::GetData(void* pData, std::size_t nSize)
2000-09-18 15:18:56 +00:00
{
try
{
2000-10-19 07:36:50 +00:00
Reference< XInputStream > xISFromS;
2000-09-18 15:18:56 +00:00
if( xIS.is() )
{
Sequence<sal_Int8> aData;
nSize = xIS->readBytes( aData, nSize );
memcpy( pData, aData.getConstArray(), nSize );
2000-09-18 15:18:56 +00:00
return nSize;
}
2000-10-19 07:36:50 +00:00
else if( xS.is() && (xISFromS = xS->getInputStream()).is() )
2000-09-18 15:18:56 +00:00
{
Sequence<sal_Int8> aData;
2000-10-19 07:36:50 +00:00
nSize = xISFromS->readBytes( aData, nSize );
memcpy(pData, aData.getConstArray(), nSize );
2000-09-18 15:18:56 +00:00
return nSize;
}
else
{
2000-09-18 15:18:56 +00:00
SetError( ERRCODE_IO_GENERAL );
}
2000-09-18 15:18:56 +00:00
}
catch(const Exception & )
2000-09-18 15:18:56 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
std::size_t UCBStream::PutData(const void* pData, std::size_t nSize)
2000-09-18 15:18:56 +00:00
{
try
{
2000-10-19 07:36:50 +00:00
Reference< XOutputStream > xOSFromS;
2012-03-30 20:52:06 +01:00
if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
2000-09-18 15:18:56 +00:00
{
Sequence<sal_Int8> aData( static_cast<const sal_Int8 *>(pData), nSize );
2000-10-19 07:36:50 +00:00
xOSFromS->writeBytes( aData );
2000-09-18 15:18:56 +00:00
return nSize;
}
else
{
2000-09-18 15:18:56 +00:00
SetError( ERRCODE_IO_GENERAL );
}
2000-09-18 15:18:56 +00:00
}
catch(const Exception & )
2000-09-18 15:18:56 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
sal_uInt64 UCBStream::SeekPos( sal_uInt64 nPos )
2000-09-18 15:18:56 +00:00
{
try
{
if( xSeek.is() )
{
sal_uInt64 nLen = static_cast<sal_uInt64>( xSeek->getLength() );
2000-09-18 15:18:56 +00:00
if( nPos > nLen )
{
2000-09-18 15:18:56 +00:00
nPos = nLen;
}
2000-09-18 15:18:56 +00:00
xSeek->seek( nPos );
return nPos;
}
else
{
2000-09-18 15:18:56 +00:00
SetError( ERRCODE_IO_GENERAL );
}
2000-09-18 15:18:56 +00:00
}
catch(const Exception & )
2000-09-18 15:18:56 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
return 0;
}
void UCBStream::FlushData()
2000-09-18 15:18:56 +00:00
{
try
{
2000-10-19 07:36:50 +00:00
Reference< XOutputStream > xOSFromS;
2012-03-30 20:52:06 +01:00
if( xS.is() && (xOSFromS = xS->getOutputStream()).is() )
{
2000-10-19 07:36:50 +00:00
xOSFromS->flush();
}
2000-09-18 15:18:56 +00:00
else
{
2000-09-18 15:18:56 +00:00
SetError( ERRCODE_IO_GENERAL );
}
2000-09-18 15:18:56 +00:00
}
catch(const Exception & )
2000-09-18 15:18:56 +00:00
{
SetError( ERRCODE_IO_GENERAL );
}
}
void UCBStream::SetSize( sal_uInt64 )
2000-09-18 15:18:56 +00:00
{
SAL_WARN("basic", "UCBStream::SetSize not allowed to call from basic" );
2000-09-18 15:18:56 +00:00
SetError( ERRCODE_IO_GENERAL );
}
ErrCode const & SbiStream::Open
( const OString& rName, StreamMode nStrmMode, SbiStreamFlags nFlags, short nL )
2000-09-18 15:18:56 +00:00
{
nMode = nFlags;
nLen = nL;
nLine = 0;
nExpandOnWriteTo = 0;
if( ( nStrmMode & ( StreamMode::READ|StreamMode::WRITE ) ) == StreamMode::READ )
{
nStrmMode |= StreamMode::NOCREATE;
}
OUString aStr(OStringToOUString(rName, osl_getThreadTextEncoding()));
OUString aNameStr = getFullPath( aStr );
2000-09-18 15:18:56 +00:00
if( hasUno() )
{
Reference< XSimpleFileAccess3 > xSFI( SimpleFileAccess::create( comphelper::getProcessComponentContext() ) );
try
2000-09-18 15:18:56 +00:00
{
// #??? For write access delete file if it already exists (not for appending)
if( (nStrmMode & StreamMode::WRITE) && !IsAppend() && !IsBinary() && !IsRandom() &&
xSFI->exists( aNameStr ) && !xSFI->isFolder( aNameStr ) )
{
xSFI->kill( aNameStr );
}
2000-09-18 15:18:56 +00:00
if( (nStrmMode & (StreamMode::READ | StreamMode::WRITE)) == (StreamMode::READ | StreamMode::WRITE) )
{
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
pStrm.reset( new UCBStream( xIS ) );
}
else if( nStrmMode & StreamMode::WRITE )
{
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr );
pStrm.reset( new UCBStream( xIS ) );
}
else //if( nStrmMode & StreamMode::READ )
{
Reference< XInputStream > xIS = xSFI->openFileRead( aNameStr );
pStrm.reset( new UCBStream( xIS ) );
}
}
catch(const Exception & )
{
nError = ERRCODE_IO_GENERAL;
2000-09-18 15:18:56 +00:00
}
}
if( !pStrm )
2000-09-26 08:02:02 +00:00
{
pStrm.reset( new OslStream( aNameStr, nStrmMode ) );
2000-09-26 08:02:02 +00:00
}
2000-09-18 15:18:56 +00:00
if( IsAppend() )
{
2000-09-18 15:18:56 +00:00
pStrm->Seek( STREAM_SEEK_TO_END );
}
2000-09-18 15:18:56 +00:00
MapError();
if( nError )
{
pStrm.reset();
}
2000-09-18 15:18:56 +00:00
return nError;
}
ErrCode const & SbiStream::Close()
2000-09-18 15:18:56 +00:00
{
if( pStrm )
{
MapError();
pStrm.reset();
2000-09-18 15:18:56 +00:00
}
return nError;
}
ErrCode SbiStream::Read(OString& rBuf, sal_uInt16 n, bool bForceReadingPerByte)
2000-09-18 15:18:56 +00:00
{
nExpandOnWriteTo = 0;
if( !bForceReadingPerByte && IsText() )
2000-09-18 15:18:56 +00:00
{
2012-01-27 20:27:18 +00:00
pStrm->ReadLine(rBuf);
2000-09-18 15:18:56 +00:00
nLine++;
}
else
{
2011-09-15 09:44:06 +01:00
if( !n )
{
2011-09-15 09:44:06 +01:00
n = nLen;
}
2000-09-18 15:18:56 +00:00
if( !n )
{
return nError = ERRCODE_BASIC_BAD_RECORD_LENGTH;
}
OStringBuffer aBuffer(read_uInt8s_ToOString(*pStrm, n));
2011-09-15 09:44:06 +01:00
//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, ' ');
2011-09-15 09:44:06 +01:00
rBuf = aBuffer.makeStringAndClear();
2000-09-18 15:18:56 +00:00
}
MapError();
if( !nError && pStrm->eof() )
{
nError = ERRCODE_BASIC_READ_PAST_EOF;
}
2000-09-18 15:18:56 +00:00
return nError;
}
ErrCode const & SbiStream::Read( char& ch )
2000-09-18 15:18:56 +00:00
{
nExpandOnWriteTo = 0;
2012-01-27 20:27:18 +00:00
if (aLine.isEmpty())
2000-09-18 15:18:56 +00:00
{
Read( aLine );
aLine = aLine + OString('\n');
2000-09-18 15:18:56 +00:00
}
2012-01-27 20:27:18 +00:00
ch = aLine[0];
aLine = aLine.copy(1);
2000-09-18 15:18:56 +00:00
return nError;
}
void SbiStream::ExpandFile()
{
if ( nExpandOnWriteTo )
{
sal_uInt64 nCur = pStrm->Seek(STREAM_SEEK_TO_END);
2000-09-18 15:18:56 +00:00
if( nCur < nExpandOnWriteTo )
{
sal_uInt64 nDiff = nExpandOnWriteTo - nCur;
2000-09-18 15:18:56 +00:00
while( nDiff-- )
{
pStrm->WriteChar( 0 );
}
2000-09-18 15:18:56 +00:00
}
else
{
pStrm->Seek( nExpandOnWriteTo );
}
nExpandOnWriteTo = 0;
}
}
namespace
{
void WriteLines(SvStream &rStream, const OString& rStr)
{
OString aStr(convertLineEnd(rStr, rStream.GetLineDelimiter()) );
write_uInt8s_FromOString(rStream, aStr);
endl( rStream );
}
}
ErrCode SbiStream::Write( const OString& rBuf )
2000-09-18 15:18:56 +00:00
{
ExpandFile();
if( IsAppend() )
{
2000-09-18 15:18:56 +00:00
pStrm->Seek( STREAM_SEEK_TO_END );
}
2000-09-18 15:18:56 +00:00
if( IsText() )
{
2012-01-27 20:27:18 +00:00
aLine = aLine + rBuf;
// Get it out, if the end is an LF, but strip CRLF before,
// because the SvStream adds a CRLF!
2012-01-27 20:27:18 +00:00
sal_Int32 nLineLen = aLine.getLength();
if (nLineLen && aLine[--nLineLen] == 0x0A)
2000-09-18 15:18:56 +00:00
{
2012-01-27 20:27:18 +00:00
aLine = aLine.copy(0, nLineLen);
if (nLineLen && aLine[--nLineLen] == 0x0D)
{
2012-01-27 20:27:18 +00:00
aLine = aLine.copy(0, nLineLen);
}
WriteLines(*pStrm, aLine);
aLine.clear();
2000-09-18 15:18:56 +00:00
}
}
else
{
if( !nLen )
{
return nError = ERRCODE_BASIC_BAD_RECORD_LENGTH;
}
pStrm->WriteBytes(rBuf.getStr(), nLen);
2000-09-18 15:18:56 +00:00
MapError();
}
return nError;
}
SbiIoSystem::SbiIoSystem()
{
for(SbiStream* & i : pChan)
{
i = nullptr;
}
2000-09-18 15:18:56 +00:00
nChan = 0;
nError = ERRCODE_NONE;
2000-09-18 15:18:56 +00:00
}
SbiIoSystem::~SbiIoSystem() COVERITY_NOEXCEPT_FALSE
2000-09-18 15:18:56 +00:00
{
Shutdown();
}
ErrCode SbiIoSystem::GetError()
2000-09-18 15:18:56 +00:00
{
ErrCode n = nError;
nError = ERRCODE_NONE;
2000-09-18 15:18:56 +00:00
return n;
}
void SbiIoSystem::Open(short nCh, const OString& rName, StreamMode nMode, SbiStreamFlags nFlags, short nLen)
2000-09-18 15:18:56 +00:00
{
nError = ERRCODE_NONE;
2000-09-18 15:18:56 +00:00
if( nCh >= CHANNELS || !nCh )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else if( pChan[ nCh ] )
{
nError = ERRCODE_BASIC_FILE_ALREADY_OPEN;
}
2000-09-18 15:18:56 +00:00
else
{
pChan[ nCh ] = new SbiStream;
nError = pChan[ nCh ]->Open( rName, nMode, nFlags, nLen );
if( nError )
{
delete pChan[ nCh ];
pChan[ nCh ] = nullptr;
}
2000-09-18 15:18:56 +00:00
}
nChan = 0;
}
void SbiIoSystem::Close()
{
if( !nChan )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else if( !pChan[ nChan ] )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else
{
nError = pChan[ nChan ]->Close();
delete pChan[ nChan ];
pChan[ nChan ] = nullptr;
2000-09-18 15:18:56 +00:00
}
nChan = 0;
}
void SbiIoSystem::Shutdown()
{
for( short i = 1; i < CHANNELS; i++ )
{
if( pChan[ i ] )
{
ErrCode n = pChan[ i ]->Close();
2000-09-18 15:18:56 +00:00
delete pChan[ i ];
pChan[ i ] = nullptr;
2000-09-18 15:18:56 +00:00
if( n && !nError )
{
2000-09-18 15:18:56 +00:00
nError = n;
}
2000-09-18 15:18:56 +00:00
}
}
nChan = 0;
// anything left to PRINT?
2012-01-27 20:27:18 +00:00
if( !aOut.isEmpty() )
2000-09-18 15:18:56 +00:00
{
vcl::Window* pParent = Application::GetDefDialogParent();
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent ? pParent->GetFrameWeld() : nullptr, VclMessageType::Warning,
VclButtonsType::Ok, aOut));
xBox->run();
2000-09-18 15:18:56 +00:00
}
aOut.clear();
2000-09-18 15:18:56 +00:00
}
void SbiIoSystem::Read(OString& rBuf)
2000-09-18 15:18:56 +00:00
{
if( !nChan )
{
2000-09-18 15:18:56 +00:00
ReadCon( rBuf );
}
2000-09-18 15:18:56 +00:00
else if( !pChan[ nChan ] )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else
{
nError = pChan[ nChan ]->Read( rBuf );
}
2000-09-18 15:18:56 +00:00
}
char SbiIoSystem::Read()
{
char ch = ' ';
if( !nChan )
{
2012-01-27 20:27:18 +00:00
if( aIn.isEmpty() )
2000-09-18 15:18:56 +00:00
{
ReadCon( aIn );
aIn = aIn + OString('\n');
2000-09-18 15:18:56 +00:00
}
2012-01-27 20:27:18 +00:00
ch = aIn[0];
aIn = aIn.copy(1);
2000-09-18 15:18:56 +00:00
}
else if( !pChan[ nChan ] )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else
{
2000-09-18 15:18:56 +00:00
nError = pChan[ nChan ]->Read( ch );
}
2000-09-18 15:18:56 +00:00
return ch;
}
void SbiIoSystem::Write(const OUString& rBuf)
2000-09-18 15:18:56 +00:00
{
if( !nChan )
{
2000-09-18 15:18:56 +00:00
WriteCon( rBuf );
}
2000-09-18 15:18:56 +00:00
else if( !pChan[ nChan ] )
{
nError = ERRCODE_BASIC_BAD_CHANNEL;
}
2000-09-18 15:18:56 +00:00
else
{
nError = pChan[ nChan ]->Write( OUStringToOString(rBuf, osl_getThreadTextEncoding()) );
}
2000-09-18 15:18:56 +00:00
}
// nChannel == 0..CHANNELS-1
SbiStream* SbiIoSystem::GetStream( short nChannel ) const
{
SbiStream* pRet = nullptr;
2000-09-18 15:18:56 +00:00
if( nChannel >= 0 && nChannel < CHANNELS )
{
2000-09-18 15:18:56 +00:00
pRet = pChan[ nChannel ];
}
2000-09-18 15:18:56 +00:00
return pRet;
}
void SbiIoSystem::CloseAll()
2000-09-18 15:18:56 +00:00
{
for( short i = 1; i < CHANNELS; i++ )
{
if( pChan[ i ] )
{
ErrCode n = pChan[ i ]->Close();
2000-09-18 15:18:56 +00:00
delete pChan[ i ];
pChan[ i ] = nullptr;
2000-09-18 15:18:56 +00:00
if( n && !nError )
{
2000-09-18 15:18:56 +00:00
nError = n;
}
2000-09-18 15:18:56 +00:00
}
}
}
void SbiIoSystem::ReadCon(OString& rIn)
2000-09-18 15:18:56 +00:00
{
OUString aPromptStr(OStringToOUString(aPrompt, osl_getThreadTextEncoding()));
SbiInputDialog aDlg(nullptr, aPromptStr);
if (aDlg.run() == RET_OK)
{
rIn = OUStringToOString(aDlg.GetInput(), osl_getThreadTextEncoding());
}
2000-09-18 15:18:56 +00:00
else
{
nError = ERRCODE_BASIC_USER_ABORT;
}
aPrompt.clear();
2000-09-18 15:18:56 +00:00
}
// output of a MessageBox, if there's a CR in the console-buffer
2000-09-18 15:18:56 +00:00
void SbiIoSystem::WriteCon(const OUString& rText)
2000-09-18 15:18:56 +00:00
{
aOut += rText;
2012-01-27 20:27:18 +00:00
sal_Int32 n1 = aOut.indexOf('\n');
sal_Int32 n2 = aOut.indexOf('\r');
if( n1 != -1 || n2 != -1 )
{
if( n1 == -1 )
{
2012-01-27 20:27:18 +00:00
n1 = n2;
}
2012-01-27 20:27:18 +00:00
else if( n2 == -1 )
{
2012-01-27 20:27:18 +00:00
n2 = n1;
}
2012-01-27 20:27:18 +00:00
if( n1 > n2 )
{
2012-01-27 20:27:18 +00:00
n1 = n2;
}
OUString s(aOut.copy(0, n1));
2012-01-27 20:27:18 +00:00
aOut = aOut.copy(n1);
while ( !aOut.isEmpty() && (aOut[0] == '\n' || aOut[0] == '\r') )
{
2012-01-27 20:27:18 +00:00
aOut = aOut.copy(1);
}
{
SolarMutexGuard aSolarGuard;
vcl::Window* pParent = Application::GetDefDialogParent();
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent ? pParent->GetFrameWeld() : nullptr, VclMessageType::Warning,
VclButtonsType::OkCancel, s));
xBox->set_default_response(RET_OK);
if (!xBox->run())
{
nError = ERRCODE_BASIC_USER_ABORT;
}
}
2000-09-18 15:18:56 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */