/*====================================================================
 *
 * TXF textfile format module
 *
 *====================================================================
 *                                   copyright(C) 1992-1994 T.Nakatani
 *====================================================================
 */
#include "txf.h"

#define writeCRLF(PTR,BUF,APTR,ABUF)  *(PTR++) = CR, *(PTR++) = LF, *PTR = NUL, _dos_write(outputh, BUF, strlen(BUF), &dmy), PTR = BUF, APTR = ABUF
extern char far *buf_base;
extern char far *bufrp;
extern long int bufremain, bufyet, bufldval, bufoffset, buffsize;

void txform(void)
{
	int ktype, retflg = 0;
	unsigned long int ix, iy, ox, oy;		/* 入桁,入行,出桁,出行 */
	unsigned int tmp, dmy;				/* 読み込み量 */
	char *wbuf, *watr;				/* Write buffer */
	char far *rptr;					/* Read pointer */
	char *wptr, *watp;				/* Write pointer */
	char *ptmp, *ptmp2, *ptmpa;

	if (viewmode > 1) {
		fprintf(stderr, "TXF main module textfile formatter.\n");
	}
#ifdef DEBUG
	if (viewmode > 5) {
		fprintf(stderr, "ぶら下げ文字=%s(%Np)\n", kl,kl);
		fprintf(stderr, "送り出し文字=%s(%Np)\n", kf,kf);
		fprintf(stderr, "引用通知文字=%s(%Np)\n", kq,kq);
	}
#endif
	rptr = buffileopen(1);
	rptr[rbufsize-1] = CR;
	rptr[rbufsize] = LF;
	rptr[rbufsize+1] = NUL;
	wbuf = malloc(max(84, maxc));
	watr = malloc(max(84, maxc));
	if ((wbuf == NULL) || (watr == NULL)) {
		logicerr("out of memory. (txform)");
	}


	wptr = wbuf;
	watp = watr;
	rptr = readnext(1);
	ix = iy = ox = oy = 1L;

	while (remainbyte()) {
/*
		printf("offset=%d, remain=%ld, ix=%ld, iy=%ld, ox=%ld, oy=%ld, yet=%ld, *rptr(+1)=%X%X(%c%c)\n", bufoffset,bufremain,ix,iy,ox,oy,bufyet, *rptr, *(rptr+1), *rptr, *(rptr+1));
*/

		if (ix == 1L) {
			quote = chktouch(rptr, iy);
		}
		if (quote == 1) {
			if (ox != 1) {
				writeCRLF(wptr, wbuf, watp, watr);
				ox = 1;
				oy++;
			}
			if ((bufoffset + (tmp = linelen(rptr))) <= rbufsize) {
				_dos_write(outputh, rptr, tmp+1, &dmy);
				rptr = readnext(tmp);
				if (remainbyte() > 0) {
					_dos_write(outputh, rptr+1, 1, &dmy);
					ix = 1L;
					iy++;
					ox = 1L;
					oy++;
					rptr = readnext(2);
				}
			}
			else {
				_dos_write(outputh, rptr, rbufsize-1 - bufoffset, &dmy);
				rptr = readnext(rbufsize-1 - bufoffset);
				while (*(rptr = readnext(1)) != LF) {
					_dos_write(outputh, rptr, 1, &dmy);
					if (!remainbyte()) break;
				}
				ix = 1L;
				iy++;
				ox = 1L;
				oy++;
			}
			quote = 0;
		}
		else {
			if ((ox == 1) && (*rptr != CR) && (left > 0)) { /* -kl 処理 */
				for (; ox <= left; ox++) {
					*(wptr++) = ' ';
					*(watp++) = CT_ANK;
				}
			}
			ktype = CT_ANK;
			while (((ox <= maxc) && (*rptr != CR)) || (ktype == CT_KJ1)) {
				*watp = ktype = chkctype(*rptr, ktype);
				*wptr = *rptr;
				wptr++;
				watp++;
				ix++;
				ox++;
				if (*rptr == TAB) {
					ox = ((int)((ox - 2) / tabsize) + 1) * tabsize + 1;
				}
				if (!remainbyte()) {
					break;
				}
				else {
					rptr = readnext(1);
				}

				if ((setline > 0) && (ox == right) 
						&& ((ox % 2) == 0) && (ktype == CT_KJ2)) {
					for (ptmp = wptr-1, ptmpa = watp-1;
							ptmp >= wbuf; ptmp--, ptmpa--) {
						if (*ptmpa == CT_ANK) {
							break;
						}
					}
					if ((*ptmpa == CT_ANK) && (ox > right-2)) {
						for (ptmp2 = wptr-1; ptmp2 > ptmp; ptmp2--) {
							*(ptmp2+1) = *(ptmp2);
						}
						*(ptmp2+1) = ' ';
						wptr++;
						ix += 1L;
						ox += 1L;

						for (ptmp2 = watp-1; ptmp2 > ptmpa; ptmp2--) {
							*(ptmp2+1) = *(ptmp2);
						}
						*(ptmp2+1) = CT_ANK;
						watp++;
/*---*
						if (rptr < rbuf) {
							printf("\nError:Parameter Dump:offset=%d, remain=%ld, ix=%ld, iy=%ld, ox=%ld, oy=%ld, yet=%ld", offset,remain,ix,iy,ox,oy,yet);
							logicerr(NULL);
						}
*---*/
					}
				}
				if (ox > (right - 3) && (ktype != CT_KJ1)) {
					if (*rptr == CR) {
						break;
					}
					if (putret(ox, rptr)) {
						writeCRLF(wptr, wbuf, watp, watr);
						ox = 1;
						oy++;
						retflg = 1;

						break; /* いらないはず */

					}
				}
			}

			if (*rptr == CR) {
				if (((ix == 1L) || findret(ox, iy, rptr) || (remainbyte() < 3L)) && !retflg) {
				
					writeCRLF(wptr, wbuf, watp, watr);
					ox = 1;
					oy++;
				}
				ix = 1L;
				iy++;
				rptr = readnext(2);
			}
/*
			if (ox > right-3) {
				if (putret(ox, rptr)) {
					writeCRLF(wptr, wbuf, watp, watr);
					ox = 1;
					oy++;
				}
			}
*/
			retflg = 0;
		}
	}
	if (ox != 1) {
		*wptr = NUL;
		_dos_write(outputh, wbuf, strlen(wbuf), &dmy);
	}

	free(wbuf);
	free(watr);
	bufclose();
}
