## FpiByteWriter

FpiByteWriter — Writes different integer, string and floating point types to a memory buffer and allows reading

## Functions

FpiByteWriter * | fpi_byte_writer_new ()
FpiByteWriter * | fpi_byte_writer_new_with_size ()
FpiByteWriter * | fpi_byte_writer_new_with_data ()
void | fpi_byte_writer_init ()
void | fpi_byte_writer_init_with_size ()
void | fpi_byte_writer_init_with_data ()
void | fpi_byte_writer_reset ()
guint8 * | fpi_byte_writer_reset_and_get_data ()
void | fpi_byte_writer_free ()
guint8 * | fpi_byte_writer_free_and_get_data ()
guint | fpi_byte_writer_get_remaining ()
gboolean | fpi_byte_writer_ensure_free_space ()
gboolean | fpi_byte_writer_put_uint8 ()
gboolean | fpi_byte_writer_put_uint16_be ()
gboolean | fpi_byte_writer_put_uint24_be ()
gboolean | fpi_byte_writer_put_uint32_be ()
gboolean | fpi_byte_writer_put_uint64_be ()
gboolean | fpi_byte_writer_put_uint16_le ()
gboolean | fpi_byte_writer_put_uint24_le ()
gboolean | fpi_byte_writer_put_uint32_le ()
gboolean | fpi_byte_writer_put_uint64_le ()
gboolean | fpi_byte_writer_put_int8 ()
gboolean | fpi_byte_writer_put_int16_be ()
gboolean | fpi_byte_writer_put_int24_be ()
gboolean | fpi_byte_writer_put_int32_be ()
gboolean | fpi_byte_writer_put_int64_be ()
gboolean | fpi_byte_writer_put_int16_le ()
gboolean | fpi_byte_writer_put_int24_le ()
gboolean | fpi_byte_writer_put_int32_le ()
gboolean | fpi_byte_writer_put_int64_le ()
gboolean | fpi_byte_writer_put_float32_be ()
gboolean | fpi_byte_writer_put_float64_be ()
gboolean | fpi_byte_writer_put_float32_le ()
gboolean | fpi_byte_writer_put_float64_le ()
gboolean | fpi_byte_writer_put_string_utf8 ()
gboolean | fpi_byte_writer_put_string_utf16 ()
gboolean | fpi_byte_writer_put_string_utf32 ()
gboolean | fpi_byte_writer_put_data ()
gboolean | fpi_byte_writer_fill ()

## Types and Values

| FpiByteWriter

## Description

FpiByteWriter provides a byte writer and reader that can write/read different integer and floating point types to/from a memory buffer. It provides functions for writing/reading signed/unsigned, little/big endian integers of 8, 16, 24, 32 and 64 bits and functions for reading little/big endian floating points numbers of 32 and 64 bits. It also provides functions to write/read NUL-terminated strings in various character encodings.

## Functions

### fpi_byte_writer_new ()

    FpiByteWriter *
    fpi_byte_writer_new (void);

Creates a new, empty FpiByteWriter instance

Free-function: fpi_byte_writer_free

[skip]

#### Returns

a new, empty FpiByteWriter instance.

[transfer full]

### fpi_byte_writer_new_with_size ()

    FpiByteWriter *
    fpi_byte_writer_new_with_size (guint size,
                                   gboolean fixed);

Creates a new FpiByteWriter instance with the given initial data size.

Free-function: fpi_byte_writer_free

[skip]

#### Parameters

size | Initial size of data |
fixed | If `TRUE` the data can't be reallocated |

#### Returns

a new FpiByteWriter instance.

[transfer full]

### fpi_byte_writer_new_with_data ()

    FpiByteWriter *
    fpi_byte_writer_new_with_data (guint8 *data,
                                   guint size,
                                   gboolean initialized);

Creates a new FpiByteWriter instance with the given memory area. If `initialized` is `TRUE` it is possible to read `size` bytes from the FpiByteWriter from the beginning.

Free-function: fpi_byte_writer_free

[skip]

#### Parameters

data | Memory area for writing |
size | Size of `data` in bytes |
initialized | If `TRUE` the complete data can be read from the beginning |

#### Returns

a new FpiByteWriter instance.

[transfer full]

### fpi_byte_writer_init ()

    void
    fpi_byte_writer_init (FpiByteWriter *writer);

Initializes `writer` to an empty instance

#### Parameters

writer | FpiByteWriter instance |

### fpi_byte_writer_init_with_size ()

    void
    fpi_byte_writer_init_with_size (FpiByteWriter *writer,
                                    guint size,
                                    gboolean fixed);

Initializes `writer` with the given initial data size.

#### Parameters

writer | FpiByteWriter instance |
size | Initial size of data |
fixed | If `TRUE` the data can't be reallocated |

### fpi_byte_writer_init_with_data ()

    void
    fpi_byte_writer_init_with_data (FpiByteWriter *writer,
                                    guint8 *data,
                                    guint size,
                                    gboolean initialized);

Initializes `writer` with the given memory area. If `initialized` is `TRUE` it is possible to read `size` bytes from the FpiByteWriter from the beginning.

#### Parameters

writer | FpiByteWriter instance |
data | Memory area for writing. | [array length=size][transfer none]
size | Size of `data` in bytes |
initialized | If `TRUE` the complete data can be read from the beginning |

### fpi_byte_writer_reset ()

    void
    fpi_byte_writer_reset (FpiByteWriter *writer);

Resets `writer` and frees the data if it's owned by `writer` .

#### Parameters

writer | FpiByteWriter instance |

### fpi_byte_writer_reset_and_get_data ()

    guint8 *
    fpi_byte_writer_reset_and_get_data (FpiByteWriter *writer);

Resets `writer` and returns the current data.

Free-function: g_free

#### Parameters

writer | FpiByteWriter instance |

#### Returns

the current data. `g_free()` after usage.

[array][transfer full]

### fpi_byte_writer_free ()

    void
    fpi_byte_writer_free (FpiByteWriter *writer);

Frees `writer` and all memory allocated by it.

#### Parameters

writer | FpiByteWriter instance. | [in][transfer full]

### fpi_byte_writer_free_and_get_data ()

    guint8 *
    fpi_byte_writer_free_and_get_data (FpiByteWriter *writer);

Frees `writer` and all memory allocated by it except the current data, which is returned.

Free-function: g_free

#### Parameters

writer | FpiByteWriter instance. | [in][transfer full]

#### Returns

the current data. `g_free()` after usage.

[transfer full]

### fpi_byte_writer_get_remaining ()

    guint
    fpi_byte_writer_get_remaining (const FpiByteWriter *writer);

Returns the remaining size of data that can still be written. If -1 is returned the remaining size is only limited by system resources.

#### Parameters

writer | FpiByteWriter instance |

#### Returns

the remaining size of data that can still be written

### fpi_byte_writer_ensure_free_space ()

    gboolean
    fpi_byte_writer_ensure_free_space (FpiByteWriter *writer,
                                       guint size);

Checks if enough free space from the current write cursor is available and reallocates if necessary.

#### Parameters

writer | FpiByteWriter instance |
size | Number of bytes that should be available |

#### Returns

`TRUE` if at least `size` bytes are still available

### fpi_byte_writer_put_uint8 ()

    gboolean
    fpi_byte_writer_put_uint8 (FpiByteWriter *writer,
                               guint8 val);

Writes a unsigned 8 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint16_be ()

    gboolean
    fpi_byte_writer_put_uint16_be (FpiByteWriter *writer,
                                   guint16 val);

Writes a unsigned big endian 16 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint24_be ()

    gboolean
    fpi_byte_writer_put_uint24_be (FpiByteWriter *writer,
                                   guint32 val);

Writes a unsigned big endian 24 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint32_be ()

    gboolean
    fpi_byte_writer_put_uint32_be (FpiByteWriter *writer,
                                   guint32 val);

Writes a unsigned big endian 32 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint64_be ()

    gboolean
    fpi_byte_writer_put_uint64_be (FpiByteWriter *writer,
                                   guint64 val);

Writes a unsigned big endian 64 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint16_le ()

    gboolean
    fpi_byte_writer_put_uint16_le (FpiByteWriter *writer,
                                   guint16 val);

Writes a unsigned little endian 16 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint24_le ()

    gboolean
    fpi_byte_writer_put_uint24_le (FpiByteWriter *writer,
                                   guint32 val);

Writes a unsigned little endian 24 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint32_le ()

    gboolean
    fpi_byte_writer_put_uint32_le (FpiByteWriter *writer,
                                   guint32 val);

Writes a unsigned little endian 32 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_uint64_le ()

    gboolean
    fpi_byte_writer_put_uint64_le (FpiByteWriter *writer,
                                   guint64 val);

Writes a unsigned little endian 64 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int8 ()

    gboolean
    fpi_byte_writer_put_int8 (FpiByteWriter *writer,
                              gint8 val);

Writes a signed 8 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int16_be ()

    gboolean
    fpi_byte_writer_put_int16_be (FpiByteWriter *writer,
                                  gint16 val);

Writes a signed big endian 16 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int24_be ()

    gboolean
    fpi_byte_writer_put_int24_be (FpiByteWriter *writer,
                                  gint32 val);

Writes a signed big endian 24 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int32_be ()

    gboolean
    fpi_byte_writer_put_int32_be (FpiByteWriter *writer,
                                  gint32 val);

Writes a signed big endian 32 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int64_be ()

    gboolean
    fpi_byte_writer_put_int64_be (FpiByteWriter *writer,
                                  gint64 val);

Writes a signed big endian 64 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int16_le ()

    gboolean
    fpi_byte_writer_put_int16_le (FpiByteWriter *writer,
                                  gint16 val);

Writes a signed little endian 16 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int24_le ()

    gboolean
    fpi_byte_writer_put_int24_le (FpiByteWriter *writer,
                                  gint32 val);

Writes a signed little endian 24 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int32_le ()

    gboolean
    fpi_byte_writer_put_int32_le (FpiByteWriter *writer,
                                  gint32 val);

Writes a signed little endian 32 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_int64_le ()

    gboolean
    fpi_byte_writer_put_int64_le (FpiByteWriter *writer,
                                  gint64 val);

Writes a signed little endian 64 bit integer to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_float32_be ()

    gboolean
    fpi_byte_writer_put_float32_be (FpiByteWriter *writer,
                                    gfloat val);

Writes a big endian 32 bit float to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_float64_be ()

    gboolean
    fpi_byte_writer_put_float64_be (FpiByteWriter *writer,
                                    gdouble val);

Writes a big endian 64 bit float to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_float32_le ()

    gboolean
    fpi_byte_writer_put_float32_le (FpiByteWriter *writer,
                                    gfloat val);

Writes a little endian 32 bit float to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_float64_le ()

    gboolean
    fpi_byte_writer_put_float64_le (FpiByteWriter *writer,
                                    gdouble val);

Writes a little endian 64 bit float to `writer` .

#### Parameters

writer | FpiByteWriter instance |
val | Value to write |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_string_utf8 ()

    gboolean
    fpi_byte_writer_put_string_utf8 (FpiByteWriter *writer,
                                     const gchar *data);

Writes a NUL-terminated UTF8 string to `writer` (including the terminator).

#### Parameters

writer | FpiByteWriter instance |
data | UTF8 string to write. | [transfer none]

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_string_utf16 ()

    gboolean
    fpi_byte_writer_put_string_utf16 (FpiByteWriter *writer,
                                      const guint16 *data);

Writes a NUL-terminated UTF16 string to `writer` (including the terminator).

#### Parameters

writer | FpiByteWriter instance |
data | UTF16 string to write. | [transfer none][array zero-terminated=1]

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_string_utf32 ()

    gboolean
    fpi_byte_writer_put_string_utf32 (FpiByteWriter *writer,
                                      const guint32 *data);

Writes a NUL-terminated UTF32 string to `writer` (including the terminator).

#### Parameters

writer | FpiByteWriter instance |
data | UTF32 string to write. | [transfer none][array zero-terminated=1]

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_put_data ()

    gboolean
    fpi_byte_writer_put_data (FpiByteWriter *writer,
                              const guint8 *data,
                              guint size);

Writes `size` bytes of `data` to `writer` .

#### Parameters

writer | FpiByteWriter instance |
data | Data to write. | [transfer none][array length=size]
size | Size of `data` in bytes |

#### Returns

`TRUE` if the value could be written

### fpi_byte_writer_fill ()

    gboolean
    fpi_byte_writer_fill (FpiByteWriter *writer,
                          guint8 value,
                          guint size);

Writes `size` bytes containing `value` to `writer` .

#### Parameters

writer | FpiByteWriter instance |
value | Value to be written |
size | Number of bytes to be written |

#### Returns

`TRUE` if the value could be written

## Types and Values

### FpiByteWriter

    typedef struct {
      FpiByteReader parent;

      guint alloc_size;

      gboolean fixed;
      gboolean owned;
    } FpiByteWriter;

A byte writer instance.

#### Members

FpiByteReader `parent`; | FpiByteReader parent |
guint `alloc_size`; | Allocation size of the data |
gboolean `fixed`; | If `TRUE` no reallocations are allowed |
gboolean `owned`; | If `FALSE` no reallocations are allowed and copies of data are returned |