/* auxui.c for !DiscEx */


#include "main.h"
#include "auxui.h"


/* for string results - the buffers are used cyclically to allow, eg,
    printf( ..., num(a), num(b), num(c))  (aargh!!) */

#define  NUMBUFFS  5
#define  BUFFSIZE 20

static char buffers[NUMBUFFS][BUFFSIZE];
static int buffi = 0;
static char *buff;



/*
 * Make 'buff' point to the next buffer; there are NUMBUFFS buffers, which
 *  are selected cyclically.
 */

static void next_buff ()
{
    if (buffi == 0)
        buffi = NUMBUFFS - 1;
    else
        buffi--;

    buff = buffers[buffi];

    return;
}


/*
 * Return a suitable text string to describe the given density.
 */

char * trans_density (int density)
{
    switch (density)
    {
    case 0:
        return "hard disc";
    case 1:
        return "single";
    case 2:
        return "double";
    case 3:
        return "double+";
    case 4:
        return "quad";
    case 8:
        return "octal";
    default:
        next_buff ();
        sprintf (buff, "%d", density);
        return buff;
    }
}


/*
 * Return x/y as a percentage in the form "zd.d".
 */

char * pc (unsigned x, unsigned y)
{
    next_buff ();

    if (y == 0)
        *buff = 0;  /* empty string */
    else
        sprintf(buff, "%.1f", ((double)x * 100.0)/(double) y);

    return buff;
}


/*
 * Return x as an integer "d...d".
 */

char * numdf (double x)
{
    next_buff ();
    sprintf(buff, "%.f", x);
    return buff;
}


/*
 * Return n as an 8-digit hexadecimal number "&hhhhhhhh".
 */

char * numh8 (unsigned int n)
{
    next_buff ();
    sprintf(buff, "&%08x", n);
    return buff;
}


/*
 * Return n as a 3-digit hexadecimal number "&hhh".
 */

char * numh3 (unsigned int n)
{
    next_buff ();
    sprintf(buff, "&%03x", n);
    return buff;
}


/*
 * Return n as an integer "d...d".
 */

char * num (unsigned int n)
{
    next_buff ();
    sprintf(buff, "%u", n);
    return buff;
}


/*
 * (Un)Fade the specified component of the given gadget.
 */

error * gadget_fade (ObjectId dbox, ComponentId id, Bool fade)
{
    unsigned int flags;

    ER ( gadget_get_flags (0, dbox, id, &flags) );
    ER ( gadget_set_flags (0, dbox, id,
                 fade ? flags | 0x80000000 : flags & 0x7fffffff) );

    return NULL;
}


/*
 * Case-insensitive string equality test.
 */

Bool equalstr_ci (char *s, char *t)
{
    int n = strlen (s);
    int m = strlen (t);
    int i = -1;

    if (n != m)
        return FALSE;

    while (++i < n)
        if (toupper (s[i]) != toupper (t[i]))
            return FALSE;

    return TRUE;
}
