/*
**    GrabKEY
**
**        © 1996 by Timo C. Nentwig
**        All Rights Reserved !
**
**        Tcn@techbase.in-berlin.de
**
**
*/

/// #include

#include "gk_GST.h"
#include "gk_Protos.h"

///
/// proto

proto BOOL      atob            (const STRPTR Key);
proto ULONG     ErrorRequest    (const STRPTR Gadgets, const STRPTR Format, ...);
proto BOOL      Exists          (const STRPTR PathFile);
proto STRPTR    GetFile         (VOID);
proto ULONG     ShowRequest     (const STRPTR Gadgets, const STRPTR Format, ...);
proto BOOL      StrEmpty        (const STRPTR str);

///

/// atob ()

	/*
	 *    FUNCTION    Convert a string to boolean.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     StrToBool ("on", FALSE);
	 *
	 */

BOOL
atob (const STRPTR Key)
{

	const struct { STRPTR Key; BOOL Value; } Table [] =
	{

		"yes",      TRUE,
		"true",     TRUE,
		"on",       TRUE,
		"1",        TRUE,
		"no",       FALSE,
		"false",    FALSE,
		"off",      FALSE,
		"0",        FALSE,

		NULL,       -1

	};


	LONG    i;

	for (i = 0; i < NumElements (Table); i++)
	{

		if (stricmp (Table [i] . Key, Key) == 0)
		{

			return (Table [i] . Value);

		}

	}

}

///
/// ErrorRequest ()

	/*
	 *    FUNCTION    ShowRequest() for errors, playing
	 *                also error sound.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     ErrorRequest ("OK|Not OK|Shut Up", "This is my cool text");
	 *
	 */

ULONG
ErrorRequest (const STRPTR Gadgets, const STRPTR Format, ...)
{

	UBYTE    Text [1024];

	va_list   Args;
	va_start (Args, Format);
	vsprintf (Text, Format, Args);
	va_end   (Args);

	PlaySound (Set -> Sound . Error);

	return (ShowRequest (Gadgets, Text));

}

///
/// Exists ()

	/*
	 *    FROM        /csh/comm3.c
	 *
	 *    FUNCTION    Check if specified file or dir
	 *                exists.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     Exists ("path:file");
	 *
	 */


BOOL
Exists (const STRPTR PathFile)
{

	BPTR    lock;

	if (lock = Lock (PathFile, ACCESS_READ))
	{

		UnLock (lock);
		return (TRUE);

	}

	return (FALSE);

}

///
/// GetFile ()

	/*
	 *    FUNCTION    Get a file via file
	 *                reqtools reqeuster.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     STRPTR File = GetFile ();
	 *
	 */


STRPTR
GetFile (VOID)
{

	struct    rtFileRequester   *FileReq;

	if (FileReq = rtAllocRequestA (RT_FILEREQ, NULL))
	{

		UBYTE    File [TT_LEN];
				 File [0] = NULL;

		if (rtFileRequest (FileReq, File, LocaleString (MSG_GUI_SELECT_FILE), TAG_END))
		{

			UBYTE    Buffer [TT_LEN];

			STRCPY  (Buffer, FileReq -> Dir);
			AddPart (Buffer, File, sizeof (Buffer));

			return (Buffer);

		}

		rtFreeRequest (FileReq);

	}

	return (NULL);

}

///
/// ShowRequest ()

	/*
	 *    FUNCTION    Show a requester with given
	 *                text and gadgets.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     ShowRequest ("OK|Not OK", "This is %ld", 120);
	 *
	 */

ULONG
ShowRequest (const STRPTR Gadgets, const STRPTR Format, ...)
{

	UBYTE    Text [1024];

	va_list   Args;
	va_start (Args, Format);
	vsprintf (Text, Format, Args);
	va_end   (Args);

	return (rtEZRequest (Text, Gadgets, NULL, NULL));

}

///
/// StrEmpty ()

	/*
	 *    FUNCTION    Check if a string is empty or not.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     StrEmpty ("  ");
	 *
	 */


BOOL
StrEmpty (const STRPTR str)
{

	if (str [0] == '\0')
	{

		return (TRUE);

	}
	else
	{

		UWORD    i;

		for (i = 0; str [i] != '\0'; i++)
		{

			if (str [i] != ' ' && str [i] != '\t')      // non-space found
			{

				return (FALSE);

			}

		}

		return (TRUE);

	}

}

///

