/***************
*
* g:\exe\txl\src\txldir2.c
*/
#include "txl.h"

int getkeyid(char *str)
{
	int i = 0;
	int key = 0;
	while (*(str + i) && *(str + i) != '.') {
		if (*(str + i + 1) && *(str + i + 1) != '.') {
			key += *(int *)(str + i);
			i += 2;
		}
		else {
			key += *(str + i);
			i++;
		}
	}
	if (key == 0) {
		key = 1;
	}
	else if ((key & 0x7fff) == 0x7fff) {
		key = 0x7ffe;
	}
	else {
		key &= 0x7fff;	/* 正数化 */
	}
	return (key);
}

void readmyforum(char *fpath)
{
/*
%%%%  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$       F&
   1  FMTOWNS フォーラム 1 (基本編)                 FTOWNS1
*/
	int i, old;
	int key, match;
	char *token;
	FILE *fp;
	char *buf = logstr_mlist;	/* 80バイト---使い回し(^^;) */

	if ((fp = fopen(fpath, "rt")) == NULL) {
		return;	/* myforum.logは存在しない */
	}
	while (!feof(fp)) {
		fgets(buf, 80, fp);
		if ((token = strchr(buf, '\n')) != NULL) {
			*token = NUL;
		}
		if (!matchstr("%%%%  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ F&",
				buf)) {
			continue;	/* ゴミは捨てる */
		}
		strncpy(log_fname, buf + 52, 8);
		*(log_fname + 8) = NUL;
		key = getkeyid(log_fname);
		match = 0;
		old   = 0;
		for (i = 0; key <= finfo[i].key; i = finfo[i].next) {
			old = i;
			if (finfo[i].key == key) {
				if (far_strcmp(finfo[i].fname, log_fname) == 0) {
					match = 1;
					break;
				}
			}
			if (finfo[i].next == 0) {
				break;
			}
		}
		if (!match) {
			maxfinfodata++;
			finfo[maxfinfodata].next = finfo[old].next;
			finfo[old].next = maxfinfodata;
			finfo[maxfinfodata].key = key;
			far_strcpy(finfo[maxfinfodata].fname, log_fname);
			*(buf + 46) = NUL;
			del_tailspace(buf, 45);
			far_strcpy(finfo[maxfinfodata].title, buf + 6);
		}
	}
	fclose(fp);
	return;
}

void readtitle(char *fpath, long *para)
{
	int i, old;
	int key, match;
	FILE *fp;
	char *buf = logstr_mlist;	/* 80バイト---使い回し(^^;) */
	char *token;

	if ((fp = fopen(fpath, "rt")) == NULL) {
		return;	/* fapxtxf.set は存在しない */
	}
	while (!feof(fp)) {
		fgets(buf, 80, fp);
		if ((*buf >= '\"') && (*buf <= '%')) {		/* "#$% */
			continue;
		}
		if ((token = strchr(buf, '\n')) != NULL) {
			*token = NUL;
		}
		if (matchstr("'&myforum=", buf)) {
			strcpy(line2, buf + 9);		/* bakfile へコピーする(^^;) */
		}
		else if (matchstr("'&exclude=", buf)) {
			strcpy((char *)(&(para[maxexclude])), buf + 9);
			maxexclude++;
		}
		else if (matchstr("'&recursive=", buf)) {
			for (i = 0; finfo[i].next > 0; i = finfo[i].next);
			maxfinfodata++;
			finfo[maxfinfodata].next = 0;
			finfo[i].next = maxfinfodata;
			finfo[maxfinfodata].key = -1;
			far_strcpy(finfo[maxfinfodata].fname, buf + 11);
		}
		else if (*buf == '!') {
			if ((token = strchr(buf + 1, '=')) != NULL) {
				*token = NUL;
				token++;
				if (strlen(buf + 1) > 14) {
					*(buf + 14 + 1) = NUL;
				}
				key = getkeyid(buf + 1);

				match = 0;
				old   = 0;
				for (i = 0; key <= finfo[i].key; i = finfo[i].next) {
					old = i;
					if (finfo[i].key == key) {
						if (far_strcmp(finfo[i].fname, buf + 1) == 0) {
							match = 1;
							break;
						}
					}
					if (finfo[i].next == 0) {
						break;
					}
				}
				if (!match) {
					maxfinfodata++;
					finfo[maxfinfodata].next = finfo[old].next;
					finfo[old].next = maxfinfodata;
					finfo[maxfinfodata].key = key;
					far_strcpy(finfo[maxfinfodata].fname, buf + 1);
					if (strlen(token) > 40) {
						*(token + 40) = NUL;
					}
					far_strcpy(finfo[maxfinfodata].title, token);
				}
			}
		}
	}
	fclose(fp);
	return;
}

int dspmyforum(char *fname, FILE *fp)
{
	int i;
	int key;
	char *buf, *tmp;

	strncpy(log_fname, fname, 14);
	*(log_fname + 14) = NUL;
	buf = logstr_mlist;	/* 80バイト---使い回し(^^;) */
	key = getkeyid(log_fname);
	for (i = 0; key <= finfo[i].key; i = finfo[i].next) {
		if (finfo[i].key == key) {
			if (far_strchr(finfo[i].fname, '.') == NULL) {
				if ((tmp = jstrchr(log_fname, '.')) != NULL) {
					*tmp = NUL;
				}
			}
			if (far_strcmp(finfo[i].fname, log_fname) == 0) {
				far_strcpy(buf, finfo[i].title);
				fputs(buf, fp);
				fputc('\n', fp);
				return (0);
			}
		}
		if (finfo[i].next == 0) {
			break;
		}
	}
	return (1);
}
