GflsByteArray

GflsByteArray — Builds an array of bytes

Functions

Types and Values

Includes

#include <gfls/gfls.h>

Description

Builds an array of bytes.

GflsByteArray is similar to GByteArray and GString but with a smaller and different API, and ensures that at the end of the operation, the resulting GBytes doesn't consume more memory than necessary by doing a reallocation at the final size.

Functions

gfls_byte_array_new ()

GflsByteArray *
gfls_byte_array_new (void);

[skip]

Returns

a new and empty GflsByteArray. Free with gfls_byte_array_free_to_bytes().

[transfer full]

Since: 0.5


gfls_byte_array_free_to_bytes ()

GBytes *
gfls_byte_array_free_to_bytes (GflsByteArray *byte_array);

Transfers the data from byte_array into a new immutable GBytes, and frees byte_array .

If no bytes have been added to byte_array , NULL is returned.

A nul byte is added at the end, but should not be seen as part of the “real” data. In other words, the data contained in the resulting GBytes is always nul-terminated, but this is not included in the GBytes length.

Note that embedded nul bytes can also be present inside the “real” data.

This function ensures that the resulting GBytes doesn't consume more memory than necessary.

Parameters

byte_array

a GflsByteArray.

 

Returns

a GBytes, or NULL.

[transfer full][nullable]

Since: 0.5


gfls_byte_array_append ()

void
gfls_byte_array_append (GflsByteArray *byte_array,
                        const guint8 *data,
                        gsize size);

Adds the given bytes to the end of byte_array , by doing a copy.

If data is NULL or if size is 0, it will return immediately.

A write operation - as started with gfls_byte_array_begin_write() - must not be ongoing.

Parameters

byte_array

a GflsByteArray.

 

data

the byte data to be added.

[nullable]

size

the number of bytes to add.

 

Since: 0.5


gfls_byte_array_begin_write ()

guint8 *
gfls_byte_array_begin_write (GflsByteArray *byte_array,
                             gsize n_bytes_to_reserve);

Begins a new write operation, to add bytes to the end of byte_array .

When a successful write operation is finished, call gfls_byte_array_end_write(). If this function returns NULL, don't call gfls_byte_array_end_write().

Only one write operation at a time is supported.

Be careful and avoid out-of-bounds writes! For a simpler method that copies the bytes, see gfls_byte_array_append().

Parameters

byte_array

a GflsByteArray.

 

n_bytes_to_reserve

the maximum number of bytes that this write operation will support.

 

Returns

the buffer position where to begin to write the new bytes, or NULL on failure.

[nullable]

Since: 0.5


gfls_byte_array_end_write ()

void
gfls_byte_array_end_write (GflsByteArray *byte_array,
                           gsize n_bytes_written);

Ends a successful write operation that was started with gfls_byte_array_begin_write().

n_bytes_written must be less than or equal to n_bytes_to_reserve that was provided at the beginning of this write operation.

After calling this function, you can call again gfls_byte_array_begin_write() if you need to write more data to the byte_array .

Parameters

byte_array

a GflsByteArray.

 

n_bytes_written

the number of bytes added during this write operation.

 

Since: 0.5

Types and Values

GflsByteArray

typedef struct _GflsByteArray GflsByteArray;