/*
		cr search & find
                      1993.6.27 v1.0
                      copyright Y.Ouchi

				BS search add 1994.7.23  by Basie

*/

#include	<jctype.h>

int		crfind(int size, char *text, int *leng, int *next_pos)
{
register	int		i,j;
			int		cr_no;

	i=cr_no=*next_pos=*leng=0;
	while ( i<size ){
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
	}
	return(cr_no);

					/* cr find process */
crproc:
	if (text[i+1]==0x0a){
		cr_no++;
		*leng=i;
		i=i+2;
		if (i<size) *next_pos=i;
		j=0;
		while(i+j<size){
			if (text[i+j]!=0x0d){
				*next_pos=i+j;
				return(cr_no);
			}
			else{
				if (text[i+j+1]==0x0a){
					cr_no++;
					j=j+2;
					if (i+j<size) *next_pos=i+j;
				}
				else return(0);
			}
		}
		return(cr_no);
	}
	return(0);
}


/*
		CR or BS search
			output	0  : no CR or BS
					+n : found CR number
					-1 : BS found
*/

int		crbsfind(int size, char *text, int *leng, int *next_pos)
{
register	int		i,j;
			int		cr_no;

	i=cr_no=*next_pos=*leng=0;
	while ( i<size ){
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
		if (text[i]==0x0d) goto crproc; 
		else i++;
	}
	return(cr_no);

					/* cr find process */
crproc:
	if (text[i+1]==0x0a){


					/* blank+bs+cr+lf(delimitter) check */
		if (text[i-1]==0x08){
			if (text[i-2]==0x20){
				cr_no++;
				*leng=i-2;
				if (i+2<size) *next_pos=i+2;
				return (-1);		/* dlimitter found & return */
			}
		}

		cr_no++;
		*leng=i;
		i=i+2;
		if (i<size) *next_pos=i;
		j=0;
		while(i+j<size){
			if (text[i+j]!=0x0d){
				*next_pos=i+j;
				return(cr_no);
			}
			else{
				if (text[i+j+1]==0x0a){
					cr_no++;
					j=j+2;
					if (i+j<size) *next_pos=i+j;
				}
				else return(0);
			}
		}
		return(cr_no);
	}
	return(0);
}


/*
		cr search & find except brank
                      1993.6.27 v1.0
                      copyright Y.Ouchi
*/
int		crfindb(int size, unsigned char *text, int *leng, int *next_pos)
{

register	int		i,j;
			int		cr_no;

	i=cr_no=*next_pos=*leng=0;
	while ( i<size ){
		if (text[i]==0x0d){
			if (text[i+1]==0x0a){
				cr_no++;
				*leng=i;
				i=i+2;
				if (i<size) *next_pos=i;
				j=0;
				while(i+j<size){
					if (text[i+j]!=0x20){
						if (text[i+j]!=0x81){
							if (text[i+j]!=0x0d){
								return(cr_no);
							}
							else{
								if (text[i+j+1]==0x0a){
									cr_no++;
									j=j+2;
									if (i+j<size) *next_pos=i+j;
								}
							}
						}
						else{
							if (text[i+j+1]==0x40){
								j=j+2;
							}
							else{
								return(cr_no);
							}
						}
					}
					else j++;
				}
				return(cr_no);
			}
			else i++;
		}
		else i++;
	}
	if (i>0) *leng=i;
	return(0);
}

/*
		cr search & find except brank for line display
                      1993.7.31 v1.0
                      copyright Y.Ouchi
*/
int		crfindl(int size, unsigned char *text, int *leng, int *next_pos)
{

register	int		i,j;
			int		cr_no;
			int		checkmax;

	i=cr_no=*next_pos=*leng=0;
	if (size>80) checkmax=80;
	else checkmax=size;
	while ( i<checkmax ){
		if (text[i]==0x0d){
			if (text[i+1]==0x0a){
				cr_no++;
				*leng=i;
				i=i+2;
				if (i<size) *next_pos=i;
				j=0;
				while(i+j<size){
					if (text[i+j]!=0x20){
						if (text[i+j]!=0x81){
							if (text[i+j]!=0x0d){
								return(cr_no);
							}
							else{
								if (text[i+j+1]==0x0a){
									cr_no++;
									j=j+2;
									if (i+j<size) *next_pos=i+j;
								}
							}
						}
						else{
							if (text[i+j+1]==0x40){
								j=j+2;
							}
							else{
								return(cr_no);
							}
						}
					}
					else j++;
				}
				return(cr_no);
			}
			else{
				if(iskanji(text[i])) i=i+2;
				else i=i+1;
			}
		}
		else{
			if(iskanji(text[i])) i=i+2;
			else i=i+1;
		}
	}
	if (i>0){
		if(i>80){
			*leng=79;
			*next_pos=79;
		}
		else{
			*leng=i;
			if(i<size) *next_pos=i;
		}
	}
	return(0);
}


/*
		bs search & find 
                      1994.8.15
                      copyright Y.Ouchi
*/
int		bsfind(int size, char *text, int *lleng, int *npos)
{
register	int		i;

	i=*npos=*lleng=0;
	while ( i<size ){
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;
		if (text[i]==0x08) goto bsproc; 
		else i++;

		goto nextproc;

					/* bs find process */
bsproc:
		if (text[i-1]==0x20 && text[i+1]==0x0d 
						&& text[i+2]==0x0a && i+3<size){
			*npos=i+3;
			if (i>0) *lleng=i-1;
			return(1);
		}
		else i++;

nextproc:

	}

	return(0);
}

