
/***** Various include *****/

#include "/triton/ped/Ped.h"
#include <strings.h>
#include <math.h>


/***** Prototypes *****/

struct Line *MySearchLine( struct Line * , int );
char *mystrstr( char * , char * );



/*****
 *
 * FUNCTION:	ULONG main(struct PEDWindow *p,LONG ccol,LONG cline,ULONG *start,ULONG *end,char *funcname)
 *
 * AIM:		Find a block enclosed into the initial and final strings
 *		pointed by "start" and "end". This block must be placed
 *		at "zero level" (not enclosed in another block).
 *
 * NOTE:	Use always the function geta4() or the directive __saveds!
 *
 * RESULT:	TRUE=block found,FALSE=not found.
 *
 ****/

ULONG ASM SAVEDS main(RG(a0) struct PEDWindow *p,RG(d0) LONG ccol,RG(d1) LONG cline,RG(a1) ULONG *start,RG(a2) ULONG *end,RG(a3) char *funcname)
{
	struct Line	*line,
			*curstartline;
	int		 nline,
			 startline;
	char		*pun,
			*pun2,
			*name;



	/***** Go up to check at which level we are *****/

	curstartline=line=MySearchLine(p->Text->FirstLine,cline);
	nline=cline;
	while(line)
	{
		/***** If we are on a fold -> sure we are at "zero level" *****/
		if (line->Folder)
		{
			line=NULL;
			break;
		}

		/***** If there's the start string -> exit! *****/
		if (name=mystrstr(line->Buffer,p->Text->FileType->FoldStartStr))	break;

		/***** Go to previous line *****/
		line=line->PrevLine;
		nline--;
	}

	/***** If we didn't find the line return FALSE *****/
	if (!line)	return(FALSE);

	/***** We found the block start. Store position in startline *****/
	startline=nline;

	/***** Starting from this point go down searching for block end *****/
	line=curstartline->NextLine;
	nline=cline+1;
	while(line)
	{
		/***** If we are on a fold -> sure we are at "zero level" *****/
		if (line->Folder)
		{
			line=NULL;
			break;
		}

		/***** If there's the stop string -> exit! *****/
		if (mystrstr(line->Buffer,p->Text->FileType->FoldStopStr))	break;

		/***** Go to next line *****/
		line=line->NextLine;
		nline++;
	}

	/***** If we didn't find the line return FALSE *****/
	if (!line)	return(FALSE);

	/***** We found the block end. Store starting and ending positions
		in output ULONGs. See prototypes *****/
	*start=startline;
	*end=nline;

	/***** Go forward to the end coping the function's name *****/

	pun=funcname;
	pun2=name+strlen(p->Text->FileType->FoldStartStr);
	while((pun2[0]==32)||(pun2[0]==9))	pun2++;
	while((pun2[0]!=32)&&(pun2[0]!=9)&&(pun2[0]))	*pun++=*pun2++;
	*pun='\0';

	/***** Return TRUE *****/
	return(TRUE);
}



/*****
 *
 * FUNCTION:	struct Line *MySearchLine(struct Line *line,int y)
 *
 * AIM:		Find the address of the y-th line (0=first) starting
 *		from the first line of file pointed by "line".
 *		NOTE: folded line are used, too.
 *
 * RESULT:	A pointer to wished line.
 *
 ****/

struct Line *MySearchLine(struct Line *line,int y)
{
	while(((y--)>0)&&(line))	line=line->NextLine;
	return(line);
}



/*****
 *
 * FUNCTION:	char *mystrstr(char *str,char *sub)
 *
 * AIM:		Find the string "sub" in the string "str"
 *		starting from right side.
 *
 * RESULT:	The address of the substring "sub" int the string.
 *
 ****/

char *mystrstr(char *str,char *sub)
{
	int	lstr,
		lsub;
	char	*p,
		*q,
		*r;

	lstr=strlen(str);
	lsub=strlen(sub);

	for(p=str;p<=str+lstr-lsub;p++)
	{
		q=p;
		r=sub;
		while(*r)
		{
			if (*r!=*q)	break;
			r++;
			q++;
		}
		if (!(*r))	return(p);
	}
	return(NULL);
}
