GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_io.h File Reference

I/O abstraction for gfxprim loaders. More...

#include <stdint.h>
#include <sys/types.h>
#include <core/gp_compiler.h>
#include <utils/gp_seek.h>
#include <loaders/gp_types.h>

Go to the source code of this file.

Data Structures

struct  gp_io
 An I/O abstraction. More...
 

Enumerations

enum  gp_io_mark_op { GP_IO_MARK , GP_IO_REWIND }
 Mark operation. More...
 
enum  gp_io_file_mode { GP_IO_RDONLY = 0x00 , GP_IO_WRONLY = 0x01 , GP_IO_RDWR = 0x02 }
 A file mode for gp_io_file(). More...
 

Functions

static ssize_t gp_io_read (gp_io *self, void *buf, size_t size)
 A wrapper for gp_io::read().
 
static ssize_t gp_io_write (gp_io *self, const void *buf, size_t size)
 A wrapper for gp_io::write().
 
static off_t gp_io_seek (gp_io *self, off_t off, enum gp_seek_whence whence)
 A wrapper for gp_io::seek().
 
static int gp_io_close (gp_io *self)
 A wrapper for gp_io::close().
 
static int gp_io_putb (gp_io *self, char b)
 A wrapper around gp_io::write() for a single byte.
 
static int gp_io_getb (gp_io *self)
 A wrapper around gp_io::read() for a single byte.
 
static off_t gp_io_tell (gp_io *self)
 Returns current offset.
 
static off_t gp_io_rewind (gp_io *self)
 Rewinds to start of the I/O stream.
 
static off_t gp_io_peek (gp_io *self, void *buf, size_t size)
 Reads bytes but does not remove then from I/O.
 
off_t gp_io_size (gp_io *self)
 Returns I/O stream size.
 
int gp_io_fill (gp_io *self, void *buf, size_t size)
 Fills whole buffer or returns error.
 
int gp_io_flush (gp_io *self, const void *buf, size_t size)
 Writes whole buffer or retuns error.
 
int gp_io_mark (gp_io *self, enum gp_io_mark_op op)
 Marks a current position or returns to mark in I/O stream.
 
int gp_io_printf (gp_io *self, const char *fmt,...)
 Printf like function.
 
gp_iogp_io_file (const char *path, enum gp_io_file_mode mode)
 Creates I/O from a file.
 
gp_iogp_io_mem (void *buf, size_t size, void(*free)(void *))
 Creates a readable I/O from a memory buffer.
 
gp_iogp_io_sub_io (gp_io *self, size_t size)
 Create a read-only sub I/O from a readable I/O.
 
gp_iogp_io_wbuffer (gp_io *self, size_t bsize)
 Creates a writeable buffered I/O on the top of the existing I/O.
 

Detailed Description

I/O abstraction for gfxprim loaders.

Definition in file gp_io.h.

Enumeration Type Documentation

◆ gp_io_file_mode

A file mode for gp_io_file().

Enumerator
GP_IO_RDONLY 

Opens file read only.

GP_IO_WRONLY 

Opens file write only and truncates it if it exists.

GP_IO_RDWR 

Opens a file read/write.

Definition at line 282 of file gp_io.h.

◆ gp_io_mark_op

Mark operation.

Enumerator
GP_IO_MARK 

Puts a mark into an I/O.

GP_IO_REWIND 

Rewinds the I/O to a mark.

Definition at line 247 of file gp_io.h.

Function Documentation

◆ gp_io_close()

static int gp_io_close ( gp_io * self)
inlinestatic

A wrapper for gp_io::close().

Finalizes writes, frees memory.

Parameters
selfAn I/O.
Returns
Zero on success -1 on a failure.

Definition at line 125 of file gp_io.h.

References gp_io::close.

◆ gp_io_file()

gp_io * gp_io_file ( const char * path,
enum gp_io_file_mode mode )

Creates I/O from a file.

The I/O is not buffered, calls to gp_io_read() and gp_io_write() are directly translated into read() and write() calls.

Parameters
pathA filesystem path.
modeOpen the I/O read-only, write-only or read-write. Write-only I/O files are truncated.
Returns
A newly allocated I/O or NULL on error and errno is set.

◆ gp_io_fill()

int gp_io_fill ( gp_io * self,
void * buf,
size_t size )

Fills whole buffer or returns error.

This is a gp_io_read() call with retries that attempts to fill the buffer as long as the underlying read returns any data. It will fail for example if we request more data than there is in a file.

Parameters
selfAn I/O.
bufA buffer to read to.
sizeA size of the buffer.
Returns
zero on success non-zero on failure.

◆ gp_io_flush()

int gp_io_flush ( gp_io * self,
const void * buf,
size_t size )

Writes whole buffer or retuns error.

This is a gp_io_write() call with retries that attempts to write whole buffer as long as the underlying write writes any data. It will fail for example if we attempt to write any data into a file on a full filesystem.

Parameters
selfAn I/O.
bufA buffer to write to.
sizeA size of the buffer.
Returns
Zero on succes non-zero on a failure.

◆ gp_io_getb()

static int gp_io_getb ( gp_io * self)
inlinestatic

A wrapper around gp_io::read() for a single byte.

Parameters
selfAn I/O.
Returns
A byte read from I/O or -1 on a failure.

Definition at line 149 of file gp_io.h.

References gp_io::read.

◆ gp_io_mark()

int gp_io_mark ( gp_io * self,
enum gp_io_mark_op op )

Marks a current position or returns to mark in I/O stream.

Only one mark can be placed on an I/O, subsequent calls to gp_io_mark() with GP_IO_MARK rewrite previous mark.

Parameters
selfAn I/O.
opAn operation either GP_IO_MARK or GP_IO_REWIND.
Returns
Zero on success, -1 on a failure.

◆ gp_io_mem()

gp_io * gp_io_mem ( void * buf,
size_t size,
void(* free )(void *) )

Creates a readable I/O from a memory buffer.

Parameters
bufA buffer with data.
sizeA size of the data buffer.
freeA callback that is called with the buf pointer as a parameter on gp_io_close(). If no callback is needed NULL should be passed.
Returns
A newly allocated and initialized I/O or NULL in a case of a allocation failure.

◆ gp_io_peek()

static off_t gp_io_peek ( gp_io * self,
void * buf,
size_t size )
inlinestatic

Reads bytes but does not remove then from I/O.

Parameters
selfAn I/O.
bufA buffer to read to.
sizeA size of the buffer.

//TODO: fix up the semantics

Returns
-1 on a failure.

Definition at line 194 of file gp_io.h.

References gp_io_read(), gp_io_seek(), and gp_io_tell().

◆ gp_io_printf()

int gp_io_printf ( gp_io * self,
const char * fmt,
... )

Printf like function.

Parameters
selfAn I/O.
fmtA printf()-like format.
...A printf()-like parameters.
Returns
Zero on success, non-zero on failure.

◆ gp_io_putb()

static int gp_io_putb ( gp_io * self,
char b )
inlinestatic

A wrapper around gp_io::write() for a single byte.

Parameters
selfAn I/O.
bA byte to write into the I/O.
Returns
Zero on success, non-zero on failure.

Definition at line 138 of file gp_io.h.

References gp_io::write.

◆ gp_io_read()

static ssize_t gp_io_read ( gp_io * self,
void * buf,
size_t size )
inlinestatic

A wrapper for gp_io::read().

Parameters
selfAn I/O.
bufA buffer to read to.
sizeA size of the buffer.
Returns
A number of bytes that were read <= size or -1 on error.

Definition at line 83 of file gp_io.h.

References gp_io::read.

Referenced by gp_io_peek().

◆ gp_io_rewind()

static off_t gp_io_rewind ( gp_io * self)
inlinestatic

Rewinds to start of the I/O stream.

Parameters
selfAn I/O.
Returns
The new offset (i.e. zero) or (off_t)-1 on a failure.

Definition at line 178 of file gp_io.h.

References gp_io::seek.

◆ gp_io_seek()

static off_t gp_io_seek ( gp_io * self,
off_t off,
enum gp_seek_whence whence )
inlinestatic

A wrapper for gp_io::seek().

Parameters
selfAn I/O.
offAn offset in the file.
whenceDefines to what the offset is relative to.
Returns
A new offset in the file or (off_t)-1 on a failure.

Definition at line 111 of file gp_io.h.

References gp_io::seek.

Referenced by gp_io_peek().

◆ gp_io_size()

off_t gp_io_size ( gp_io * self)

Returns I/O stream size.

Parameters
selfAn I/O.
Returns
A I/O size or (off_t)-1 in case that gp_io_seek(io, 0, SEEK_END) is not possible.

◆ gp_io_sub_io()

gp_io * gp_io_sub_io ( gp_io * self,
size_t size )

Create a read-only sub I/O from a readable I/O.

The sub I/O starts at current offset in the parent I/O (which is also point where gp_io_tell() for the new I/O will return zero) and continues for up to size bytes in the parent I/O.

WARNING: If you combine reading/writing in the Sub I/O and parent I/O the result is undefined.

Parameters
selfA parent I/O.
sizeA size limit of the sub I/O.
Returns
A newly allocated and initialized sub I/O or a NULL in a case of allocation failure.

◆ gp_io_tell()

static off_t gp_io_tell ( gp_io * self)
inlinestatic

Returns current offset.

Parameters
selfAn I/O.
Returns
Current offset in I/O.

Definition at line 166 of file gp_io.h.

References gp_io::seek.

Referenced by gp_io_peek().

◆ gp_io_wbuffer()

gp_io * gp_io_wbuffer ( gp_io * self,
size_t bsize )

Creates a writeable buffered I/O on the top of the existing I/O.

Parameters
selfA writeable I/O.
bsizeA buffer size. Passing zero as bsize select default buffer size.
Returns
A newly allocated I/O or NULL in a case of an allocation failure.

◆ gp_io_write()

static ssize_t gp_io_write ( gp_io * self,
const void * buf,
size_t size )
inlinestatic

A wrapper for gp_io::write().

Parameters
selfAn I/O.
bufA buffer to write to.
sizeA size of the buffer.
Returns
A number of bytes that were written <= size or -1 on error.

Definition at line 97 of file gp_io.h.

References gp_io::write.