#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*データベースのパス*/
char CLR_PATH[] = "/home/www/cgi-bin/clr_db";
char DATA_PATH[] = "/home/www/cgi-bin/data_db";
char GEO_PATH[] = "/home/www/cgi-bin/geo_db";
char NAME_PATH[] = "/home/www/cgi-bin/name_db";

/*プロトコル各オプションの検出フラグ*/
struct options {
	int W;
	int N;
	int E;
	int S;
	int D;
	int DR;
	int DG;
	int DB;
	int L;
	int T;
	int M;
	int LG;
};
struct options option;

/*プロトコル値*/
float W = 0;
float N = 0;
float E = 0;
float S= 0;
char D[16] = "";
char DR[16] = "";
char DG[16] = "";
char DB[16] = "";
int L[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
int T=0;
int M=0;
char LG[8] = "ja";
int L_NUM = 12;

/*計算用の参照変数：プロトコルによって一意に決定される*/
int	Ordered_L_cnt;
float	SCOPEwidth;
float	sr;
int	match_id[33][16384];
int	match_L[32];
float	clr[36][5][256];
int	clr_class[36];
long	clrID[36][16384];
float	clrData[36][16384];
char	townName[4][4096][16];

int MAX_DEG_VAL = 999;
int MIN_DEG_VAL = 0;

/*計算用スタック：出力ポリゴン頂点セットを表示解像度にあわせて縮約する際*/
int	stack[2][16384];
float	x, y;

/*ログファイル：プログラムデバッグ用*/
char LOGFILE_DEBUG[] = "CGI_output.log";
FILE	*fz;
int log_R, log_L, log_T;

/*ログファイル：実際のユーザアクセスの記録用*/
char LOGFILE_ACCESS[] = "access.log";

int error_cnt_R;
int error_cnt_L;
long error_ID_R[1024];
long error_ID_L[1024];
/*-----------------------------------------------------------*/
int	main(argc,argv)
int	argc;
char	*argv[];
{
int	N1 = 0;
int	N2 = 0;
int i,j;
char log_file[32];
int outN1=0;
int outN2= 0;

	/*debug*/ sprintf(log_file, LOGFILE_DEBUG);
	/*debug*/ fz = fopen(log_file,"w");
	/*debug*/ log_R = log_L = log_T = 1;

	memset(match_id,0,sizeof(match_id));
	memset(match_L,0,sizeof(match_L));
	error_cnt_R = 0;
	error_cnt_L = 0;
	memset(error_ID_R,0,sizeof(error_ID_R));
	memset(error_ID_L,0,sizeof(error_ID_L));
	/*ユーザーアクセスのログ記録*/
	log_lec();

	/*ヘッダー出力*/
	printf("Content-type: text/html\n\n");

	/*アプレットからのクエリーシーケンス解析*/
	protocol_analisys();

	/*クエリーのスコープにマッチするRegionデータのID検索*/
	N1 = match_query('R',0);
	/*debug*/ fprintf(fz,"<log// Match Count : Redion %d>\n",N1);

	/*クエリーのスコープにマッチするLineデータのID検索*/
	for(i=0; i<L_NUM; i++) {
		if(L[i] ==1){
			match_L[i] = match_query('L',i+1);
			/*debug*/ fprintf(fz,"<log// Match Count : Line %d>\n",match_L[i]);
			N2 += match_L[i];
		}
	}
	/*debug*/ fprintf(fz,"\n<log// Output for Applet>\n\n");

	/*マッチしたRegionとLineとTextのID数をアウトプット*/
	if(M==1) N1=N1+48;
	if(T==0) {
		printf("%d %d %d\n",N1,N2,0);
		/*debug*/ fprintf(fz,"%d %d %d\n",N1,N2,0);
	} else if(T==3) {
		printf("%d %d %d\n",N1,N2,N1*2);
		/*debug*/ fprintf(fz,"%d %d %d\n",N1,N2,N1*2);
	} else {
		printf("%d %d %d\n",N1,N2,N1);
		/*debug*/ fprintf(fz,"%d %d %d\n",N1,N2,N1);
	}
	if(M==1) N1=N1-48;

	/*Region用のclrファイルと統計データの読み込み*/
	if(M==0) {
		clr_and_data_read('R',0,D);
	} else if(M==1) {
		clr_and_data_read('R',33,DR);
		clr_and_data_read('R',34,DG);
		clr_and_data_read('R',35,DB);
	}

	/*Line用のclrファイルと統計データの読み込み*/
	for(i=0; i<L_NUM; i++) {
		if(L[i] ==1){
			clr_and_data_read('L',i+1,"");
		}
	}

	/*マッチしたRegionの色彩データとgeoデータをアウトプット*/
	for(i=0;i<N1;i++){
		if(M==0) {
			outN1 += output_one_region_data('R', match_id[0][i],0);
		} else if(M==1) {
			outN1 += output_one_region_data('R', match_id[0][i],33);
		}			
	}
	if(M==1 && outN1==N1) {
		/*debug*/ fprintf(fz,"\n<OK, match output number>\n\n");
		/*print_colorPatern();*/
	}

	/*マッチしたLineの色彩データとgeoデータをアウトプット*/
	for(i=0; i<L_NUM; i++) {
		if(L[i] ==1){
			for(j=0;j<match_L[i];j++){
				outN2 += output_one_region_data('L', match_id[i+1][j],i+1);
			} 
		}
	}
	if(outN1==N1) {
		/*debug*/ fprintf(fz,"<OK, matched count of N1 and output count of N1 = equal>\n");
	}
	if(N2==outN2) {
		/*debug*/ fprintf(fz,"<OK, matched count of N2 and output count of N2 = equal>\n");
	}
		/*debug*/ fprintf(fz,"<matching with stat, error_cnt of matching ID of R= %d>\n", error_cnt_R);
	for(i=0; i<error_cnt_R;i++) {
		fprintf(fz,"<error ID = %d>\n", error_ID_R[i]);
	}
		/*debug*/ fprintf(fz,"<matching with stat, error_cnt of matching ID of L= %d>\n", error_cnt_L);
	for(i=0; i<error_cnt_L;i++) {
		fprintf(fz,"<error ID = %d>\n", error_ID_L[i]);
	}

	/*マッチしたRegionの名前テキストと位置をアウトプット*/
	if(T>0) {
		townName_read();
	}
	for(i=0;i<N1;i++){
		if(T == 3) {
			output_one_region_text(match_id[0][i],0);
			output_one_region_text(match_id[0][i],2);
		} else if(T == 2) {
			output_one_region_text(match_id[0][i],0);
		} else if(T == 1) {
			output_one_region_text(match_id[0][i],1);
		}
	}

	/*出力終了*/
	printf("END\n");

	/*CGI終了*/
	fclose(fz);
	return(1);
}

/*-----------------------------------------------------------*/
int protocol_analisys()
{
char qs[256];
int   cc;
int	 i,j,k;
char	buff[256];
char flag[8];

	memset(qs,0,sizeof(qs));
	cc = 0x00;
	memset(buff,0,sizeof(buff));
	memset(flag,0,sizeof(flag));

	sprintf(qs,"%s",getenv("QUERY_STRING"));
	/*debug*/ fprintf(fz,"<log// Qurry from Applet ; %s>\n", qs);

	option.W = 0;
	option.N = 0;
	option.E = 0;
	option.S = 0;
	option.D = 0;
	option.DR = 0;
	option.DG = 0;
	option.DB = 0;
	option.L = 0;
	option.T = 0;
	option.M = 0;
	option.LG = 0;

	Ordered_L_cnt = 0;
	j = 0;
	for(i=0;i<256;i++) {
		cc = qs[i]; 
		if (cc == '=') {
			buff[j] = 0x00;
			strcpy(flag,buff);
			j=0;
		} else if (cc== '&' || cc == NULL) {
			buff[j] = 0x00;
			if(!strcmp(flag,"W")){
				option.W = 1;
				W = atof(buff);
			} else if(!strcmp(flag,"N")){
				option.N = 1;
				N = atof(buff);
			} else if(!strcmp(flag,"E")){
				option.E = 1;
				E = atof(buff);
			} else if(!strcmp(flag,"S")){
				option.S = 1;
				S = atof(buff);
			} else if(!strcmp(flag,"D")){
				option.D = 1;
				strcpy(D,buff);
			} else if(!strcmp(flag,"DR")){
				option.DR = 1;
				strcpy(DR,buff);
			} else if(!strcmp(flag,"DG")){
				option.DG = 1;
				strcpy(DG,buff);
			} else if(!strcmp(flag,"DB")){
				option.DB = 1;
				strcpy(DB,buff);
			} else if(!strcmp(flag,"L")){
				option.L = 1;
				for(k=0;k<12;k++) {
					if(buff[k]=='1') {
						L[k] = 1;
						Ordered_L_cnt++;
					}
				}
			} else if(!strcmp(flag,"T")){
				option.T = 1;
				T = atoi(buff);
			} else if(!strcmp(flag,"M")){
				option.M = 1;
				M = atoi(buff);
			} else if(!strcmp(flag,"LG")){
				option.LG = 1;
				buff[3] = 0x00;
				strcpy(LG,buff);
			}
			if(cc == NULL) break;
			j=0;
		} else {
			buff[j] = cc;
			j++;
		}
	}

	/*フィルタリングモードの時は海岸線を自動的に出す*/
	if(M==1) {
		L[2] = 1;
		Ordered_L_cnt++;
	}

	/*debug*/
	fprintf(fz,"<Option// option.W = %d, value = %f>\n", option.W,W);
	fprintf(fz,"<Option// option.N = %d, value = %f>\n", option.N, N);
	fprintf(fz,"<Option// option.E = %d, value = %f>\n", option.E,E);
	fprintf(fz,"<Option// option.S = %d, value = %f>\n", option.S,S);
	fprintf(fz,"<Option// option.D = %d, value = %s>\n", option.D,D);
	fprintf(fz,"<Option// option.DR = %d, value = %s>\n", option.DR, DR);
	fprintf(fz,"<Option// option.DG = %d, value = %s>\n", option.DG, DG);
	fprintf(fz,"<Option// option.DB = %d, value = %s>\n", option.DB, DB);
	fprintf(fz,"<Option// option.L = %d, value = ", option.L);
	for(k=0;k<12;k++) fprintf(fz," L[%d]=%d", k, L[k]);
	fprintf(fz,">\n");
	fprintf(fz,"<Option// option.T = %d, value = %d>\n", option.T, T);
	fprintf(fz,"<Option// option.M = %d, value = %d>\n", option.M, M);
	fprintf(fz,"<Option// option.LG = %d, value = %s>\n", option.LG,LG);

	return 1;
}

/*-----------------------------------------------------------*/
int match_query(type,Num)
char type;
int Num;
{

FILE	*fp;
char file[512];
float w,n,e,s;
int id;
char geo_dir[32];
char	buff[8192];
int i,j;

	SCOPEwidth = E-W;
	fprintf(fz,"<log// SCOPEwidth : %f>\n",SCOPEwidth);

	if(type == 'R') {
		if(SCOPEwidth>10) sprintf(geo_dir,"R_1");
		else if(SCOPEwidth<2) sprintf(geo_dir,"R_3");
		else sprintf(geo_dir,"R_2");
	} else {
		if(SCOPEwidth>10) sprintf(geo_dir,"L%d_1",Num);
		else if(SCOPEwidth<2) sprintf(geo_dir,"L%d_3",Num);
		else sprintf(geo_dir,"L%d_2",Num);
	}
	sprintf(file,"%s/%s/id_index",GEO_PATH,geo_dir);

	fprintf(fz,	"<log// Open : %s>\n",file);

	fp = fopen(file,"r");
	if (fp != NULL) {
		i=0;
		j=1;
		while(1) {
			if(get_till_delimiter(fp,buff)==EOF) {
				break;
			} else {
			if(!strcmp(buff,"")){
				get_till_delimiter(fp,buff);
			}
				w = atof(buff);
			}
			get_till_delimiter(fp,buff);
			n = atof(buff);

			get_till_delimiter(fp,buff);
			e = atof(buff);

			get_till_delimiter(fp,buff);
			s = atof(buff);

			get_till_delimiter(fp,buff);
			id = atoi(buff);

			if(w>E || e<W || n<S || s>N) {
			} else {
				match_id[Num][i] = id;
				i++;
			}
			j++;
		}
		fclose(fp);
	} else {
		printf("Error : Cannot open %s\n",file);
		exit(1);
	}

	/*return matched count*/
	return(i);

}

/*-----------------------------------------------------------*/
int clr_and_data_read(type,Num,data)
char type;
int Num;
char data[];
{
FILE *fp;
char clr_file[512];
char data_file[512];
int i,j,k;
char	buff[8192];
int ThisCRL_is_WHITE = 0;

	/*clrファイルの読み込み*/
	if(type == 'R') {
		sprintf(clr_file,"%s/%s.clr",CLR_PATH,data);
	} else {
		sprintf(clr_file,"%s/L%d.clr",CLR_PATH,Num);
	}
	fp = fopen(clr_file,"r");
	
	i = 1;
	if (fp != NULL) {
		while(1) {
			if(get_till_delimiter(fp,buff)==EOF) {
				break;
			} else {
				clr[Num][0][i] = atof(buff);
				get_till_delimiter(fp,buff);
				clr[Num][1][i]  = atof(buff);
				get_till_delimiter(fp,buff);
				clr[Num][2][i] = atof(buff);
				get_till_delimiter(fp,buff);
				clr[Num][3][i] = atof(buff);
				get_till_delimiter(fp,buff);
				clr[Num][4][i] = atof(buff);
				i++;
			}
		}

		if((M==1) &&(clr[Num][0][1] == 0) && (clr[Num][1][1] == 0)) ThisCRL_is_WHITE = 1;

		fprintf(fz,"Now  at Color, Num = %d\n",Num);	
		if(33 == Num) {
			for(k=1;k<17;k++) {
				clr[33][2][k] = 255-17*(k-1);
				clr[33][3][k] = 0;
				clr[33][4][k] = 0;
			}
		}
		if(34 == Num) {
			for(k=1;k<17;k++) {
				clr[34][2][k] = 0;
				clr[34][3][k] = 255-17*(k-1);
				clr[34][4][k] = 0;
				fprintf(fz,"Color[%d] ,in =%d, out=%d\n",k,255-17*k,(int)clr[34][3][k] );				
			}
		}		
		if(35 == Num) {
			for(k=1;k<17;k++) {
				clr[35][2][k] = 0;
				clr[35][3][k] = 0;
				clr[35][4][k] = 255-17*(k-1);
			}		
		}

		if(clr[Num][0][1] > clr[Num][0][2]) {
			clr[Num][0][0] = clr[Num][1][1];
			clr[Num][1][0]  = MAX_DEG_VAL;
		} else {
			clr[Num][0][0] = MIN_DEG_VAL;
			clr[Num][1][0]  = clr[Num][0][1];
		}
		clr[Num][2][0] = clr[Num][2][1];
		clr[Num][3][0] = clr[Num][3][1];
		clr[Num][4][0] = clr[Num][4][1];
		i--;
		if(clr[Num][0][1] > clr[Num][0][2]) {
			clr[Num][0][i] = MIN_DEG_VAL;
			clr[Num][1][i]  = clr[Num][0][i-1];
		} else {
			clr[Num][0][i] = clr[Num][1][i-1];
			clr[Num][1][i]  = MAX_DEG_VAL;
		}
		clr[Num][2][i] = clr[Num][2][i-1];
		clr[Num][3][i] = clr[Num][3][i-1];
		clr[Num][4][i] = clr[Num][4][i-1];

		if(ThisCRL_is_WHITE == 1) clr[Num][2][0] = clr[Num][3][0] = clr[Num][4][0] = 0 ;

		clr_class[Num] = i;
	} else {
		printf("Error : Cannot open %s\n",clr_file);
		exit(1);
	}
	fclose(fp);

	/*統計ファイルの読み込み*/
	i = 0;
	if(type == 'R') {
		sprintf(data_file,"%s/%s.txt",DATA_PATH,data);
	} else {
		sprintf(data_file,"%s/L%d.txt",DATA_PATH,Num);
	}
	fp = fopen(data_file,"r");
	if (fp != NULL) {
		while(1) {
			if(get_till_delimiter(fp,buff)==EOF) {
				break;
			} else {
	/*fprintf(fz,"<log// txt,0 ; %s, ", buff);*/
				clrID[Num][i] = atol(buff);
	/*fprintf(fz,"mem m=%d>" ,clrID[Num][i]);*/
			}
			if(get_till_delimiter(fp,buff)==EOF) {
				break;
			} else {
	/*fprintf(fz,"txt,1 ; %s>\n", buff);*/
				clrData[Num][i] = atof(buff);
			}
			i++;
		}
	} else {
		printf("Error : Cannot open %s\n",data_file);
		exit(1);
	}
	fclose(fp);

	return(1);
}

/*-----------------------------------------------------------*/
int	output_one_region_data(type, output_id,num)
char type;
int output_id;
int num;
{

int ck,m,n;

FILE *fq;
char geo_file[512];
char geo_dir[32];
int s,t;
int region, apex; 
int p;
int i,j,k,g;
char	buff[8192];
int z;

	if(type == 'R') {
		fprintf(fz,"<log// R(%d),ID=%d>\n",log_R,output_id); log_R++;
	} else {
		fprintf(fz,"<log// L(%d),ID=%d>\n",log_L,output_id); log_L++;
	}

	/* 色彩データのアウトプット*/
	if(num!=33) {		/*シンプルモード*/
		ck = 0;
		for(m=0;m<16384;m++){
			if(clrID[num][m] == output_id) {
				fprintf(fz,"match ID, at m=%d, " ,m);
				for(n=0;n<clr_class[num];n++) {
					if(clr[num][0][n] <= clrData[num][m] && clrData[num][m] < clr[num][1][n]) {
						printf("%.0f %.0f %.0f\n",clr[num][2][n] ,clr[num][3][n] ,clr[num][4][n]);
						fprintf(fz,"Data = %.2f, m=%d, ",clrData[num][m], m);
						fprintf(fz,"%.0f %.0f %.0f\n",clr[num][2][n] ,clr[num][3][n] ,clr[num][4][n]);
						ck = 1;
						break;
					}
				}
				break;
			}
		}
		if(ck == 0) {
			if(type == 'R') {
				error_ID_R[error_cnt_R] = output_id;
				error_cnt_R++;
			} else {
				error_ID_L[error_cnt_L] = output_id;
				error_cnt_L++;
			}
			printf("255 255 255\n");
			/*fprintf(fz,"255 255 255\n");*/
		}
	} else {		/*num==33はフィルタリングモード*/
		for(g=0;g<3;g++) {		/*g=0でnum==33はR、g=1でnum==34はG、g=2でnum==35はB*/
			if(g!=0) {
				printf(" ");
				fprintf(fz," ");
			}
			ck = 0;
			for(m=0;m<16384;m++){
				if(clrID[num+g][m] == output_id) {
					for(n=0;n<clr_class[num+g];n++) {
						if(clr[num+g][0][n] <= clrData[num+g][m] && clrData[num+g][m] < clr[num+g][1][n]) {
							printf("%d",(int)clr[num+g][2+g][n]);
							fprintf(fz,"Data = %.2f, n=%d, ",clrData[num+g][m],n);
							fprintf(fz,"RGB = %d",(int)clr[num+g][2+g][n]);
							ck = 1;
							break;
						}
					}
					break;
				}
			}
			if(ck == 0) {
				if(type == 'R') {
					error_ID_R[error_cnt_R] = output_id;
					error_cnt_R++;
				} else {
					error_ID_L[error_cnt_L] = output_id;
					error_cnt_L++;
				}
				printf("255");
				fprintf(fz,"255");
			}
		}
		printf("\n");
		fprintf(fz,"\n");
	}

	/*geoデータのアウトプット*/
	sr = 400/SCOPEwidth;
	
	if(type == 'R') {
		if(SCOPEwidth>10) sprintf(geo_dir,"R_1");
		else if(SCOPEwidth<2) sprintf(geo_dir,"R_3");
		else sprintf(geo_dir,"R_2");
	} else {
		if(SCOPEwidth>10) sprintf(geo_dir,"L%d_1",num);
		else if(SCOPEwidth<2) sprintf(geo_dir,"L%d_3",num);
		else sprintf(geo_dir,"L%d_2",num);
	}
	sprintf(geo_file,"%s/%s/%d.geo",GEO_PATH,geo_dir,output_id);

	fq = fopen(geo_file,"r");
	if (fq != NULL) {

		fgets(buff,256,fq);
		region = region_read(buff);
		if(type == 'R') {
				printf("Region %d\n",region);
	/*fprintf(fz,"Region %d\n",region);*/
		} else if(type == 'L') {
				printf("Region 0\n");
	/*fprintf(fz,"Region 0\n");*/
		} 
		for(i=0; i<region; i++) {
			fgets(buff,256,fq);
			apex = apex_read(buff);
			p=1;
			stack[0][0] = stack[1][0] = stack[1][0] = stack[1][1] = 0;
			for(j=0; j<apex; j++) {
				fgets(buff,256,fq);
				string_to_x_y(buff,2);

				s = (x-W)*sr;
				t = (N-y)*1.2*sr;

				if(s==stack[0][p] && t==stack[1][p]) {
					/* no push of stack */
				} else if (s==stack[0][p-1] && t==stack[1][p-1]) {
					p--;
				} else {
					p++;
					stack[0][p] = s;
					stack[1][p] = t;
				}
			}
			printf("%d\n",p-1);
	/*fprintf(fz,"%d\n",p-1);*/
			for(k=2;k<=p;k++) {
				printf("%d %d\n",stack[0][k],stack[1][k]);
	//fprintf(fz,"%d %d\n",stack[0][k],stack[1][k]);
			}
		}

	} else {
		printf("Error : Cannot open %s\n",geo_file);
		exit(1);
	}
	fclose(fq);

	return(1);
}

/*-----------------------------------------------------------*/
int townName_read()
{
FILE *fp;
char name_file[512];
int i;
char	buff[8192];

	/*市町村Nameファイルの読み込み*/
	sprintf(name_file,"%s/townList.%s",NAME_PATH,LG);
	fp = fopen(name_file,"r");
	
	i = 0;
	if (fp != NULL) {
		while(1) {
			if(get_till_delimiter(fp,buff)==EOF) {
				break;
			} else {
				strcpy(townName[0][i] ,buff);
				get_till_delimiter(fp,buff);
				strcpy(townName[1][i] ,buff);
				get_till_delimiter(fp,buff);
				strcpy(townName[2][i] ,buff);
				get_till_delimiter(fp,buff);
				strcpy(townName[3][i] ,buff);
				i++;
			}
		}
	} else {
		printf("Error : Cannot open %s\n",name_file);
		exit(1);
	}
	fclose(fp);

	return(1);
}

/*-----------------------------------------------------------*/
int	output_one_region_text(output_id,num)
int output_id;
int num;
{
int m,n;
int s,t;

	/* 市区町村名のアウトプット*/
	if(num == 0) {
		for(m=0;m<4096;m++){
			if(atoi(townName[0][m]) == output_id) {
				s = (atof(townName[3][m])-W)*sr;
				t = (N-atof(townName[2][m]))*1.2*sr;
				printf("Text %d %d %s\n",s,t ,townName[1][m]);
				fprintf(fz,"<log// T(%d),ID=%d,%d %d>\n",log_T,output_id,s,t);
				log_T++;
				break;
			}
		}
	} else {
		for(m=0;m<4096;m++){
			if(clrID[0][m] == output_id) {
				for(n=0;n<4096;n++){
					if(clrID[0][m] == atoi(townName[0][n])) {
						s = (atof(townName[3][n])-W)*sr;
						t = (N-atof(townName[2][n]))*1.2*sr;
						if(num == 2) t += 12;
						printf("Text %d %d %.1f\n",s ,t ,clrData[0][m]);
						fprintf(fz,"<log// T(%d),ID=%d,%d %d>\n",log_T,output_id,s ,t); 
						log_T++;
						break;
					}
				}
				break;
			}
		}
	}		
	
	return(1);
}

/*-----------------------------------------------------------*/
int	get_till_delimiter(fp,buf)
FILE	*fp;
char	buf[];
{
int	i;
char	get[2];

	i = 0;
	while(fread(get,1,1,fp)!=NULL) {
		if (get[0]==0x20) {
			buf[i] = 0;
			return(i);
		}
		if (get[0]==0x09) {
			buf[i] = 0;
			return(i);
		}
		if (get[0]==0x0a) {
			buf[i] = 0;
			return(i);
		}
		if (get[0]==0x0d) {
			buf[i] = 0;
			return(i);
		}
		buf[i] = get[0];
		i++;
	}
	return(EOF);
}

/*------------------------------------------------------*/
int region_read(s)
char s[256];
{
	char buf[256];
	int i,k,ck;
	int r;

	/*printf("\nregion_read\n");
	printf("%s",s);*/

	i=0;
	ck=0;
	k=0;
	memset(buf,0,sizeof(buf));
	while(1) {
		if(s[i]!=0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]==0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]!=0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]==0X20 || s[i]==0X0a || s[i]==0X0d) {
			break;
		} else {
			ck=1;
			buf[k] = s[i];
			i++;
			k++;
		}
	}
	r = atoi(buf);
	
	/*printf("r=%d,ck=%d\n",r,ck);*/

	return(r);

}

/*------------------------------------------------------*/
int apex_read(s)
char s[256];
{
	char buf[256];
	int i,k,ck;
	int a;

	i=0;
	ck=0;
	a=0;

	/*printf("\napex_read\n");
	printf("%s",s);*/

	k=0;
	memset(buf,0,sizeof(buf));

	while(1) {
		if(s[i]!=0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]==0X20 || s[i]==0X0a || s[i]==0X0d) {
			break;
		} else {
			ck=1;
			buf[k] = s[i];
			i++;
			k++;
		}
	}
	a = atoi(buf);
	/*printf("a=%d,ck=%d\n",a,ck);*/

	return(a);

}

/*------------------------------------------------------*/
int string_to_x_y(s,item_num)
char    s[256];
int item_num;
{
	char buf[256];
	int i,k,ck;

	/*printf("\nstring_to_x_y\n");
	printf("%s",s);*/

	i=0;
	ck=0;
	k=0;
	memset(buf,0,sizeof(buf));

	while(1) {
		if(s[i]!=0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]==0X20) {
			break;
		} else {
			ck=1;
			buf[k] = s[i];
			i++;
			k++;
		}
	}
	x = atof(buf);

	k=0;
	memset(buf,0,sizeof(buf));
	while(1) {
		if(s[i]!=0X20) {
			break;
		} else {
			i++;
		}
	}
	while(1) {
		if(s[i]==0X20 || s[i]==0X0a || s[i]==0X0d) {
			break;
		} else {
			ck=2;
			buf[k] = s[i];
			i++;
			k++;
		}
	}
	y = atof(buf);
	
	/*printf("x=%.2f,y=%.2f,ck=%d\n",x,y,ck);*/

	if(ck == item_num) ck=0;
	return(ck);

}

log_lec()
{

FILE *fx;
char log_file[32];
char	dt[2][128];

	make_date(dt[0],dt[1]);

	sprintf(log_file,LOGFILE_ACCESS);
	fx = fopen(log_file,"a");
	fprintf(fx,"%s %s",dt[0],dt[1]);
	fprintf(fx,"／%s", getenv("REMOTE_HOST"));
	fprintf(fx,"／%s", getenv("HTTP_USER_AGENT"));
	fprintf(fx,"／%s\n", getenv("QUERY_STRING"));
	fclose(fx);
}

int	make_date(date1,date2)
char	date1[],date2[];
{
struct	tm	*ltm;
long	timecd;

	timecd = time(0);
	ltm = localtime(&timecd);

	sprintf(date1,"%04d.%02d.%02d",ltm->tm_year+1900,ltm->tm_mon+1,ltm->tm_mday);
	sprintf(date2,"%02d:%02d:%02d(pid=%08d)",ltm->tm_hour,ltm->tm_min,ltm->tm_sec,getpid());
	return(0);
}
