/*************************************************************************
 *                                                                       *
 * FILE:    SCStrip.c                                                    *
 * DATE:    22-July-1991                                                 *
 * AUTHOR:  Randall Crook                                                *
 * VERSION: 1.2                                                          *
 *                                                                       *
 * DESCRIPTION:                                                          *
 *                                                                       *
 * This program is used to strip the screen colour codes from text files *
 *                                                                       *
 *************************************************************************/


#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/libraries.h>
#include <exec/interrupts.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <stdio.h>
#include <intuition/intuition.h>

#define ESCAPE 27
#define INTUITION_REV 33
#define COF 1
#define CCF 2
#define NODF 3
#define NOSF 4


static SHORT BorderVectors1[] = {
	0,0,
	70,0,
	70,17,
	0,17,
	0,0
};
static struct Border Border1 = {
	-1,-1,	         /* XY origin relative to container TopLeft */
	2,0,JAM1,	      /* front pen, back pen and drawmode */
	5,	               /* number of XY vectors */
	BorderVectors1,   /* pointer to XY vectors */
	NULL	            /* next border in list */
};

static struct IntuiText IText1 = {
	2,0,JAM2,	      /* front and back text pens, drawmode and fill byte */
	18,4,	            /* XY origin relative to container TopLeft */
	NULL,	            /* font pointer or NULL for default */
	"QUIT",	         /* pointer to text */
	NULL	            /* next IntuiText structure */
};

static struct Gadget quitgad = {
	NULL,	         /* next gadget */
	190,80,	      /* origin XY of hit box relative to window TopLeft */
	69,16,	      /* hit box width and height */
	GADGHCOMP,	         /* gadget flags */
	RELVERIFY,	   /* activation flags */
	BOOLGADGET,	   /* gadget type flags */
	(APTR)&Border1,/* gadget border or image to be rendered */
	NULL,	         /* alternate imagery for selection */
	&IText1,	      /* first IntuiText structure */
	NULL,	         /* gadget mutual-exclude long word */
	NULL,	         /* SpecialInfo structure */
	2,	         /* user-definable data */
	NULL	         /* pointer to user-definable data */
};

static SHORT BorderVectors2[] = {
	0,0,
	70,0,
	70,17,
	0,17,
	0,0
};
static struct Border Border2 = {
	-1,-1,	      /* XY origin relative to container TopLeft */
	1,0,JAM1,	   /* front pen, back pen and drawmode */
	5,	            /* number of XY vectors */
	BorderVectors2,/* pointer to XY vectors */
	NULL	         /* next border in list */
};

static struct IntuiText IText2 = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	15,4,	         /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"STRIP",	      /* pointer to text */
	NULL	         /* next IntuiText structure */
};

static struct Gadget stripgad = {
	&quitgad,	   /* next gadget */
	22,80,	      /* origin XY of hit box relative to window TopLeft */
	69,16,	      /* hit box width and height */
	GADGHCOMP,     /* gadget flags */
	RELVERIFY,	   /* activation flags */
	BOOLGADGET,	   /* gadget type flags */
	(APTR)&Border2,/* gadget border or image to be rendered */
	NULL,	         /* alternate imagery for selection */
	&IText2,	      /* first IntuiText structure */
	NULL,	         /* gadget mutual-exclude long word */
	NULL,	         /* SpecialInfo structure */
	1,	         /* user-definable data */
	NULL	         /* pointer to user-definable data */
};

static UBYTE destfilegadSIBuff[50];
static struct StringInfo destfilegadSInfo = {
	destfilegadSIBuff,/* buffer where text will be edited */
	NULL,	         /* optional undo buffer */
	0,	            /* character position in buffer */
	50,	         /* maximum number of characters to allow */
	0,	            /* first displayed character buffer position */
	0,0,0,0,0,	   /* Intuition initialized and maintained variables */
	0,	            /* Rastport of gadget */
	0,	            /* initial value for integer gadgets */
	NULL	         /* alternate keymap (fill in if you set the flag) */
};

static SHORT BorderVectors3[] = {
	0,0,
	254,0,
	254,11,
	0,11,
	0,0
};
static struct Border Border3 = {
	-1,-1,	      /* XY origin relative to container TopLeft */
	3,0,JAM1,	   /* front pen, back pen and drawmode */
	5,	            /* number of XY vectors */
	BorderVectors3,/* pointer to XY vectors */
	NULL	         /* next border in list */
};

static struct Gadget destfilegad = {
	&stripgad,	   /* next gadget */
	15,49,	      /* origin XY of hit box relative to window TopLeft */
	253,10,	      /* hit box width and height */
	NULL,	         /* gadget flags */
	RELVERIFY+STRINGCENTER,/* activation flags */
	STRGADGET,	   /* gadget type flags */
	(APTR)&Border3,/* gadget border or image to be rendered */
	NULL,	         /* alternate imagery for selection */
	NULL,	         /* first IntuiText structure */
	NULL,	         /* gadget mutual-exclude long word */
	(APTR)&destfilegadSInfo,/* SpecialInfo structure */
	NULL,	         /* user-definable data */
	NULL	         /* pointer to user-definable data */
};

static UBYTE soucfilegadSIBuff[50];
static struct StringInfo soucfilegadSInfo = {
	soucfilegadSIBuff,/* buffer where text will be edited */
	NULL,	         /* optional undo buffer */
	0,	            /* character position in buffer */
	50,	         /* maximum number of characters to allow */
	0,	            /* first displayed character buffer position */
	0,0,0,0,0,	   /* Intuition initialized and maintained variables */
	0,	            /* Rastport of gadget */
	0,	            /* initial value for integer gadgets */
	NULL	         /* alternate keymap (fill in if you set the flag) */
};

static SHORT BorderVectors4[] = {
	0,0,
	254,0,
	254,11,
	0,11,
	0,0
};
static struct Border Border4 = {
	-1,-1,	      /* XY origin relative to container TopLeft */
	3,0,JAM1,	   /* front pen, back pen and drawmode */
	5,	            /* number of XY vectors */
	BorderVectors4,/* pointer to XY vectors */
	NULL	         /* next border in list */
};

static struct Gadget soucfilegad = {
	&destfilegad,	/* next gadget */
	15,25,	      /* origin XY of hit box relative to window TopLeft */
	253,10,	      /* hit box width and height */
	NULL,	         /* gadget flags */
	RELVERIFY+STRINGCENTER,/* activation flags */
	STRGADGET,	   /* gadget type flags */
	(APTR)&Border4,/* gadget border or image to be rendered */
	NULL,	         /* alternate imagery for selection */
	NULL,	         /* first IntuiText structure */
	NULL,	         /* gadget mutual-exclude long word */
	(APTR)&soucfilegadSInfo,/* SpecialInfo structure */
	NULL,	         /* user-definable data */
	NULL	         /* pointer to user-definable data */
};

#define GadgetList1 soucfilegad

static struct IntuiText IText6 = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"     Welcome...     ",/* pointer to text */
	NULL	         /* next IntuiText structure */
};

static struct IntuiText IText5 = {
	2,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,62,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"© 1991 Randall Crook",/* pointer to text */
	&IText6	      /* next IntuiText structure */
};

static struct IntuiText IText4 = {
	3,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	13,38,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"Destination File",/* pointer to text */
	&IText5	      /* next IntuiText structure */
};

static struct IntuiText IText3 = {
	3,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	14,14,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"File To Be Striped",/* pointer to text */
	&IText4	      /* next IntuiText structure */
};

#define IntuiTextList1 IText3

static struct IntuiText NOSFIText = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"   No Source File  ",/* pointer to text */
	NULL  	      /* next IntuiText structure */
};

static struct IntuiText NODFIText = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"    No dest file   ",/* pointer to text */
	NULL	      /* next IntuiText structure */
};
static struct IntuiText COFIText = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	" Error opening file",/* pointer to text */
	NULL  	      /* next IntuiText structure */
};
static struct IntuiText CCFIText = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	" Error closing file",/* pointer to text */
	NULL  	      /* next IntuiText structure */
};
static struct IntuiText OKIText = {
	1,0,JAM2,	   /* front and back text pens, drawmode and fill byte */
	59,70,	      /* XY origin relative to container TopLeft */
	NULL,	         /* font pointer or NULL for default */
	"       OK!!!       ",    /* pointer to text */
	NULL  	      /* next IntuiText structure */
};

static struct NewWindow scstripwin = {
	7,25,	         /* window XY origin relative to TopLeft of screen */
	283,104,	      /* window width and height */
	2,3,	         /* detail and block pens */
	CLOSEWINDOW+GADGETUP,	/* IDCMP flags */
	WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH, /* other window flags */
	&soucfilegad,	/* first gadget in gadget list */
	NULL,	         /* custom CHECKMARK imagery */
	"SCStrip V1.2",/* window title */
	NULL,	         /* custom screen pointer */
	NULL,	         /* custom bitmap */
	5,5,	         /* minimum width and height */
	-1,-1,	      /* maximum width and height */
	WBENCHSCREEN	/* destination screen type */
};

struct IntuitionBase *IntuitionBase;
struct Window *scstwin;       /* Window Pointer */
struct IntuiMessage *mesg;    /* Window Message */

main()
{

   extern int strip();        /* Function to do the striping */
   
   int result;                /* Error message from strip() */
   
   ULONG msclass=NULL;
   struct Gadget *msiadd;     /* Gadget pointer for window massage */
   
   IntuitionBase=(struct IntuitionBase *)
                     OpenLibrary("intuition.library",INTUITION_REV);
   
   if(IntuitionBase==NULL) exit(100);

   scstwin=(struct Window *) OpenWindow(&scstripwin);
   
   if(scstwin==NULL) exit(100);
   
   PrintIText(scstwin->RPort,&IntuiTextList1,0,0);

   while(msclass!=CLOSEWINDOW){     /* Continue until closed */
   
      Wait(1<<scstwin->UserPort->mp_SigBit); /* wait for somthing to happen */
 
      while((mesg=(struct IntuiMessage *) GetMsg(scstwin->UserPort))!=NULL) {
         msclass=mesg->Class;       /* Find out what happened */
         msiadd=(struct Gadget *)mesg->IAddress;
         ReplyMsg(mesg);            /* Reply */
      }
      if(msclass==GADGETUP) {       /* Was a gadget pressed */
         if(msiadd->GadgetID==2) {  /* Was it quit */
            CloseWindow(scstwin);   /* If so shut down */
            exit();
         }
         if(msiadd->GadgetID==1){   /* Was it strip */
            if((result=strip())!=NULL) {  /* If so, strip */
                  switch(result) {
                     case CCF:
                           PrintIText(scstwin->RPort,&CCFIText,0,0);
                           break;  /* If somthing went wrong say so */
                     case COF:
                           PrintIText(scstwin->RPort,&COFIText,0,0);
                           break;  /* If somthing went wrong say so */
                     case NODF:
                           PrintIText(scstwin->RPort,&NODFIText,0,0);
                           break;  /* If somthing went wrong say so */
                     case NOSF:
                           PrintIText(scstwin->RPort,&NOSFIText,0,0);
                           break;  /* If somthing went wrong say so */
                  }
            }
            else {
               PrintIText(scstwin->RPort,&OKIText,0,0);
            }     /* If nothing went wrong say so */
         }
      }
   }
   CloseWindow(scstwin);   /* Shut down */
   exit();
}




int strip() /* Function to strip out screen codes */
{
	FILE *origf, *newf;     /* Source & Destination file pointers */
	int item, fnum, count;  /* Used in strip process */
	
	if(soucfilegadSIBuff[0] == NULL) {  /* If no source name error */
      return(NOSF);
	}
	
	if((origf=fopen(soucfilegadSIBuff,"r")) == NULL) { 
      return(COF);   /* If unable to open source file, error */
	}
	
   if(destfilegadSIBuff[0]==NULL) { /* If no dest file, error */
      fclose(origf);
      return(NODF);
   }

	if((newf=fopen(destfilegadSIBuff,"w")) == NULL) {
      fclose(origf); /* If unable to open dest file, error */
		return(COF);
	}
	
	while((item=fgetc(origf)) != EOF) {
		if(item == ESCAPE) {
			count=1;
			while(item != 0x6d && count < 10) {
				item=fgetc(origf);
				count++;
			}
			item=fgetc(origf);
		}
		fputc(item,newf);
	}
	
	fnum=fclose(origf);

	if(fnum != NULL) {
   return(CCF);
	}	

	fnum=fclose(newf);
	
	if(fnum != NULL) {
		exit(CCF);
	}
}
