//		  ÚÄÄÄÄÄÄÄ¿
//	ÄÄÄÄÄÄÄÄÄ>³ AVNER ³
//	ÄÄÄÄÄÄÄÄÄ>³  BEN  ³ÄÄÄÄÄÄ> Software Engineering Method
//		  ÀÄÄÄÄÄÄÄÙ
//	10 Dov-Hoz st. Tel-Aviv 63416 Israel tel. 972-3-221535

// The Screen NAVigator, ver 1.20 April 1990
// Copyright (c) 1990 by Avner Ben
// Snav is not, and never was, free software.
// for conditions for use refer to file "copyrigh.txt"

// The Screen Navigator is an object-oriented device-independent
// character-graphics driver package, written in the C++ language,
// distributed in the form of C++ source code.
// For further information refer to the documentation files.

// this simple example is intended as a template to be extended and modified
// by the user, provided the above title and copyright notice are unchanged
// and are not ommitted.

/***************************************************************************/

// process typewriter graphics in file <fil>.<ext> giving file <fil>.twg.
// pseudo-screen object used for the conversion.


// 31.7.90 avner ben adapted from demo1.

// site history (of this copy):
// __.__.__: __________________

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "/mtad/snav/snav2.hpp"

class input_page : public panel
{ // pseudo-screen for storing input page for twg processing.

	private :
		char *cell;

	public :
		input_page(square_pos *max_size=NULL);
		~input_page(void)
		{ delete cell; }

		char& operator[](point_pos *pt0=NULL);
		char get_c(point_pos *pt=NULL)
		{	return (*this)[pt]; }
		void put_c(char c, point_pos *pt=NULL, direction *dir=NULL)
		{	(*this)[pt]=c;
			if (!pt) wpmove((dir? dir : &curdir)); }
		void put_color(color_ind &at, point_pos *pt) {}
		void put_attr(vd_attr at, point_pos *pt) {}
		void put_background(vd_clr colornum, point_pos *pt) {}
		void put_forground(vd_clr colornum, point_pos *pt) {}
		boolean get_attr(vd_attr at, point_pos *pt) { return FALSE; }
		color_ind &get_color(point_pos *pt) { return tmpclr1; }
		void posit(point_pos *pt)
		{ if (ask_legal(pt)) cursor=*pt; }
		void fix(void)
		 { panel::fix(); }
		void save(void) {}
		void restore(void) {}
		void clear_save_area(void) {}
		unsigned int ask_cursor(void) { return 0; }
		void set_cursor(unsigned int cursor_type) {}
		unsigned int hide_cursor(void) { return 0; }
};

input_page :: input_page(square_pos *max_size)
 : panel(max_size,NULL,NULL,TRUE,FALSE)
{
	cell=new char[win.eny()*win.enx()];
	if (!cell) { printf("insufficient memory!"); exit(1); }
	clear();
}

char& input_page :: operator[](point_pos *pt0)
{
	static point_pos pt;
	if (pt0) pt=*pt0; else pt=cursor;
	if (!ask_legal(&pt)) pt=point_pos(1,1);
	return *(cell+(pt.y()-1)*ask_limit(rt)+(pt.x()-1));
}

class twg_file
{
	protected :
		char *name;
		FILE *disk;
		input_page *buff;
		int page_num;

	public :
		twg_file(char *nm, char *filmode, char *ext=NULL, input_page *bf=NULL);
		virtual ~twg_file(void)
		{ fclose(disk); delete name; }
};

twg_file :: twg_file(char *nm, char *filmode, char *ext, input_page *bf)
{
	name=new char[strlen(nm)+4]; strcpy(name,nm);
	if (ext) {
		for (int i=0; name[i]!='.' && name[i]!='\0'; ++i) continue;
		name[i++]='.';
		strcpy(&name[i],ext);
	}
	if ((disk=fopen(name,filmode))==NULL) {
		if (*filmode=='r') puts("file not found!");
		else puts("cannot open file!");
		exit(1);
	}
	buff=bf;
	page_num=0;
}

class twg_in : public twg_file
{
	private :
		char cc;

	public :
		twg_in(char *nm);
		~twg_in(void)
		 { delete buff; }
		boolean get_page(void);
		input_page *ask_buff(void)
		 { return buff; }
};

twg_in :: twg_in(char *nm) : twg_file(nm, "r")
{
	printf("measuring file...");
	int maxy=1, maxx=1;
	int lns=1, cols=1;
	while ((cc=getc(disk))!=EOF) {
		if (cc=='\t') {
			cols++;
			while (cols%8!=1) cols++;
		} if (cc=='\n') {
			cols=1; lns++;
			if (lns>maxy) maxy=lns;
		} else if (cc==12)
			cols=lns=1;
		else {
			if (cols>maxx) maxx=cols;
			cols++;
		}
	}
	if (cols>maxx) maxx=cols; if (lns>maxy) maxy=lns;
	buff=new input_page(&square_pos(point_pos(1,1),point_pos(maxy,maxx)));
	printf("\rpage size %s         \n",buff->ask_corner(dn).name());
	fclose(disk); disk=fopen(name,"r");
	cc=getc(disk);
}

class twg_out : public twg_file
{
	public :
		twg_out(char *nm, input_page *bf) : twg_file(nm, "w", "twg", bf) {}
		void put_page(void);
};

boolean twg_in :: get_page()
{
	boolean result=TRUE;
	buff->clear();
	if (cc==EOF) return FALSE;
	buff->home();
	while (cc!=EOF) {
		if (cc=='\n') {
			buff->cr(); buff->move(&dn);
		} else if (cc==12) {
			cc=getc(disk); break;
		} else if (cc=='\t') {
			buff->put_c(' ');
			while (buff->ask_where().x()!=buff->ask_limit(rt)
			 && buff->ask_where().x()%8!=1)
				buff->put_c(' ');
		} else buff->put_c(cc);
		cc=getc(disk);
	}
	printf("page %3d: %4d characters changed\n",++page_num,buff->twg());
	return result;
}

void twg_out :: put_page()
{
	if (page_num++>1) putc(12,disk);
	point_pos pt; int lns=0, cols=0;
	for (pt=buff->ask_corner(up); pt.y()<buff->ask_limit(dn); pt.move(dn)) {
		int ln=1; cols=0;
		for (pt.setx(1); pt.x()<buff->ask_limit(rt); pt.move(rt)) {
			int i;
			if ((*buff)[&pt]==' ') ++cols;
			else {
				if (lns) {
					for (i=0; i<lns; i++)
						putc('\n',disk);
					lns=0;
				}
				if (cols)
					for (i=0; i<cols; i++)
						putc(' ',disk);
				putc((*buff)[&pt],disk);
				ln=cols=0;
			}
		}
		if (!ln) {
			putc('\n',disk); lns=0;
		} else lns++;
	}
}

int main(int argc,char *argv[])
{
	if (argc<2)
		{ puts("file name missing!"); exit(1); }
	twg_in infil(argv[1]);
	twg_out outfil(argv[1],infil.ask_buff());
	while (infil.get_page())
		outfil.put_page();
	return(0);
}
