silc_buffer_format

SYNOPSIS

    int silc_buffer_format(SilcBuffer dst, ...);

DESCRIPTION

Formats a buffer from a variable argument list. Returns -1 if the system is out of memory and the length of the formatted buffer otherwise. The buffer is enlarged automatically during formatting, if it doesn't already have enough space. Sets silc_errno in case of error.

EXAMPLE

    Three basic ways of using silc_buffer_format:

    // Statically allocated zero size buffer
    SilcBufferStruct buffer;

    memset(&buffer, 0, sizeof(buffer));
    ret = silc_buffer_format(&buffer,
                             SILC_STR_UINT32(intval),
                             SILC_STR_UINT8(charval),
                             SILC_STR_UINT32(intval),
                             SILC_STR_UINT16(str_len),
                             SILC_STR_DATA(str, str_len),
                             SILC_STR_END);
    if (ret < 0)
      error;

    // Free the allocated data
    silc_buffer_purge(&buffer);

    // Dynamically allocated zero size buffer
    SilcBuffer buf;
    buf = silc_buffer_alloc(0);
    ret = silc_buffer_format(buf,
                             SILC_STR_UINT32(intval),
                             SILC_STR_UINT8(charval),
                             SILC_STR_END);
    if (ret < 0)
      error;

    // Free the allocated buffer
    silc_buffer_free(buf);

    // Dynamically allocated buffer with enough space
    SilcBuffer buf;
    buf = silc_buffer_alloc(2 + str_len);
    ret = silc_buffer_format(buf,
                             SILC_STR_UINT16(str_len),
                             SILC_STR_DATA(str, str_len),
                             SILC_STR_END);
    if (ret < 0)
      error;