| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
#include <vcl/svapp.hxx> |
| 22 |
#include <vcl/weld.hxx> |
| 23 |
#include <osl/diagnose.h> |
| 24 |
#include <osl/file.hxx> |
| 25 |
|
| 26 |
#include <runtime.hxx> |
| 27 |
|
| 28 |
#include <rtl/strbuf.hxx> |
| 29 |
#include <sal/log.hxx> |
| 30 |
|
| 31 |
#include <comphelper/processfactory.hxx> |
| 32 |
#include <comphelper/string.hxx> |
| 33 |
|
| 34 |
#include <com/sun/star/uno/Sequence.hxx> |
| 35 |
#include <com/sun/star/ucb/SimpleFileAccess.hpp> |
| 36 |
#include <com/sun/star/ucb/UniversalContentBroker.hpp> |
| 37 |
#include <com/sun/star/io/XInputStream.hpp> |
| 38 |
#include <com/sun/star/io/XOutputStream.hpp> |
| 39 |
#include <com/sun/star/io/XStream.hpp> |
| 40 |
#include <com/sun/star/io/XSeekable.hpp> |
| 41 |
#include <iosys.hxx> |
| 42 |
|
| 43 |
using namespace com::sun::star::uno; |
| 44 |
using namespace com::sun::star::ucb; |
| 45 |
using namespace com::sun::star::io; |
| 46 |
|
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
class SbiInputDialog : public weld::GenericDialogController |
| 51 |
{ |
| 52 |
std::unique_ptr<weld::Entry> m_xInput; |
| 53 |
std::unique_ptr<weld::Button> m_xOk; |
| 54 |
std::unique_ptr<weld::Button> m_xCancel; |
| 55 |
std::unique_ptr<weld::Label> m_xPromptText; |
| 56 |
OUString m_aText; |
| 57 |
DECL_LINK(Ok, weld::Button&, void); |
| 58 |
DECL_LINK(Cancel, weld::Button&, void); |
| 59 |
public: |
| 60 |
SbiInputDialog(weld::Window*, const OUString&); |
| 61 |
const OUString& GetInput() const { return m_aText; } |
| 62 |
}; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
SbiInputDialog::SbiInputDialog(weld::Window* pParent, const OUString& rPrompt) |
| 67 |
: GenericDialogController(pParent, u"svt/ui/inputbox.ui"_ustr, u"InputBox"_ustr) |
| 68 |
, m_xInput(m_xBuilder->weld_entry(u"entry"_ustr)) |
| 69 |
, m_xOk(m_xBuilder->weld_button(u"ok"_ustr)) |
| 70 |
, m_xCancel(m_xBuilder->weld_button(u"cancel"_ustr)) |
| 71 |
, m_xPromptText(m_xBuilder->weld_label(u"prompt"_ustr)) |
| 72 |
{ |
| 73 |
m_xDialog->set_title(rPrompt); |
| 74 |
m_xPromptText->set_label(rPrompt); |
| 75 |
m_xOk->connect_clicked( LINK( this, SbiInputDialog, Ok ) ); |
| 76 |
m_xCancel->connect_clicked( LINK( this, SbiInputDialog, Cancel ) ); |
| 77 |
} |
| 78 |
|
| 79 |
IMPL_LINK_NOARG( SbiInputDialog, Ok, weld::Button&, void ) |
| 80 |
{ |
| 81 |
m_aText = m_xInput->get_text(); |
| 82 |
m_xDialog->response(RET_OK); |
| 83 |
} |
| 84 |
|
| 85 |
IMPL_LINK_NOARG( SbiInputDialog, Cancel, weld::Button&, void ) |
| 86 |
{ |
| 87 |
m_xDialog->response(RET_CANCEL); |
| 88 |
} |
| 89 |
|
| 90 |
SbiStream::SbiStream() |
| 91 |
: nExpandOnWriteTo(0) |
| 92 |
, nLine(0) |
| 93 |
, nLen(0) |
| 94 |
, nMode(SbiStreamFlags::NONE) |
| 95 |
, nError(0) |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
SbiStream::~SbiStream() |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
// map an SvStream-error to StarBASIC-code |
| 104 |
|
| 105 |
void SbiStream::MapError() |
| 106 |
{ |
| 107 |
if( !pStrm ) |
| 108 |
return; |
| 109 |
|
| 110 |
ErrCode nEC = pStrm->GetError(); |
| 111 |
if (nEC == ERRCODE_NONE) |
| 112 |
nError = ERRCODE_NONE; |
| 113 |
else if (nEC == SVSTREAM_FILE_NOT_FOUND) |
| 114 |
nError = ERRCODE_BASIC_FILE_NOT_FOUND; |
| 115 |
else if (nEC ==SVSTREAM_PATH_NOT_FOUND) |
| 116 |
nError = ERRCODE_BASIC_PATH_NOT_FOUND; |
| 117 |
else if (nEC ==SVSTREAM_TOO_MANY_OPEN_FILES) |
| 118 |
nError = ERRCODE_BASIC_TOO_MANY_FILES; |
| 119 |
else if (nEC ==SVSTREAM_ACCESS_DENIED) |
| 120 |
nError = ERRCODE_BASIC_ACCESS_DENIED; |
| 121 |
else if (nEC ==SVSTREAM_INVALID_PARAMETER) |
| 122 |
nError = ERRCODE_BASIC_BAD_ARGUMENT; |
| 123 |
else if (nEC ==SVSTREAM_OUTOFMEMORY) |
| 124 |
nError = ERRCODE_BASIC_NO_MEMORY; |
| 125 |
else |
| 126 |
nError = ERRCODE_BASIC_IO_ERROR; |
| 127 |
} |
| 128 |
|
| 129 |
// Returns sal_True if UNO is available, otherwise the old file |
| 130 |
// system implementation has to be used |
| 131 |
// #89378 New semantic: Don't just ask for UNO but for UCB |
| 132 |
bool hasUno() |
| 133 |
{ |
| 134 |
static const bool bRetVal = [] { |
| 135 |
const Reference< XComponentContext >& xContext = comphelper::getProcessComponentContext(); |
| 136 |
if( !xContext.is() ) |
| 137 |
{ |
| 138 |
// No service manager at all |
| 139 |
return false; |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
Reference< XUniversalContentBroker > xManager = UniversalContentBroker::create(xContext); |
| 144 |
|
| 145 |
if ( !( xManager->queryContentProvider( u"file:///"_ustr ).is() ) ) |
| 146 |
{ |
| 147 |
// No UCB |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
return true; |
| 152 |
}(); |
| 153 |
return bRetVal; |
| 154 |
} |
| 155 |
|
| 156 |
namespace { |
| 157 |
|
| 158 |
class OslStream : public SvStream |
| 159 |
{ |
| 160 |
osl::File maFile; |
| 161 |
|
| 162 |
public: |
| 163 |
OslStream( const OUString& rName, StreamMode nStrmMode ); |
| 164 |
virtual ~OslStream() override; |
| 165 |
virtual std::size_t GetData(void* pData, std::size_t nSize) override; |
| 166 |
virtual std::size_t PutData(const void* pData, std::size_t nSize) override; |
| 167 |
virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override; |
| 168 |
virtual void FlushData() override; |
| 169 |
virtual void SetSize( sal_uInt64 nSize) override; |
| 170 |
}; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
OslStream::OslStream( const OUString& rName, StreamMode nStrmMode ) |
| 175 |
: maFile( rName ) |
| 176 |
{ |
| 177 |
sal_uInt32 nFlags; |
| 178 |
|
| 179 |
if( (nStrmMode & (StreamMode::READ | StreamMode::WRITE)) == (StreamMode::READ | StreamMode::WRITE) ) |
| 180 |
{ |
| 181 |
nFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write; |
| 182 |
} |
| 183 |
else if( nStrmMode & StreamMode::WRITE ) |
| 184 |
{ |
| 185 |
nFlags = osl_File_OpenFlag_Write; |
| 186 |
} |
| 187 |
else //if( nStrmMode & StreamMode::READ ) |
| 188 |
{ |
| 189 |
nFlags = osl_File_OpenFlag_Read; |
| 190 |
} |
| 191 |
|
| 192 |
osl::FileBase::RC nRet = maFile.open( nFlags ); |
| 193 |
if( nRet == osl::FileBase::E_NOENT && nFlags != osl_File_OpenFlag_Read ) |
| 194 |
{ |
| 195 |
nFlags |= osl_File_OpenFlag_Create; |
| 196 |
nRet = maFile.open( nFlags ); |
| 197 |
} |
| 198 |
|
| 199 |
if( nRet != osl::FileBase::E_None ) |
| 200 |
{ |
| 201 |
SetError( ERRCODE_IO_GENERAL ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
OslStream::~OslStream() |
| 207 |
{ |
| 208 |
maFile.close(); |
| 209 |
} |
| 210 |
|
| 211 |
std::size_t OslStream::GetData(void* pData, std::size_t nSize) |
| 212 |
{ |
| 213 |
sal_uInt64 nBytesRead = nSize; |
| 214 |
maFile.read( pData, nBytesRead, nBytesRead ); |
| 215 |
return nBytesRead; |
| 216 |
} |
| 217 |
|
| 218 |
std::size_t OslStream::PutData(const void* pData, std::size_t nSize) |
| 219 |
{ |
| 220 |
sal_uInt64 nBytesWritten; |
| 221 |
maFile.write( pData, nSize, nBytesWritten ); |
| 222 |
return nBytesWritten; |
| 223 |
} |
| 224 |
|
| 225 |
sal_uInt64 OslStream::SeekPos( sal_uInt64 nPos ) |
| 226 |
{ |
| 227 |
::osl::FileBase::RC rc = ::osl::FileBase::E_None; |
| 228 |
// check if a truncated STREAM_SEEK_TO_END was passed |
| 229 |
assert(nPos != SAL_MAX_UINT32); |
| 230 |
if( nPos == STREAM_SEEK_TO_END ) |
| 231 |
{ |
| 232 |
rc = maFile.setPos( osl_Pos_End, 0 ); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
rc = maFile.setPos( osl_Pos_Absolut, nPos ); |
| 237 |
} |
| 238 |
OSL_VERIFY(rc == ::osl::FileBase::E_None); |
| 239 |
sal_uInt64 nRealPos(0); |
| 240 |
rc = maFile.getPos( nRealPos ); |
| 241 |
OSL_VERIFY(rc == ::osl::FileBase::E_None); |
| 242 |
return nRealPos; |
| 243 |
} |
| 244 |
|
| 245 |
void OslStream::FlushData() |
| 246 |
{ |
| 247 |
} |
| 248 |
|
| 249 |
void OslStream::SetSize( sal_uInt64 nSize ) |
| 250 |
{ |
| 251 |
maFile.setSize( nSize ); |
| 252 |
} |
| 253 |
|
| 254 |
namespace { |
| 255 |
|
| 256 |
class UCBStream : public SvStream |
| 257 |
{ |
| 258 |
Reference< XInputStream > xIS; |
| 259 |
Reference< XStream > xS; |
| 260 |
Reference< XSeekable > xSeek; |
| 261 |
public: |
| 262 |
explicit UCBStream( Reference< XInputStream > const & xIS ); |
| 263 |
explicit UCBStream( Reference< XStream > const & xS ); |
| 264 |
virtual ~UCBStream() override; |
| 265 |
virtual std::size_t GetData( void* pData, std::size_t nSize ) override; |
| 266 |
virtual std::size_t PutData( const void* pData, std::size_t nSize ) override; |
| 267 |
virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override; |
| 268 |
virtual void FlushData() override; |
| 269 |
virtual void SetSize( sal_uInt64 nSize ) override; |
| 270 |
}; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
UCBStream::UCBStream( Reference< XInputStream > const & rStm ) |
| 275 |
: xIS( rStm ) |
| 276 |
, xSeek( rStm, UNO_QUERY ) |
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
UCBStream::UCBStream( Reference< XStream > const & rStm ) |
| 281 |
: xS( rStm ) |
| 282 |
, xSeek( rStm, UNO_QUERY ) |
| 283 |
{ |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
UCBStream::~UCBStream() |
| 288 |
{ |
| 289 |
try |
| 290 |
{ |
| 291 |
if( xIS.is() ) |
| 292 |
{ |
| 293 |
xIS->closeInput(); |
| 294 |
} |
| 295 |
else if( xS.is() ) |
| 296 |
{ |
| 297 |
Reference< XInputStream > xIS_ = xS->getInputStream(); |
| 298 |
if( xIS_.is() ) |
| 299 |
{ |
| 300 |
xIS_->closeInput(); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
catch(const Exception & ) |
| 305 |
{ |
| 306 |
SetError( ERRCODE_IO_GENERAL ); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
std::size_t UCBStream::GetData(void* pData, std::size_t nSize) |
| 311 |
{ |
| 312 |
try |
| 313 |
{ |
| 314 |
Reference< XInputStream > xISFromS; |
| 315 |
if( xIS.is() ) |
| 316 |
{ |
| 317 |
Sequence<sal_Int8> aData; |
| 318 |
nSize = xIS->readBytes( aData, nSize ); |
| 319 |
memcpy( pData, aData.getConstArray(), nSize ); |
| 320 |
return nSize; |
| 321 |
} |
| 322 |
else if( xS.is() && (xISFromS = xS->getInputStream()).is() ) |
| 323 |
{ |
| 324 |
Sequence<sal_Int8> aData; |
| 325 |
nSize = xISFromS->readBytes( aData, nSize ); |
| 326 |
memcpy(pData, aData.getConstArray(), nSize ); |
| 327 |
return nSize; |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
SetError( ERRCODE_IO_GENERAL ); |
| 332 |
} |
| 333 |
} |
| 334 |
catch(const Exception & ) |
| 335 |
{ |
| 336 |
SetError( ERRCODE_IO_GENERAL ); |
| 337 |
} |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
std::size_t UCBStream::PutData(const void* pData, std::size_t nSize) |
| 342 |
{ |
| 343 |
try |
| 344 |
{ |
| 345 |
Reference< XOutputStream > xOSFromS; |
| 346 |
if( xS.is() && (xOSFromS = xS->getOutputStream()).is() ) |
| 347 |
{ |
| 348 |
Sequence<sal_Int8> aData( static_cast<const sal_Int8 *>(pData), nSize ); |
| 349 |
xOSFromS->writeBytes( aData ); |
| 350 |
return nSize; |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
SetError( ERRCODE_IO_GENERAL ); |
| 355 |
} |
| 356 |
} |
| 357 |
catch(const Exception & ) |
| 358 |
{ |
| 359 |
SetError( ERRCODE_IO_GENERAL ); |
| 360 |
} |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
sal_uInt64 UCBStream::SeekPos( sal_uInt64 nPos ) |
| 365 |
{ |
| 366 |
try |
| 367 |
{ |
| 368 |
if( xSeek.is() ) |
| 369 |
{ |
| 370 |
sal_uInt64 nLen = static_cast<sal_uInt64>( xSeek->getLength() ); |
| 371 |
if( nPos > nLen ) |
| 372 |
{ |
| 373 |
nPos = nLen; |
| 374 |
} |
| 375 |
xSeek->seek( nPos ); |
| 376 |
return nPos; |
| 377 |
} |
| 378 |
else |
| 379 |
{ |
| 380 |
SetError( ERRCODE_IO_GENERAL ); |
| 381 |
} |
| 382 |
} |
| 383 |
catch(const Exception & ) |
| 384 |
{ |
| 385 |
SetError( ERRCODE_IO_GENERAL ); |
| 386 |
} |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
void UCBStream::FlushData() |
| 391 |
{ |
| 392 |
try |
| 393 |
{ |
| 394 |
Reference< XOutputStream > xOSFromS; |
| 395 |
if( xS.is() && (xOSFromS = xS->getOutputStream()).is() ) |
| 396 |
{ |
| 397 |
xOSFromS->flush(); |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
SetError( ERRCODE_IO_GENERAL ); |
| 402 |
} |
| 403 |
} |
| 404 |
catch(const Exception & ) |
| 405 |
{ |
| 406 |
SetError( ERRCODE_IO_GENERAL ); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
void UCBStream::SetSize( sal_uInt64 ) |
| 411 |
{ |
| 412 |
SAL_WARN("basic", "UCBStream::SetSize not allowed to call from basic" ); |
| 413 |
SetError( ERRCODE_IO_GENERAL ); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
ErrCode const & SbiStream::Open |
| 418 |
( const OUString& rName, StreamMode nStrmMode, SbiStreamFlags nFlags, short nL ) |
| 419 |
{ |
| 420 |
nMode = nFlags; |
| 421 |
nLen = nL; |
| 422 |
nLine = 0; |
| 423 |
nExpandOnWriteTo = 0; |
| 424 |
if( ( nStrmMode & ( StreamMode::READ|StreamMode::WRITE ) ) == StreamMode::READ ) |
| 425 |
{ |
| 426 |
nStrmMode |= StreamMode::NOCREATE; |
| 427 |
} |
| 428 |
OUString aNameStr = getFullPath(rName); |
| 429 |
|
| 430 |
if( hasUno() ) |
| 431 |
{ |
| 432 |
Reference< XSimpleFileAccess3 > xSFI( SimpleFileAccess::create( comphelper::getProcessComponentContext() ) ); |
| 433 |
try |
| 434 |
{ |
| 435 |
|
| 436 |
// #??? For write access delete file if it already exists (not for appending) |
| 437 |
if( (nStrmMode & StreamMode::WRITE) && !IsAppend() && !IsBinary() && !IsRandom() && |
| 438 |
xSFI->exists( aNameStr ) && !xSFI->isFolder( aNameStr ) ) |
| 439 |
{ |
| 440 |
xSFI->kill( aNameStr ); |
| 441 |
} |
| 442 |
|
| 443 |
if( (nStrmMode & (StreamMode::READ | StreamMode::WRITE)) == (StreamMode::READ | StreamMode::WRITE) ) |
| 444 |
{ |
| 445 |
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr ); |
| 446 |
pStrm.reset( new UCBStream( xIS ) ); |
| 447 |
} |
| 448 |
else if( nStrmMode & StreamMode::WRITE ) |
| 449 |
{ |
| 450 |
Reference< XStream > xIS = xSFI->openFileReadWrite( aNameStr ); |
| 451 |
pStrm.reset( new UCBStream( xIS ) ); |
| 452 |
} |
| 453 |
else //if( nStrmMode & StreamMode::READ ) |
| 454 |
{ |
| 455 |
Reference< XInputStream > xIS = xSFI->openFileRead( aNameStr ); |
| 456 |
pStrm.reset( new UCBStream( xIS ) ); |
| 457 |
} |
| 458 |
|
| 459 |
} |
| 460 |
catch(const Exception & ) |
| 461 |
{ |
| 462 |
nError = ERRCODE_IO_GENERAL; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if( !pStrm ) |
| 467 |
{ |
| 468 |
pStrm.reset( new OslStream( aNameStr, nStrmMode ) ); |
| 469 |
} |
| 470 |
if( IsAppend() ) |
| 471 |
{ |
| 472 |
pStrm->Seek( STREAM_SEEK_TO_END ); |
| 473 |
} |
| 474 |
MapError(); |
| 475 |
if( nError ) |
| 476 |
{ |
| 477 |
pStrm.reset(); |
| 478 |
} |
| 479 |
return nError; |
| 480 |
} |
| 481 |
|
| 482 |
ErrCode const & SbiStream::Close() |
| 483 |
{ |
| 484 |
if( pStrm ) |
| 485 |
{ |
| 486 |
MapError(); |
| 487 |
pStrm.reset(); |
| 488 |
} |
| 489 |
return nError; |
| 490 |
} |
| 491 |
|
| 492 |
ErrCode SbiStream::Read(OString& rBuf, sal_uInt16 n, bool bForceReadingPerByte) |
| 493 |
{ |
| 494 |
nExpandOnWriteTo = 0; |
| 495 |
if( !bForceReadingPerByte && IsText() ) |
| 496 |
{ |
| 497 |
pStrm->ReadLine(rBuf); |
| 498 |
nLine++; |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
if( !n ) |
| 503 |
{ |
| 504 |
n = nLen; |
| 505 |
} |
| 506 |
if( !n ) |
| 507 |
{ |
| 508 |
return nError = ERRCODE_BASIC_BAD_RECORD_LENGTH; |
| 509 |
} |
| 510 |
rBuf = read_uInt8s_ToOString(*pStrm, n); |
| 511 |
//Pad it out with ' ' to the requested length on short read |
| 512 |
if (rBuf.getLength() < static_cast<sal_Int32>(n)) |
| 513 |
rBuf += OString::Concat(RepeatedChar(' ', n)); |
| 514 |
} |
| 515 |
MapError(); |
| 516 |
if( !nError && pStrm->eof() ) |
| 517 |
{ |
| 518 |
nError = ERRCODE_BASIC_READ_PAST_EOF; |
| 519 |
} |
| 520 |
return nError; |
| 521 |
} |
| 522 |
|
| 523 |
ErrCode const & SbiStream::Read( char& ch ) |
| 524 |
{ |
| 525 |
nExpandOnWriteTo = 0; |
| 526 |
if (aLine.isEmpty()) |
| 527 |
{ |
| 528 |
Read( aLine ); |
| 529 |
aLine += "\n"; |
| 530 |
} |
| 531 |
ch = aLine[0]; |
| 532 |
aLine = aLine.copy(1); |
| 533 |
return nError; |
| 534 |
} |
| 535 |
|
| 536 |
void SbiStream::ExpandFile() |
| 537 |
{ |
| 538 |
if ( !nExpandOnWriteTo ) |
| 539 |
return; |
| 540 |
|
| 541 |
sal_uInt64 nCur = pStrm->Seek(STREAM_SEEK_TO_END); |
| 542 |
if( nCur < nExpandOnWriteTo ) |
| 543 |
{ |
| 544 |
sal_uInt64 nDiff = nExpandOnWriteTo - nCur; |
| 545 |
while( nDiff-- ) |
| 546 |
{ |
| 547 |
pStrm->WriteChar( 0 ); |
| 548 |
} |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
pStrm->Seek( nExpandOnWriteTo ); |
| 553 |
} |
| 554 |
nExpandOnWriteTo = 0; |
| 555 |
} |
| 556 |
|
| 557 |
namespace |
| 558 |
{ |
| 559 |
void WriteLines(SvStream &rStream, const OString& rStr) |
| 560 |
{ |
| 561 |
OString aStr(convertLineEnd(rStr, rStream.GetLineDelimiter()) ); |
| 562 |
rStream.WriteBytes(aStr.getStr(), aStr.getLength()); |
| 563 |
endl( rStream ); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
ErrCode SbiStream::Write( const OString& rBuf ) |
| 568 |
{ |
| 569 |
ExpandFile(); |
| 570 |
if( IsAppend() ) |
| 571 |
{ |
| 572 |
pStrm->Seek( STREAM_SEEK_TO_END ); |
| 573 |
} |
| 574 |
if( IsText() ) |
| 575 |
{ |
| 576 |
aLine += rBuf; |
| 577 |
// Get it out, if the end is an LF, but strip CRLF before, |
| 578 |
// because the SvStream adds a CRLF! |
| 579 |
sal_Int32 nLineLen = aLine.getLength(); |
| 580 |
if (nLineLen && aLine[--nLineLen] == 0x0A) |
| 581 |
{ |
| 582 |
aLine = aLine.copy(0, nLineLen); |
| 583 |
if (nLineLen && aLine[--nLineLen] == 0x0D) |
| 584 |
{ |
| 585 |
aLine = aLine.copy(0, nLineLen); |
| 586 |
} |
| 587 |
WriteLines(*pStrm, aLine); |
| 588 |
aLine.clear(); |
| 589 |
} |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
if( !nLen ) |
| 594 |
{ |
| 595 |
return nError = ERRCODE_BASIC_BAD_RECORD_LENGTH; |
| 596 |
} |
| 597 |
pStrm->WriteBytes(rBuf.getStr(), nLen); |
| 598 |
MapError(); |
| 599 |
} |
| 600 |
return nError; |
| 601 |
} |
| 602 |
|
| 603 |
SbiIoSystem::SbiIoSystem() |
| 604 |
{ |
| 605 |
for(SbiStream* & i : pChan) |
| 606 |
{ |
| 607 |
i = nullptr; |
| 608 |
} |
| 609 |
nChan = 0; |
| 610 |
nError = ERRCODE_NONE; |
| 611 |
} |
| 612 |
|
| 613 |
SbiIoSystem::~SbiIoSystem() |
| 614 |
{ |
| 615 |
suppress_fun_call_w_exception(Shutdown()); |
| 616 |
} |
| 617 |
|
| 618 |
ErrCode SbiIoSystem::GetError() |
| 619 |
{ |
| 620 |
ErrCode n = nError; |
| 621 |
nError = ERRCODE_NONE; |
| 622 |
return n; |
| 623 |
} |
| 624 |
|
| 625 |
void SbiIoSystem::Open(short nCh, const OUString& rName, StreamMode nMode, SbiStreamFlags nFlags, short nLen) |
| 626 |
{ |
| 627 |
nError = ERRCODE_NONE; |
| 628 |
if( nCh >= CHANNELS || !nCh ) |
| 629 |
{ |
| 630 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 631 |
} |
| 632 |
else if( pChan[ nCh ] ) |
| 633 |
{ |
| 634 |
nError = ERRCODE_BASIC_FILE_ALREADY_OPEN; |
| 635 |
} |
| 636 |
else |
| 637 |
{ |
| 638 |
pChan[ nCh ] = new SbiStream; |
| 639 |
nError = pChan[ nCh ]->Open( rName, nMode, nFlags, nLen ); |
| 640 |
if( nError ) |
| 641 |
{ |
| 642 |
delete pChan[ nCh ]; |
| 643 |
pChan[ nCh ] = nullptr; |
| 644 |
} |
| 645 |
} |
| 646 |
nChan = 0; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
void SbiIoSystem::Close() |
| 651 |
{ |
| 652 |
if( !nChan ) |
| 653 |
{ |
| 654 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 655 |
} |
| 656 |
else if( !pChan[ nChan ] ) |
| 657 |
{ |
| 658 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 659 |
} |
| 660 |
else |
| 661 |
{ |
| 662 |
nError = pChan[ nChan ]->Close(); |
| 663 |
delete pChan[ nChan ]; |
| 664 |
pChan[ nChan ] = nullptr; |
| 665 |
} |
| 666 |
nChan = 0; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
void SbiIoSystem::Shutdown() |
| 671 |
{ |
| 672 |
for( short i = 1; i < CHANNELS; i++ ) |
| 673 |
{ |
| 674 |
if( pChan[ i ] ) |
| 675 |
{ |
| 676 |
ErrCode n = pChan[ i ]->Close(); |
| 677 |
delete pChan[ i ]; |
| 678 |
pChan[ i ] = nullptr; |
| 679 |
if( n && !nError ) |
| 680 |
{ |
| 681 |
nError = n; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
nChan = 0; |
| 686 |
// anything left to PRINT? |
| 687 |
if( !aOut.isEmpty() ) |
| 688 |
{ |
| 689 |
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(Application::GetDefDialogParent(), VclMessageType::Warning, |
| 690 |
VclButtonsType::Ok, aOut)); |
| 691 |
xBox->run(); |
| 692 |
} |
| 693 |
aOut.clear(); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
void SbiIoSystem::Read(OString& rBuf) |
| 698 |
{ |
| 699 |
if( !nChan ) |
| 700 |
{ |
| 701 |
ReadCon( rBuf ); |
| 702 |
} |
| 703 |
else if( !pChan[ nChan ] ) |
| 704 |
{ |
| 705 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 706 |
} |
| 707 |
else |
| 708 |
{ |
| 709 |
nError = pChan[ nChan ]->Read( rBuf ); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
char SbiIoSystem::Read() |
| 714 |
{ |
| 715 |
char ch = ' '; |
| 716 |
if( !nChan ) |
| 717 |
{ |
| 718 |
if( aIn.isEmpty() ) |
| 719 |
{ |
| 720 |
ReadCon( aIn ); |
| 721 |
aIn += "\n"; |
| 722 |
} |
| 723 |
ch = aIn[0]; |
| 724 |
aIn = aIn.copy(1); |
| 725 |
} |
| 726 |
else if( !pChan[ nChan ] ) |
| 727 |
{ |
| 728 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 729 |
} |
| 730 |
else |
| 731 |
{ |
| 732 |
nError = pChan[ nChan ]->Read( ch ); |
| 733 |
} |
| 734 |
return ch; |
| 735 |
} |
| 736 |
|
| 737 |
void SbiIoSystem::Write(std::u16string_view rBuf) |
| 738 |
{ |
| 739 |
if( !nChan ) |
| 740 |
{ |
| 741 |
WriteCon( rBuf ); |
| 742 |
} |
| 743 |
else if( !pChan[ nChan ] ) |
| 744 |
{ |
| 745 |
nError = ERRCODE_BASIC_BAD_CHANNEL; |
| 746 |
} |
| 747 |
else |
| 748 |
{ |
| 749 |
nError = pChan[ nChan ]->Write( OUStringToOString(rBuf, osl_getThreadTextEncoding()) ); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
// nChannel == 0..CHANNELS-1 |
| 754 |
|
| 755 |
SbiStream* SbiIoSystem::GetStream( short nChannel ) const |
| 756 |
{ |
| 757 |
SbiStream* pRet = nullptr; |
| 758 |
if( nChannel >= 0 && nChannel < CHANNELS ) |
| 759 |
{ |
| 760 |
pRet = pChan[ nChannel ]; |
| 761 |
} |
| 762 |
return pRet; |
| 763 |
} |
| 764 |
|
| 765 |
void SbiIoSystem::CloseAll() |
| 766 |
{ |
| 767 |
for( short i = 1; i < CHANNELS; i++ ) |
| 768 |
{ |
| 769 |
if( pChan[ i ] ) |
| 770 |
{ |
| 771 |
ErrCode n = pChan[ i ]->Close(); |
| 772 |
delete pChan[ i ]; |
| 773 |
pChan[ i ] = nullptr; |
| 774 |
if( n && !nError ) |
| 775 |
{ |
| 776 |
nError = n; |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
void SbiIoSystem::ReadCon(OString& rIn) |
| 783 |
{ |
| 784 |
SbiInputDialog aDlg(nullptr, aPrompt); |
| 785 |
if (aDlg.run() == RET_OK) |
| 786 |
{ |
| 787 |
rIn = OUStringToOString(aDlg.GetInput(), osl_getThreadTextEncoding()); |
| 788 |
} |
| 789 |
else |
| 790 |
{ |
| 791 |
nError = ERRCODE_BASIC_USER_ABORT; |
| 792 |
} |
| 793 |
aPrompt.clear(); |
| 794 |
} |
| 795 |
|
| 796 |
// output of a MessageBox, if there's a CR in the console-buffer |
| 797 |
|
| 798 |
void SbiIoSystem::WriteCon(std::u16string_view rText) |
| 799 |
{ |
| 800 |
aOut += rText; |
| 801 |
sal_Int32 n1 = aOut.indexOf('\n'); |
| 802 |
sal_Int32 n2 = aOut.indexOf('\r'); |
| 803 |
if( n1 == -1 && n2 == -1 ) |
| 804 |
return; |
| 805 |
|
| 806 |
if( n1 == -1 ) |
| 807 |
{ |
| 808 |
n1 = n2; |
| 809 |
} |
| 810 |
else if( n2 == -1 ) |
| 811 |
{ |
| 812 |
n2 = n1; |
| 813 |
} |
| 814 |
if( n1 > n2 ) |
| 815 |
{ |
| 816 |
n1 = n2; |
| 817 |
} |
| 818 |
OUString s(aOut.copy(0, n1)); |
| 819 |
aOut = aOut.copy(n1); |
| 820 |
while ( !aOut.isEmpty() && (aOut[0] == '\n' || aOut[0] == '\r') ) |
| 821 |
{ |
| 822 |
aOut = aOut.copy(1); |
| 823 |
} |
| 824 |
{ |
| 825 |
SolarMutexGuard aSolarGuard; |
| 826 |
|
| 827 |
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(Application::GetDefDialogParent(), VclMessageType::Warning, |
| 828 |
VclButtonsType::OkCancel, s)); |
| 829 |
xBox->set_default_response(RET_OK); |
| 830 |
if (!xBox->run()) |
| 831 |
{ |
| 832 |
nError = ERRCODE_BASIC_USER_ABORT; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |