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
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-11 01:48:46 +08:00
|
|
|
#include "stream.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-03-30 23:26:11 +08:00
|
|
|
/* TODO: add cmake step to check availability of <sys/xxx.h> header files */
|
2015-03-24 01:15:31 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
static gmio_bool_t gmio_stream_stdio_at_end(void* cookie)
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
return feof((FILE*) cookie);
|
2013-01-11 01:48:46 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
static int gmio_stream_stdio_error(void* cookie)
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
return ferror((FILE*) cookie);
|
2013-01-15 17:40:35 +08:00
|
|
|
}
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2015-03-13 18:04:14 +08:00
|
|
|
static size_t gmio_stream_stdio_read(
|
|
|
|
void* cookie, void* ptr, size_t item_size, size_t item_count)
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
return fread(ptr, item_size, item_count, (FILE*) cookie);
|
2013-01-11 01:48:46 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 18:04:14 +08:00
|
|
|
static size_t gmio_stream_stdio_write(
|
|
|
|
void* cookie, const void* ptr, size_t item_size, size_t item_count)
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
return fwrite(ptr, item_size, item_count, (FILE*) cookie);
|
2013-01-11 01:48:46 +08:00
|
|
|
}
|
|
|
|
|
2015-03-24 01:15:31 +08:00
|
|
|
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)
|
|
|
|
{
|
2015-03-30 23:26:11 +08:00
|
|
|
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
|
|
|
}
|