gmio/src/gmio_core/stream.c

78 lines
2.2 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
****************************************************************************/
#include "stream.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* TODO: add cmake step to check availability of <sys/xxx.h> header files */
#include <sys/types.h>
#include <sys/stat.h>
2015-03-19 23:34:53 +08:00
gmio_stream_t gmio_stream_null()
{
gmio_stream_t null_stream = { 0 };
return null_stream;
}
static gmio_bool_t gmio_stream_stdio_at_end(void* cookie)
{
2015-03-03 17:35:36 +08:00
return feof((FILE*) cookie);
}
static int gmio_stream_stdio_error(void* cookie)
{
2015-03-03 17:35:36 +08:00
return ferror((FILE*) cookie);
}
static size_t gmio_stream_stdio_read(
void* cookie, void* ptr, size_t item_size, size_t item_count)
{
2015-03-03 17:35:36 +08:00
return fread(ptr, item_size, item_count, (FILE*) cookie);
}
static size_t gmio_stream_stdio_write(
void* cookie, const void* ptr, size_t item_size, size_t item_count)
{
2015-03-03 17:35:36 +08:00
return fwrite(ptr, item_size, item_count, (FILE*) cookie);
}
static size_t gmio_stream_stdio_size(void* cookie)
{
struct stat stat_buf;
fstat(fileno((FILE*) cookie), &stat_buf);
return stat_buf.st_size;
}
static void gmio_stream_stdio_rewind(void* cookie)
{
rewind((FILE*) cookie);
}
2015-03-19 23:34:53 +08:00
gmio_stream_t gmio_stream_stdio(FILE* file)
{
gmio_stream_t stream = { 0 };
stream.cookie = file;
stream.at_end_func = gmio_stream_stdio_at_end;
stream.error_func = gmio_stream_stdio_error;
stream.read_func = gmio_stream_stdio_read;
stream.write_func = gmio_stream_stdio_write;
stream.size_func = gmio_stream_stdio_size;
stream.rewind_func = gmio_stream_stdio_rewind;
return stream;
2015-03-19 23:34:53 +08:00
}