#include "tr.h"

/*
	Executes the `tr' command (as in *NIX's tr(1) or in PERL).
	It's seeks for first occurence of the symbol 'ch'
	(and not for the last, as in tr(1)).

	Does not translate SPACE (' ');

	do_notfound_stays:	if !=0: if 'ch' not in "from", return 'ch'.
				if ==0: if 'ch' not in "from", return QUESTION.
*/
BYTE trChar (	const BYTE * const from,
		const BYTE * const to,
		const BYTE   ch,
		int do_notfound_stays
		)
{
	int i;
	int len;

	if (ch == ASCII_32)	return ch;
	if (ch == '\n')	return ch;

	len = strlen(from);
	for (i=0; i<len; i++)
		if (from[i] == ch)	break;

	if (i >= len)			/* Not found.	*/
		return (do_notfound_stays) ? ch : QUESTION;
	else if (i >= strlen(to))	/* strlen(to)<strlen(from)*/
		return (do_notfound_stays) ? ch : QUESTION;
	else
		return to[i];
}/*trChar*/


BYTE * trString (	const BYTE * const trFrom,
			const BYTE * const trTo,
			const BYTE * const from,
			      BYTE *       to,
			int do_notfound_stays	)
{
	int i;
	for (i=0; from[i]; i++)
		to[i] = trChar (trFrom, trTo, from[i], do_notfound_stays);
	to[i]='\0';
	return to;
}/*trString*/


/*<EOF>*/

