-----------------------------------------------------------------------------
| 342 | |
| 343 | //----------------------------------------------------------------------------- |
| 344 | bool FileStream::_write(const U32 i_numBytes, const void *i_pBuffer) |
| 345 | { |
| 346 | AssertFatal(0 != mStreamCaps, "FileStream::_write: the stream isn't open"); |
| 347 | AssertFatal(NULL != i_pBuffer || i_numBytes == 0, "FileStream::_write: NULL source buffer pointer on non-zero write request"); |
| 348 | |
| 349 | if (!hasCapability(Stream::StreamWrite)) |
| 350 | { |
| 351 | AssertFatal(false, "FileStream::_write: file stream lacks capability"); |
| 352 | Stream::setStatus(IllegalCall); |
| 353 | return(false); |
| 354 | } |
| 355 | |
| 356 | // exit on pre-existing errors |
| 357 | if (Ok != getStatus() && EOS != getStatus()) |
| 358 | return(false); |
| 359 | |
| 360 | // if a request of non-zero length was made |
| 361 | if (0 != i_numBytes) |
| 362 | { |
| 363 | U8 *pSrc = (U8 *)i_pBuffer; |
| 364 | U32 writeSize; |
| 365 | U32 remaining = i_numBytes; |
| 366 | U32 bytesWrit; |
| 367 | U32 blockHead; |
| 368 | U32 blockTail; |
| 369 | |
| 370 | // check if the buffer is valid |
| 371 | if (BUFFER_INVALID != mBuffHead) |
| 372 | { |
| 373 | // copy as much as possible from the source to the buffer |
| 374 | calcBlockBounds(mBuffHead, &blockHead, &blockTail); |
| 375 | writeSize = (mBuffPos > blockTail) ? 0 : blockTail - mBuffPos + 1; |
| 376 | writeSize = getMin(writeSize, remaining); |
| 377 | |
| 378 | AssertFatal(0 == writeSize || (mBuffPos - blockHead) < BUFFER_SIZE, "FileStream::_write: out of bounds buffer position"); |
| 379 | dMemcpy(mBuffer + (mBuffPos - blockHead), pSrc, writeSize); |
| 380 | // reduce the remaining amount to be written |
| 381 | remaining -= writeSize; |
| 382 | // advance the buffer pointers |
| 383 | mBuffPos += writeSize; |
| 384 | mBuffTail = getMax(mBuffTail, mBuffPos - 1); |
| 385 | pSrc += writeSize; |
| 386 | // mark the buffer dirty |
| 387 | if (0 < writeSize) |
| 388 | mDirty = true; |
| 389 | } |
| 390 | |
| 391 | // if the request wasn't satisfied by the buffer |
| 392 | if (0 < remaining) |
| 393 | { |
| 394 | // flush the buffer if its dirty, since we now need to go to disk |
| 395 | if (mDirty) |
| 396 | flush(); |
| 397 | |
| 398 | // make sure we know the current write location in the underlying file |
| 399 | mBuffPos = mFile->getPosition(); |
| 400 | calcBlockBounds(mBuffPos, &blockHead, &blockTail); |
| 401 |
nothing calls this directly
no test coverage detected