/********************************************************************/
/*                                                                  */
/*  TXTTODOC.C                                                      */
/*                                                                  */
/*  This program will accept an ASCII text file, and convert it     */
/* into a DOC file for Word Writer ST, or First Word plus.  Written */
/* in Laser_C v2.1 and free to all ST users everywhere.  Leave me   */
/* a note on GEnie if you like it! (This file created with a tab    */
/* size of 2.)                                                      */
/*                                                                  */
/*                                     Boyd Gafford                 */
/*                                     08/24/90                     */
/*                                     GEnie id: B.GAFFORD1         */
/********************************************************************/
#include <stdio.h>
#include <gemdefs.h>
#include <obdefs.h>
#include <ctype.h>
#include <osbind.h>

#define WW_HEADER_SIZE			87
#define FWP_HEADER_SIZE			110

typedef unsigned char uchar;

int		Phys_handle;							/* the workstation variables!				*/
int		intin[128];
int		intout[128];
int		ptsin[128];
int		ptsout[128];
int		contrl[12];
MFDB	Main_fdb;

char	Buffer[81], Fullname[81], Path[81], Filename[13], Wildcard[13];
char	*Format_alert = "[2][Which file format do you wish|to use in converting this|text file?][WW ST|First Word]";

/*------- This is the default Word Writer ST DOC file header --------*/
uchar WW_header[] =
{
	0x1F, 0x30, 0x36, 0x36, 0x30, 0x31, 0x30, 0x33,
	0x30, 0x33, 0x30, 0x31, 0x38, 0x30, 0x30, 0x0A,
	0x1F, 0x36, 0x4C, 0x2E, 0x2E, 0x2E, 0x2E, 0x54,
	0x2E, 0x2E, 0x2E, 0x2E, 0x54, 0x2E, 0x2E, 0x2E,
	0x2E, 0x54, 0x2E, 0x2E, 0x2E, 0x2E, 0x54, 0x2E,
	0x2E, 0x2E, 0x2E, 0x54, 0x2E, 0x2E, 0x2E, 0x2E,
	0x54, 0x2E, 0x2E, 0x2E, 0x2E, 0x54, 0x2E, 0x2E,
	0x2E, 0x2E, 0x54, 0x2E, 0x2E, 0x2E, 0x2E, 0x54,
	0x2E, 0x2E, 0x2E, 0x2E, 0x54, 0x2E, 0x2E, 0x2E,
	0x2E, 0x54, 0x2E, 0x2E, 0x2E, 0x2E, 0x54, 0x2E,
	0x2E, 0x2E, 0x2E, 0x52, 0x0A, 0x1B, 0x80
};

/*------- This is the default First Word Plus DOC file header -------*/
uchar FWP_header[] =
{
	0x1F, 0x30, 0x36, 0x36, 0x30, 0x31, 0x30, 0x33,
	0x30, 0x33, 0x30, 0x35, 0x38, 0x30, 0x30, 0x0A,
	0x1F, 0x31, 0x1F, 0x1F, 0x0A, 0x1F, 0x32, 0x1F,
	0x23, 0x1F, 0x0A, 0x1F, 0x46, 0x30, 0x31, 0x31,
	0x30, 0x30, 0x33, 0x30, 0x0A, 0x1F, 0x39, 0x5B,
	0x2E, 0x2E, 0x2E, 0x2E, 0x7F, 0x2E, 0x2E, 0x2E,
	0x2E, 0x7F, 0x2E, 0x2E, 0x2E, 0x2E, 0x7F, 0x2E,
	0x2E, 0x2E, 0x2E, 0x7F, 0x2E, 0x2E, 0x2E, 0x2E,
	0x7F, 0x2E, 0x2E, 0x2E, 0x2E, 0x7F, 0x2E, 0x2E,
	0x2E, 0x2E, 0x7F, 0x2E, 0x2E, 0x2E, 0x2E, 0x7F,
	0x2E, 0x2E, 0x2E, 0x2E, 0x7F, 0x2E, 0x2E, 0x2E,
	0x2E, 0x7F, 0x2E, 0x2E, 0x2E, 0x2E, 0x7F, 0x2E,
	0x2E, 0x2E, 0x2E, 0x7F, 0x2E, 0x2E, 0x2E, 0x5D,
	0x30, 0x30, 0x31, 0x0A, 0x1B, 0x80
};


/*********/
/* ~main */
/*       ************************************************************/
/********************************************************************/
main()
{
	FILE	*input_fp, *output_fp;
	int		i, ch, header_num;
	uchar *header;
	
	appl_init();														/* init the application		*/
	Phys_handle = open_vwork(&Main_fdb); 		/* Open main workstation	*/
	strcpy(Filename, "\0");
	strcpy(Wildcard, "*.*");
	if ( select_file(Wildcard, Path, Filename, Fullname) )
		if ((input_fp = fopen(Fullname, "rb")) != NULL)
	{
		switch(form_alert(1, Format_alert))
		{
			case 1:
				header = WW_header;
				header_num = WW_HEADER_SIZE;
				break;
			case 2:
				header = FWP_header;
				header_num = FWP_HEADER_SIZE;
				break;
		}
		strcpy(Filename, "SAVEFILE.DOC");
		strcpy(Wildcard, "*.DOC");
			if ( select_file(Wildcard, Path, Filename, Fullname) )
				if ((output_fp = fopen(Fullname, "wb")) != NULL)
		{
			for (i=0; i<header_num; i++)
				fputc(header[i], output_fp);
			while ( (ch = fgetc(input_fp)) != EOF )
			{
				if (ch == ' ')
					fputc(0x1E, output_fp);
				else
					fputc(ch, output_fp);
			}
			fclose(output_fp);
		}
		fclose(input_fp);
	}
	v_clsvwk(Phys_handle);						/* Close virtual workstation		*/
	appl_exit();															/* exit the application */
}

/***************/
/* ~open_vwork */
/*             ******************************************************/
/* Open a virtual workstation.																			*/
/* Note: a virtual workstation is associated with each window				*/
/* created.  This means that each window's graphic attributes are		*/
/* independent of the other's.																			*/
/********************************************************************/
open_vwork(form)
register MFDB	*form;
{
	int x, work_in[11], work_out[57], handle, d;

	for (x=0; x<10; x++)
		work_in[x] = 1;
	work_in[10] = 2;
	handle = graf_handle(&d, &d, &d, &d);
	v_opnvwk(work_in, &handle, work_out);
	form->fd_addr			= Logbase();
	form->fd_w				= work_out[0] + 1;
	form->fd_h				= work_out[1] + 1;
	form->fd_wdwidth	= form->fd_w / 16;
	form->fd_stand		= 0;
	switch(work_out[13]) 
	{
		case 16: form->fd_nplanes = 4; break;
		case 08: form->fd_nplanes = 3; break;
		case 04: form->fd_nplanes = 2; break;
		default: form->fd_nplanes = 1; break;
	}
	return handle;
}
