cwFile.h/cpp : read()/write() now check 'lastRC' as a precondition.

This commit is contained in:
kevin 2020-12-29 11:20:24 -05:00
parent 7bb41b24e4
commit d3c7b3a572
2 changed files with 11 additions and 3 deletions

View File

@ -215,6 +215,9 @@ cw::rc_t cw::file::read( handle_t h, void* buf, unsigned bufByteCnt, unsigned
rc_t rc = kOkRC;
this_t* p = _handleToPtr(h);
unsigned actualByteCnt = 0;
if( p->lastRC != kOkRC )
return p->lastRC;
errno = 0;
if(( actualByteCnt = fread(buf,1,bufByteCnt,p->fp)) != bufByteCnt )
@ -234,6 +237,9 @@ cw::rc_t cw::file::read( handle_t h, void* buf, unsigned bufByteCnt, unsigned
cw::rc_t cw::file::write( handle_t h, const void* buf, unsigned bufByteCnt )
{
this_t* p = _handleToPtr(h);
if( p->lastRC != kOkRC )
return p->lastRC;
errno = 0;
if( fwrite(buf,bufByteCnt,1,p->fp) != 1 )

View File

@ -46,9 +46,11 @@ namespace cw
// Read a block bytes from a file. Equivalent to fread().
// 'actualByteCntRef is always the smae as bufByteCnt unless an error occurs or EOF is encountered.
// This function checks lastRC() as a precondition and only proceeds if it is not set.
rc_t read( handle_t h, void* buf, unsigned bufByteCnt, unsigned* actualByteCntRef=nullptr );
// Write a block of bytes to a file. Equivalent to fwrite().
// This function checks lastRC() as a precondition and only proceeds if it is not set.
rc_t write( handle_t h, const void* buf, unsigned bufByteCnt );
enum seekFlags_t
@ -162,8 +164,8 @@ namespace cw
rc_t getLineAuto( handle_t h, char** bufPtrPtr, unsigned* bufByteCntPtr );
// Binary Array Reading Functions
// Each of these functions reads a block of binary data from a file.
// The advantage to using these functions over fileRead() is only that they are type specific.
// Each of these functions reads a block of binary data from a file and is a wrapper around file::read(h,buf,bufN).
// The advantage to using these functions over file::read() is only that they are type specific.
rc_t readChar( handle_t h, char* buf, unsigned cnt=1 );
rc_t readUChar( handle_t h, unsigned char* buf, unsigned cnt=1 );
rc_t readShort( handle_t h, short* buf, unsigned cnt=1 );
@ -190,7 +192,7 @@ namespace cw
// Binary Array Writing Functions
// Each of these functions writes an array to a binary file.
// Each of these functions writes an array to a binary file and is a wrapper around file::write(h,buf,bufN)
// The advantage to using functions rather than fileWrite() is only that they are type specific.
rc_t writeChar( handle_t h, const char* buf, unsigned cnt=1 );
rc_t writeUChar( handle_t h, const unsigned char* buf, unsigned cnt=1 );