GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_io.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2025 Cyril Hrubis <metan@ucw.cz>
4 */
5
11#ifndef LOADERS_GP_IO_H
12#define LOADERS_GP_IO_H
13
14#include <stdint.h>
15#include <sys/types.h>
16#include <core/gp_compiler.h>
17#include <utils/gp_seek.h>
18#include <loaders/gp_types.h>
19
23struct gp_io {
33 ssize_t (*read)(gp_io *self, void *buf, size_t size);
43 ssize_t (*write)(gp_io *self, const void *buf, size_t size);
52 off_t (*seek)(gp_io *self, off_t off, enum gp_seek_whence whence);
62 int (*close)(gp_io *self);
63
67 off_t mark;
68
69 char priv[];
70};
71
72#define GP_IO_PRIV(io) ((void *)(io)->priv)
73
83static inline ssize_t gp_io_read(gp_io *self, void *buf, size_t size)
84{
85 return self->read(self, buf, size);
86}
87
97static inline ssize_t gp_io_write(gp_io *self, const void *buf, size_t size)
98{
99 return self->write(self, buf, size);
100}
101
111static inline off_t gp_io_seek(gp_io *self, off_t off, enum gp_seek_whence whence)
112{
113 return self->seek(self, off, whence);
114}
115
125static inline int gp_io_close(gp_io *self)
126{
127 return self->close(self);
128}
129
138static inline int gp_io_putb(gp_io *self, char b)
139{
140 return self->write(self, &b, 1) != 1;
141}
142
149static inline int gp_io_getb(gp_io *self)
150{
151 unsigned char c;
152
153 if (self->read(self, &c, 1) != 1)
154 return -1;
155
156 return c;
157}
158
166static inline off_t gp_io_tell(gp_io *self)
167{
168 return self->seek(self, 0, GP_SEEK_CUR);
169}
170
178static inline off_t gp_io_rewind(gp_io *self)
179{
180 return self->seek(self, 0, GP_SEEK_SET);
181}
182
194static inline off_t gp_io_peek(gp_io *self, void *buf, size_t size)
195{
196 off_t cur_off = gp_io_tell(self);
197
198 if (gp_io_read(self, buf, size) != (ssize_t)size)
199 return -1;
200
201 return gp_io_seek(self, cur_off, GP_SEEK_SET);
202}
203
212off_t gp_io_size(gp_io *self);
213
227int gp_io_fill(gp_io *self, void *buf, size_t size);
228
242int gp_io_flush(gp_io *self, const void *buf, size_t size);
243
253
265int gp_io_mark(gp_io *self, enum gp_io_mark_op op);
266
276int gp_io_printf(gp_io *self, const char *fmt, ...)
277 GP_FMT_PRINTF(2, 3);
278
290
303gp_io *gp_io_file(const char *path, enum gp_io_file_mode mode);
304
315gp_io *gp_io_mem(void *buf, size_t size, void (*free)(void *));
316
333gp_io *gp_io_sub_io(gp_io *self, size_t size);
334
344gp_io *gp_io_wbuffer(gp_io *self, size_t bsize);
345
346#endif /* LOADERS_GP_IO_H */
A compiler dependent macros.
#define GP_FMT_PRINTF(fmt, list)
Expands to format printf attribute when supported by the compiler.
Definition gp_compiler.h:31
gp_io * gp_io_file(const char *path, enum gp_io_file_mode mode)
Creates I/O from a file.
static int gp_io_getb(gp_io *self)
A wrapper around gp_io::read() for a single byte.
Definition gp_io.h:149
off_t gp_io_size(gp_io *self)
Returns I/O stream size.
int gp_io_flush(gp_io *self, const void *buf, size_t size)
Writes whole buffer or retuns error.
static off_t gp_io_peek(gp_io *self, void *buf, size_t size)
Reads bytes but does not remove then from I/O.
Definition gp_io.h:194
static off_t gp_io_rewind(gp_io *self)
Rewinds to start of the I/O stream.
Definition gp_io.h:178
gp_io_file_mode
A file mode for gp_io_file().
Definition gp_io.h:282
@ GP_IO_WRONLY
Opens file write only and truncates it if it exists.
Definition gp_io.h:286
@ GP_IO_RDWR
Opens a file read/write.
Definition gp_io.h:288
@ GP_IO_RDONLY
Opens file read only.
Definition gp_io.h:284
static off_t gp_io_seek(gp_io *self, off_t off, enum gp_seek_whence whence)
A wrapper for gp_io::seek().
Definition gp_io.h:111
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.
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.
gp_io * gp_io_mem(void *buf, size_t size, void(*free)(void *))
Creates a readable I/O from a memory buffer.
static off_t gp_io_tell(gp_io *self)
Returns current offset.
Definition gp_io.h:166
gp_io_mark_op
Mark operation.
Definition gp_io.h:247
@ GP_IO_REWIND
Rewinds the I/O to a mark.
Definition gp_io.h:251
@ GP_IO_MARK
Puts a mark into an I/O.
Definition gp_io.h:249
static ssize_t gp_io_write(gp_io *self, const void *buf, size_t size)
A wrapper for gp_io::write().
Definition gp_io.h:97
static int gp_io_putb(gp_io *self, char b)
A wrapper around gp_io::write() for a single byte.
Definition gp_io.h:138
int gp_io_printf(gp_io *self, const char *fmt,...)
Printf like function.
static ssize_t gp_io_read(gp_io *self, void *buf, size_t size)
A wrapper for gp_io::read().
Definition gp_io.h:83
static int gp_io_close(gp_io *self)
A wrapper for gp_io::close().
Definition gp_io.h:125
gp_io * gp_io_sub_io(gp_io *self, size_t size)
Create a read-only sub I/O from a readable I/O.
int gp_io_fill(gp_io *self, void *buf, size_t size)
Fills whole buffer or returns error.
Seek contants and transformations.
gp_seek_whence
Seek constants.
Definition gp_seek.h:24
An I/O abstraction.
Definition gp_io.h:23
ssize_t(* write)(gp_io *self, const void *buf, size_t size)
A write() callback.
Definition gp_io.h:43
off_t mark
A mark to store offset to by the gp_io_mark().
Definition gp_io.h:67
int(* close)(gp_io *self)
A close() callback.
Definition gp_io.h:62
off_t(* seek)(gp_io *self, off_t off, enum gp_seek_whence whence)
A seek() callback.
Definition gp_io.h:52
ssize_t(* read)(gp_io *self, void *buf, size_t size)
A read() callback.
Definition gp_io.h:33