SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
ostream.h File Reference

SofaBuffers C - Output stream encoder for Sofab messages. More...

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "sofab/sofab.h"
Include dependency graph for ostream.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  sofab_ostream
 

Macros

#define SOFAB_OSTREAM_EXTERN   extern
 
#define SOFAB_LAZY_SEQ_DEPTH   8
 Sofab output stream context.
 

Typedefs

typedef struct sofab_ostream sofab_ostream_t
 Opaque Sofab output stream context.
 
typedef void(* sofab_ostream_flush_cb_t) (sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr)
 Output stream flush callback type.
 

Functions

void sofab_ostream_init (sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset, sofab_ostream_flush_cb_t flush, void *usrptr)
 Initialize an Sofab output stream context.
 
size_t sofab_ostream_flush (sofab_ostream_t *ctx)
 Flush any pending bytes from the output stream.
 
size_t sofab_ostream_bytes_used (sofab_ostream_t *ctx)
 Get the number of bytes used in the current buffer.
 
void sofab_ostream_buffer_set (sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset)
 Replace the active buffer used by the output stream.
 
sofab_ret_t sofab_ostream_write_unsigned (sofab_ostream_t *ctx, sofab_id_t id, sofab_unsigned_t value)
 Write an unsigned integer field.
 
sofab_ret_t sofab_ostream_write_signed (sofab_ostream_t *ctx, sofab_id_t id, sofab_signed_t value)
 Write a signed integer field.
 
sofab_ret_t sofab_ostream_write_fixlen (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t datalen, sofab_fixlentype_t type)
 Write a fixed-length field.
 
static sofab_ret_t sofab_ostream_write_boolean (sofab_ostream_t *ctx, sofab_id_t id, bool value)
 Write a boolean value (1 byte).
 
static sofab_ret_t sofab_ostream_write_fp32 (sofab_ostream_t *ctx, sofab_id_t id, float value)
 Write a 32-bit floating-point value.
 
static sofab_ret_t sofab_ostream_write_fp64 (sofab_ostream_t *ctx, sofab_id_t id, double value)
 Write a 64-bit floating-point value.
 
static sofab_ret_t sofab_ostream_write_string (sofab_ostream_t *ctx, sofab_id_t id, const char *text)
 Write a null-terminated string.
 
static sofab_ret_t sofab_ostream_write_blob (sofab_ostream_t *ctx, sofab_id_t id, const void *data, size_t size)
 Write a binary blob field.
 
sofab_ret_t sofab_ostream_write_array_of_unsigned (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
 Write an array of unsigned integers.
 
sofab_ret_t sofab_ostream_write_array_of_signed (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
 Write an array of signed integers.
 
sofab_ret_t sofab_ostream_write_array_of_fixlen (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size, sofab_fixlentype_t type)
 Write an array of fixed-length elements.
 
static sofab_ret_t sofab_ostream_write_array_of_i8 (sofab_ostream_t *ctx, sofab_id_t id, const int8_t *var, int32_t element_count)
 Writes an array of 8-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u8 (sofab_ostream_t *ctx, sofab_id_t id, const uint8_t *var, int32_t element_count)
 Writes an array of 8-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i16 (sofab_ostream_t *ctx, sofab_id_t id, const int16_t *var, int32_t element_count)
 Writes an array of 16-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u16 (sofab_ostream_t *ctx, sofab_id_t id, const uint16_t *var, int32_t element_count)
 Writes an array of 16-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i32 (sofab_ostream_t *ctx, sofab_id_t id, const int32_t *var, int32_t element_count)
 Writes an array of 32-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u32 (sofab_ostream_t *ctx, sofab_id_t id, const uint32_t *var, int32_t element_count)
 Writes an array of 32-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i64 (sofab_ostream_t *ctx, sofab_id_t id, const int64_t *var, int32_t element_count)
 Writes an array of 64-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u64 (sofab_ostream_t *ctx, sofab_id_t id, const uint64_t *var, int32_t element_count)
 Writes an array of 64-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_fp32 (sofab_ostream_t *ctx, sofab_id_t id, const float *data, int32_t element_count)
 Write an array of 32-bit floating-point values.
 
static sofab_ret_t sofab_ostream_write_array_of_fp64 (sofab_ostream_t *ctx, sofab_id_t id, const double *data, int32_t element_count)
 Write an array of 64-bit floating-point values.
 
sofab_ret_t sofab_ostream_write_sequence_begin (sofab_ostream_t *ctx, sofab_id_t id)
 Begin encoding a nested sequence (start marker).
 
sofab_ret_t sofab_ostream_write_sequence_begin_lazy (sofab_ostream_t *ctx, sofab_id_t id)
 Begin a nested sequence whose header is held back until it has content.
 
sofab_ret_t sofab_ostream_write_sequence_end_keep (sofab_ostream_t *ctx)
 End a nested sequence, keeping its frame even without content.
 
sofab_ret_t sofab_ostream_write_sequence_end (sofab_ostream_t *ctx)
 End encoding a nested sequence (end marker).
 

Detailed Description

SofaBuffers C - Output stream encoder for Sofab messages.

This module implements a streaming encoder for SofaBuffers (Sofab), a compact, TLV-like (Type-Length-Value) binary message format. The encoder writes fields into a user-provided buffer and calls a user-supplied flush callback when the buffer is full or when the user explicitly requests a flush.

Typical usage:

  • Initialize an sofab_ostream_t with sofab_ostream_init(), providing a buffer and optionally a flush callback.
  • Use the sofab_ostream_write_*() functions to append fields (unsigned, signed, fixed-length, arrays, nested sequences).
  • If a flush callback is provided, it will be invoked automatically when the internal buffer needs space; the callback is responsible for handling (sending, persisting) the buffered bytes and usually calls sofab_ostream_buffer_set() to provide a fresh buffer.
  • Call sofab_ostream_flush() to flush any remaining bytes before teardown.

Ownership and semantics (brief):

  • The sofab_ostream_t holds pointers into a buffer supplied by the caller. The stream never allocates or frees that buffer. The caller owns the memory and must ensure it remains valid until replaced with sofab_ostream_buffer_set() or the stream is destroyed.
  • The flush callback is optional (may be NULL). If NULL and the buffer becomes full, write calls will return an appropriate error code.

SPDX-License-Identifier: MIT

Definition in file ostream.h.