/***************
*
* g:\exe\txl\src\txlnpcut.c
*/
#include "txl.h"

void result()
{
	if (make_val > 0) {
		fprintf(fpmes, "\n%4d file(s) picked !\n",make_val);
	}
	else {
		fprintf(fpmes, "\n no files picked.\n");
	}
}

void make_new_name(char *tellptr)
{
	char tellchr;
	struct find_t filedata;

	if (tellptr == NULL) {
		tellptr = strchr(line1, NUL) - 1;
	}
	tellchr = *tellptr;
	tellchr++;

	if (tellchr == '\\') {
		fprintf(fpmes, "Error:increment point is overrun the filename.\n");
		result();
		Exit(1);
	}
	if (isalnum((char)tellchr)) {
		*tellptr = tellchr;
	}
	else if (tellchr == ':') {
		*tellptr = '0';
		make_new_name(tellptr - 1);
	}
	else if ((tellchr=='{') || (tellchr=='[')) {
		*tellptr = tellchr - 26;
		make_new_name(tellptr - 1);
	}
	else {
		fprintf(fpmes, "Error:cannot make new filename...\n");
		Exit(1);
	}
	if (_dos_findfirst(line1, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
		== 0) {
		make_new_name(strrchr(line1, '.') - 1);
	}
}

int findfront(FILE **input, FILE **output)
{
	int i = 0, chr = NUL;
	struct find_t filedata;

	while (i < sizeof(npcut_front)) {
		chr = getc(*input);
		if (chr == npcut_front[i]) {
			i++;
		}
		else if (chr == 0x0d) {
			chr = getc(*input);
			if (chr != 0x0a) i = 0;
		}
		else {
			i = 0;
		}
		if (chr == EOF) {
			return (-1);
		}
	}
	if (_dos_findfirst(line1, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
		==0) {
		make_new_name(strchr(line1, '.') - 1);
	}
	if (*line1 != NUL) {
		*output = fopen(line1, "wb");
	}
	else {
		*output = NULL;
	}
	if (*output == NULL) {
		fprintf(fpmes, "File=%s\n", line1);
		errexit("cannot open line1");
	}
	return (0);
}

int findtell(int endflg, FILE **input, FILE **output)
{
	int i=0, chr=NUL, j=0, k=0, diff=0, same=0, l=0;
	char tmpbuf[24];
	char *delstr[NP_DEL], *delptr[NP_DEL];

	delstr[0] = npcut_tellstr;
	delstr[1] = npcut_niffirst;
	delstr[2] = npcut_nifend;
	delstr[3] = npcut_weather;
	weatherflg = 0;

	for (j = 0; j < NP_DEL; j++) {
		delptr[j] = delstr[j];
	}

	if (endflg) return (-1);

	while (i < (sizeof(npcut_tellstr) - 1)) {
		chr = getc(*input);
		if (chr == EOF) {
			return (-1);
		}
		if (chr == npcut_tellstr[i]) {
			i++;
		}
		else if (chr == 0x0d) {
			putc(0x0d, *output);
			chr = getc(*input);
			if (chr != 0x0a) {
				i = 0;
			}
		}
		else {
			i = 0;
		}

		diff = 0;
		same = 0;
		for (k = 0; k < NP_DEL; k++) {
			if (chr == *delptr[k]) {
				delptr[k] += 1;
				if (*delptr[k] == NUL) {
					same = 1;
					delptr[k] = delstr[k];
					if (k == 3) {
						weatherflg = 1;
					}
				}
			}
			else {
				delptr[k] = delstr[k];
				diff++;
			}
		}
		if (same == 0) {
			if (diff < NP_DEL) {
				tmpbuf[l] = chr;
				l++;
				tmpbuf[l] = NUL;
			}
			else {
				if (weatherflg == 1) {
					fclose(*output);
					fprintf(fpmes, "%s\n", line1);
					make_val += 1;
					make_new_name(strrchr(line1, '.') - 1);
					if((*output = fopen(line1,"wb")) == NULL) {
						fprintf(fpmes, "Error:cannot open line1\n");
						Exit(0);
					}
					weatherflg = 0;
				}
				if (l > 0) {
					fwrite(tmpbuf, 1, l, *output);
				}
				putc((char)chr, *output);
				l = 0;
			}
		}
		else {
			l = 0;
		}
	}

	fclose(*output);
	fprintf(fpmes, "%s\n", line1);
	make_val += 1;
	make_new_name(strrchr(line1, '.') - 1);

	return (0);
}

void npcut()
{
	FILE *input, *output = NULL;
	int ret = 0;
	if (strchr(line1,'.') == NULL) {
		strcat(line1, ".NPB");
	}

	if (*inputfile != NUL) {
		input = fopen(inputfile, "rb");
	}
	else {
		errexit("Cannot open inputfile.");
	}
	if (input == NULL) {
		errexit("cannot open inputfile");
	}

	while (!ret) {
		ret = findfront(&input, &output);
		ret = findtell(ret, &input, &output);
	}
	result();
}

void npcutinit()
{
	fprintf(stderr, "TXF inline module. NPCUT Ver1.7\n");
	strcpy(line1, "TXF00000.NPB");

}

void npcutdriver(char *val[])
{
	int i = 0;

	npcutinit();
	for (i = 0; val[i] != NULL; i++) {
		if (val[i][0] == '-') {
			switch (val[i][1]) {
			case 'i':
			case 'I':
				if (val[i+1][0] != '-') {
					inputfile = val[i+1];
				}
				else {
					i--;
				}
				break;
			case 'o':
			case 'O':
				if (val[i+1][0] != '-') {
					strcpy(line1,val[i+1]);
				}
				else {
					i--;
				}
				break;
			default:
				fprintf(stderr, "Error:invailed parameter'%Fs'\n", val[i]);
				Exit(1);
			}
			i++;
		}
		else if (inputfile == NULL) {
			inputfile = val[i];
		}
		else if (inputfile != NULL) {
			strcpy(line1,val[i]);
		}
	}
	npcut();
	Exit(0);
}
