home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adaptor.zip / adapt.zip / adaptor / src / fstrip.c < prev    next >
Text File  |  1994-01-03  |  5KB  |  168 lines

  1. /*************************************************************************
  2. *                                                                        *
  3. *  Name : fstrip.c                                                       *
  4. *                                                                        *
  5. *  Purpose : Splitting Fortran Source programs with longer lines         *
  6. *                                                                        *
  7. *  Author : James Cownie, Meiko, Bristol                                 *
  8. *                                                                        *
  9. *  Last Update : June 1993                                               *
  10. *                                                                        *
  11. *                                                                        *
  12. *   The behaviour of this code was not actually safe. It will break      *
  13. *   if it has to split lines with long character string definitions,     *
  14. *   as it is not aware that that is what it is doing.                    *
  15. *                                                                        *
  16. *   It would actually be safer not to try to choose a good place to      *
  17. *   split, but always split at the correct column, and ASSUME that the   *
  18. *   Fortran compiler will re-assemble the lines at the same width.       *
  19. *                                                                        *
  20. *************************************************************************/
  21.  
  22. #include <stdio.h>    /* file handling */
  23. #include <ctype.h>
  24.  
  25. #define MAXLEN 200
  26.  
  27. main (argc, argv)
  28. int argc;
  29. char **argv;
  30. {
  31.     char buffer [MAXLEN+100];
  32.     FILE *infile, *outfile, *ptmp ;
  33.     int c;
  34.     int buflen;
  35.  
  36.     char infilename[100];
  37.     char outfilename[100];
  38.     char move[200];
  39.  
  40.     int  strip_length;
  41.  
  42.     if (argc < 2)
  43.     {
  44.        printf ("Correct Call is : \n");
  45.        printf ("fstrip filename [strip_length]\n");
  46.        exit (-1);
  47.     }
  48.  
  49.     strcpy (infilename, argv[1]);
  50.  
  51.     strip_length = 132;
  52.  
  53.     if (argc >= 3)
  54.     {
  55.        sscanf (argv[2], "%d", &strip_length);
  56.     }
  57.  
  58.     printf ("File %s will be stripped to length = %d\n",
  59.            infilename, strip_length);
  60.  
  61.     if ((strip_length < 50) || (strip_length > 150))
  62.     {
  63.        printf ("strip_length must be in range [50 - 150]\n");
  64.        exit (-1);
  65.     }
  66.  
  67.     infile  = fopen (infilename, "r");
  68.  
  69.     if (infile == NULL)
  70.     {
  71.        printf ("Input File %s could not be opened\n", infilename);
  72.        exit (-1);
  73.     }
  74.  
  75.     strcpy (outfilename, "tmpxy07");
  76.  
  77.     outfile = fopen (outfilename, "w");
  78.  
  79.     if (outfile == NULL)
  80.     {
  81.        printf ("Output File %s could not be opened\n", outfilename);
  82.        exit (-1);
  83.     }
  84.  
  85.     buflen = 0;  /* actual buffer length */
  86.  
  87.     while ((c = fgetc(infile)) != EOF)
  88.     {
  89.        int i;
  90.  
  91.        if (c == '\n')
  92.        { /* new line, print current line */
  93.            for (i=0; i<buflen; i++)
  94.         putc (buffer[i], outfile);
  95.            putc ('\n', outfile);
  96.            buflen = 0;
  97.        }
  98.        else
  99.        { /* set new character */
  100.            buffer[buflen++] = c;
  101.            if (buflen == strip_length)
  102.            {   /* find a place to break */
  103.         for (c = buffer[buflen-1];buflen > 0;)
  104.         {
  105.             switch (c)
  106.             {
  107.          case ' ':    /* Characters on which to break */
  108.          case '\t':
  109.          case ',':
  110.          case '(':
  111.          case ')':
  112.          case '+':
  113.          case '-':
  114.          case '*':
  115.          case '/': break;
  116.  
  117.          default:  buflen -= 1;
  118.                    c = buffer[buflen-1];
  119.                    continue;
  120.             }
  121.             break;
  122.         }
  123.         /* If we can't find a good place to break it doesn't actually
  124.          * matter, as we can just break anywhere provided we
  125.          * fill to the right hand side.
  126.          */
  127.         if (buflen == 0)
  128.             buflen = strip_length;
  129.  
  130.         /* buffer[0], ..., buffer[buflen-1], buffer[buflen], ..
  131.            ---- printed   characters         -- next line   --- */
  132.         /* print out buffer */
  133.         for (i=0; i<buflen; i++)
  134.             putc (buffer[i], outfile);
  135.         putc ('\n', outfile);
  136.         /* make continuation line */
  137.         for (i=0;i<5;i++) buffer[i] = ' ';
  138.         buffer[5] = '&';
  139.         for (i=buflen;i<strip_length;i++)
  140.             buffer[i-buflen+6] = buffer[i];
  141.         /* fill in rest of line */
  142.         buflen = strip_length-buflen+6;
  143.            }
  144.        }
  145.     }
  146.  
  147.     /* print rest of the buffer (last line if no \n in input) */
  148.     if (buflen > 0)
  149.     {
  150.        int i;
  151.  
  152.        for (i=0; i<buflen; i++)
  153.            putc (buffer[i], outfile);
  154.        putc ('\n', outfile);
  155.     }
  156.  
  157.     fcloseall();
  158.  
  159.     sprintf (move, "mv %s %s", outfilename, infilename);
  160.     system(move);
  161. /*
  162.     ptmp = popen (move,"w");
  163.     pclose (ptmp);
  164. */
  165.     exit(0);
  166. }
  167.  
  168.