home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************\
- * StripANSI v1.2 *
- * *
- * Intuition Full Version *
- * *
- * Written by Syd L. Bolton *
- * Copyright ©1991,92 Legendary Design Technologies Inc. *
- * *
- * Revision: 001 Date: August 3, 1991 Time: 10:57:24 *
- * Revision: 002 Date: Sept. 18, 1992 Time: 21:57:22 *
- * Revision: 003 Date: Nov. 14, 1992 Time: 23:25:00 *
- \*********************************************************************/
-
- #include <exec/memory.h>
- #include <stdio.h>
- #include <intuition/intuition.h>
- #include <graphics/gfxbase.h>
- #include <functions.h>
-
- #define ENTRIES 26
- #define EXTENTRIES 11
- #include "main.h"
-
- long *IntuitionBase=0,*GfxBase=0;
- struct Window *Window,*MessageWindow,*GNWindow;
- struct RastPort *rport,*mrport;
-
- FILE *rf,*wf;
- int count=0,iterations[ENTRIES+EXTENTRIES],read_dir=0,dir_only=0,nb=0,doinc=0;
- int dotab=0; tabspc=8;
- int windowtopedge,windowleftedge,doit[ENTRIES+EXTENTRIES];
-
- int powers[] = {
- 1,10,100,1000,10000
- };
-
- int codes[] = {
- 99,64,65,66,67,68,69,70,72,74,75,76,77,80,83,84,104,108,109,110,
- 112,113,116,117,120,121
- };
-
- char *cnames[] = {
- "RESET TO INITIAL STATE",
- "INSERT [N] CHARACTERS",
- "CURSOR UP [N] CHARACTERS",
- "CURSOR DOWN [N] CHARACTERS",
- "CURSOR FWD [N] CHARACTERS",
- "CURSOR BKWD [N] CHARACTERS",
- "CURSOR NEXT LINE [N]",
- "CURSOR PRECEDING LINE [N]",
- "MOVE CURSOR TO ROW/COLUMN",
- "ERASE TO END OF DISPLAY",
- "ERASE TO END OF LINE",
- "INSERT LINE",
- "DELETE LINE",
- "DELETE CHARACTER [N]",
- "SCROLL UP [N] LINES",
- "SCROLL DOWN [N] LINES",
- "SET LINEFEED MODE",
- "RESET NEWLINE MODE",
- "SELECT GRAPHIC RENDITION",
- "DEVICE STATUS REPORT",
- "»SET CURSOR RENDITION",
- "»WINDOW STATUS REQUEST",
- "»SET PAGE LENGTH",
- "»SET LINE LENGTH",
- "»SET LEFT OFFSET",
- "»SET TOP OFFSET",
- "BELL",
- "BACKSPACE",
- "FORM FEED",
- "CARRIAGE RETURN",
- "DOUBLE WIDTH MODE",
- "CONDENSED MODE",
- "SELECT PRINTER",
- "CANCEL CONDENSED",
- "DESELECT PRINTER",
- "CANCEL DOUBLE WIDTH",
- "CANCEL LINE"
- };
-
- int extcodes[] = {
- 7,8,12,13,14,15,17,18,19,20,24
- };
-
- char filename[128],pattern[40],title[25],path[75],mytitle[30],mypattern[10],oldcode[80],file1[128],file2[128];
- char string[30];
-
- main()
- {
- int i,exit_flag=0;
- char c;
- struct IntuiMessage *message;
- ULONG class;
-
- filename[0]='\0';
-
- openstuff();
-
- do {
- WaitPort(Window->UserPort);
- while ( ( message=(struct IntuiMessage *)
- GetMsg(Window->UserPort) ) != NULL)
- {
- class=message->Class;
- ReplyMsg(message);
-
- if (class==RAWKEY && message->Code==95) help();
- if (class==GADGETUP) option(message);
- if (class==MENUPICK) exit_flag=menu(message);
- if (class==CLOSEWINDOW) exit_flag=1;
- }
- } while (exit_flag==0);
-
- closestuff();
- }
-
- #include "about.c"
- #include "message.c"
- #include "alert.c"
- #include "pointer.c"
- #include "report.h"
- #include "help.h"
- #include "getnum.c"
-
- menu(message)
- struct IntuiMessage *message;
- {
- int rv=0;
-
- switch(MENUNUM(message->Code)) {
- case 0: rv=project(message);
- break;
-
- case 1: special(message);
- break;
-
- default: break;
- }
- return(rv);
- }
-
- project(message)
- struct IntuiMessage *message;
- {
- switch(ITEMNUM(message->Code)) {
-
- case 0: help();
- break;
-
- case 1: about();
- break;
-
- case 2: return(1); /* quit */
-
- default: break;
- }
- return(0);
- }
-
- special(message)
- struct IntuiMessage *message;
- {
- switch(ITEMNUM(message->Code)) {
-
- case 0: doinc=1-doinc; /* toggle entry */
- if (doinc==0) MenuItem4.Flags &= ~CHECKED;
- else MenuItem4.Flags |= CHECKED;
- break;
-
- case 1: dotab=1-dotab; /* toggle entry */
- if (dotab==0) MenuItem5.Flags &= ~CHECKED;
- else MenuItem5.Flags |= CHECKED;
- break;
-
- case 2: get_num();
- break;
-
- default: break;
- }
- }
-
- option(message)
- struct IntuiMessage *message;
- {
- struct Gadget *igad;
- register short i;
- int gadgid;
-
- igad=(struct Gadget *) message->IAddress;
- gadgid=igad->GadgetID;
-
- switch(gadgid) {
- case 10: reportonly();
- break;
-
- case 20: go();
- break;
-
- case 30: for (i=0; i<37; i++) {
- if (Gad[i].Flags & SELECTED) Gad[i].Flags &= ~SELECTED;
- else Gad[i].Flags |= SELECTED;
- }
- RefreshGadgets(&Gad[0],Window,NULL);
- break;
-
- default: break;
- }
- }
-
- reportonly()
- {
- int x;
- char c,bytecount[28],ansicount[24];
-
- for (x=0; x<ENTRIES+EXTENTRIES; x++)
- iterations[x]=0;
-
- strcpy(mytitle,"Choose file for REPORT...");
- strcpy(mypattern,"#?");
- x=getfile(mytitle,mypattern);
- if (x==FALSE) return();
- rf=fopen(filename,"rb");
-
- if (rf==0) {
- alert("Can't open file!");
- return();
- }
-
- strcpy(file1,filename);
- strcpy(file2,"N/A");
-
- count=0; nb=0;
- Message(" Working...");
- SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
-
- do {
- c=getc(rf); nb++;
- if(!(feof(rf))) {
- if (c=='\0x9b' || c==27) countcode(); /* either CSI or ESC */
- else checkextended(c,1);
- }
- } while(!(feof(rf)));
- nb--;
- fclose(rf);
- ClearPointer(MessageWindow);
- CloseWindow(MessageWindow);
- report();
- }
-
- countcode()
- {
- int i,exit_flag=0;
- char c;
-
- do {
- c=getc(rf); nb++;
- for (i=0; i<ENTRIES; i++) {
- if (c==codes[i]) {
- exit_flag=1;
- iterations[i]++;
- count++;
- }
- }
- } while (exit_flag==0) ;
- }
-
- go()
- {
- int x;
-
- for (x=0; x<ENTRIES+EXTENTRIES; x++)
- iterations[x]=0;
-
- filename[0]='\0';
- strcpy(mytitle,"Choose SOURCE file...");
- strcpy(mypattern,"#?");
- x=getfile(mytitle,mypattern);
- if (x==FALSE) return();
-
- strcpy(file1,filename);
-
- filename[0]='\0';
- strcpy(mytitle,"Choose DESTINATION file...");
- strcpy(mypattern,"#?");
- if(!(stdfile("Choose DESTINATION file...",NULL,"#?",filename))) return();
-
- strcpy(file2,filename);
-
- if(!(rf=fopen(file1,"rb"))) {
- alert("Can't open SOURCE!");
- return();
- }
-
- if(!(wf=fopen(file2,"wb"))) {
- fclose(rf); /* don't forget this! */
- alert("Can't open DEST!");
- return();
- }
-
- count=0; nb=0;
- Message(" Working...");
- SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
-
- processgadgets();
-
- readfile();
-
- ClearPointer(MessageWindow);
- CloseWindow(MessageWindow);
- report();
- }
-
- readfile()
- {
- int rv;
- char c;
-
- do {
- c=getc(rf); nb++;
- if(!(feof(rf))) {
- if (c=='\0x9b' || c==27) stripcode(c); /* either CSI or ESC */
- else {
- rv=checkextended(c,0);
- if (rv==0) fputc(c,wf);
- }
- }
- } while(!(feof(rf)));
- nb--;
- fclose(rf);
- fclose(wf);
- }
-
- stripcode(csi)
- char csi;
- {
- int i,num=1,exit_flag=0;
- char c;
-
- oldcode[0]=csi;
-
- do {
- c=getc(rf); oldcode[num++]=c; nb++;
- for (i=0; i<ENTRIES; i++) {
- if (c==codes[i]) {
- exit_flag=1;
- if (doit[i]) putback(num);
- else {
- iterations[i]++;
- count++;
- /* check for putback sequence-new to 1.2*/
- if (c==64 && doinc==1 && doit[i]==0) translate_insertn(num);
- }
- }
- }
- } while (exit_flag==0) ;
- }
-
- checkextended(c,type)
- char c;
- int type;
- {
- register short i;
- int rv=0;
-
- for (i=0; i<EXTENTRIES; i++) {
- if ((c==extcodes[i] && type==1) || (c==extcodes[i] && (doit[ENTRIES+i]==0 && type==0))) {
- count++;
- iterations[ENTRIES+i]++;
- rv=1;
- }
- }
- if (c==9 && dotab==1) {
- dothetabs(); /* new to 1.2 */
- rv=1;
- }
- return(rv);
- }
-
- putback(num)
- int num;
- {
- int i;
-
- for (i=0; i<num; i++)
- fputc(oldcode[i],wf);
- }
-
- report()
- {
- struct IntuiMessage *message;
- struct Window *RWindow;
- struct RastPort *rrport;
- ULONG class;
- int x,y,i,report_exit=0;
- char string[35];
-
- if (count==0) return();
-
- ReportWin.TopEdge=Window->TopEdge;
- ReportWin.LeftEdge=Window->LeftEdge;
-
- if(!(RWindow=OpenWindow(&ReportWin))) {
- alert("Couldn't open Window!");
- return();
- }
-
- rrport=RWindow->RPort;
- x=y=i=0;
-
- SetAPen(rrport,3); SetBPen(rrport,0);
- Move(rrport,3,18); Text(rrport,"SOURCE: ",8);
- SetAPen(rrport,2);
- sprintf(string,"%-14s",file1);
- Text(rrport,string,14);
- Move(rrport,202,18); SetAPen(rrport,3);
- Text(rrport,"DEST: ",6);
- sprintf(string,"%-14s",file2);
- SetAPen(rrport,2);
- Text(rrport,string,14);
- SetAPen(rrport,3); Move(rrport,374,18);
- Text(rrport,"Codes: ",7);
- sprintf(string,"%-6d",count);
- SetAPen(rrport,2);
- Text(rrport,string,6);
- SetAPen(rrport,3); Move(rrport,490,18);
- Text(rrport,"Bytes: ",7);
- sprintf(string,"%-8d",nb);
- SetAPen(rrport,2);
- Text(rrport,string,8);
-
- while (i<ENTRIES+EXTENTRIES) {
- SetAPen(rrport,3); SetBPen(rrport,0);
- Move(rrport,x*320+3,y*8+33);
- sprintf(string,"%27s: ",cnames[i]);
- Text(rrport,string,29);
- SetAPen(rrport,1);
- sprintf(string,"%5d %3d%%",iterations[i],iterations[i]*100/count);
- Text(rrport,string,10);
- y++;
- if (y > 18) {
- y=0;
- x++;
- }
- i++;
- }
-
- do {
- WaitPort(RWindow->UserPort);
- while ( ( message=(struct IntuiMessage *)
- GetMsg(RWindow->UserPort) ) != NULL)
- {
- class=message->Class;
- ReplyMsg(message);
-
- if (class==CLOSEWINDOW) report_exit=1;
- }
- } while (report_exit==0);
-
- CloseWindow(RWindow);
- }
-
- processgadgets()
- {
- register short i;
-
- for (i=0; i<ENTRIES+EXTENTRIES; i++) {
- if (Gad[i].Flags & SELECTED) doit[i]=1; else doit[i]=0;
- }
- }
-
- getfile(titl,path)
- char *titl,*path;
- {
- windowtopedge=Window->TopEdge;
- windowleftedge=Window->LeftEdge;
- while(stdfile(titl, filename, path, filename)) {
- BPTR stdlock;
- struct FileInfoBlock *stdfib;
- FILE *fp;
- int c;
-
- stdfib = AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC);
- if(!stdfib) {
- alert(" Out of memory!");
- break;
- }
-
- if(!(stdlock = Lock(filename, ACCESS_READ))) {
- alert(" Can't obtain lock.");
- FreeMem(stdfib, sizeof(struct FileInfoBlock));
- continue;
- }
- if(!(Examine(stdlock, stdfib)) && dir_only==0) {
- alert("Can't examine file.");
- UnLock(stdlock);
- FreeMem(stdfib, sizeof(struct FileInfoBlock));
- continue;
- }
- UnLock(stdlock);
-
- if(stdfib->fib_DirEntryType >= 0 && dir_only==0) {
- alert("That's a directory.");
- FreeMem(stdfib, sizeof(struct FileInfoBlock));
- continue;
- }
- if(!(fp = fopen(filename, "r")) && dir_only==0) {
- perror(filename);
- FreeMem(stdfib, sizeof(struct FileInfoBlock));
- fclose(fp);
- continue;
- }
- FreeMem(stdfib, sizeof(struct FileInfoBlock));
- if (fp) fclose(fp);
- return(TRUE);
- break;
- }
- return(FALSE);
- }
-
- help()
- {
- struct Window *HWindow;
- struct IntuiMessage *message;
- int help_exit=0,hpage=0;
- ULONG class;
-
- NewHPWin.LeftEdge=Window->LeftEdge+116;
- NewHPWin.TopEdge=Window->TopEdge+26;
-
- if(!(HWindow=OpenWindow(&NewHPWin)))
- return();
-
- PrintIText(HWindow->RPort,&HelpTextList,0,0);
-
- do {
- WaitPort(HWindow->UserPort);
- while ( ( message=(struct IntuiMessage *)
- GetMsg(HWindow->UserPort) ) != NULL)
- {
- class=message->Class;
- ReplyMsg(message);
-
- if (class==GADGETUP) {
- hpage=1-hpage;
- dohelppage(HWindow,hpage);
- }
- if (class==CLOSEWINDOW) help_exit=1;
- }
- } while (help_exit==0);
- CloseWindow(HWindow);
- }
-
- dohelppage(win,hpage)
- struct Window *win;
- int hpage;
- {
- switch (hpage) {
- case 0: SetAPen(win->RPort,0);
- RectFill(win->RPort,2,10,405,145);
- PrintIText(win->RPort,&HelpTextList,0,0);
- HPIText1.IText=(UBYTE *)"MORE";
- RefreshGadgets(&HPGadget1,win,0);
- break;
-
- case 1: SetAPen(win->RPort,0);
- RectFill(win->RPort,2,10,405,145);
- PrintIText(win->RPort,&HelpTextList2,0,0);
- HPIText1.IText=(UBYTE *)"PREV";
- RefreshGadgets(&HPGadget1,win,0);
- break;
-
- default: break;
- }
- }
-
- /*** This is new to 1.2 for Robert Gould. Use this model to add other
- codes if necessary ***/
- translate_insertn(num)
- int num;
- {
- register short i;
- int spaces=0,digits,csi=1;
-
- /* Here we have to find out if two characters (ESC + [) are used for the
- code, or just a $9B CSI ... we have to account for both just in case */
- if (oldcode[0]==27) csi=2;
- digits=num-(1+csi);
- if (oldcode[0]==27) digits=num-3;
-
- /* Now we are going to calculate the spaces. Remember, ALL ANSI codes
- are represented with multiple digits that are ASCII ... so, we have to
- get their numeric value by subtracting 48, and then multiplying by
- the digit it is ... add it all up and you have the number in normal
- form */
- for (i=0; i<digits; i++)
- spaces+=(oldcode[csi+i]-48)*powers[digits-i-1]; /* numbers are in ASCII */
-
- if (spaces > 0) {
- for (i=0; i<spaces; i++)
- fputc(32,wf);
- }
- }
-
- dothetabs()
- {
- register short i;
-
- for (i=0; i<tabspc; i++)
- fputc(32,wf);
- }
-
- openstuff()
- {
- if((IntuitionBase=OpenLibrary("intuition.library",0L))==NULL)
- exit(1);
- if((GfxBase=OpenLibrary("graphics.library",0L))==NULL)
- exit(1);
-
- if (!(Window=OpenWindow(&NewWindowStructure1))) {
- closestuff();
- exit(1);
- }
-
- rport=Window->RPort;
- PrintIText(rport,&IntuiTextList1,0,0);
- DrawBorder(rport,&BorderList1,0,0);
- SetMenuStrip(Window,&MenuList1);
- }
-
- closestuff()
- {
- if (Window) ClearMenuStrip(Window);
- if (Window) CloseWindow(Window);
- if (IntuitionBase) CloseLibrary(IntuitionBase);
- if (GfxBase) CloseLibrary(GfxBase);
- }
-