/***************
*
* g:\exe\txl\src\txldll.c
*/
#include "txl.h"

char dllform[68]= "%%-%%-%% %%:%% $$$$$$$$$$$$ $             %%%% $$$$$$$$$$$$ %%%%.% ";

void marklist(int *dlst, FILE *fp, FILE *fw, int maxno)
{
	int i = 0, listno;

	while (!feof(fp)) {
		fgets(line1, 80, fp);
		if (sscanf(line1, "%d", &listno)) {
			while (((dlst[i]) & 0x3fff) > listno) {
				i++;
			}
			if (((dlst[i]) & 0x3fff) == listno) {
				if (*(line1+4) != ' ') {
					*(line1+4) = ' ';
				}
				else {
					*(line1+4) = (((dlst[i]) & 0x8000) ? '!' : 
								(((dlst[i]) & 0x4000) ? '･' : '#'));
				}
			}
		}
		fputs(line1, fw);
	}
}

int readdownlist(int *dlst, FILE *fp)
{
/*
1994-03-02 07:16 FLABO      2  1125  XSORT100.LZH    7803    48    162
%%%%-%%-%% %%:%%               %%%%  $$$$$$$$$$$$ %%%%%%% %%%%%  %%%%%
1994-03-02 07:16 FLABO      2  1125  XSORT100.GGG    2128  ----    ---
*/
/* ニューバージョン
  日 付  時 刻  電話番号 ROAD FORUM名  DL  No.  ファイル名 ｻｲｽﾞ[KB]  時 間  ﾚｰﾄ
94-06-26 05:59 xxxx-xx-xxxx x FLABO     2 1664 QKC190.GGG      0.9             
94-06-20 07:05      99-0200 2 FRAV     12  152 MPTW23BI.LZH   69.1    6:07  192
94-08-23 07:30      99-0200 2 BMAIL            FPX419ﾒﾓ.LZH    0.5      11   42
94-07-21 06:36 ------------ - FRAV     12  167 MPTW23KP.LZH  107.4 ------- ----
*/
	int i;
	char *tmp;

	sprintf(log_libstr, "%2d", log_libno);
	for (tmp = log_fname, i = 30; *tmp != NUL; tmp++, i++) {
		dllform[i] = *tmp;
	}
	for (tmp = log_libstr, i = 39; *tmp != NUL; tmp++, i++) {
		dllform[i] = *tmp;
	}
	i = 0;
	while (!feof(fp)) {
		fgets(line1, 80, fp);
		if (matchstr(dllform, line1)) {
			sscanf(line1 + 42, "%d", &(dlst[i]));
			if (*(line1 + 78) == '-') {
				dlst[i] |= 0x8000;
			}
			if (*(line1 + 78) == ' ') {
				dlst[i] |= 0x4000;
			}
			i++;
		}
	}
	return (i);
}

int cmpdll(int *x, int *y)
{
	int ret;

	ret = ((*y) & 0x3fff) - ((*x) & 0x3fff);
	if (!ret) {
		if ((*y)^(*x)) {
			if ((*y) & 0x0C000) {
				return (-1);
			}
			else {
				return (1);
			}
		}
	}
	return (ret);
}

void dllmark()
{
	FILE *fl, *fr;
	int *dlst;
	int maxno;

	if ((dlst = calloc(1024, 2)) ==NULL) {
		errexit("out of memory");
	}
	if ((fl = fopen(outputfile, "rt")) == NULL) {
		fprintf(fpmes, "TXL:can't open '%s'", outputfile);
		free(dlst);
		Exit(1);
	}
	if ((fr = fopen(inputfile, "rt")) == NULL) {
		fprintf(fpmes, "TXL:can't open '%s'", inputfile);
		fclose(fl);
		free(dlst);
		Exit(1);
	}
	fgets(line1, 80, fl);
	if (sscanf(line1, "FORUM:%s LIB:%d", log_fname, &log_libno) < 2) {
		free(dlst);
		errexit("checking logfile!!(Is it liblist ?)");
	}
	fputs(line1, fpmes);

	do {
		fgets(line1, 80, fl);
		fputs(line1, fpmes);
	} while(jstrncmp(line1, "番号", 2) && !feof(fl));	/* 空読み */
	maxno = readdownlist(dlst, fr);
	fclose(fr);

	qsort(dlst, maxno, sizeof(int), cmpdll);

	marklist(dlst, fl, fpmes, maxno);
	fclose(fl);
	fclose(fpmes);
	free(dlst);
}

void dllinit()
{
/*	fprintf(stderr, "TXL: open DownLoadListMarker\n");	*/
}

void dlldriver(char *val[])
{
	dllinit();

	inputfile = val[0];		/* downlist.dat */
	outputfile = val[1];	/* logfile		*/
	dllmark();
	Exit(0);
}
