home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 602b.lha / StripANSI_v1.0 / Source / source.lzh / wbprog.c < prev   
C/C++ Source or Header  |  1991-08-04  |  11KB  |  527 lines

  1. /*********************************************************************\
  2.  *                          StripANSI v1.0                           *
  3.  *                                      *
  4.  *                      Intuition Full Version                       *
  5.  *                                                                   *
  6.  *                     Written by Syd L. Bolton                      *
  7.  *         Copyright ©1991 Legendary Design Technologies Inc.        *
  8.  *                                                                   *
  9.  *        Revision: 001  Date: August 3, 1991  Time: 10:57:24        *
  10. \*********************************************************************/
  11.  
  12. #include <exec/memory.h>
  13. #include <stdio.h>
  14. #include <intuition/intuition.h>
  15. #include <graphics/gfxbase.h>
  16. #include <functions.h>
  17.  
  18. #define ENTRIES 26
  19. #define BACKSPACE ENTRIES
  20. #define FORM_FEED ENTRIES+1
  21. #define CR_ONLY   ENTRIES+2
  22. #include "main.h"
  23.  
  24. long *IntuitionBase=0,*GfxBase=0;
  25. struct Window *Window,*MessageWindow;
  26. struct RastPort *rport,*mrport;
  27.  
  28. FILE *rf,*wf;
  29. int count=0,iterations[ENTRIES+3],read_dir=0,dir_only=0,nb=0;
  30. int windowtopedge,windowleftedge,doit[ENTRIES+3];
  31.  
  32. int codes[] = {
  33.     99,64,65,66,67,68,69,70,72,74,75,76,77,80,83,84,104,108,109,110,
  34.     112,113,116,117,120,121
  35.     };
  36.  
  37. char *cnames[] = {
  38.     "RESET TO INITIAL STATE",
  39.     "INSERT [N] CHARACTERS",
  40.     "CURSOR UP [N] CHARACTERS",
  41.     "CURSOR DOWN [N] CHARACTERS",
  42.     "CURSOR FWD [N] CHARACTERS",
  43.     "CURSOR BKWD [N] CHARACTERS",
  44.     "CURSOR NEXT LINE [N]",
  45.     "CURSOR PRECEDING LINE [N]",
  46.     "MOVE CURSOR TO ROW/COLUMN",
  47.     "ERASE TO END OF DISPLAY",
  48.     "ERASE TO END OF LINE",
  49.     "INSERT LINE",
  50.     "DELETE LINE",
  51.     "DELETE CHARACTER [N]",
  52.     "SCROLL UP [N] LINES",
  53.     "SCROLL DOWN [N] LINES",
  54.     "SET LINEFEED MODE",
  55.     "RESET NEWLINE MODE",
  56.     "SELECT GRAPHIC RENDITION",
  57.     "DEVICE STATUS REPORT",
  58.     "»SET CURSOR RENDITION",
  59.     "»WINDOW STATUS REQUEST",
  60.     "»SET PAGE LENGTH",
  61.     "»SET LINE LENGTH",
  62.     "»SET LEFT OFFSET",
  63.     "»SET TOP OFFSET",
  64.     "BACKSPACE",
  65.     "FORM FEED",
  66.     "CARRIAGE RETURN"
  67.     };
  68. char filename[128],pattern[40],title[25],path[75],mytitle[30],mypattern[10],oldcode[80],file1[128],file2[128];
  69.  
  70. main()
  71. {
  72. int i,exit_flag=0;
  73. char c;
  74. struct IntuiMessage *message;
  75. ULONG class;
  76.  
  77. filename[0]='\0';
  78.  
  79. openstuff();
  80.  
  81. do {
  82.     WaitPort(Window->UserPort);
  83.         while ( ( message=(struct IntuiMessage *)
  84.             GetMsg(Window->UserPort) ) != NULL)
  85.         {
  86.         class=message->Class;
  87.         ReplyMsg(message); 
  88.  
  89.         if (class==GADGETUP) option(message);
  90.         if (class==MENUPICK) exit_flag=menu(message);
  91.         if (class==CLOSEWINDOW) exit_flag=1;
  92.         }
  93.     } while (exit_flag==0);
  94.  
  95. closestuff();
  96. }
  97.  
  98. #include "about.c"
  99. #include "message.c"
  100. #include "alert.c"
  101. #include "pointer.c"
  102. #include "report.h"
  103.  
  104. menu(message)
  105. struct IntuiMessage *message;
  106. {
  107. int rv=0;
  108.  
  109. switch(MENUNUM(message->Code)) {
  110.     case 0: rv=project(message);
  111.         break;
  112.  
  113.     default: break;
  114.     }
  115. return(rv);
  116. }
  117.  
  118. project(message)
  119. struct IntuiMessage *message;
  120. {
  121. switch(ITEMNUM(message->Code)) {
  122.  
  123.     case 0: about();
  124.         break;
  125.  
  126.     case 1: return(1);            /* quit */
  127.  
  128.     default: break;
  129.     }
  130. return(0);
  131. }
  132.  
  133. option(message)
  134. struct IntuiMessage *message;
  135. {
  136. struct Gadget *igad;
  137. int gadgid;
  138.  
  139. igad=(struct Gadget *) message->IAddress;
  140. gadgid=igad->GadgetID;
  141.  
  142. switch(gadgid) {
  143.     case 10: reportonly();
  144.          break;
  145.  
  146.     case 20: go();
  147.          break;
  148.  
  149.     default: break;
  150.     }
  151. }
  152.  
  153. reportonly()
  154. {
  155. int x;
  156. char c,bytecount[28],ansicount[24];
  157.  
  158. for (x=0; x<ENTRIES+3; x++)
  159.     iterations[x]=0;
  160.  
  161. strcpy(mytitle,"Choose file for REPORT...");
  162. strcpy(mypattern,"#?");
  163. x=getfile(mytitle,mypattern);
  164. if (x==FALSE) return();
  165. rf=fopen(filename,"rb");
  166.  
  167. if (rf==0) {
  168.     alert("Can't open file!");
  169.     return();
  170.     }
  171.  
  172. strcpy(file1,filename);
  173. strcpy(file2,"N/A");
  174.  
  175. count=0; nb=0;
  176. Message("              Working...");
  177. SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
  178.  
  179. do {
  180.     c=getc(rf); nb++;
  181.     if(!(feof(rf))) {
  182.     if (c=='\0x9b' || c==27) countcode();  /* either CSI or ESC */
  183.     else if (c == 8) {
  184.             count++;
  185.             iterations[BACKSPACE]++;
  186.             }
  187.     else if (c == 12) {
  188.             count++;
  189.             iterations[FORM_FEED]++;
  190.             }
  191.     else if (c == 13) {
  192.             count++;
  193.             iterations[CR_ONLY]++;
  194.             }
  195.         }
  196.     } while(!(feof(rf)));
  197. nb--;
  198. fclose(rf);
  199. ClearPointer(MessageWindow);
  200. CloseWindow(MessageWindow);
  201. report();
  202. }
  203.  
  204. countcode()
  205. {
  206. int i,exit_flag=0;
  207. char c;
  208.  
  209. do {
  210.     c=getc(rf); nb++;
  211.     for (i=0; i<ENTRIES; i++) { 
  212.         if (c==codes[i]) {
  213.             exit_flag=1;
  214.             iterations[i]++;
  215.             count++;
  216.             }
  217.         }
  218.     } while (exit_flag==0) ;
  219. }
  220.  
  221. go()
  222. {
  223. int x;
  224.  
  225. for (x=0; x<ENTRIES+3; x++)
  226.     iterations[x]=0;
  227.  
  228. filename[0]='\0';
  229. strcpy(mytitle,"Choose SOURCE file...");
  230. strcpy(mypattern,"#?");
  231. x=getfile(mytitle,mypattern);
  232. if (x==FALSE) return();
  233.  
  234. strcpy(file1,filename);
  235.  
  236. filename[0]='\0';
  237. strcpy(mytitle,"Choose DESTINATION file...");
  238. strcpy(mypattern,"#?");
  239. if(!(stdfile("Choose DESTINATION file...",NULL,"#?",filename))) return();
  240.  
  241. strcpy(file2,filename);
  242.  
  243. if(!(rf=fopen(file1,"rb"))) {
  244.     alert("Can't open SOURCE!");    
  245.     return();
  246.     }
  247.  
  248. if(!(wf=fopen(file2,"wb"))) {
  249.     fclose(rf);        /* don't forget this! */
  250.     alert("Can't open DEST!");
  251.     return();
  252.     }
  253.  
  254. count=0; nb=0;
  255. Message("              Working...");
  256. SetPointer(MessageWindow,&BusyPointer,22,16,0,0);
  257.  
  258. processgadgets();
  259.  
  260. readfile();
  261.  
  262. ClearPointer(MessageWindow);
  263. CloseWindow(MessageWindow);
  264. report();
  265. }
  266.  
  267. readfile()
  268. {
  269. char c;
  270.  
  271. do {
  272.     c=getc(rf); nb++;
  273.     if(!(feof(rf))) {
  274.     if (c=='\0x9b' || c==27) stripcode(c);  /* either CSI or ESC */
  275.     else if (c == 8) {
  276.         if (doit[BACKSPACE]) fputc(c,wf);
  277.         else {
  278.             iterations[BACKSPACE]++;    
  279.             count++;
  280.             }
  281.         }
  282.     else if (c == 12) {
  283.         if (doit[FORM_FEED]) fputc(c,wf);
  284.         else {
  285.             iterations[FORM_FEED]++;
  286.             count++;
  287.             }
  288.         }
  289.     else if (c == 13) {
  290.         if (doit[CR_ONLY]) fputc(c,wf);
  291.         else {
  292.             iterations[CR_ONLY]++;
  293.             count++;
  294.             }
  295.         }
  296.     else fputc(c,wf);
  297.         }
  298.     } while(!(feof(rf)));
  299. nb--;
  300. fclose(rf);
  301. fclose(wf);    
  302. }
  303.  
  304. stripcode(csi)
  305. char csi;
  306. {
  307. int i,num=1,exit_flag=0;
  308. char c;
  309.  
  310. oldcode[0]=csi;
  311.  
  312. do {
  313.     c=getc(rf); oldcode[num++]=c; nb++;
  314.     for (i=0; i<ENTRIES; i++) { 
  315.         if (c==codes[i]) {
  316.             exit_flag=1;
  317.             if (doit[i]) putback(num);
  318.             else {
  319.                 iterations[i]++;
  320.                 count++;
  321.                 }
  322.             }
  323.         }
  324.     } while (exit_flag==0) ;
  325. }
  326.  
  327. putback(num)
  328. int num;
  329. {
  330. int i;
  331.  
  332. for (i=0; i<num; i++)
  333.     fputc(oldcode[i],wf);
  334. }
  335.  
  336. report()
  337. {
  338. struct IntuiMessage *message;
  339. struct Window *RWindow;
  340. struct RastPort *rrport;
  341. ULONG class;
  342. int x,y,i,report_exit=0;
  343. char string[35];
  344.  
  345. if (count==0) return();
  346.  
  347. ReportWin.TopEdge=Window->TopEdge;
  348. ReportWin.LeftEdge=Window->LeftEdge;
  349.  
  350. if(!(RWindow=OpenWindow(&ReportWin))) {
  351.     alert("Couldn't open Window!");
  352.     return();
  353.     }
  354.  
  355. rrport=RWindow->RPort;
  356. x=y=i=0;
  357.  
  358. SetAPen(rrport,3); SetBPen(rrport,0);
  359. Move(rrport,3,18); Text(rrport,"SOURCE: ",8);
  360. SetAPen(rrport,2);
  361. sprintf(string,"%-30s",file1);
  362. Text(rrport,string,30);
  363. Move(rrport,330,18); SetAPen(rrport,3);
  364. Text(rrport,"DEST: ",6);
  365. sprintf(string,"%-30s",file2);
  366. SetAPen(rrport,2);
  367. Text(rrport,string,30);
  368. SetAPen(rrport,3); Move(rrport,3,28);
  369. Text(rrport,"# of ANSI Codes: ",17);
  370. sprintf(string,"%-6d",count);
  371. SetAPen(rrport,2);
  372. Text(rrport,string,6);
  373. SetAPen(rrport,3); Move(rrport,282,28);
  374. Text(rrport,"# of bytes: ",12);
  375. sprintf(string,"%-8d",nb);
  376. SetAPen(rrport,2);
  377. Text(rrport,string,8);
  378.  
  379. while (i<ENTRIES+3) {
  380.         SetAPen(rrport,3); SetBPen(rrport,0);
  381.         Move(rrport,x*320+3,y*9+43);
  382.         sprintf(string,"%27s: ",cnames[i]);
  383.         Text(rrport,string,29);
  384.         SetAPen(rrport,1);
  385.         sprintf(string,"%5d %3d%%",iterations[i],iterations[i]*100/count);
  386.         Text(rrport,string,10);
  387.         y++;
  388.         if (y > 14) {
  389.             y=0;
  390.             x++;
  391.             }
  392.     i++;
  393.     }
  394.         
  395. do {
  396.     WaitPort(RWindow->UserPort);
  397.         while ( ( message=(struct IntuiMessage *)
  398.             GetMsg(RWindow->UserPort) ) != NULL)
  399.         {
  400.         class=message->Class;
  401.         ReplyMsg(message); 
  402.  
  403.         if (class==GADGETUP) report_exit=1;
  404.         }
  405.     } while (report_exit==0);
  406.  
  407. CloseWindow(RWindow);
  408. }
  409.  
  410. processgadgets()
  411. {
  412. int i;
  413.  
  414. for (i=0; i<ENTRIES+3; i++)
  415.     doit[i]=0;
  416.  
  417. /* NOTE: I could have put these gadgets in an array, and it would have
  418.     made life easier here, but since I wanted to use PowerWindows,
  419.     I left it this way ... I know it sucks but oh well! What'ya gonna
  420.     do? */
  421.  
  422. if (Gadget1.Flags & SELECTED) doit[0]=1;
  423. if (Gadget2.Flags & SELECTED) doit[1]=1;
  424. if (Gadget3.Flags & SELECTED) doit[2]=1;
  425. if (Gadget4.Flags & SELECTED) doit[3]=1;
  426. if (Gadget5.Flags & SELECTED) doit[4]=1;
  427. if (Gadget6.Flags & SELECTED) doit[5]=1;
  428. if (Gadget7.Flags & SELECTED) doit[6]=1;
  429. if (Gadget8.Flags & SELECTED) doit[7]=1;
  430. if (Gadget9.Flags & SELECTED) doit[8]=1;
  431. if (Gadget10.Flags & SELECTED) doit[9]=1;
  432. if (Gadget11.Flags & SELECTED) doit[10]=1;
  433. if (Gadget12.Flags & SELECTED) doit[11]=1;
  434. if (Gadget13.Flags & SELECTED) doit[12]=1;
  435. if (Gadget14.Flags & SELECTED) doit[13]=1;
  436. if (Gadget15.Flags & SELECTED) doit[14]=1;
  437. if (Gadget16.Flags & SELECTED) doit[15]=1;
  438. if (Gadget17.Flags & SELECTED) doit[16]=1;
  439. if (Gadget18.Flags & SELECTED) doit[17]=1;
  440. if (Gadget19.Flags & SELECTED) doit[18]=1;
  441. if (Gadget20.Flags & SELECTED) doit[19]=1;
  442. if (Gadget21.Flags & SELECTED) doit[20]=1;
  443. if (Gadget22.Flags & SELECTED) doit[21]=1;
  444. if (Gadget23.Flags & SELECTED) doit[22]=1;
  445. if (Gadget24.Flags & SELECTED) doit[23]=1;
  446. if (Gadget25.Flags & SELECTED) doit[24]=1;
  447. if (Gadget26.Flags & SELECTED) doit[25]=1;
  448. if (Gadget27.Flags & SELECTED) doit[26]=1;
  449. if (Gadget28.Flags & SELECTED) doit[27]=1;
  450. if (Gadget29.Flags & SELECTED) doit[28]=1;
  451. }
  452.  
  453. getfile(titl,path)
  454. char *titl,*path;
  455. {
  456.     windowtopedge=Window->TopEdge;
  457.     windowleftedge=Window->LeftEdge;
  458.     while(stdfile(titl, filename, path, filename)) {
  459.         BPTR stdlock;
  460.         struct FileInfoBlock *stdfib;
  461.         FILE *fp;
  462.         int c;
  463.  
  464.         stdfib = AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC);
  465.         if(!stdfib) {
  466.             alert(" Out of memory!");
  467.             break;
  468.         }
  469.  
  470.         if(!(stdlock = Lock(filename, ACCESS_READ))) {
  471.             alert(" Can't obtain lock.");
  472.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  473.             continue;
  474.         }
  475.         if(!(Examine(stdlock, stdfib)) && dir_only==0) {
  476.             alert("Can't examine file.");
  477.             UnLock(stdlock);
  478.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  479.             continue;
  480.         }
  481.         UnLock(stdlock);
  482.  
  483.         if(stdfib->fib_DirEntryType >= 0 && dir_only==0) {
  484.             alert("That's a directory.");
  485.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  486.             continue;
  487.             }
  488.         if(!(fp = fopen(filename, "r")) && dir_only==0) {
  489.             perror(filename);
  490.             FreeMem(stdfib, sizeof(struct FileInfoBlock));
  491.             fclose(fp);
  492.             continue;
  493.             }
  494.         FreeMem(stdfib, sizeof(struct FileInfoBlock));
  495.         if (fp) fclose(fp);
  496.         return(TRUE);
  497.         break;
  498.     }
  499.     return(FALSE);
  500. }
  501.  
  502. openstuff()
  503. {
  504. if((IntuitionBase=OpenLibrary("intuition.library",0L))==NULL)
  505.     exit(1);
  506. if((GfxBase=OpenLibrary("graphics.library",0L))==NULL)
  507.     exit(1);
  508.  
  509. if (!(Window=OpenWindow(&NewWindowStructure1))) {
  510.     closestuff();
  511.     exit(1);
  512.     }
  513.  
  514. rport=Window->RPort;
  515. PrintIText(rport,&IntuiTextList1,0,0);
  516. DrawBorder(rport,&BorderList1,0,0);
  517. SetMenuStrip(Window,&MenuList1);
  518. }
  519.  
  520. closestuff()
  521. {
  522. if (Window) ClearMenuStrip(Window);
  523. if (Window) CloseWindow(Window);
  524. if (IntuitionBase) CloseLibrary(IntuitionBase);
  525. if (GfxBase) CloseLibrary(GfxBase);
  526. }
  527.