/*
        log text check (title, log )
                      1993.6.27 v1.0
                      copyright Y.Ouchi
*/

#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	"egb.h"
#include	"mos.h"
#include	"bb.h"

extern	char	egbwork[1536];
extern	int		max_content;
extern	int		max_title;
extern	int		max_textn;

static	int		textcheckflag;
static	char	endchar[3][40]={
				{0x81,0x84,0x81,0x9F,0x8E,0x9F,0x95,0xC5,0x82,0xCD,
				 0x82,0xA0,0x82,0xE8,0x82,0xDC,0x82,0xB9,0x82,0xF1,
				 0x81,0x9F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/* No 0*/
				{0x52,0x65,0x73,0x6f,0x72,0x74,0x20,0x62,0x79,0x20,
				 0x43,0x41,0x54,0x4c,0x4f,0x47,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/* No 1*/
				{0x20,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
				 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};/* No 1*/

	/* 関数プロトタイプ宣言 */
extern	void	linedsp(int, int, char *);
extern	void	mesout ( int );
extern	void	box(int, int, int, int, int, int);
extern	int		crfind(int, char *, int *, int *);

		int		mescheck(unsigned char *, int, int *, text_content *);
		int		titlecheck(unsigned char *, int, int *, title_content *);
		int		lastcheck(int, char *, int *, int *);

int logcheck ( unsigned char *log, int leng, char **title, 
								struct logcontent *intext, short *disptextn)
{
	int		i,j,k,isav;
	int		endcode=0;
	int		textno=0;
	int		titleno=0;
	char	*waitmes={"now analizing text"};
	int		lleng,npos;
	text_content	textcheckresult;
	title_content	titlecheckresult;

		/* log file check */

				/* message display */
	MOS_setpos(320,200);
	MOS_sysIcon(82,16,16,0);
	MOS_color( 0,15 );
	MOS_disp(1);
	EGB_writePage(egbwork,0);
	box(220,250,420,270,ARROW_COL,0);
	EGB_color(egbwork, 0, TEXT_COL);
	linedsp(248,290,waitmes);

	intext->text[textno].text.pos=0;
	intext->text[textno].text.id=NOTITLE_TEXT_ID;
	intext->text[textno].text.leng=0;
	intext->text[textno].text.com_lvl=0;
	intext->text[textno].text.title_index=-1;
	crfind(leng,(char *)&log[0],&lleng,&npos);
	intext->text[textno].text.lleng=lleng;
	textcheckflag=2;
	i=k=0;

	while(i<leng){
		isav=i;
		if (log[i+0]<=0x39){
		  if (log[i+0]>=0x30){

			switch ( mescheck (log,leng,&i,&textcheckresult) ){
				case 0 :	/*	3digit message	*/

					if (textcheckflag!=0){
						if (isav-intext->text[textno].text.pos>0){
							intext->text[textno].text.leng=
									isav-intext->text[textno].text.pos;
							textno++;
							endcode=1;
						}
						textcheckflag=0;
					}

					intext->text[textno].text.pos=textcheckresult.pos;
					intext->text[textno].text.id=textcheckresult.id;
					intext->text[textno].text.mes_no=textcheckresult.mes_no;
					intext->text[textno].text.leng=textcheckresult.leng;
					intext->text[textno].text.text_no=textcheckresult.text_no;
					intext->text[textno].text.com_no=textcheckresult.com_no;
					intext->text[textno].text.com_lvl=textcheckresult.com_lvl;
					intext->text[textno].text.lleng=textcheckresult.lleng;

					if (titleno>0)
						intext->text[textno].text.title_index=titleno-1;
					else intext->text[textno].text.title_index=-1;
					textcheckflag=1;
					goto next_proc;
					break;

				case 1 :	/*	5digit message	*/

					if (textcheckflag!=0){
						if (isav-intext->text[textno].text.pos>0){
							intext->text[textno].text.leng=
									isav-intext->text[textno].text.pos;
							textno++;
							endcode=1;
						}
						textcheckflag=0;
					}

					intext->text[textno].text.pos=textcheckresult.pos;
					intext->text[textno].text.id=textcheckresult.id;
					intext->text[textno].text.mes_no=textcheckresult.mes_no;
					intext->text[textno].text.leng=textcheckresult.leng;
					intext->text[textno].text.text_no=textcheckresult.text_no;
					intext->text[textno].text.com_no=textcheckresult.com_no;
					intext->text[textno].text.com_lvl=textcheckresult.com_lvl;
					intext->text[textno].text.lleng=textcheckresult.lleng;

					if (titleno>0)
						intext->text[textno].text.title_index=titleno-1;
					else intext->text[textno].text.title_index=-1;
					textcheckflag=1;

					goto next_proc;
					break;

				case -1 :	/*	no message		*/
					goto next_proc;
					break;
				case -2 :	/*	abnormal end	*/
					goto end_proc;
					break;
			}


		  }
		  else{
		  	if (log[i+0]==0x2d){
		  		if (log[i+1]==0x20 && log[i+2]==0x46){

		/*	title check (line head = '- F')		*/
					if(titlecheck(log,leng,&i,&titlecheckresult)==0){

				/* title found process */
		if (textcheckflag!=0){
			if (isav-intext->text[textno].text.pos>0){
				intext->text[textno].text.leng=
					isav-intext->text[textno].text.pos;
				textno++;
				endcode=1;
			}
			textcheckflag=0;
		}

		intext->text[textno].title.pos=titlecheckresult.pos;
		intext->text[textno].title.id=titlecheckresult.id;
		intext->text[textno].title.mes_no=titlecheckresult.mes_no;
		intext->text[textno].title.com_lvl=titlecheckresult.com_lvl;
		intext->text[textno].title.lleng=titlecheckresult.lleng;

		intext->text[textno].title.index=titleno;

		lleng=i-isav-2;
		for (j=0;j<lleng;j++) title[titleno][j]=log[isav+j];
		title[titleno][j]=0x00;
		titleno++;
		textno++;

		intext->text[textno].text.pos=i;
		intext->text[textno].text.id=NOTITLE_TEXT_ID;
		intext->text[textno].text.leng=0;
		intext->text[textno].text.com_lvl=0;
		intext->text[textno].text.title_index=-1;
		textcheckflag=2;
		goto next_proc;


					}
					else{
						goto next_proc;
					}
		  		}
		  	}

					/*	'SET DLM ON' check	
							1994.1.17	add by Basie	*/
			else{
				if (log[i+1]==0x08){
					if (log[i]==0x20){
						goto end3;
					}
				}
			}


			if(crfind(leng-i,(char *)&log[i],&lleng,&npos)==0 || npos==0){
				i=i+lleng;
				goto end_proc;
			}
			i=i+npos;
			goto next_proc;
		  }
		}
		else{
			if(log[i+1]==0x84){
				j=0;
				while(endchar[0][j]){
					if (endchar[0][j]!=(char)log[i+j]) break;
					j++;
				}
				if (j<21){
					if(crfind(leng-i,(char *)&log[i],&lleng,&npos)==0 ||
																npos==0){
					i=i+lleng;
					goto end_proc;
					}
					i=i+npos;
					goto next_proc;
				}
				goto end1;
			}
			else{
				if(log[i+0]==0x46 && log[i+1]==0x4f && log[i+2]==0x52){
					j=30;
					while(j<55){
						if (log[i+j]!=0x2d) break;
						j++;
					}
					if (j<55){
						if(crfind(leng-i,(char *)&log[i],&lleng,&npos)==0 ||
																npos==0){
						i=i+lleng;
						goto end_proc;
						}
						i=i+npos;
						goto next_proc;
					}
					j=0;
					while(endchar[1][j]){
						if(endchar[1][j]!=(char)log[i+j+57])break;
						j++;
					}
					if (j<16){
						if(crfind(leng-i,(char *)&log[i],&lleng,&npos)==0 ||
																npos==0){
						i=i+lleng;
						goto end_proc;
						}
						i=i+npos;
						goto next_proc;
					}
					goto end2;
				}
			}
		}
		if(crfind(leng-i,(char *)&log[i],&lleng,&npos)==0 || npos==0){
			i=i+lleng;
			goto end_proc;
		}
		i=i+npos;
		goto next_proc;


	end1:			/* end no.1 found */
	end2:			/* end no.2 found */
	end3:			/* end no.3 found */
		if (textcheckflag!=0){
			if (i-intext->text[textno].text.pos>0){
				intext->text[textno].text.leng=
							i-intext->text[textno].text.pos;
				textno++;
				endcode=1;
			}
			textcheckflag=0;
		}

		j=crfind(leng-i,(char *)&log[i],&lleng,&npos);
		if (j==0 || npos==0){
			i=i+lleng;
			goto end_proc;
		}
		i=i+npos;

		intext->text[textno].text.pos=i;
		intext->text[textno].text.id=NOTITLE_TEXT_ID;
		intext->text[textno].text.leng=0;
		intext->text[textno].text.com_lvl=0;
		intext->text[textno].text.title_index=-1;
		crfind(leng-i,(char *)&log[i],&lleng,&npos);
		intext->text[textno].text.lleng=lleng;
		textcheckflag=2;

		goto next_proc;

/*	end2:			 end no.2 found 
		if (textcheckflag!=0){
			if (i-intext->text[textno].text.pos>0){
				intext->text[textno].text.leng=
							i-intext->text[textno].text.pos;
				textno++;
				endcode=1;
			}
			textcheckflag=0;
		}

		j=crfind(leng-i,(char *)&log[i],&lleng,&npos);
		if (j==0 || npos==0){
			i=i+lleng;
			goto end_proc;
		}
		i=i+npos;
		if (j==1) crfind(leng-i,(char *)&log[i],&lleng,&npos);
		if (npos==0){
			i=i+lleng;
			goto end_proc;
		}
		i=i+npos;

		intext->text[textno].text.pos=i;
		intext->text[textno].text.id=NOTITLE_TEXT_ID;
		intext->text[textno].text.leng=0;
		intext->text[textno].text.com_lvl=0;
		intext->text[textno].text.title_index=-1;
		crfind(leng-i,(char *)&log[i],&lleng,&npos);
		intext->text[textno].text.lleng=lleng;
		textcheckflag=2;

		goto next_proc;
*/


	next_proc:		/* next line check */
		if (i-k>1000){
			box(220,250,(220+(i*200/leng)),270,ARROW_COL,1);
			k=i;
		}
		if (textno > max_content-2){
			mesout (TEXTNO_ERROR);
			MOS_end( );
			exit(1);
		}
		if (titleno > max_title-2){
			mesout (TITLENO_ERROR);
			MOS_end( );
			exit(1);
		}

	}

end_proc:
	if (textcheckflag!=0){
		if (leng-intext->text[textno].text.pos>0){
			intext->text[textno].text.leng=
						leng-intext->text[textno].text.pos;
			textno++;
			endcode=1;
		}
	}
	intext->maxtext=textno;

	for(j=0;j<textno;j++){
		disptextn[j]=j;
	}
	max_textn=textno;

	MOS_sysIcon(170,0,0,0);
	MOS_disp(0);
	MOS_color( 0,ARROW_COL );
	EGB_color(egbwork, 0, BACK_COL);
	EGB_clearScreen(egbwork);
	EGB_color(egbwork, 0, TEXT_COL);

	return(endcode);
}



/*
	message check
		(called when first charactor of line is number)
			in		log : text
					leng: text length
					*pos: relative posision
					result : check result (if check ok)
			out		return 0 : message found (3digit number)
					       1 : message found (5digit number)
					       -1: not message
					       -2: abnormal end (log end)
					*pos   : next line head posision
*/
int		mescheck(unsigned char * log, int leng, int *pos, text_content *result)
{
int		endcode;
int		crno,lleng,npos;

	endcode=-1;

	crno=crfind(leng-*pos,(char *)&log[*pos],&lleng,&npos);
	if (crno==0 || npos==0){
		if (lleng>0) *pos=*pos+lleng;
		return (-2);
	}
	if (lleng>80){
		*pos=*pos+npos;
		return (-1);
	}

	if (log[*pos+1]<=0x39 && log[*pos+1]>=0x30){
	  if (log[*pos+2]<=0x39 && log[*pos+2]>=0x30){
		if (log[*pos+3]==0x2f){
		  if (log[*pos+4]<=0x39 && log[*pos+4]>=0x30){
			if (log[*pos+5]<=0x39 && log[*pos+5]>=0x30){
			  if (log[*pos+6]<=0x39 && log[*pos+6]>=0x30){

	/*		3digit no+'/'+3digit no found		*/

				if (log[*pos+npos]!=0x28 || log[*pos+npos+3]!=0x29){
					*pos=*pos+npos;
					return (-1);
				}


				result->text_no=
						(log[*pos]-0x30)*100+
						(log[*pos+1]-0x30)*10+
						(log[*pos+2]-0x30);
				if (log[*pos+npos+1]==0x20){
					if (log[*pos+npos+2]<=0x39 && log[*pos+npos+2]>=0x30){
						result->mes_no=log[*pos+npos+2]-0x30;
					}
					else result->mes_no=0;
				}
				else{
					if (log[*pos+npos+1]<=0x39 && log[*pos+npos+1]>=0x30 
						&& log[*pos+npos+2]<=0x39 && log[*pos+npos+2]>=0x30){
						result->mes_no=
							(log[*pos+npos+1]-0x30)*10+
							(log[*pos+npos+2]-0x30);
					}
					else result->mes_no=0;
				}

				if (log[*pos+npos+23]!=0x20){
					if (log[*pos+npos+23]<=0x39 && log[*pos+npos+23]>=0x30 
						&& log[*pos+npos+24]<=0x39 && log[*pos+npos+24]>=0x30
						&& log[*pos+npos+25]<=0x39 && log[*pos+npos+25]>=0x30){
						result->com_no=
								(log[*pos+npos+23]-0x30)*100
								+(log[*pos+npos+24]-0x30)*10
								+(log[*pos+npos+25]-0x30);
						result->id=TEXT3C_ID;
					}
					else{
						result->com_no=0;
						result->id=TEXT3_ID;
					}
				}
				else{
					result->com_no=0;
					result->id=TEXT3_ID;
				}

				result->pos=*pos;
				result->leng=0;
				result->com_lvl=0;
				result->lleng=lleng;
				endcode=0;

			  }
			}
		  }
		}

		else{
		  if (log[*pos+3]<=0x39 && log[*pos+3]>=0x30){
			if (log[*pos+4]<=0x39 && log[*pos+4]>=0x30){
			  if (log[*pos+5]==0x2f){
				if (log[*pos+6]<=0x39 && log[*pos+6]>=0x30){
				  if (log[*pos+7]<=0x39 && log[*pos+7]>=0x30){
					if (log[*pos+8]<=0x39 && log[*pos+8]>=0x30){
					  if (log[*pos+9]<=0x39 && log[*pos+9]>=0x30){
						if (log[*pos+10]<=0x39 && log[*pos+10]>=0x30){

	/*		5digit no+'/'+5digit no found		*/
		if (log[*pos+lleng+2]!=0x28 || log[*pos+lleng+5]!=0x29){
			*pos=*pos+npos;
			return (-1);
		}

		result->text_no=
			(log[*pos]-0x30)*10000+(log[*pos+1]-0x30)*1000+
			(log[*pos+2]-0x30)*100+(log[*pos+3]-0x30)*10+(log[*pos+4]-0x30);
		if (log[*pos+npos+1]==0x20){
			if (log[*pos+npos+2]<=0x39 && log[*pos+npos+2]>=0x30){
				result->mes_no=log[*pos+npos+2]-0x30;
			}
			else result->mes_no=0;
		}
		else{
			if (log[*pos+npos+1]<=0x39 && log[*pos+npos+1]>=0x30 
					&& log[*pos+npos+2]<=0x39 && log[*pos+npos+2]>=0x30){
				result->mes_no=
						(log[*pos+npos+1]-0x30)*10+(log[*pos+npos+2]-0x30);
			}
			else result->mes_no=0;
		}

		if (log[*pos+npos+23]!=0x20){
			if (log[*pos+npos+23]<=0x39 && log[*pos+npos+23]>=0x30 
					&& log[*pos+npos+24]<=0x39 && log[*pos+npos+24]>=0x30
					&& log[*pos+npos+25]<=0x39 && log[*pos+npos+25]>=0x30
					&& log[*pos+npos+26]<=0x39 && log[*pos+npos+26]>=0x30
					&& log[*pos+npos+27]<=0x39 && log[*pos+npos+27]>=0x30){
				result->com_no=
					(log[*pos+npos+23]-0x30)*10000
					+(log[*pos+npos+24]-0x30)*1000
					+(log[*pos+npos+25]-0x30)*100
					+(log[*pos+npos+26]-0x30)*10
					+(log[*pos+npos+27]-0x30);
				result->id=TEXT5C_ID;
			}
			else{
				result->com_no=0;
				result->id=TEXT5_ID;
			}
		}
		else{
			result->com_no=0;
			result->id=TEXT5_ID;
		}

		result->pos=*pos;
		result->leng=0;
		result->com_lvl=0;
		result->lleng=lleng;

							endcode=1;

						}
					  }
					}
				  }
				}
			  }
			}
		  }
		}
	  }
	}

	*pos=*pos+npos;

	return (endcode);
}


/*
	title check
		(called when first charactor of line is '-')
			in		log : text
					leng: text length
					*pos: relative posision
					result : check result (if check ok)
			out		return 0 : title found
					       -1: not message
					       -2: abnormal end (log end)
					*pos   : next line head posision
*/
int		titlecheck(unsigned char *log,int leng,int *pos,title_content *result)
{
	int		j,endcode;
	int		crno,lleng,npos;
	char	*workchar;

		/*	return code initial set		*/
	endcode=-1;

		/*	title check ( MES(nn) search	*/
	crno=crfind(leng-*pos,(char *)&log[*pos],&lleng,&npos);
	if (crno==0 || npos==0){
		if (lleng>0) *pos=*pos+lleng;
		return (-2);
	}
	if (lleng>80){
		*pos=*pos+npos;
		return (-1);
	}

	for (j=8;j<20;j++) if (log[*pos+j]==0x28) break;
	if (j>19){
		*pos=*pos+npos;
		return (-1);
	}
	if (log[*pos+j+3]!=0x29 || log[*pos+j-1]!=0x53 
					|| log[*pos+j-2]!=0x45 || log[*pos+j-3]!=0x4d){
		*pos=*pos+npos;
		return (-1);
	}

		/*	title found process		*/
	endcode=0;
	if (log[*pos+j+1]==0x20){
		if (log[*pos+j+2]<=0x39 && log[*pos+j+2]>=0x30){
			result->mes_no=log[*pos+j+2]-0x30;
		}
		else{
			result->mes_no=0;
		}
	}
	else{
		if (log[*pos+j+1]<=0x39 && log[*pos+j+1]>=0x30){
			if (log[*pos+j+2]<=0x39 && log[*pos+j+2]>=0x30){
				result->mes_no=(log[*pos+j+1]-0x30)*10+(log[*pos+j+2]-0x30);
			}
			else result->mes_no=0;
		}
		else result->mes_no=0;
	}

	result->pos=*pos;
	result->id=TITLE_ID;
	workchar=&(result->a);
	for (j=0;j<7;j++){
		if (log[*pos+j+3]==0x20) break;
		workchar[j]=log[*pos+j+3];
	}
	workchar[j]=0x00;
	result->lleng=lleng;

	*pos=*pos+npos;

	return (endcode);
}


