/* =====================================
 * Generated by Mod-c 0.26.5
 * MODULE dynbuf
 * ===================================== */

#include "dynbuf.h"

void dynbuf(void);

/**
 * Mod-c
 * By M. Scott Reynolds
 * Date 20 February 2026
 *
 * dynbuf.c - Dynamic null-terminated string buffer with exponential growth.
 */
#include "stddef.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/**
 * Dynamic string buffer with exponential growth and null-termination.
 * All functions maintain null-termination (data[used] == '\0').
 * The bufffer owns its data pointer - caller must call dynbuf_free().
 */
static const cardinal INITIAL_BUFFER_SIZE = 8192u;
/**
 * Initialize a dynamic buffer with initial capacity.
 * Exits program on allocation failure.
 */
void dynbuf_init(DynBuf* b)
{
    if (b == nil)
    {
        fprintf(stderr, "ERROR: dynbuf_init: NULL buffer pointer\n");
        exit(1);
    }
    ((*b)).size = INITIAL_BUFFER_SIZE;
    ((*b)).used = 0;
    ((*b)).data = malloc(((*b)).size);
    if (!((*b)).data)
    {
        fprintf(stderr, "ERROR: dynbuf_init: malloc(%zu) failed\n", ((*b)).size);
        exit(1);
    }
    ((*b)).data[0] = '\0';
}

/**
 * Append a null-terminated string to the buffer.
 * Automatically grows if needed.
 * Exits on reallocation failure.
 */
void dynbuf_appendn(DynBuf* b, const char* str, size_t str_len)
{
    if (b == nil || ((*b)).data == nil || str == nil)
    {
        fprintf(stderr, "ERROR: dynbuf_appendn: invalid argument (NULL pointer)\n");
        exit(1);
    }
    const size_t needed_l70_c15 = ((*b)).used + str_len + 1;
    while (needed_l70_c15 > ((*b)).size)
    {
        {
            size_t new_size = {0};
            char* new_data = {0};
            if (((*b)).size > 0)
            {
                new_size = ((*b)).size * 2;
            }
            else
            {
                new_size = INITIAL_BUFFER_SIZE;
            }
            if (new_size / 2 != ((*b)).size || new_size < ((*b)).size)
            {
                fprintf(stderr, "ERROR: dynbuf_appendn: capacity overflow (current %zu)\n", ((*b)).size);
                exit(1);
            }
            new_data = realloc(((*b)).data, new_size);
            if (!new_data)
            {
                fprintf(stderr, "ERROR: dynbuf_appendn: realloc(%zu) failed\n", new_size);
                exit(1);
            }
            ((*b)).size = new_size;
            ((*b)).data = new_data;
        }
    }
    memcpy(((*b)).data + ((*b)).used, str, str_len);
    ((*b)).used += str_len;
    ((*b)).data[((*b)).used] = '\0';
}

/**
 * Append n chars of a string to the buffer.
 */
void dynbuf_append(DynBuf* b, const char* str)
{
    if (b == nil || ((*b)).data == nil || str == nil)
    {
        fprintf(stderr, "ERROR: dynbuf_append: invalid argument (NULL pointer)\n");
        exit(1);
    }
    const size_t str_len_l115_c16 = strlen(str);
    dynbuf_appendn(b, str, str_len_l115_c16);
}

/**
 * Append a single character to the buffer.
 * Automatically grows if needed.
 * Exits on reallocation failure.
 */
void dynbuf_append_char(DynBuf* b, char c)
{
    if (b == nil || ((*b)).data == nil)
    {
        fprintf(stderr, "ERROR: dynbuf_append_char: invalid buffer\n");
        exit(1);
    }
    const size_t needed_l133_c15 = ((*b)).used + 2;
    if (needed_l133_c15 > ((*b)).size)
    {
        size_t new_size = {0};
        char* new_data = {0};
        if (((*b)).size > 0)
        {
            new_size = ((*b)).size * 2;
        }
        else
        {
            new_size = INITIAL_BUFFER_SIZE;
        }
        if (new_size / 2 != ((*b)).size || new_size < ((*b)).size)
        {
            fprintf(stderr, "ERROR: dynbuf_append_char: capacity overflow (current %zu)\n", ((*b)).size);
            exit(1);
        }
        new_data = realloc(((*b)).data, new_size);
        if (!new_data)
        {
            fprintf(stderr, "ERROR: dynbuf_append_char: realloc(%zu) failed\n", new_size);
            exit(1);
        }
        ((*b)).data = new_data;
        ((*b)).size = new_size;
    }
    ((*b)).data[((*b)).used] = c;
    ((*b)).used++;
    ((*b)).data[((*b)).used] = '\0';
}

/**
 * Free the buffer's memory and reset all fields.
 * Safe to call multiple times or on uninitialized buffer.
 */
void dynbuf_free(DynBuf* b)
{
    if (b == nil)
    {
                return;
    }
    if (((*b)).data)
    {
        free(((*b)).data);
    }
    ((*b)).data = nil;
    ((*b)).size = 0;
    ((*b)).used = 0;
}


void dynbuf(void)
{
}
