2018-07-19 16:04:20 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
IO Write
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/io/write.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/memContext.h"
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
IO write object
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
struct IoWrite
|
|
|
|
{
|
|
|
|
MemContext *memContext; // Mem context of driver
|
|
|
|
void *driver; // Driver object
|
2018-07-24 21:08:27 -04:00
|
|
|
IoFilterGroup *filterGroup; // IO filters
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
IoWriteOpen open; // Driver open
|
2018-07-24 21:08:27 -04:00
|
|
|
IoWriteProcess write; // Driver write
|
2018-07-19 16:04:20 -04:00
|
|
|
IoWriteClose close; // Driver close
|
2018-07-24 21:08:27 -04:00
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
size_t size; // Total bytes written
|
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Create a new write IO
|
|
|
|
|
|
|
|
Allocations will be in the memory context of the caller.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
IoWrite *
|
2018-07-24 21:08:27 -04:00
|
|
|
ioWriteNew(void *driver, IoWriteOpen open, IoWriteProcess write, IoWriteClose close)
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(VOIDP, driver);
|
|
|
|
FUNCTION_DEBUG_PARAM(FUNCTIONP, open);
|
2018-07-24 21:08:27 -04:00
|
|
|
FUNCTION_DEBUG_PARAM(FUNCTIONP, write);
|
2018-07-19 16:04:20 -04:00
|
|
|
FUNCTION_DEBUG_PARAM(FUNCTIONP, close);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(driver != NULL);
|
2018-07-24 21:08:27 -04:00
|
|
|
FUNCTION_TEST_ASSERT(write != NULL);
|
2018-07-19 16:04:20 -04:00
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
|
|
|
IoWrite *this = memNew(sizeof(IoWrite));
|
|
|
|
this->memContext = memContextCurrent();
|
|
|
|
this->driver = driver;
|
|
|
|
this->open = open;
|
2018-07-24 21:08:27 -04:00
|
|
|
this->write = write;
|
2018-07-19 16:04:20 -04:00
|
|
|
this->close = close;
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT(IO_WRITE, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Open the IO
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
ioWriteOpen(IoWrite *this)
|
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(IO_WRITE, this);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
|
|
|
if (this->open != NULL)
|
|
|
|
this->open(this->driver);
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Write data to IO
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
ioWrite(IoWrite *this, const Buffer *buffer)
|
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(IO_WRITE, this);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
|
|
|
// Only write if there is data to write
|
|
|
|
if (buffer != NULL && bufSize(buffer) > 0)
|
|
|
|
{
|
2018-07-24 21:08:27 -04:00
|
|
|
// Apply filters
|
|
|
|
if (this->filterGroup != NULL)
|
|
|
|
ioFilterGroupProcess(this->filterGroup, buffer);
|
|
|
|
|
|
|
|
// Write data
|
|
|
|
this->write(this->driver, buffer);
|
2018-07-19 16:04:20 -04:00
|
|
|
this->size += bufUsed(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Close the IO
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
ioWriteClose(IoWrite *this)
|
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(IO_WRITE, this);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
// Close the filter group and gather results
|
|
|
|
if (this->filterGroup != NULL)
|
|
|
|
ioFilterGroupClose(this->filterGroup);
|
|
|
|
|
|
|
|
// Close the driver if there is a close function
|
2018-07-19 16:04:20 -04:00
|
|
|
if (this->close != NULL)
|
|
|
|
this->close(this->driver);
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Get/set filters
|
|
|
|
|
|
|
|
Filters must be set before open and cannot be reset.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
const IoFilterGroup *
|
|
|
|
ioWriteFilterGroup(const IoWrite *this)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_BEGIN();
|
|
|
|
FUNCTION_TEST_PARAM(IO_WRITE, this);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_TEST_END();
|
|
|
|
|
|
|
|
FUNCTION_TEST_RESULT(IO_FILTER_GROUP, this->filterGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup)
|
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(IO_WRITE, this);
|
|
|
|
FUNCTION_DEBUG_PARAM(IO_FILTER_GROUP, filterGroup);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_TEST_ASSERT(filterGroup != NULL);
|
|
|
|
FUNCTION_TEST_ASSERT(this->filterGroup == NULL);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
|
|
|
this->filterGroup = filterGroup;
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Total bytes written
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
size_t
|
|
|
|
ioWriteSize(const IoWrite *this)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_BEGIN();
|
|
|
|
FUNCTION_TEST_PARAM(IO_WRITE, this);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_TEST_END();
|
|
|
|
|
|
|
|
FUNCTION_TEST_RESULT(SIZE, this->size);
|
|
|
|
}
|