/** \file util.c -- utility functions
 * \copyright 2025 Matthias Andree, GNU GPL v2+
 */

#define _XOPEN_SOURCE 600
#include "util.h"
#include <string.h>

size_t str_split(char *str, const char *delim, char *outarray[], const size_t maxsplit, char **remainder)
{
        char *saveptr = NULL;
        char *startptr = str;
        char *curptr = NULL;
        size_t i;
        for (i = 0; i < maxsplit; ++i) {
                curptr = strtok_r(startptr, delim, &saveptr);
                outarray[i] = curptr;
                if (!curptr) break;
                startptr = NULL; // null this for subsequent strtok_r calls, which will look at saveptr
        }
        if (maxsplit == i && remainder && curptr) {
                *remainder = strtok_r(NULL, "", &saveptr);
        }
        return i;
}

bool str_startswith(const char *str /** string to check for \a prefix */, 
        const char *prefix /** prefix of string to check */)
{
        return 0 == strncmp(str, prefix, strlen(prefix));
}
