home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Printer / add16.dms / in.adf / Free_Software / StripANSI_v1.2 / Source / source.lzh / wbprog.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-15  |  12.3 KB  |  638 lines

  1. /*********************************************************************\
  2.  *                          StripANSI v1.2                           *
  3.  *                                      *
  4.  *                      Intuition Full Version                       *
  5.  *                                                                   *
  6.  *                     Written by Syd L. Bolton                      *
  7.  *       Copyright ©1991,92 Legendary Design Technologies Inc.       *
  8.  *                                                                   *
  9.  *        Revision: 001  Date: August 3, 1991  Time: 10:57:24        *
  10.  *        Revision: 002  Date: Sept. 18, 1992  Time: 21:57:22        *
  11.  *      Revision: 003  Date: Nov.  14, 1992  Time: 23:25:00        *
  12. \*********************************************************************/
  13.  
  14. #include <exec/memory.h>
  15. #include <stdio.h>
  16. #include <intuition/intuition.h>
  17. #include <graphics/gfxbase.h>
  18. #include <functions.h>
  19.  
  20. #define ENTRIES 26
  21. #define EXTENTRIES 11
  22. #include "main.h"
  23.  
  24. long *IntuitionBase=0,*GfxBase=0;
  25. struct Window *Window,*MessageWindow,*GNWindow;
  26. struct RastPort *rport,*mrport;
  27.  
  28. FILE *rf,*wf;
  29. int count=0,iterations[ENTRIES+EXTENTRIES],read_dir=0,dir_only=0,nb=0,doinc=0;
  30. int dotab=0; tabspc=8;
  31. int windowtopedge,windowleftedge,doit[ENTRIES+EXTENTRIES];
  32.  
  33. int powers[] = {
  34.     1,10,100,1000,10000
  35.     };
  36.  
  37. int codes[] = {
  38.     99,64,65,66,67,68,69,70,72,74,75,76,77,80,83,84,104,108,109,110,
  39.     112,113,116,117,120,121
  40.     };
  41.  
  42. char *cnames[] = {
  43.     "RESET TO INITIAL STATE",
  44.     "INSERT [N] CHARACTERS",
  45.     "CURSOR UP [N] CHARACTERS",
  46.     "CURSOR DOWN [N] CHARACTERS",
  47.     "CURSOR FWD [N] CHARACTERS",
  48.     "CURSOR BKWD [N] CHARACTERS",
  49.     "CURSOR NEXT LINE [N]",
  50.     "CURSOR PRECEDING LINE [N]",
  51.     "MOVE CURSOR TO ROW/COLUMN",
  52.     "ERASE TO END OF DISPLAY",
  53.     "ERASE TO END OF LINE",
  54.     "INSERT LINE",
  55.     "DELETE LINE",
  56.     "DELETE CHARACTER [N]",
  57.     "SCROLL UP [N] LINES",
  58.     "SCROLL DOWN [N] LINES",
  59.     "SET LINEFEED MODE",
  60.     "RESET NEWLINE MODE",
  61.     "SELECT GRAPHIC RENDITION",
  62.     "DEVICE STATUS REPORT",
  63.     "»SET CURSOR RENDITION",
  64.     "»WINDOW STATUS REQUEST",
  65.     "»SET PAGE LENGTH",
  66.     "»SET LINE LENGTH",
  67.     "»SET LEFT OFFSET",
  68.     "»SET TOP OFFSET",
  69.     "BELL",
  70.     "BACKSPACE",
  71.     "FORM FEED",
  72.     "CARRIAGE RETURN",
  73.     "DOUBLE WIDTH MODE",
  74.     "CONDENSED MODE",
  75.     "SELECT PRINTER",
  76.     "CANCEL CONDENSED",
  77.     "DESELECT PRINTER",
  78.     "CANCEL DOUBLE WIDTH",
  79.     "CANCEL LINE"
  80.     };
  81.  
  82. int extcodes[] = {
  83.     7,8,12,13,14,15,17,18,19,20,24
  84.     };
  85.  
  86. char filename[128],pattern[40],title[25],path[75],mytitle[30],mypattern[10],oldcode[80],file1[128],file2[128];
  87. char string[30];
  88.  
  89. main()
  90. {
  91. int i,exit_flag=0;
  92. char c;
  93. struct IntuiMessage *message;
  94. ULONG class;
  95.  
  96. filename[0]='\0';
  97.  
  98. openstuff();
  99.  
  100. do {
  101.     WaitPort(Window->UserPort);
  102.         while ( ( message=(struct IntuiMessage *)
  103.             GetMsg(Window->UserPort) ) != NULL)
  104.         {
  105.         class=message->Class;
  106.         ReplyMsg(message); 
  107.  
  108.         if (class==RAWKEY && message->Code==95) help();
  109.         if (class==GADGETUP) option(message);
  110.         if (class==MENUPICK) exit_flag=menu(message);
  111.         if (class==CLOSEWINDOW) exit_flag=1;
  112.         }
  113.     } while (exit_flag==0);
  114.  
  115. closestuff();
  116. }
  117.  
  118. #include "about.c"
  119. #include "message.c"
  120. #include "alert.c"
  121. #include "pointer.c"
  122. #include "report.h"
  123. #include "help.h"
  124. #include "getnum.c"
  125.  
  126. menu(message)
  127. struct IntuiMessage *message;
  128. {
  129. int rv=0;
  130.  
  131. switch(MENUNUM(message->Code)) {
  132.     case 0: rv=project(message);
  133.         break;
  134.  
  135.     case 1: special(message);
  136.         break;
  137.  
  138.     default: break;
  139.     }
  140. return(rv);
  141. }
  142.  
  143. project(message)
  144. struct IntuiMessage *message;
  145. {
  146. switch(ITEMNUM(message->Code)) {
  147.  
  148.     case 0: help();
  149.         break;
  150.  
  151.     case 1: about();
  152.         break;
  153.  
  154.     case 2: return(1);            /* quit */
  155.  
  156.     default: break;
  157.     }
  158. return(0);
  159. }
  160.  
  161. special(message)
  162. struct IntuiMessage *message;
  163. {
  164. switch(ITEMNUM(message->Code)) {
  165.  
  166.     case 0: doinc=1-doinc;        /* toggle entry */
  167.         if (doinc==0) MenuItem4.Flags &= ~CHECKED;
  168.             else MenuItem4.Flags |= CHECKED;
  169.         break;
  170.  
  171.     case 1: dotab=1-dotab;        /* toggle entry */
  172.         if (dotab==0) MenuItem5.Flags &= ~CHECKED;
  173.             else MenuItem5.Flags |= CHECKED;
  174.         break;
  175.  
  176.     case 2: get_num();
  177.         break;
  178.  
  179.     default: break;
  180.     }
  181. }
  182.  
  183. option(message)
  184. struct IntuiMessage *message;
  185. {
  186. struct Gadget *igad;
  187. register short i;
  188. int gadgid;
  189.  
  190. igad=(struct Gadget *) message->IAddress;
  191. gadgid=igad->GadgetID;
  192.  
  193. switch(gadgid) {
  194.     case 10: reportonly();
  195.          break;
  196.  
  197.     case 20: go();
  198.          break;
  199.  
  200.     case 30: for (i=0; i<37; i++) {
  201.             if (Gad[i].Flags & SELECTED) Gad[i].Flags &= ~SELECTED;
  202.                 else Gad[i].Flags |= SELECTED;
  203.             }
  204.          RefreshGadgets(&Gad[0],Window,NULL);
  205.          break;
  206.  
  207.     default: break;
  208.     }
  209. }
  210.  
  211. reportonly()
  212. {
  213. int x;
  214. char c,bytecount[28],ansicount[24];
  215.  
  216. for (x=0; x<ENTRIES+EXTENTRIES; x++)
  217.     iterations[x]=0;
  218.  
  219. strcpy(mytitle,"Choose file for REPORT...");
  220. strcpy(mypattern,"#?");
  221. x=getfile(mytitle,mypattern);
  222. if (x==FALSE) return();
  223. rf=fopen(filename,"rb");
  224.  
  225. if (rf==0) {
  226.     alert("Can't open file!");
  227.     return();
  228.     }
  229.  
  230. strcpy(file1,filename);
  231. strcpy(file2,"N/A");
  232.  
  233. count=0; nb=0;
  234. Message("              Working...");
  235. SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
  236.  
  237. do {
  238.     c=getc(rf); nb++;
  239.     if(!(feof(rf))) {
  240.         if (c=='\0x9b' || c==27) countcode();  /* either CSI or ESC */
  241.         else checkextended(c,1);
  242.         }
  243.     } while(!(feof(rf)));
  244. nb--;
  245. fclose(rf);
  246. ClearPointer(MessageWindow);
  247. CloseWindow(MessageWindow);
  248. report();
  249. }
  250.  
  251. countcode()
  252. {
  253. int i,exit_flag=0;
  254. char c;
  255.  
  256. do {
  257.     c=getc(rf); nb++;
  258.     for (i=0; i<ENTRIES; i++) { 
  259.         if (c==codes[i]) {
  260.             exit_flag=1;
  261.             iterations[i]++;
  262.             count++;
  263.             }
  264.         }
  265.     } while (exit_flag==0) ;
  266. }
  267.  
  268. go()
  269. {
  270. int x;
  271.  
  272. for (x=0; x<ENTRIES+EXTENTRIES; x++)
  273.     iterations[x]=0;
  274.  
  275. filename[0]='\0';
  276. strcpy(mytitle,"Choose SOURCE file...");
  277. strcpy(mypattern,"#?");
  278. x=getfile(mytitle,mypattern);
  279. if (x==FALSE) return();
  280.  
  281. strcpy(file1,filename);
  282.  
  283. filename[0]='\0';
  284. strcpy(mytitle,"Choose DESTINATION file...");
  285. strcpy(mypattern,"#?");
  286. if(!(stdfile("Choose DESTINATION file...",NULL,"#?",filename))) return();
  287.  
  288. strcpy(file2,filename);
  289.  
  290. if(!(rf=fopen(file1,"rb"))) {
  291.     alert("Can't open SOURCE!");    
  292.     return();
  293.     }
  294.  
  295. if(!(wf=fopen(file2,"wb"))) {
  296.     fclose(rf);        /* don't forget this! */
  297.     alert("Can't open DEST!");
  298.     return();
  299.     }
  300.  
  301. count=0; nb=0;
  302. Message("              Working...");
  303. SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
  304.  
  305. processgadgets();
  306.  
  307. readfile();
  308.  
  309. ClearPointer(MessageWindow);
  310. CloseWindow(MessageWindow);
  311. report();
  312. }
  313.  
  314. readfile()
  315. {
  316. int rv;
  317. char c;
  318.  
  319. do {
  320.     c=getc(rf); nb++;
  321.     if(!(feof(rf))) {
  322.     if (c=='\0x9b' || c==27) stripcode(c);  /* either CSI or ESC */
  323.         else {
  324.             rv=checkextended(c,0);
  325.             if (rv==0) fputc(c,wf);
  326.             }
  327.         }
  328.     } while(!(feof(rf)));
  329. nb--;
  330. fclose(rf);
  331. fclose(wf);    
  332. }
  333.  
  334. stripcode(csi)
  335. char csi;
  336. {
  337. int i,num=1,exit_flag=0;
  338. char c;
  339.  
  340. oldcode[0]=csi;
  341.  
  342. do {
  343.     c=getc(rf); oldcode[num++]=c; nb++;
  344.     for (i=0; i<ENTRIES; i++) { 
  345.         if (c==codes[i]) {
  346.             exit_flag=1;
  347.             if (doit[i]) putback(num);
  348.             else {
  349.                 iterations[i]++;
  350.                 count++;
  351.                 /* check for putback sequence-new to 1.2*/
  352.                 if (c==64 && doinc==1 && doit[i]==0) translate_insertn(num);
  353.                 }
  354.             }
  355.         }
  356.     } while (exit_flag==0) ;
  357. }
  358.  
  359. checkextended(c,type)
  360. char c;
  361. int type;
  362. {
  363. register short i;
  364. int rv=0;
  365.  
  366. for (i=0; i<EXTENTRIES; i++) {
  367.     if ((c==extcodes[i] && type==1) || (c==extcodes[i] && (doit[ENTRIES+i]==0 && type==0))) {
  368.         count++;
  369.         iterations[ENTRIES+i]++;
  370.         rv=1;
  371.         }
  372.     }
  373. if (c==9 && dotab==1) {
  374.     dothetabs();    /* new to 1.2 */
  375.     rv=1;
  376.     }
  377. return(rv);
  378. }
  379.  
  380. putback(num)
  381. int num;
  382. {
  383. int i;
  384.  
  385. for (i=0; i<num; i++)
  386.     fputc(oldcode[i],wf);
  387. }
  388.  
  389. report()
  390. {
  391. struct IntuiMessage *message;
  392. struct Window *RWindow;
  393. struct RastPort *rrport;
  394. ULONG class;
  395. int x,y,i,report_exit=0;
  396. char string[35];
  397.  
  398. if (count==0) return();
  399.  
  400. ReportWin.TopEdge=Window->TopEdge;
  401. ReportWin.LeftEdge=Window->LeftEdge;
  402.  
  403. if(!(RWindow=OpenWindow(&ReportWin))) {
  404.     alert("Couldn't open Window!");
  405.     return();
  406.     }
  407.  
  408. rrport=RWindow->RPort;
  409. x=y=i=0;
  410.  
  411. SetAPen(rrport,3); SetBPen(rrport,0);
  412. Move(rrport,3,18); Text(rrport,"SOURCE: ",8);
  413. SetAPen(rrport,2);
  414. sprintf(string,"%-14s",file1);
  415. Text(rrport,string,14);
  416. Move(rrport,202,18); SetAPen(rrport,3);
  417. Text(rrport,"DEST: ",6);
  418. sprintf(string,"%-14s",file2);
  419. SetAPen(rrport,2);
  420. Text(rrport,string,14);
  421. SetAPen(rrport,3); Move(rrport,374,18);
  422. Text(rrport,"Codes: ",7);
  423. sprintf(string,"%-6d",count);
  424. SetAPen(rrport,2);
  425. Text(rrport,string,6);
  426. SetAPen(rrport,3); Move(rrport,490,18);
  427. Text(rrport,"Bytes: ",7);
  428. sprintf(string,"%-8d",nb);
  429. SetAPen(rrport,2);
  430. Text(rrport,string,8);
  431.  
  432. while (i<ENTRIES+EXTENTRIES) {
  433.         SetAPen(rrport,3); SetBPen(rrport,0);
  434.         Move(rrport,x*320+3,y*8+33);
  435.         sprintf(string,"%27s: ",cnames[i]);
  436.         Text(rrport,string,29);
  437.         SetAPen(rrport,1);
  438.         sprintf(string,"%5d %3d%%",iterations[i],iterations[i]*100/count);
  439.         Text(rrport,string,10);
  440.         y++;
  441.         if (y > 18) {
  442.             y=0;
  443.             x++;
  444.             }
  445.     i++;
  446.     }
  447.         
  448. do {
  449.     WaitPort(RWindow->UserPort);
  450.         while ( ( message=(struct IntuiMessage *)
  451.             GetMsg(RWindow->UserPort) ) != NULL)
  452.         {
  453.         class=message->Class;
  454.         ReplyMsg(message); 
  455.  
  456.         if (class==CLOSEWINDOW) report_exit=1;
  457.         }
  458.     } while (report_exit==0);
  459.  
  460. CloseWindow(RWindow);
  461. }
  462.  
  463. processgadgets()
  464. {
  465. register short i;
  466.  
  467. for (i=0; i<ENTRIES+EXTENTRIES; i++) {
  468.     if (Gad[i].Flags & SELECTED) doit[i]=1; else doit[i]=0;
  469.     }
  470. }
  471.  
  472. getfile(titl,path)
  473. char *titl,*path;
  474. {
  475.     windowtopedge=Window->TopEdge;
  476.     windowleftedge=Window->LeftEdge;
  477.     while(stdfile(titl, filename, path, filename)) {
  478.         BPTR stdlock;
  479.         struct FileInfoBlock *stdfib;
  480.         FILE *fp;
  481.         int c;
  482.  
  483.         stdfib = AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC);
  484.         if(!stdfib) {
  485.             alert(" Out of memory!");
  486.             break;
  487.         }
  488.  
  489.         if(!(stdlock = Lock(filename, ACCESS_READ))) {
  490.             alert(" Can't obtain lock.");
  491.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  492.             continue;
  493.         }
  494.         if(!(Examine(stdlock, stdfib)) && dir_only==0) {
  495.             alert("Can't examine file.");
  496.             UnLock(stdlock);
  497.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  498.             continue;
  499.         }
  500.         UnLock(stdlock);
  501.  
  502.         if(stdfib->fib_DirEntryType >= 0 && dir_only==0) {
  503.             alert("That's a directory.");
  504.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  505.             continue;
  506.             }
  507.         if(!(fp = fopen(filename, "r")) && dir_only==0) {
  508.             perror(filename);
  509.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  510.             fclose(fp);
  511.             continue;
  512.             }
  513.         FreeMem(stdfib, sizeof(struct FileInfoBlock));
  514.         if (fp) fclose(fp);
  515.         return(TRUE);
  516.         break;
  517.     }
  518.     return(FALSE);
  519. }
  520.  
  521. help()
  522. {
  523. struct Window *HWindow;
  524. struct IntuiMessage *message;
  525. int help_exit=0,hpage=0;
  526. ULONG class;
  527.  
  528. NewHPWin.LeftEdge=Window->LeftEdge+116;
  529. NewHPWin.TopEdge=Window->TopEdge+26;
  530.  
  531. if(!(HWindow=OpenWindow(&NewHPWin)))
  532.     return();
  533.  
  534. PrintIText(HWindow->RPort,&HelpTextList,0,0);
  535.  
  536. do {
  537.     WaitPort(HWindow->UserPort);
  538.         while ( ( message=(struct IntuiMessage *)
  539.             GetMsg(HWindow->UserPort) ) != NULL)
  540.         {
  541.         class=message->Class;
  542.         ReplyMsg(message); 
  543.  
  544.         if (class==GADGETUP) {
  545.             hpage=1-hpage;
  546.             dohelppage(HWindow,hpage);
  547.             }
  548.         if (class==CLOSEWINDOW) help_exit=1;
  549.         }
  550.     } while (help_exit==0);
  551. CloseWindow(HWindow);
  552. }
  553.  
  554. dohelppage(win,hpage)
  555. struct Window *win;
  556. int hpage;
  557. {
  558. switch (hpage) {
  559.     case 0: SetAPen(win->RPort,0);
  560.         RectFill(win->RPort,2,10,405,145);
  561.         PrintIText(win->RPort,&HelpTextList,0,0);
  562.         HPIText1.IText=(UBYTE *)"MORE";
  563.         RefreshGadgets(&HPGadget1,win,0);
  564.         break;
  565.  
  566.     case 1: SetAPen(win->RPort,0);
  567.         RectFill(win->RPort,2,10,405,145);
  568.         PrintIText(win->RPort,&HelpTextList2,0,0);
  569.         HPIText1.IText=(UBYTE *)"PREV";
  570.         RefreshGadgets(&HPGadget1,win,0);
  571.         break;
  572.  
  573.     default: break;
  574.     }
  575. }
  576.  
  577. /*** This is new to 1.2 for Robert Gould. Use this model to add other
  578.     codes if necessary ***/
  579. translate_insertn(num)
  580. int num;
  581. {
  582. register short i;
  583. int spaces=0,digits,csi=1;
  584.  
  585. /* Here we have to find out if two characters (ESC + [) are used for the
  586.    code, or just a $9B CSI ... we have to account for both just in case */
  587. if (oldcode[0]==27) csi=2;
  588. digits=num-(1+csi);
  589. if (oldcode[0]==27) digits=num-3;
  590.  
  591. /* Now we are going to calculate the spaces. Remember, ALL ANSI codes
  592.    are represented with multiple digits that are ASCII ... so, we have to
  593.    get their numeric value by subtracting 48, and then multiplying by
  594.    the digit it is ... add it all up and you have the number in normal
  595.    form */
  596. for (i=0; i<digits; i++)
  597.     spaces+=(oldcode[csi+i]-48)*powers[digits-i-1];    /* numbers are in ASCII */
  598.  
  599. if (spaces > 0) {
  600.     for (i=0; i<spaces; i++)
  601.         fputc(32,wf);
  602.     }
  603. }
  604.  
  605. dothetabs()
  606. {
  607. register short i;
  608.  
  609. for (i=0; i<tabspc; i++)
  610.     fputc(32,wf);
  611. }
  612.  
  613. openstuff()
  614. {
  615. if((IntuitionBase=OpenLibrary("intuition.library",0L))==NULL)
  616.     exit(1);
  617. if((GfxBase=OpenLibrary("graphics.library",0L))==NULL)
  618.     exit(1);
  619.  
  620. if (!(Window=OpenWindow(&NewWindowStructure1))) {
  621.     closestuff();
  622.     exit(1);
  623.     }
  624.  
  625. rport=Window->RPort;
  626. PrintIText(rport,&IntuiTextList1,0,0);
  627. DrawBorder(rport,&BorderList1,0,0);
  628. SetMenuStrip(Window,&MenuList1);
  629. }
  630.  
  631. closestuff()
  632. {
  633. if (Window) ClearMenuStrip(Window);
  634. if (Window) CloseWindow(Window);
  635. if (IntuitionBase) CloseLibrary(IntuitionBase);
  636. if (GfxBase) CloseLibrary(GfxBase);
  637. }
  638.