view th_printf1.c @ 722:4ca6a3b30fe8

Bump copyright years.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Jan 2021 11:35:54 +0200
parents 7f1efa37288b
children 29e44a58bc73
line wrap: on
line source

/*
 * A printf() implementation helper function template
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2016-2021 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */


int TH_VPRINTF_INTFMT_NAME (char *buf, const int len, int *pos,
    TH_VPRINTF_INTFMT_TYPE_S pval, const int f_radix, const BOOL f_upcase,
    const BOOL f_unsig, BOOL *f_neg)
#ifdef TH_VPRINTF_INTFMT_HEADER
;
#else
{
    if (f_radix > 16)
        return EOF;

    // Check for negative value
    if (!f_unsig && pval < 0)
    {
        *f_neg = TRUE;
        pval = -pval;
    }
    else
        *f_neg = FALSE;

    // Render the value to a string in buf (reversed)
    TH_VPRINTF_INTFMT_TYPE_U val = pval;

    // Special case for value of 0. This would seem like a
    // dirty kludge, but allows us to handle NULL ptr (nul)
    // situation a bit better elsewhere.
    if (val == 0)
        return 0;

    // We assume that the output buffer is at least 2 bytes long
    *pos = 0;
    do
    {
        TH_VPRINTF_INTFMT_TYPE_U digit = val % f_radix;

        if (digit < 10)
            buf[*pos] = '0' + digit;
        else
            buf[*pos] = (f_upcase ? 'A' : 'a') + digit - 10;

        val /= f_radix;
        (*pos)++;
    }
    while (val > 0 && *pos < len - 1);
    buf[*pos] = 0;

    return (val > 0) ? EOF : 1;
}
#endif


#undef TH_VPRINTF_INTFMT_NAME
#undef TH_VPRINTF_INTFMT_SIGNED
#undef TH_VPRINTF_INTFMT_TYPE_S
#undef TH_VPRINTF_INTFMT_TYPE_U
#undef TH_VPRINTF_INTFMT_HEADER