/*
	keyword.c
	キーワードの処理関数
*/

#include <stdlib.h>
#include "fswch.h"

int split( char *s, char **str, const char *sep); /* 文字の切り分け */

void dispkeyword( void ){/* キーワード表示 */
	int i,j,cpro;
	/* KEYWORD 表示 と カウント */
	clstext();
	cputs(  ESC_SKYBULE"===== KEYWORD =====  "
		ESC_WHITE"[白:選択] "
		ESC_YELLOW"[黄:否定選択]\r\n");
	for(i=0;i<kno;i++){
		for(cpro=j=0;j<ino;j++)
			if(idxpkey[j][i]==ON) cpro++;
		cputs( (skey[i]==SELLECT) ? ESC_WHITE :
			(skey[i]==NONSELLECT) ? ESC_YELLOW :
			ESC_RED );
		cprintf( "[%2d:%-10.10s%4d]",i,keyword[i],cpro);
		if(((i+1)%4)==0) cputs("\r\n");
	}
	cputs( ESC_WHITE );
}

int chekkeyword( void ){/* 作品選択チェック */
	int i,j,cpro;
	for(cpro=i=0;i<ino;i++){
		int cc;
		for(cc=j=0;j<kno;j++){
			if(idxpkey[i][j]!=ON) continue;
			if(skey[j]==SELLECT ){
				cc++;
			}else
			if(skey[j]==NONSELLECT){
				cc=0;
				break;
			}
		}
		if(cc==0 || cc!=ckey) continue;
		selprog[cpro++]=i;
	}
	if(cpro==0) return -1;
	return cpro;
}

void setkf( char *s ){ /* キーフラグにセット */
	int c,i;
	char *str[10];
	c = split(s+1,str,",/");
	for(i=0;i<c;i++){
		int no;
		no=atoi(str[i]);
		if( no < 0 || no >= KEY_MAX ) continue;
		if( *s == '+' ){
			skey[no] = SELLECT ;
			ckey++;
		} else {
			skey[no] = NONSELLECT ;
		}
	}
}
