/*Original code, written by Davion. */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "include.h"

int  symbol_lookup args ( ( const char symbol ) );
extern const struct colour_type colour_table[];

const struct colour_type colour_table[] =
{
    {  "White", 		"x", 		"\e[0m" 	},
    {  "DRed",      	"r",    	"\e[0;31m"	},
    {  "DGreen",		"g",		"\e[0;32m"	},
    {  "DYellow",   	"y",		"\e[0;33m"	},
    {  "DBlue", 		"b",		"\e[0;34m"      },
    {  "DPurple",   	"m",		"\e[0;35m"	},
    {  "DCyan",			"c",		"\e[0;36m"      },
    {  "Dwhite",		"w",		"\e[0;37m"	},
    {  "DGrey",			"D",		"\e[1;30m"	},
    {  "BRed",			"R",		"\e[1;31m"	},
    {  "BGreen",  		"G",		"\e[1;32m"	},
    {  "BYellow", 		"Y",		"\e[1;33m"	},
    {  "BBlue",   		"B",		"\e[1;34m"	},
	{  "BPink",  		"M",		"\e[1;35m"	},
	{  "BCyan",   		"C",		"\e[1;36m"	},
    {  "BWhite",  		"W",		"\e[1;37m"	},
	{  "Bracer",		"{",		"{"			},
	{  "Beep",			"*",		"\a"		},
	{  "NewLine",		"\\",		"\n\r",		},
    {  NULL,	 	NULL, 		NULL		}
};


char *colour_string( const char *txt )
{
    const char *string = txt;
    static char buf[MSL*4];
    char symbol;
    char *coloured, *code;
    int i;
    bool found;

    buf[0] = '\0';
    coloured = buf;

    for(;*string != '\0' ; string++ )
    {	found = false;
		if(*string == '{' )
		{   string++;
			symbol = *string;
			
	   	    if( ( i = symbol_lookup(symbol) ) == -1 )
				i = 0;

			code = colour_table[i].code;

		    while(*code != '\0' )
		    {	*coloured++ = *code++;
				*coloured = '\0';
		    }
		    found = true;
		}
		if(!found)
		{	*coloured++ = *string;
		    *coloured = '\0';
		}
    }	
    *coloured = '\0';
	return buf;
}

int symbol_lookup( const char symbol )
{
    int i;

    for ( i = 0; colour_table[i].name != NULL ; i++ )
    { 
        if( symbol == *colour_table[i].symbol  )
	    return i;
    }
    return -1;
}