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

Commmon functions to parse data. More...

#include <loaders/gp_io.h>

Go to the source code of this file.

Enumerations

enum  gp_io_fmt_types {
  GP_IO_CONST = 0x0000 , GP_IO_BYTE = 0x0100 , GP_IO_L2 = 0x0200 , GP_IO_L4 = 0x0300 ,
  GP_IO_B2 = 0x0400 , GP_IO_B4 = 0x0500 , GP_IO_ARRAY = 0x0600 , GP_IO_IGN = 0x0700 ,
  GP_IO_I1 = GP_IO_IGN | 1 , GP_IO_I2 = GP_IO_IGN | 2 , GP_IO_I3 = GP_IO_IGN | 3 , GP_IO_I4 = GP_IO_IGN | 4 ,
  GP_IO_PPSTR = 0x0800 , GP_IO_END = 0xff00
}
 Formatted binary read and write types. More...
 

Functions

int gp_io_readf (gp_io *self, const uint16_t *types,...)
 Parses a data accodingly to the array of types.
 
int gp_io_writef (gp_io *self, const uint16_t *types,...)
 Writes a data accodingly to the array of types.
 
int gp_io_read_b4 (gp_io *self, uint32_t *val)
 Reads a single 32bit big endian integer.
 
int gp_io_read_b2 (gp_io *self, uint16_t *val)
 Reads a single 16bit big endian integer.
 
char * gp_io_read_b2_utf16 (gp_io *io, size_t nchars)
 Reads unicode string stored in big endian utf16 format.
 

Detailed Description

Commmon functions to parse data.

Definition in file gp_io_parser.h.

Enumeration Type Documentation

◆ gp_io_fmt_types

Formatted binary read and write types.

This interface is a bit similar to a printf and scanf but instead of format string we have an array of 16bit unsigned integers describing the data.

Enumerator
GP_IO_CONST 

A constant byte in lower half of the 16bit integer.

Since this is defined as a zero values less than or equal to 0xff are interpreted as constant bytes and matched or written exactly.

GP_IO_BYTE 

Reads or writes a single byte.

GP_IO_L2 

Reads or writes two bytes in a little endian order.

The bytes are read or written into the corresponding pionter as a 16bit integer in a machine endinanity.

GP_IO_L4 

Reads or writes four bytes in a little endian order.

The bytes are read or written into the corresponding pionter as a 32bit integer in a machine endinanity.

GP_IO_B2 

Reads or writes two bytes in a big endian order.

The bytes are read or written into the corresponding pionter as a 16bit integer in a machine endinanity.

GP_IO_B4 

Reads or writes four bytes in a big endian order.

The bytes are read or written into the corresponding pionter as a 32bit integer in a machine endinanity.

GP_IO_ARRAY 

Pointer to byte array, size in lower half.

GP_IO_IGN 

Ignore bytes on read, size in lower half.

GP_IO_I1 

Ignore single byte on read.

GP_IO_I2 

Ignore two bytes on read.

GP_IO_I3 

Ignore three bytes on read.

GP_IO_I4 

Ignore four bytes on read.

GP_IO_PPSTR 

A photoshop pascal string.

First byte stores size and string is padded to even number bytes.

The lower half stores passed buffer size.

TODO: Unfinished

GP_IO_END 

End of the types array.

This is terminating entry for the array.

Definition at line 22 of file gp_io_parser.h.

Function Documentation

◆ gp_io_read_b2()

int gp_io_read_b2 ( gp_io * self,
uint16_t * val )

Reads a single 16bit big endian integer.

The integer is read as a big endian from the I/O and stored into the ppointer in the machine endianity.

Parameters
selfA readable I/O.
valA pointer to store the integer value into.
Returns
Zero on success non-zero otherwise.

◆ gp_io_read_b2_utf16()

char * gp_io_read_b2_utf16 ( gp_io * io,
size_t nchars )

Reads unicode string stored in big endian utf16 format.

Parameters
ioA readable I/O.
nbytesNumber of characters, the number of bytes is twice the number of the characters for utf16.
Returns
A newly allocated null terminated utf8 string or NULL in a case of a allocation failure.

◆ gp_io_read_b4()

int gp_io_read_b4 ( gp_io * self,
uint32_t * val )

Reads a single 32bit big endian integer.

The integer is read as a big endian from the I/O and stored into the ppointer in the machine endianity.

Parameters
selfA readable I/O.
valA pointer to store the integer value into.
Returns
Zero on success non-zero otherwise.

◆ gp_io_readf()

int gp_io_readf ( gp_io * self,
const uint16_t * types,
... )

Parses a data accodingly to the array of types.

Example usage:

uint16_t ihdr_size, width, height;
const uint16_t png_header[] = {
0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n',
GP_IO_B4, // IHDR size
'I', 'H', 'D', 'R',
GP_IO_B4, // Width
GP_IO_B4, // Height
};
int ret = gp_io_readf(io, png_header, &ihdr_size, &width, &height);
if (ret != ARRAY_SIZE(png_header) - 1) {
//error
}
printf("PNG image size %ux%u\n", (unsigned int)width, (unsigned int)height);
int gp_io_readf(gp_io *self, const uint16_t *types,...)
Parses a data accodingly to the array of types.
@ GP_IO_B4
Reads or writes four bytes in a big endian order.
@ GP_IO_END
End of the types array.
Parameters
selfA readable I/O.
typesAn array of enum gp_io_fmt_types terminated by GP_IO_END describing the values to be read.
...Pointers to data describes in the types array.
Returns
A number of read types or -1 in a case of a failure.

◆ gp_io_writef()

int gp_io_writef ( gp_io * self,
const uint16_t * types,
... )

Writes a data accodingly to the array of types.

Example usage:

uint16_t bitmap_header[] = {
'B', 'M', // signature
GP_IO_L4, // offset to pixels
0x00, 0x00, 0x00, 0x00, // reserved
GP_IO_L4, // file size
};
if (gp_io_writef(io, bitmap_header, file_size, pixel_offset))
return EIO;
@ GP_IO_L4
Reads or writes four bytes in a little endian order.
int gp_io_writef(gp_io *self, const uint16_t *types,...)
Writes a data accodingly to the array of types.
Parameters
selfA writeable I/O.
typesAn array of enum gp_io_fmt_types terminated by GP_IO_END describing the values to be written.
...Pointers to data describes in the types array.
Returns
A zero on success.