Main MRPT website > C++ reference for MRPT 1.5.6
List of all members | Public Member Functions | Protected Member Functions | Protected Attributes
IoInterfaceFile Class Reference

Detailed Description

The low-level file communication class.

Definition at line 23 of file iointerfacefile.h.

#include <xSens_MT4/xcommunication/include/xsens/iointerfacefile.h>

Inheritance diagram for IoInterfaceFile:
Inheritance graph

Public Member Functions

 IoInterfaceFile ()
 
 ~IoInterfaceFile ()
 
XsResultValue close (void)
 Close the file, overrides IoInterface::close(). More...
 
XsResultValue closeFile (void)
 Close the file. More...
 
XsResultValue flushData (void)
 Flush all data in the buffers to and from the device.
More...
 
bool isOpen (void) const
 Return whether the file is open or not. More...
 
XsResultValue getLastResult (void) const
 Return the result code of the last operation. More...
 
XsResultValue writeData (const XsByteArray &data, XsSize *written=NULL)
 Write the data contained in data to the device. More...
 
XsResultValue readData (XsSize maxLength, XsByteArray &data)
 Read at most maxLength bytes from the device into data. More...
 
XsResultValue readTerminatedData (XsSize maxLength, unsigned char terminator, XsByteArray &bdata)
 Read data from the file and put it into the data buffer. More...
 
XsResultValue appendData (const XsByteArray &bdata)
 Write data to the end of the file. More...
 
XsResultValue closeAndDelete (void)
 Close the file and delete it. More...
 
XsResultValue create (const XsString &filename)
 Create an empty file. More...
 
XsResultValue deleteData (XsFilePos start, XsSize length)
 Delete the given data from the file. More...
 
XsResultValue find (const XsByteArray &data, XsFilePos &pos)
 Find a string of bytes in the file. More...
 
XsFilePos getFileSize (void) const
 Return the size of the file. More...
 
XsTimeStamp getFileDate (void) const
 Return the creation date of the file. More...
 
XsResultValue getName (XsString &filename) const
 Retrieve the filename that was last successfully opened. More...
 
XsFilePos getReadPosition (void) const
 Return the current read position. More...
 
XsFilePos getWritePosition (void) const
 Return the current write position. More...
 
XsResultValue insertData (XsFilePos start, const XsByteArray &data)
 Insert the given data into the file. More...
 
bool isReadOnly (void) const
 Return whether the file is readonly or not. More...
 
XsResultValue open (const XsString &filename, bool createNew, bool readOnly)
 Open a file. More...
 
XsResultValue setReadPosition (XsFilePos pos)
 Set the new absolute read position. More...
 
XsResultValue setWritePosition (XsFilePos pos=-1)
 Set the new absolute write position. More...
 
virtual XsResultValue open (const XsPortInfo &portInfo, uint32_t readBufSize=XS_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=XS_DEFAULT_WRITE_BUFFER_SIZE)
 Open a communication channel to the given port info.
More...
 
virtual XsResultValue setTimeout (uint32_t ms)
 Set the default timeout value to use in blocking operations.
More...
 
virtual XsResultValue waitForData (XsSize maxLength, XsByteArray &data)
 Wait for data to arrive or a timeout to occur.
More...
 
virtual void cancelIo (void) const
 Cancel any pending io requests.
More...
 

Protected Member Functions

void gotoRead (void)
 Change from writing to reading mode. More...
 
void gotoWrite (void)
 Change from reading to writing mode. More...
 

Protected Attributes

XsFileHandlem_handle
 The file handlem, also indicates if the file is open or not. More...
 
XsFilePos m_fileSize
 Contains the size of the file. More...
 
XsFilePos m_readPos
 The last read position in the file. More...
 
XsFilePos m_writePos
 The last write position in the file. More...
 
XsResultValue m_lastResult
 The last result of an operation. More...
 
XsString m_filename
 Contains the name of the file that was last successfully opened. More...
 
bool m_reading
 Indicates whether the last operation was a read or write operation. More...
 
bool m_readOnly
 Indicates if the file was opened in read-only mode. More...
 

Constructor & Destructor Documentation

◆ IoInterfaceFile()

IoInterfaceFile::IoInterfaceFile ( )

Default constructor, initializes all members to their default values.

Definition at line 59 of file iointerfacefile.cpp.

References m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_readPos, m_writePos, and XRV_OK.

◆ ~IoInterfaceFile()

IoInterfaceFile::~IoInterfaceFile ( )

Destructor

Definition at line 72 of file iointerfacefile.cpp.

References closeFile().

Member Function Documentation

◆ appendData()

XsResultValue IoInterfaceFile::appendData ( const XsByteArray bdata)
virtual

Write data to the end of the file.

The function writes the given data to the file at the end. The current write position is also moved to the end of the file.

Parameters
bdataThe byte data to append to the file
Returns
XRV_OK if the write was successful

Reimplemented from IoInterface.

Definition at line 86 of file iointerfacefile.cpp.

References FSEEK_R, FTELL, m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_writePos, void(), XRV_NOFILEOPEN, XRV_OK, and XRV_READONLY.

◆ cancelIo()

void IoInterface::cancelIo ( void  ) const
virtualinherited

Cancel any pending io requests.

Reimplemented in SerialInterface.

Definition at line 72 of file iointerface.cpp.

◆ close()

XsResultValue IoInterfaceFile::close ( void  )
virtual

Close the file, overrides IoInterface::close().

Returns
XRV_OK if the file was closed successfully

Implements IoInterface.

Definition at line 112 of file iointerfacefile.cpp.

References closeFile().

◆ closeAndDelete()

XsResultValue IoInterfaceFile::closeAndDelete ( void  )
virtual

Close the file and delete it.

Returns
XRV_OK if the file was closed and deleted successfully

Reimplemented from IoInterface.

Definition at line 145 of file iointerfacefile.cpp.

References mrpt::system::os::fclose(), m_filename, m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_readPos, m_writePos, XRV_NOFILEOPEN, XRV_OK, and XRV_READONLY.

◆ closeFile()

XsResultValue IoInterfaceFile::closeFile ( void  )

Close the file.

Returns
XRV_OK if the file was closed successfully

Definition at line 120 of file iointerfacefile.cpp.

References mrpt::system::os::fclose(), m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_readPos, m_writePos, and XRV_OK.

Referenced by close(), and ~IoInterfaceFile().

◆ create()

XsResultValue IoInterfaceFile::create ( const XsString filename)
virtual

Create an empty file.

Parameters
filenameThe desired (path+)name of the file
Returns
XRV_OK if the file was created successfully

Reimplemented from IoInterface.

Definition at line 187 of file iointerfacefile.cpp.

References mrpt::system::os::fclose(), mrpt::system::os::fopen(), m_filename, m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_readPos, m_writePos, XRV_ALREADYOPEN, XRV_INVALIDPARAM, XRV_OK, XRV_OUTPUTCANNOTBEOPENED, and XS_MAX_FILENAME_LENGTH.

◆ deleteData()

XsResultValue IoInterfaceFile::deleteData ( XsFilePos  start,
XsSize  length 
)
virtual

Delete the given data from the file.

The function erases the given data from the file at the given write position. This operation may take a while to complete, but is faster than insertData.

The write position is not changed and the read position is checked for validity upon function exit.

Parameters
startThe offset of the first byte to delete
lengthThe total number of bytes to delete
Returns
XRV_OK if the data was deleted successfully

Reimplemented from IoInterface.

Definition at line 245 of file iointerfacefile.cpp.

References FSEEK, gotoWrite(), m_fileSize, m_handle, m_lastResult, m_readOnly, m_writePos, XRV_BUSY, XRV_ERROR, XRV_INVALIDINSTANCE, XRV_INVALIDPARAM, XRV_NOFILEOPEN, XRV_OK, XRV_OUTOFMEMORY, and XRV_READONLY.

◆ find()

XsResultValue IoInterfaceFile::find ( const XsByteArray needleV,
XsFilePos pos 
)
virtual

Find a string of bytes in the file.

The function searches from the current read position until the given needle is found. If the needle is not found, XsResultValue::NOT_FOUND is returned. The function will update the seek position to the first character of the found needle.

Parameters
needleVThe byte string to find.
posThe position where needleV was found. This will point to the first character of the found needleV.
Returns
XRV_OK if the data was found, XRV_ENDOFFILE if it wasn't found

Reimplemented from IoInterface.

Definition at line 328 of file iointerfacefile.cpp.

References FSEEK, gotoRead(), m_handle, m_lastResult, m_readPos, XRV_ENDOFFILE, XRV_NOFILEOPEN, and XRV_OK.

◆ flushData()

XsResultValue IoInterfaceFile::flushData ( void  )
virtual

Flush all data in the buffers to and from the device.

Returns
XRV_OK if the data was flushed successfully

Implements IoInterface.

Definition at line 417 of file iointerfacefile.cpp.

References m_handle, m_lastResult, and XRV_OK.

◆ getFileDate()

XsTimeStamp IoInterfaceFile::getFileDate ( void  ) const

Return the creation date of the file.

Returns
The creation date of the file

Definition at line 400 of file iointerfacefile.cpp.

References _stat, and m_filename.

◆ getFileSize()

XsFilePos IoInterfaceFile::getFileSize ( void  ) const
virtual

Return the size of the file.

Returns
The size of the file.

Reimplemented from IoInterface.

Definition at line 392 of file iointerfacefile.cpp.

References m_fileSize.

◆ getLastResult()

XsResultValue IoInterfaceFile::getLastResult ( void  ) const
virtual

Return the result code of the last operation.

Returns
The result code of the last operation.

Implements IoInterface.

Definition at line 803 of file iointerfacefile.cpp.

References m_lastResult.

◆ getName()

XsResultValue IoInterfaceFile::getName ( XsString filename) const
virtual

Retrieve the filename that was last successfully opened.

Parameters
filenameThe XsString which will contain the filename.
Returns
XRV_OK

Reimplemented from IoInterface.

Definition at line 429 of file iointerfacefile.cpp.

References m_filename, m_lastResult, and XRV_OK.

◆ getReadPosition()

XsFilePos IoInterfaceFile::getReadPosition ( void  ) const
virtual

Return the current read position.

Returns
The current read position.

Reimplemented from IoInterface.

Definition at line 787 of file iointerfacefile.cpp.

References m_readPos.

◆ getWritePosition()

XsFilePos IoInterfaceFile::getWritePosition ( void  ) const
virtual

Return the current write position.

Returns
The current write position.

Reimplemented from IoInterface.

Definition at line 795 of file iointerfacefile.cpp.

References m_writePos.

◆ gotoRead()

void IoInterfaceFile::gotoRead ( void  )
protected

Change from writing to reading mode.

Definition at line 436 of file iointerfacefile.cpp.

References FSEEK, m_handle, m_reading, and m_readPos.

Referenced by find(), readData(), and readTerminatedData().

◆ gotoWrite()

void IoInterfaceFile::gotoWrite ( void  )
protected

Change from reading to writing mode.

Definition at line 447 of file iointerfacefile.cpp.

References FSEEK, m_handle, m_reading, and m_writePos.

Referenced by deleteData(), insertData(), and writeData().

◆ insertData()

XsResultValue IoInterfaceFile::insertData ( XsFilePos  start,
const XsByteArray data 
)
virtual

Insert the given data into the file.

The function writes the given data to the file at the current write position. This operation may take a while to complete.

The write position is placed at the end of the inserted data.

Parameters
startThe offset in the file to write the first byte
dataThe data to insert in the file
Returns
XRV_OK if the data was inserted successfully

Reimplemented from IoInterface.

Definition at line 466 of file iointerfacefile.cpp.

References FSEEK, gotoWrite(), m_fileSize, m_handle, m_lastResult, m_readOnly, m_writePos, XRV_NOFILEOPEN, XRV_OK, XRV_OUTOFMEMORY, and XRV_READONLY.

◆ isOpen()

bool IoInterfaceFile::isOpen ( void  ) const
virtual

Return whether the file is open or not.

Returns
true if the file is open

Implements IoInterface.

Definition at line 811 of file iointerfacefile.cpp.

References m_handle.

Referenced by isReadOnly().

◆ isReadOnly()

bool IoInterfaceFile::isReadOnly ( void  ) const
virtual

Return whether the file is readonly or not.

Returns
true if the file is readonly

Reimplemented from IoInterface.

Definition at line 819 of file iointerfacefile.cpp.

References isOpen(), and m_readOnly.

◆ open() [1/2]

XsResultValue IoInterfaceFile::open ( const XsString filename,
bool  createNew,
bool  readOnly 
)
virtual

Open a file.

Parameters
filenameThe name of the file to open
createNewWhen true, the file will be created if it doesn't exist yet
readOnlyWhen true, the file will be marked as read only for IoInterfaceFile, preventing accidental writes to the file.
Returns
XRV_OK if the file was opened successfully
See also
createFile

Reimplemented from IoInterface.

Definition at line 543 of file iointerfacefile.cpp.

References mrpt::system::os::fclose(), mrpt::system::os::fopen(), FSEEK, FSEEK_R, FTELL, m_filename, m_fileSize, m_handle, m_lastResult, m_reading, m_readOnly, m_readPos, m_writePos, XRV_ALREADYOPEN, XRV_INPUTCANNOTBEOPENED, XRV_INVALIDPARAM, XRV_OK, and XS_MAX_FILENAME_LENGTH.

◆ open() [2/2]

XsResultValue IoInterface::open ( const XsPortInfo portInfo,
uint32_t  readBufSize = XS_DEFAULT_READ_BUFFER_SIZE,
uint32_t  writeBufSize = XS_DEFAULT_WRITE_BUFFER_SIZE 
)
virtualinherited

Open a communication channel to the given port info.

If the baudrate in portInfo is set to XBR_Invalid, the baud rate is automatically detected if possible.

Parameters
portInfoThe details of the port that should be opened. Depending on the type of interface, parts of this parameter may be ignored.
readBufSizeThe size of the read buffer in bytes (if appliccable to the device)
writeBufSizeThe size of the write buffer in bytes (if appliccable to the device)
Returns
XRV_OK if the device was opened successfully

Reimplemented in SerialInterface, and UsbInterface.

Definition at line 52 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ readData()

XsResultValue IoInterfaceFile::readData ( XsSize  maxLength,
XsByteArray data 
)
virtual

Read at most maxLength bytes from the device into data.

Parameters
maxLengthThe maximum number of bytes to read. Depending on the device type and timeout settings, the function may return with less than this number of bytes read.
dataA buffer that will contain the read data.
Returns
XRV_OK if all data was read successfully, XRV_TIMEOUT if some data was read, but not maxLength, XRV_TIMEOUTNODATA if no data was read at all.
Note
This function reads exactly the number of bytes as requested from the file unless the end of file boundary is encountered.

Implements IoInterface.

Definition at line 616 of file iointerfacefile.cpp.

References gotoRead(), m_handle, m_lastResult, m_readPos, XRV_ENDOFFILE, XRV_NOFILEOPEN, and XRV_OK.

◆ readTerminatedData()

XsResultValue IoInterfaceFile::readTerminatedData ( XsSize  maxLength,
unsigned char  terminator,
XsByteArray bdata 
)

Read data from the file and put it into the data buffer.

This function reads upp to the number of bytes as requested from the file. The function will also stop if the given terminator character is encountered. The terminator is included in the output buffer.

Parameters
maxLengthThe amount of data that will be read.
terminatorA character that will end the read operation if encountered.
bdataA buffer that will store the read data.
Returns
XRV_OK if the data was read successfully

Definition at line 655 of file iointerfacefile.cpp.

References gotoRead(), m_handle, m_lastResult, m_readPos, XRV_ENDOFFILE, XRV_NOFILEOPEN, and XRV_OK.

◆ setReadPosition()

XsResultValue IoInterfaceFile::setReadPosition ( XsFilePos  pos)
virtual

Set the new absolute read position.

The read position is checked against the filesize first.

Parameters
posThe new read position
Returns
XRV_OK if the read position was updated successfully

Reimplemented from IoInterface.

Definition at line 700 of file iointerfacefile.cpp.

References FSEEK, m_handle, m_lastResult, m_reading, m_readPos, XRV_NOFILEOPEN, and XRV_OK.

◆ setTimeout()

XsResultValue IoInterface::setTimeout ( uint32_t  ms)
virtualinherited

Set the default timeout value to use in blocking operations.

This function sets the value of m_timeout. There is no infinity value. The value 0 means that all blocking operations now become polling (non-blocking) operations. If the value is set to or from 0, the low-level serial port settings may be changed in addition to the m_timeout value.

Parameters
msThe new timeout in milliseconds
Returns
XRV_OK if the function succeeded

Reimplemented in SerialInterface, UsbInterface, and StreamInterface.

Definition at line 59 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ setWritePosition()

XsResultValue IoInterfaceFile::setWritePosition ( XsFilePos  pos = -1)
virtual

Set the new absolute write position.

The write position is checked against the filesize first.

Parameters
posThe new write position
Returns
XRV_OK if the write position was updated successfully

Reimplemented from IoInterface.

Definition at line 720 of file iointerfacefile.cpp.

References FSEEK, FSEEK_R, FTELL, m_handle, m_lastResult, m_reading, m_readOnly, m_writePos, XRV_NOFILEOPEN, XRV_OK, and XRV_READONLY.

◆ waitForData()

XsResultValue IoInterface::waitForData ( XsSize  maxLength,
XsByteArray data 
)
virtualinherited

Wait for data to arrive or a timeout to occur.

The function waits until maxLength data is available or until a timeout occurs. The function returns success if data is available or XsResultValue::TIMEOUT if a timeout occurred. A timeout value of 0 indicates that the default timeout stored in the class should be used.

Parameters
maxLengthThe maximum number of bytes to read before returning
dataThe buffer to put the read data in.
Returns
XRV_OK if maxLength bytes were read, XRV_TIMEOUT if less was read, XRV_TIMEOUTNODATA if nothing was read

Reimplemented in SerialInterface.

Definition at line 65 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ writeData()

XsResultValue IoInterfaceFile::writeData ( const XsByteArray data,
XsSize written = NULL 
)
virtual

Write the data contained in data to the device.

Parameters
dataThe data to write to the device.
writtenAn optional XsSize value that will receive the number of bytes that were actually written.
Returns
XRV_OK if the data was written successfully
Note
The function writes the given data to the file at the current write position.

Implements IoInterface.

Definition at line 750 of file iointerfacefile.cpp.

References gotoWrite(), m_fileSize, m_handle, m_lastResult, m_readOnly, m_writePos, XRV_ERROR, XRV_INSUFFICIENTSPACE, XRV_NOFILEOPEN, XRV_OK, XRV_OUTOFMEMORY, and XRV_READONLY.

Member Data Documentation

◆ m_filename

XsString IoInterfaceFile::m_filename
protected

Contains the name of the file that was last successfully opened.

Definition at line 39 of file iointerfacefile.h.

Referenced by closeAndDelete(), create(), getFileDate(), getName(), and open().

◆ m_fileSize

XsFilePos IoInterfaceFile::m_fileSize
protected

Contains the size of the file.

Definition at line 31 of file iointerfacefile.h.

Referenced by appendData(), closeAndDelete(), closeFile(), create(), deleteData(), getFileSize(), insertData(), IoInterfaceFile(), open(), and writeData().

◆ m_handle

XsFileHandle* IoInterfaceFile::m_handle
protected

◆ m_lastResult

XsResultValue IoInterfaceFile::m_lastResult
mutableprotected

◆ m_reading

bool IoInterfaceFile::m_reading
protected

Indicates whether the last operation was a read or write operation.

This value is used to check whether or not a seek is required to perform a requested read or write operation.

Definition at line 45 of file iointerfacefile.h.

Referenced by appendData(), closeAndDelete(), closeFile(), create(), gotoRead(), gotoWrite(), IoInterfaceFile(), open(), setReadPosition(), and setWritePosition().

◆ m_readOnly

bool IoInterfaceFile::m_readOnly
protected

Indicates if the file was opened in read-only mode.

Definition at line 47 of file iointerfacefile.h.

Referenced by appendData(), closeAndDelete(), closeFile(), create(), deleteData(), insertData(), IoInterfaceFile(), isReadOnly(), open(), setWritePosition(), and writeData().

◆ m_readPos

XsFilePos IoInterfaceFile::m_readPos
protected

The last read position in the file.

Definition at line 33 of file iointerfacefile.h.

Referenced by closeAndDelete(), closeFile(), create(), find(), getReadPosition(), gotoRead(), IoInterfaceFile(), open(), readData(), readTerminatedData(), and setReadPosition().

◆ m_writePos

XsFilePos IoInterfaceFile::m_writePos
protected



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019