/***************
*
* g:\exe\txf\src\txldirs.c
*/
#include "txl.h"

#define SORT_DATE	1
#define SORT_SIZE	2
#define SORT_NAME	3
#define SORT_EXT	4
#define SORT_FAPX	5
#define PRI_NUM		30

int dirsortoption;
PRIORITY kindofpri[PRI_NUM] = {
/*   表示文字列                ﾃﾞｨﾚｸﾄﾘ/ﾌｧｲﾙ名 比較文字数 優先順位	*/
	{"全体のログ",					"NIFTYLOG",      8,  1},
	{"テキストメール",				"NIFMAIL",       7,  2},
	{"バイナリメール",				"NIFBMAIL",      8,  3},
	{"NAPLPSメール",				"NAPMAIL",       7,  4},
	{"ホームパーティー",			"HP",            3,  5},
	{"パティオ",					"PATIO",         6,  6},
	{"ＦＲＡＶ(^^) --- ＦＡＰＸサポート", "FRAV",    4,  7},
	{"ＴＯＷＮＳフォーラム",		"FTOWNS",        6,  8},
	{"自由行動",					"FREELOG",       7, 10},
	{"フリーソフトウェア",			"FREESOFT",      8, 12},
	{"ＦＭ情報",					"FMNEWS",        6, 15},
	{"フォーラム",					"F",             1,  9 + 0x100},
	{"NAPLPS天気予報",				"WEATHER",       7, 11},
	{"課金情報",					"BILL",          4, 13},
	{"今週のお知らせ",				"NEW",           3, 14},
	{"プロフィール情報",			"PROFILE",       8, 16},
	{"フォーラムメニュー一覧",		"SIGN",          5, 17},
	{"ＲＴＮリスト",				"RTN",           4, 18},
	{"ライブラリリスト",			"LIB",           4, 19},
	{"前回ログアウト情報",			"LASTLOG",       7, 20},
	{"メール送信簿",				"MAILSDL",       8, 21},
	{"送信済メール(バックアップ)",	"SENDMAIL",      8, 22},
	{"加入フォーラム一覧",			"MYFORUM.LOG",  12, 23},
	{"ダウンロード記録",			"DOWNLIST.DAT", 13, 24},
	{"アップロード記録",			"UPLDLIST.DAT", 13, 25},
	{"ダウンロード転送レート",		"DOWNRATE.DAT", 13, 26},
	{"アップロード転送レート",		"UPLDRATE.DAT", 13, 27},
	{"通信記録情報",				"CONNECT.DAT",  12, 28},
	{"ＦＡＰＸ管理ディレクトリ",	"SYSTEM",        7, 29},
	{"",                            "",              0, 255},
};

int sort_date(struct find_t_s *x, struct find_t_s *y)
{
	int ret = 0;
	int sop = sortoption;
	if (x->attrib & y->attrib & _A_SUBDIR) {
		sop = dirsortoption;
	}
	else if ((x->attrib | y->attrib) & _A_SUBDIR) {
		if (x->attrib & _A_SUBDIR) {
			ret = -1;
		}
		else {
			ret = 1;
		}
		return (ret);
	}
	if (x->wr_date > y->wr_date) {
		ret = -1;
	}
	else if (x->wr_date < y->wr_date) {
		ret = 1;
	}
	else {
		if (x->wr_time > y->wr_time) {
			ret = -1;
		}
		else if (x->wr_time < y->wr_time) {
			ret = 1;
		}
	}
	if (sop < 0) {
		ret = -(ret);
	}
	return (ret);
}

int sort_size(struct find_t_s *x, struct find_t_s *y)
{
	int ret = 0;
	int sop = sortoption;
	if (x->attrib & y->attrib & _A_SUBDIR) {
		sop = dirsortoption;
	}
	else if ((x->attrib | y->attrib) & _A_SUBDIR) {
		if (x->attrib & _A_SUBDIR) {
			ret = -1;
		}
		else {
			ret = 1;
		}
		return (ret);
	}
	if (x->size > y->size) {
		ret = -1;
	}
	else if (x->size < y->size) {
		ret = 1;
	}
	if (sop < 0) {
		ret = -(ret);
	}
	return (ret);
}

int sort_name(struct find_t_s *x, struct find_t_s *y)
{
	int ret = 0;
	int sop = sortoption;
	if (x->attrib & y->attrib & _A_SUBDIR) {
		sop = dirsortoption;
	}
	else if ((x->attrib | y->attrib) & _A_SUBDIR) {
		if (x->attrib & _A_SUBDIR) {
			ret = -1;
		}
		else {
			ret = 1;
		}
		return (ret);
	}
	ret = strcmp(x->name, y->name);
	if (sop > 0) {
		ret = -(ret);
	}
	return (ret);
}

int sort_ext(struct find_t_s *x, struct find_t_s *y)
{
	int ret = 0;
	char *xperiod = strchr(x->name, '.');
	char *yperiod = strchr(y->name, '.');
	int sop = sortoption;
	if (x->attrib & y->attrib & _A_SUBDIR) {
		sop = dirsortoption;
	}
	else if ((x->attrib | y->attrib) & _A_SUBDIR) {
		if (x->attrib & _A_SUBDIR) {
			ret = -1;
		}
		else {
			ret = 1;
		}
		return (ret);
	}
	if ((xperiod == NULL) && (yperiod == NULL)) {
		ret = sort_name(x, y);
		if (sortoption < 0) {
			ret = -(ret);
		}
	}
	else if (xperiod == NULL) {
		ret = -1;
	}
	else if (yperiod == NULL) {
		ret = 1;
	}
	else {
		ret = -(strcmp(xperiod, yperiod));
	}
	if (sop < 0) {
		ret = -(ret);
	}
	return (ret);
}

PRIORITY *howpri(struct find_t_s *z)
{
	int i;
	for (i = 0; i < PRI_NUM; i++) {
		if (strncmp(z->name, kindofpri[i].name, kindofpri[i].cmp) == 0) {
			break;
		}
	}
	return (&(kindofpri[i]));
}

int sort_fapx(struct find_t_s *x, struct find_t_s *y)
{
	int ret = 0;
	PRIORITY *xpri, *ypri;
	int sop = sortoption;
	if (x->attrib & y->attrib & _A_SUBDIR) {
		sop = dirsortoption;
	}
	else if ((x->attrib | y->attrib) & _A_SUBDIR) {
		if (x->attrib & _A_SUBDIR) {
			ret = -1;
		}
		else {
			ret = 1;
		}
		return (ret);
	}

	xpri = howpri(x);
	ypri = howpri(y);
	if (((xpri->pri) & 0x0ff) > ((ypri->pri) & 0x0ff)) {
		ret = -1;
	}
	else if (((xpri->pri) & 0x0ff) < ((ypri->pri) & 0x0ff)) {
		ret = 1;
	}
	if (sop < 0) {
		ret = -(ret);
	}
	if (ret == 0) {
		ret = sort_name(x, y);
	}
	return (ret);
}

int (*qsort_func_tbl[6])(struct find_t_s *x, struct find_t_s *y) = {
	NULL,
	sort_date,
	sort_size,
	sort_name,
	sort_ext,
	sort_fapx,
};

