/*
		cr search & find
                      1993.6.27 v1.0
                      copyright Y.Ouchi
*/

#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 search & findexcept 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 & findexcept 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);
}
