#ifndef __FILEUTIL_H__ # define __FILEUTIL_H__ #ifdef __cplusplus extern "C" { #endif #include #include #include /* Check whether a buffer has written data to flush. */ #define __fp_hasbuf_write(fp) \ (fp->buf && fp->bufdir == __FILE_BUF_WRITE && fp->bufpos > 0) /* Check whether a buffer has read data to yield. */ #define __fp_hasbuf_read(fp) \ (fp->buf && fp->bufdir == __FILE_BUF_READ && fp->bufpos < fp->bufread) /* Open a file descriptor in a pre-allocated FILE. */ int __fp_open(FILE *fp, int fd, bool use_buffering); /* Close fp and free all of its resources. */ void __fp_close(FILE *fp, bool free_fp); /* Remove the buffer on fp. */ void __fp_remove_buffer(FILE *fp); /* Set the specified buffer on fp (can be NULL), in which case malloc(). Returns true on success, false on error. */ bool __fp_set_buffer(FILE *fp, void *buf, size_t size); /* Set reading mode on the buffer. */ void __fp_buffer_mode_read(FILE *fp); /* Set writing mode on the buffer. */ void __fp_buffer_mode_write(FILE *fp); /* fread() with an extra option to stop at a certain character (usually '\n'); set -1 to disable. */ ssize_t __fp_fread2(FILE *fp, void *data, size_t request_size, int stop_char); /* Reads data from the buffer. Always reads as much as possible, up to size. Returns amount of data read; if >= 0 but < size, the buffer should be refilled. Returns -1 to indicate that unbuffered access should be used. Allows reading from temporary ungetc() buffers and cleans them. */ ssize_t __fp_buffered_read(FILE *fp, void *data, size_t size, int stop_char); /* Reads data from a file descriptor; updates the fdpos and sets the error indicator. Returns 0 on success, EOF on error. */ ssize_t __fp_read(FILE *fp, void *data, size_t size); /* Write data to a file descriptor; updates the fdpos and sets the error indicator. Returns 0 on success, EOF on error. */ ssize_t __fp_write(FILE *fp, void const *data, size_t size); /* Parse mode bits. Returns corresponding open() flags if successful, -1 if the mode is invalid. If [fp != NULL], also sets the fields of [fp]. Sets errno = EINVAL in case of error. */ int __fp_parse_mode(char const *mode, FILE *fp); #ifdef __cplusplus } #endif #endif /*__FILEUTIL_H__*/