/*
**  WBGenie is Copyright 1992 by Steven Velletri.  All Rights Reserved
**
**	Filename:   sup_lib.c
**	Release:    1.0
**	Revision:   0.0
**	Date:       22/05/92
**  Author:     Steven Velletri
**  History:
**
**  Version Author  Date        Reason
**  0.00    SV      23/07/92    Created
**  1.00    SV      08/11/92    Created this version
**
**  To compile this module I used the SAS/C compiler version 5.10a.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <exec/types.h>

#include "sup_lib.h"

char ** sup_get_buf(WORD height, WORD width)
    {
    register WORD i;
    char **buf = calloc(1, height*(sizeof(char *) + width));

    if (buf == NULL)
        return(buf);

    for (i = 0; i < height; i++)
        {
        *(buf + i) = (char*) buf + (width * i) + (height*sizeof(char *));
        }
    return(buf);
    }

BOOL sup_string_scan(char * token, char * str)
    {

    /*
    char *tok = token, *s
    */
    BOOL retcd;

    retcd = !strnicmp(token, str, strlen(token));
    /*

    while(*s++)
        {
        if (*s == *tok)
            {
            while(*tok++)
                {
                if *token = *str


                }
            }
        }
    */

    return(retcd);
    }

int sup_get_num_tool_types(char **tool_types)
    {
    int ret_cnt = 0;

    while(*(tool_types++))
        {
        ++ret_cnt;
        }
    return(ret_cnt);
    }

void sup_free_tool_type_array(char **tool_types)
    {
    while(*(tool_types))
        {
        free(*(tool_types++));
        }

    free(tool_types);
    }

/******************************************************************************
    sup_create_tool_types()
        This is a support function and should be in a separate module.
        It allocates memory for the new tool type array and inserts
        the passed tool types in this array intelligently.

*******************************************************************************/

char ** sup_create_tool_type_array(char **old_tool_types,
                                   char **new_tool_types,
                                   struct SUPTOOLTYPES *def_tool_types, int num_new_tt)
    {

    register int found, num_tt, i, j;
    char **retcd,
         **ret_tt,
         **new_tt = new_tool_types,
         **old_tt = old_tool_types;

    BOOL err = FALSE;

    struct SUPTOOLTYPES *def_tt = def_tool_types;

    num_tt = sup_get_num_tool_types(old_tool_types);

    /* Get memory for new tool types array */
    retcd  = calloc(num_tt + num_new_tt + 1, sizeof(char *));

    if (retcd == NULL)
        return(retcd);

    /* Ensure that flags are initialised to FALSE */
    for(def_tt = def_tool_types, i = 0; i < num_new_tt; i++, def_tt++)
        {
        def_tt->flag1 = FALSE;
        }

    /*
    ** This double loop finds all the pre-existing tools types, changes them
    ** to the required value and marks the tool type as being processed
    */

    for (ret_tt = retcd; *old_tt; old_tt++, ret_tt++)
        {
        for (i = 0, def_tt = def_tool_types; def_tt->tt_name, i < num_new_tt; def_tt++, i++)
            {
            found = FALSE;

            if (sup_string_scan(def_tt->tt_name, *old_tt))
                {
                for(new_tt = new_tool_types, j = 0; j < num_new_tt; j++, new_tt++)
                    {
                    if ((sup_string_scan(def_tt->tt_name, *new_tt)))
                        {
                        if (def_tt->flag1)
                            break;
                        *ret_tt = strdup(*new_tt);

                        def_tt->flag1 = found = TRUE;
                        break;
                        }
                    }
                break;
                }
            }
        if (!found)
            *ret_tt = strdup(*old_tt);

        if (*ret_tt == NULL)
            {
            err = TRUE;
            break;
            }
        }

    if (err) /* an error occured */
        {
        sup_free_tool_type_array(ret_tt);
        return(NULL);
        }

    /*
    ** This double loop adds the tools which were not marked as found
    */

    for(new_tt = new_tool_types, i = 0; i < num_new_tt; i++, new_tt++)
        {
        found = FALSE;

        for(def_tt = def_tool_types, j = 0; j < num_new_tt; j++, def_tt++)
            {
            if (!def_tt->flag1 && sup_string_scan(def_tt->tt_name, *new_tt))
                {
                found = TRUE;
                *ret_tt = strdup(*new_tt);
                break;
                }
            }
        if (found && *ret_tt++ == NULL)
            {
            err = TRUE;
            break;
            }
        }

    if (err) /* an error occured */
        {
        sup_free_tool_type_array(ret_tt);
        return(NULL);
        }
    *(ret_tt) = NULL;

    /* No errors */

    return(retcd);
    }
