home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 392.lha / FileLab_V0.85 / filelab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-03  |  8.8 KB  |  230 lines

  1. /***************************************************************************
  2.   FileLab V0.85 by Chris Papademetrious
  3.     Last modified: 02/07/90
  4.  
  5.     If you find this useful in day-to-day use, a contribution would be
  6.     greatly appreciated. However, if converting text files is all you
  7.     do all day, you have my sympathy...
  8.  
  9.     Chris Papademetrious
  10.     217 Linda Terrace
  11.     Ephrata, PA  17522
  12.  
  13. Modifies text files; strips extra spaces, linefeeds, and formfeeds (clear-
  14. screen characters) from pesky PD text files
  15. ***************************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <intuition/intuition.h>
  19. #include "gadgets.c"
  20.  
  21. FILE *input, *output, *fopen(); /* pointers to buffered input and output files*/
  22.  
  23. struct IntuiMessage *message;   /* pointer to IDCMP messages */
  24.  
  25. ULONG msgclass;             /* IDCMP message class */
  26. USHORT code, gadgetnum;     /* gadget code and number */
  27. struct Message *GetMsg();   /* GetMsg() returns a pointer to a Message */
  28. long total;         /* total file read */
  29.  
  30. main()
  31.     {
  32.     open_all(); /* open window, libraries */
  33.     for(;;)     /* FOREVER */
  34.         {
  35.         if ((message = (struct IntuiMessage *) GetMsg(window->UserPort)) == NULL)
  36.             {
  37.             Wait(1L << window->UserPort->mp_SigBit);    /* wait if no msg */
  38.             continue;
  39.             }
  40.         msgclass = message->Class;
  41.         code = message->Code;
  42.         gadgetnum = ((struct Gadget *)message->IAddress)->GadgetID;
  43.         ReplyMsg(message);  /* Let Intuition know we got it */
  44.         switch(msgclass)
  45.             {
  46.             case REFRESHWINDOW:
  47.                 refreshwindow();    /* homebrew refresh routine */
  48.                 continue;
  49.  
  50.             case CLOSEWINDOW:
  51.                 close_all();        /* bye bye */
  52.  
  53.             case GADGETUP:
  54.                 switch(gadgetnum)   /* gadget released, find out which */
  55.                     {
  56.                     case 3:         /* START gadget */
  57.                         convertfiles(inputbuffer, outputbuffer);
  58.                         continue;   /* continue FOREVER loop */
  59.                     }
  60.             }
  61.         }
  62.     close_all();    /* de-allocate all */
  63.     }
  64.  
  65. convertfiles(inputname, outputname) /* routine to convert files; inputname
  66.                                        outputname are pointers to 80-char
  67.                                        buffers containing file names */
  68. char *inputname, *outputname;
  69.  
  70.     {
  71.     register int loopcntr, spacecntr=0, lfcntr=2;
  72.     register int curchar, oldchar;          /* REGISTER vars for speed */
  73.     long bar=0;
  74.     BOOL format, indent, clear, abort=FALSE;
  75.  
  76.     if (
  77.     !(input = (FILE *) fopen(inputname, "r")) ||
  78.     !(output = (FILE *) fopen(outputname, "w")) )
  79.         {               /* oops, couldn't lock files */
  80.         DisplayBeep(NULL);
  81.         if (input) fclose(input);       /* close what I DID have open */
  82.         if (output) fclose(output);
  83.         return;
  84.         }
  85.  
  86.     format = formatgadget.Flags & SELECTED;
  87.     indent = indentgadget.Flags & SELECTED; /* set conversion flags */
  88.     clear = cleargadget.Flags & SELECTED;
  89.     swapgadgets();  /* enable/disable */
  90.  
  91.     fseek(input, 0L, 2L);   /* go to end of file... */
  92.     bar = ftell(input);     /* and find length.. */
  93.     rewind(input);          /* and back again! */
  94.  
  95.     for(;;) /* endless loop */
  96.          {
  97.          if (message = (struct IntuiMessage *) GetMsg(window->UserPort))
  98.                     /* ^^^ get any IntuiMessages  ^^^ */
  99.             {
  100.             msgclass = message->Class;
  101.             code = message->Code;
  102.             gadgetnum = ((struct Gadget *)message->IAddress)->GadgetID;
  103.             ReplyMsg(message);
  104.             switch(msgclass)
  105.                 {
  106.                 case REFRESHWINDOW: /* simple refresh */
  107.                     refreshwindow();
  108.                     continue;
  109.                 case GADGETUP:  /* STOP gadget must have been hit */
  110.                     abort = TRUE;
  111.                     continue;
  112.                 }
  113.             }
  114.         for(loopcntr=0;loopcntr<1024;loopcntr++)
  115.             {
  116.  
  117.         /* ^^^ inside loop, gadgets only checked every 1024 bytes
  118.            to improve inside loop speed */
  119.  
  120.             oldchar = curchar;
  121.             curchar = fgetc(input); /* update old, read new */
  122.             if (curchar == EOF) break;  /* end of file */
  123.             if ((clear) && (curchar == 12)) curchar = 10;
  124.                 /* ^^^ if 0x0C found (CTRL-L) convert to linefeed */
  125.             if ((indent) && (curchar == 9)) curchar = 32;
  126.                 /* ^^^ if tab found and indent enabled, convert to space */
  127.             if (format)
  128.                 {
  129.                 if (curchar == 32)  /* if current char. is space,...  */
  130.                     {               /*                                */
  131.                     spacecntr++;    /* increment space counter...     */
  132.                     continue;       /* and continue w/out writing     */
  133.                     }
  134.                 if (curchar == 10)  /* if curent char. is linefeed... */
  135.                     {               /*                                */
  136.                     spacecntr=0;    /* reset space counter to zero... */
  137.                     lfcntr++;       /* and update linefeed counter... */
  138.                     continue;       /* and continue w/out writing     */
  139.                     }
  140.                 if (
  141.                 ((curchar != 10) && (curchar != 32)) &&
  142.                 (( oldchar == 10) || (oldchar == 32)) )
  143.                     {
  144.  
  145.                 /* ^^^ if oldchar isn't space or linefeed, and
  146.                    curchar IS, must be time to write the queued
  147.                    spaces and/or linefeeds! */
  148.  
  149.                     if (lfcntr < 2) /* not new paragraph */
  150.                         {
  151.                         lfcntr = 0; /* no linefeeds (joins lines) */
  152.                         spacecntr = 1;  /* extra space to seperate */
  153.                         }
  154.                     else
  155.                         {
  156.                         if (indent) spacecntr = 5;  /* indent if set */
  157.                         }
  158.                     if (lfcntr)     for (;lfcntr>0;lfcntr--) putc(10L, output);
  159.                     if (spacecntr)  for (;spacecntr>0;spacecntr--) putc(32L, output);
  160.                     }   /* ^^^ write linefeeds and spaces */
  161.                 }
  162.             fputc (curchar, output);    /* put current character to file */
  163.             }
  164.         SetAPen(window->RPort, 3L); /* set current foreground pen */
  165.         RectFill(window->RPort, 25L, 73L, 25 + (512 * ftell(input) / bar), 79L);
  166.                         /* ^^^ draw percentage bar */
  167.         if ((curchar == EOF) || abort) break;   /* abort if EOF */
  168.         }
  169.     fclose(output); /* close */
  170.     fclose(input);  /* files */
  171.     swapgadgets();  /* you're outta here! */
  172.     return;
  173.     }
  174.  
  175. swapgadgets()   /* swaps DISABLED flag of each gadget */
  176.     {
  177.     RemoveGList(window, &cleargadget, NULL);    /* disconnect gadgets, */
  178.     fileinputgadget.Flags ^= GADGDISABLED;
  179.     fileoutputgadget.Flags ^= GADGDISABLED;
  180.     startgadget.Flags ^= GADGDISABLED;
  181.     stopgadget.Flags ^= GADGDISABLED;           /* make changes, */
  182.     formatgadget.Flags ^= GADGDISABLED;
  183.     indentgadget.Flags ^= GADGDISABLED;
  184.     cleargadget.Flags ^= GADGDISABLED;
  185.     AddGList(window, &cleargadget, -1L, 7, NULL);   /* stick 'em back in, */
  186.     refreshwindow();                            /* and clean up */
  187.     }
  188.  
  189. refreshwindow()
  190.     {
  191.     SetAPen(window->RPort, 0L); /* set foreground pen to background (0L) */
  192.     RectFill(window->RPort, 2L, 11L, 557L, 82L);    /* clear window */
  193.     RefreshGadgets(&cleargadget, window, NULL);     /* refresh gadgets */
  194.     SetAPen(window->RPort, 2L);                /* set foreground again */
  195.     RectFill(window->RPort, 24L, 72L, 540L, 80L);   /* draw black bar */
  196.     }
  197.  
  198. open_all()
  199.     {
  200.     if (!(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", NULL)))
  201.         {   /* ^^^ library ptr not returned, no library */
  202.         DisplayBeep(NULL);
  203.         close_all();
  204.         }
  205.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", NULL)))
  206.         {   /* ^^^ library ptr not returned, no library */
  207.         DisplayBeep(NULL);
  208.         close_all();
  209.         }
  210.     if (!(window = (struct Window *) OpenWindow(&MainWindow)))
  211.         {   /* ^^^ window ptr not returned, no window */
  212.         DisplayBeep(NULL);
  213.         close_all();
  214.         }
  215.     SetAPen(window->RPort, 2L); /* draw black bar */
  216.     RectFill(window->RPort, 24L, 72L, 536L, 80L);
  217.     }
  218.  
  219. close_all()
  220.     {
  221.     if (window) CloseWindow(window);    /* close window */
  222.     if (GfxBase) CloseLibrary(GfxBase); /* close graphics.library */
  223.     if (IntuitionBase) CloseLibrary(IntuitionBase); /* close intuition */
  224.     exit(NULL); /* exit program */
  225.     }
  226.  
  227. /* Enjoy! Feel free to hack this code to pieces any way you wish. Please
  228.  don't sell this or include it in any non-public domain product without
  229.  notifying me, or include it in any public domain product being sold for
  230.  a cost exceeding (US)$5. */