# include <pslib.h>
# include "psLine.h"

static void psLineFree (psLine *line)
{

    if (line == NULL)
        return;

    psFree (line->line);
    return;
}

// allocate a psLine structrue
psLine *psLineAlloc (int Nline)
{

    psLine *line;
    line = psAlloc (sizeof(psLine));
    psMemSetDeallocator(line, (psFreeFunc) psLineFree);

    line->Nline = 0;
    line->NLINE = Nline;
    line->line = psAlloc (Nline);
    return (line);
}

bool psLineInit (psLine *line)
{
    if (line == NULL)
        return (false);
    line->Nline = 0;
    return (true);
}

bool psLineAdd (psLine *line, char *format, ...)
{

    int Nchar;
    va_list ap;

    if (line == NULL)
        return (false);

    int nMax = line->NLINE - line->Nline;

    va_start (ap, format);
    Nchar = vsnprintf (&line->line[line->Nline], nMax, format, ap);
    line->Nline += PS_MIN (nMax - 1, Nchar);
    va_end (ap);

    if (Nchar >= nMax)
        return (false);
    return (true);
}
