/***************
*
* g:\exe\txf\src\txfnpcut.c
*/
#include "txf.h"

void result()
{
	if (make_val > 0) {
		printf("\n%4d file(s) picked !\n",make_val);
	}
	else {
		printf("\n no files picked.\n");
	}
}

void make_new_name(char *tellptr)
{
	char tellchr;
	struct find_t filedata;

	if (tellptr == NULL) {
		tellptr = strchr(outputfile, NUL) - 1;
	}
	tellchr = *tellptr;
	tellchr++;

	if (tellchr == '\\') {
		printf("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 {
		printf("Error:cannot make new filename...\n");
		exit(1);
	}
	if (_dos_findfirst(outputfile, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
		== 0) {
		make_new_name(strrchr(outputfile, '.') - 1);
	}
}

int findfront()
{
	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(outputfile, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
		==0) {
		make_new_name(strchr(outputfile, '.') - 1);
	}
	if (*outputfile != NUL) {
		output = fopen(outputfile, "wb");
	}
	else {
		output = NULL;
	}
	if (output == NULL) {
		printf("file=%s\n", outputfile);
		errexit("cannot open outputfile");
	}
	return (0);
}

int findtell(int endflg)
{
	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);
					printf("%s\n", outputfile);
					make_val += 1;
					make_new_name(strrchr(outputfile, '.') - 1);
					if((output = fopen(outputfile,"wb")) == NULL) {
						printf("Error:cannot open outputfile\n");
						exit(0);
					}
					weatherflg = 0;
				}
				if (l > 0) {
					fwrite(tmpbuf, 1, l, output);
				}
				putc((char)chr, output);
				l = 0;
			}
		}
		else {
			l = 0;
		}
	}

	fclose(output);
	printf("%s\n", outputfile);
	make_val += 1;
	make_new_name(strrchr(outputfile, '.') - 1);

	return (0);
}

void npcut()
{
	if (strchr(outputfile,'.') == NULL) {
		strcat(outputfile, ".NPB");
	}

	if (*inputfile != NUL) {
		input = fopen(inputfile, "rb");
	}
	else {
		errexit("Cannot open inputfile.");
	}
	if (input == NULL) {
		errexit("cannot open inputfile");
	}

	while (!findtell(findfront()));
	result();
}

void npcutinit()
{
	fprintf(stderr, "TXF inline module. NPCUT Ver1.7 \n");
	strcpy(outputfile, "TXF00000.NPB");

}

void npcutdriver(char far **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] != '-') {
					far_strcpy(inputfile, val[i+1]);
				}
				else {
					i--;
				}
				break;
			case 'o':
			case 'O':
				if (val[i+1][0] != '-') {
					far_strcpy(outputfile, val[i+1]);
				}
				else {
					i--;
				}
				break;
			default:
				fprintf(stderr, "Error:invailed parameter'%Fs'\n", val[i]);
				exit(1);
			}
			i++;
		}
		else if (*inputfile == NUL) {
			far_strcpy(inputfile, val[i]);
		}
		else if (*inputfile != NUL) {
			far_strcpy(outputfile, val[i]);
		}
	}
	npcut();
	exit(0);
}
