2013-01-11 01:48:46 +08:00
|
|
|
#ifndef FOUG_C_STREAM_H
|
|
|
|
#define FOUG_C_STREAM_H
|
|
|
|
|
2013-01-16 00:56:24 +08:00
|
|
|
#include "global.h"
|
2013-03-03 04:51:08 +08:00
|
|
|
#include <stdio.h>
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Stream that can get input from an arbitrary data source or can write output to an arbitrary
|
|
|
|
* data sink.
|
2014-01-21 17:51:23 +08:00
|
|
|
*
|
|
|
|
* This is pretty much the same as
|
|
|
|
* <a href="http://www.gnu.org/software/libc/manual/html_mono/libc.html#Custom-Streams">
|
|
|
|
* custom streams</a> in the GNU C Library
|
|
|
|
*
|
|
|
|
* It uses a cookie being basically an opaque pointer on a hidden data type. The custom stream is
|
|
|
|
* implemented by defining hook functions that know how to read/write the data.
|
|
|
|
*
|
|
|
|
*/
|
2014-02-13 18:18:46 +08:00
|
|
|
struct foug_stream
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Opaque pointer on the user stream, passed as first argument to hook functions */
|
2013-03-03 04:51:08 +08:00
|
|
|
void* cookie;
|
2014-01-21 17:51:23 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Pointer on a function that checks end-of-stream indicator.
|
|
|
|
*
|
|
|
|
* Checks whether the end-of-stream indicator associated with stream pointed by \p cookie
|
|
|
|
* is set, returning FOUG_TRUE if is.
|
|
|
|
*
|
|
|
|
* The function should behaves like C standard feof()
|
|
|
|
*/
|
|
|
|
foug_bool_t (*at_end_func)(void* cookie);
|
|
|
|
|
|
|
|
/*! Pointer on a function that checks error indicator.
|
|
|
|
*
|
|
|
|
* Checks if the error indicator associated with stream pointed by \p cookie is set, returning
|
|
|
|
* a value different from zero if it is.
|
|
|
|
*
|
|
|
|
* The function should behaves like C standard ferror()
|
|
|
|
*/
|
|
|
|
int (*error_func)(void* cookie);
|
|
|
|
|
|
|
|
/*! Pointer on a function that reads block of data from stream.
|
|
|
|
*
|
|
|
|
* \details Reads an array of \p count elements, each one with a size of \p size bytes, from the
|
|
|
|
* stream pointed by \p cookie and stores them in the block of memory specified by \p ptr
|
|
|
|
*
|
|
|
|
* The function should behaves like C standard fread()
|
|
|
|
*
|
|
|
|
* \returns The total number of elements successfully read
|
|
|
|
*/
|
|
|
|
size_t (*read_func)(void* cookie, void* ptr, size_t size, size_t count);
|
|
|
|
|
|
|
|
/*! Pointer on a function that writes block of data to stream.
|
|
|
|
*
|
|
|
|
* \details Writes an array of \p count elements, each one with a size of \p size bytes, from the
|
|
|
|
* block of memory pointed by \p ptr to the current position in the stream pointed by \p cookie
|
|
|
|
*
|
|
|
|
* The function should behaves like C standard fwrite()
|
|
|
|
*
|
|
|
|
* \returns The total number of elements successfully written
|
|
|
|
*/
|
|
|
|
size_t (*write_func)(void* cookie, const void* ptr, size_t size, size_t count);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! Convenient typedef for struct foug_stream */
|
|
|
|
typedef struct foug_stream foug_stream_t;
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2013-03-27 20:00:48 +08:00
|
|
|
/* Initialization */
|
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Installs a null stream */
|
2013-03-03 04:51:08 +08:00
|
|
|
FOUG_LIB_EXPORT void foug_stream_set_null(foug_stream_t* stream);
|
2014-02-13 18:18:46 +08:00
|
|
|
|
|
|
|
/*! Configures \p stream for standard FILE* (cookie will hold \p file) */
|
2013-03-03 04:51:08 +08:00
|
|
|
FOUG_LIB_EXPORT void foug_stream_set_stdio(foug_stream_t* stream, FILE* file);
|
2013-01-11 01:48:46 +08:00
|
|
|
|
|
|
|
/* Services */
|
2013-02-21 21:51:29 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Safe and convenient function for foug_stream::at_end_func().
|
|
|
|
*
|
|
|
|
* Basically the same as : \code stream->at_end_func(stream->cookie) \endcode
|
|
|
|
*/
|
2013-01-15 23:45:01 +08:00
|
|
|
FOUG_LIB_EXPORT foug_bool_t foug_stream_at_end(foug_stream_t* stream);
|
2013-02-21 21:51:29 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Safe and convenient function for foug_stream::error_func().
|
|
|
|
*
|
|
|
|
* Basically the same as : \code stream->error_func(stream->cookie) \endcode
|
|
|
|
*/
|
2013-01-15 23:45:01 +08:00
|
|
|
FOUG_LIB_EXPORT int foug_stream_error(foug_stream_t* stream);
|
2013-02-21 21:51:29 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Safe and convenient function for foug_stream::read_func().
|
|
|
|
*
|
|
|
|
* Basically the same as : \code stream->read_func(stream->cookie) \endcode
|
|
|
|
*/
|
2013-01-15 23:45:01 +08:00
|
|
|
FOUG_LIB_EXPORT size_t foug_stream_read(foug_stream_t* stream,
|
2013-02-21 21:51:29 +08:00
|
|
|
void* ptr,
|
2014-02-13 18:18:46 +08:00
|
|
|
size_t size,
|
|
|
|
size_t count);
|
2013-02-21 21:51:29 +08:00
|
|
|
|
2014-02-13 18:18:46 +08:00
|
|
|
/*! Safe and convenient function for foug_stream::write_func().
|
|
|
|
*
|
|
|
|
* Basically the same as : \code stream->write_func(stream->cookie) \endcode
|
|
|
|
*/
|
2013-01-15 23:45:01 +08:00
|
|
|
FOUG_LIB_EXPORT size_t foug_stream_write(foug_stream_t* stream,
|
2013-02-21 21:51:29 +08:00
|
|
|
const void* ptr,
|
2014-02-13 18:18:46 +08:00
|
|
|
size_t size,
|
|
|
|
size_t count);
|
2013-02-21 21:51:29 +08:00
|
|
|
|
2013-01-11 01:48:46 +08:00
|
|
|
#endif /* FOUG_C_STREAM_H */
|