/*
 *	File functions.
 */

#include "hc.h"

UBYTE *FileNames[NUM_FILES] =
{
	"\004" "Root",
	"\001" "1",
	"\001" "2",
	"\001" "3",
	"\001" "4",
	"\001" "5",
	"\001" "6",
	"\004" "Loop",
	"\005" "Modem",
	"\004" "From",
	"\002" "To",
	"\004" "Baud",
};

UBYTE *ShowNames[NUM_FILES] =
{
	"\007" "RootHC:",
	"\025" "1     » HC11 numéro 1",
	"\025" "2     » HC11 numéro 2",
	"\025" "3     » HC11 numéro 3",
	"\025" "4     » HC11 numéro 4",
	"\025" "5     » HC11 numéro 5",
	"\025" "6     » HC11 numéro 6",
	"\031" "Loop  » Retour de données",
	"\022" "Modem » Pour modem",
	"\025" "From  » Décodage HC11",
	"\025" "To    » Encodage HC11",
	"\027" "Baud  » Vitesse du port",
};

/*
 *	UBYTE *GetName (UWORD File)
 *
 *		Find the name of a file.
 */

UBYTE *GetName (UWORD File)
{
	return FileNames[File];
}


/*
 *	UBYTE *ShowName (UWORD File)
 *
 *		Find the name of a file.
 */

UBYTE *ShowName (UWORD File)
{
	return ShowNames[File];
}


/*
 *	UBYTE *GetComment (UWORD File)
 *
 *		Find the comment of a file.
 */
 
UBYTE *GetComment (UWORD File)
{
	return "";
}


/*
 *	UWORD WhichFile (UBYTE *Name)
 *
 *		Find the number associated with a name.
 */

UWORD WhichFile (UBYTE *Name)
{
	UWORD i, len;
	UBYTE *temp;


	/*
	 *	Get the position (numerical and pointer) of the first character of a
	 *	column (:).  If there are no column, set it to the first character.
	 */

	i = 0;
	len = *Name;
	temp = Name + 1;

	while (1)
	{
		if (i == len)
		{
			i = 0;
			temp = Name + 1;
			break;
		}
		if (*temp == ':')
		{
			temp++;
			i++;
			break;
		}
		temp++;
		i++;
	}

	/*
	 *	Now check if the character position we have is valid, that is to say
	 *	that it's not past the end of the string.
	 */

	if (i >= len)
		return 0;

	/*
	 *	Now, go past all heading slashes in the name.
	 */

	while (i < len)
	{
		if (*temp != '/')
			break;
		temp++;
		i++;
	}

	if (i >= len)
		return 0;

	/*
	 *	Now we are at the filename !
	 */

	len -= i;

	for (i = 1; i < NUM_FILES; i++)
		if (!strnicmp (temp, FileNames[i] + 1, (UWORD)FileNames[i][0]))
			return i;

	return -1;
}
