gmio/tests/stream_buffer.c

103 lines
3.6 KiB
C
Raw Normal View History

2015-03-03 00:38:33 +08:00
/****************************************************************************
** GeomIO Library
** Copyright FougSys (2 Mar. 2015)
** contact@fougsys.fr
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** This software is governed by the CeCILL-B license under French law and
** abiding by the rules of distribution of free software. You can use,
** modify and/ or redistribute the software under the terms of the CeCILL-B
** license as circulated by CEA, CNRS and INRIA at the following URL
2015-03-30 15:05:25 +08:00
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
2015-03-03 00:38:33 +08:00
****************************************************************************/
2014-02-04 19:41:03 +08:00
#include "stream_buffer.h"
#include <string.h>
static gmio_bool_t gmio_stream_buffer_at_end(void* cookie)
2014-02-04 19:41:03 +08:00
{
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
2015-03-03 17:35:36 +08:00
return buff->pos >= buff->len;
2014-02-04 19:41:03 +08:00
}
static int gmio_stream_buffer_error(void* cookie)
2014-02-04 19:41:03 +08:00
{
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
2015-03-03 17:35:36 +08:00
return buff == NULL || buff->pos > buff->len;
2014-02-04 19:41:03 +08:00
}
static size_t gmio_stream_buffer_read(
void* cookie, void* ptr, size_t item_size, size_t item_count)
2014-02-04 19:41:03 +08:00
{
2015-03-03 17:35:36 +08:00
if (item_size > 0 && item_count > 0) {
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
const void* buff_ptr =
buff->readonly_ptr != NULL ?
buff->readonly_ptr : buff->readwrite_ptr;
2015-03-03 17:35:36 +08:00
const size_t buff_remaining_size = buff->len - buff->pos;
const size_t wanted_read_size = item_size * item_count;
const size_t next_read_size =
wanted_read_size <= buff_remaining_size ?
wanted_read_size : buff_remaining_size;
2015-03-03 17:35:36 +08:00
const size_t next_read_item_count = next_read_size / item_size;
2014-02-04 19:41:03 +08:00
memcpy(ptr,
(const char*)buff_ptr + buff->pos,
next_read_item_count * item_size);
2015-03-03 17:35:36 +08:00
buff->pos += next_read_item_count * item_size;
return next_read_item_count;
}
else {
return 0;
}
2014-02-04 19:41:03 +08:00
}
static size_t gmio_stream_buffer_write(
void* cookie, const void* ptr, size_t item_size, size_t item_count)
2014-02-04 19:41:03 +08:00
{
2015-03-03 17:35:36 +08:00
if (item_size > 0 && item_count > 0) {
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
2015-03-03 17:35:36 +08:00
const size_t buff_remaining_size = buff->len - buff->pos;
const size_t wanted_write_size = item_size * item_count;
const size_t next_write_size =
wanted_write_size <= buff_remaining_size ?
wanted_write_size : buff_remaining_size;
2015-03-03 17:35:36 +08:00
const size_t next_write_item_count = next_write_size / item_size;
2014-02-04 19:41:03 +08:00
memcpy((char*)buff->readwrite_ptr + buff->pos,
ptr,
next_write_item_count * item_size);
2015-03-03 17:35:36 +08:00
buff->pos += next_write_item_count * item_size;
return next_write_item_count;
}
else {
return 0;
}
2014-02-04 19:41:03 +08:00
}
static size_t gmio_stream_buffer_size(void* cookie)
{
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
return buff->len;
}
static void gmio_stream_buffer_rewind(void* cookie)
{
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
buff->pos = 0;
}
void gmio_stream_set_buffer(gmio_stream_t *stream, gmio_stream_buffer_t* buff)
2014-02-04 19:41:03 +08:00
{
2015-03-03 17:35:36 +08:00
stream->cookie = buff;
stream->at_end_func = gmio_stream_buffer_at_end;
stream->error_func = gmio_stream_buffer_error;
stream->read_func = gmio_stream_buffer_read;
stream->write_func = gmio_stream_buffer_write;
stream->size_func = gmio_stream_buffer_size;
stream->rewind_func = gmio_stream_buffer_rewind;
2014-02-04 19:41:03 +08:00
}