/*====================================================================
 *
 * TXF tmporary file module
 *
 *====================================================================
 *                                   copyright(C) 1992-1994 T.Nakatani
 *====================================================================
 */
#include "txf.h"

char *get_filename(char *wildcard)
{
	static struct find_t filedata;
	static char wc[76] = "";
	static char fullpath[80];
	char *npos;
	int ret;

	if (strcmp(wc, wildcard) != 0) {
		strcpy(wc, wildcard);
		ret = _dos_findfirst(wc, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata);
	}
	else {
		ret = _dos_findnext(&filedata);
	}

	if (ret == 0) {
		strcpy(fullpath, wildcard);
		if ((npos = jstrrchr(fullpath, '\\')) != NULL) {
			strcpy(npos + 1, filedata.name);
		}
		else if ((npos = jstrchr(fullpath, ':')) != NULL) {
			strcpy(npos + 1, filedata.name);
		}
		else {
			strcpy(fullpath, filedata.name);
		}
		if (strstr(fullpath, ".BAK") != NULL) {
			return (get_filename(wildcard));
		}
		return (fullpath);
	}
	else {
		return (NULL);
	}
}

void mktfilename()
{
	int i;

	if (tfile[0][0] == NUL) {
		for (i = 0x31; i < 0x33; i++) {
			strcpy(tfile[i-0x31], "$txftmp$.)i(");
			tfile[i - 0x31][10] = i;
		}
	}
	else {
		if (strchr(tfile[0],'.') == NULL) {
			if (*(strchr(tfile[0], NUL) - 1) != '\\') strcat(tfile[0], "\\");
			strcat(tfile[0], "$txftmp$.)1(");
			strcpy(tfile[1], tfile[0]);
			tfile[1][strlen(tfile[0]) - 2] = 0x32;
		}
		else {
			tfile[1][strlen(tfile[1])-1] += 1;
		}
	}
#ifdef DEBUG
	if (viewmode > 3) {
		fprintf(stderr, "TMP 1:%s\nTMP 2:%s\n", tfile[0], tfile[1]);
	}
#endif
}

void input_to_output()
{
	int i = 0, chr = NUL;
	char *tmpinname, *flast, clinebuf[176];
	struct find_t filedata;
	struct stat sbuf;

	if (viewmode > 0) {
		fprintf(stderr, "Copying TMP to output ...");
	}
	if ((viewmode > 0) && (*outputfile == NUL)) {
		fprintf(stderr, "\n");
	}
	tmpinname = ((tmpinfile == -1) ? inputfile : tfile[tmpinfile]);

	if (stat(tmpinname, &sbuf) == 0) {
		fsize = sbuf.st_size;
	}
	else {
		fsize = 0;
	}
	input = fopen(tmpinname, "rb");

	if (*outputfile != NUL) {
		if (_dos_findfirst(outputfile,
			_A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) == 0) {
			strcpy(bakfile, outputfile);
			flast = strrchr(bakfile, '.');
			if (flast == NULL) {
				strcat(bakfile, ".bak");
			}
			else {
				strcpy(flast, ".bak");
			}
			if (removebakfile == 0) {
				if (_dos_findfirst(bakfile,
					_A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) == 0) {
					remove(bakfile);
				}
#ifdef DEBUG
				if (viewmode > 2) {
					fprintf(stderr, "\nInfo:copy %s %s", outputfile, bakfile);
				}
#endif
				sprintf(clinebuf, "copy %s %s > NUL", outputfile, bakfile);
				if (system(clinebuf) != 0) {
					fprintf(stderr, "\nError:cannot copying to BAK file");
					exit(1);
				}
			}
		}
	}

	if (*outputfile != NUL) {
		output = fopen(outputfile, "wb");
	}
	else {
		output = stdout;
	}
#ifdef DEBUG
	if (viewmode > 2)
		fprintf(stderr, "\nInfo:read=%s(%d),write=%s(%d),BAK=%s\n"
			,tmpinname, input, outputfile, output, bakfile);
#endif
	if ((input == NULL) || (output == NULL)) {
		fprintf(stderr, "\nError:cannot open input/output file(3)\n");
		exit(1);
	}

	for (; fsize > 0; fsize--) {
		chr = fgetc(input);
		if (chr == TAB) {
			if (tabx) {
				putspace(((int)(i / tabsize) + 1) * tabsize - i, output);
			}
			else {
				putc((char)TAB, output);
			}
			i = ((int)(i / tabsize) + 1) * tabsize;
		}
		else {
			i++;
			if (chr == LF) i = 0;
		}
		if ((chr != EOF) && (chr != TAB)) fputc(chr, output);
	}
	if (retflg && (chr != LF)) {
		fputc(CR, output);
		fputc(LF, output);
	}
	fclose(input);
	fclose(output);
	if (*outputfile != NUL) {
		utime(outputfile, ftime);
		if (viewmode > 0)
			fprintf(stderr, " done.\n");
	}
}

void tfileopen(int i)
{
	char *tmpinname, *tmpoutname;
	struct stat sbuf;

	tmpinname = ((tmpinfile == -1) ? inputfile : tfile[tmpinfile]);
	tmpoutname = tfile[((tmpinfile > 0) ? 0 : 1)];
	if (stat(tmpinname, &sbuf) == 0) {
		fsize = sbuf.st_size;
	}
	else {
		fsize = 0;
	}

	if (*tmpinname != NUL) {
		input = fopen(tmpinname, "rb");
	}
	else {
		errexit("Cannot open inputfile.");
	}
	output = fopen(tmpoutname, "wb");
#ifdef DEBUG
	if (viewmode > 2) {
		fprintf(stderr, "Info:read=%s(%d),write=%s(%d)\n",
			tmpinname, input, tmpoutname, output);
	}
#endif

	if ((input == NULL) || (output == NULL)) {
		fprintf(stderr, "Error:cannot open TMP file(%d)\n", i);
		exit(1);
	}
}

int wcchk()
{
	static int count = 0;
	char *wctmp;

	if (*wcfile == NUL) {
		if (count == 0) {
			count++;
			if (*inputfile == NUL) {
				do {
					printf("Input Filename >>");
					wctmp = gets(inputfile);
				} while (wctmp == NULL);
			}
			gftime(inputfile, ftime);
			return (1);
		}
		else {
			return (0);
		}
	}
	wctmp = get_filename(wcfile);
	if (wctmp == NULL) {
		return (0);
	}
	strcpy(inputfile, wctmp);
	strcpy(outputfile, wctmp);
	fprintf(stderr, "TXF: %s\n", wctmp);
	gftime(wctmp, ftime);
	
	return (1);
}
