home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / system / eolcon.lha / EOLCon.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  6KB  |  227 lines

  1. /*
  2.  *  EOLCon 1.3
  3.  *
  4.  *    Completed 5/26/92
  5.  *    by Ernest Crvich
  6.  *
  7.  *
  8.  *   HISTORY :
  9.  *
  10.  *     V1.3  (5/26/92)
  11.  *         Fixed a memory allocation bug.
  12.  *        (tempbuf wasn't getting set
  13.  *         right in some cases)
  14.  *         Added option to REPLACE carriage
  15.  *        returns by linefeeds.  This
  16.  *        is for files that do not even
  17.  *        contain ANY linefeeds at all
  18.  *        originally (just CR's).  Thanks
  19.  *        to the outrageous Pres in Belgium
  20.  *        for alerting me to these types
  21.  *        of files!
  22.  *
  23.  *     V1.2  (2/17/92)
  24.  *         Added option to make the output file
  25.  *        be the same as the input file so
  26.  *        you don't have to constantly
  27.  *        create a new file, delete the old
  28.  *        one, then rename the new one.
  29.  *        The template for the options isn't
  30.  *        very good, but this is a pretty
  31.  *        cheesy program anyways.  :-)
  32.  *        Finally, I released this version!
  33.  *
  34.  *
  35.  *     V1.1  (7/17/91)
  36.  *         Added option to make end-of-line char
  37.  *        any character the user wants, not
  38.  *        just a linefeed or carriage return.
  39.  *        I was really going to release this,
  40.  *        but for many reasons never got to
  41.  *        actually doing it.
  42.  *
  43.  *
  44.  *     V1.0a (7/10/91)
  45.  *         Added option to let user define the
  46.  *        size of the buffer used.  Again
  47.  *        came extremely close to releasing
  48.  *        this version, but decided to let
  49.  *        time improve upon it just a bit
  50.  *        more.
  51.  *
  52.  *
  53.  *     V1.0  (6/28/91)
  54.  *         Original bare-bones converter.  I came
  55.  *        *very* close to releasing this to
  56.  *        the public, but decided it needed
  57.  *        'something special' to warrant that
  58.  *        kind of action.
  59.  *
  60.  */
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65.  
  66. #define LF '\012'
  67. #define CR '\015'
  68. #define CR_ADD '1'
  69. #define CR_DEL '0'
  70. #define LF_ADD '2'
  71. #define TMP_NAME   "ThisFileBetterNotExist!!"
  72.  
  73. /*
  74.  * Prototypes
  75.  *
  76.  */
  77. void ConvertFile( FILE *, FILE *, char, char *, char *, unsigned int ) ;
  78. void main( int, char *[] ) ;
  79.  
  80.  
  81.  
  82.  
  83. void main( int argc, char *argv[] )
  84. {
  85.    char     convert       ; /* the command character              */
  86.    int        scan_test      ; /* tests for a valid command line buffer size */
  87.    int        replace = 0      ; /* did the user want to use the same files?   */
  88.    unsigned int buffer      = 10000 ; /* buffer size for input file memory      */
  89.    unsigned int tempbuf       ; /* buffer size for the working memory used      */
  90.    char     *chstr          ; /* where the bytes read in are stored      */
  91.    char     *temp          ; /* where the actual output bytes are put      */
  92.    FILE     *infile       ; /* input file structure              */
  93.    FILE     *outfile      ; /* output file structure              */
  94.  
  95.  
  96.    if ((argc > 5) || (argc < 4))
  97.       {
  98.      printf( "\nUsage : EOLCon <inputfile> <outputfile>" ) ;
  99.      printf( " <%c|%c|%c|x> [buffersize]\n\n", CR_DEL, CR_ADD, LF_ADD ) ;
  100.      printf( "        In which   %c          means delete all carriage returns\n", CR_DEL ) ;
  101.      printf( "                   %c          means add a carriage return\n", CR_ADD ) ;
  102.      printf( "                   %c          means replace carriage returns with linefeeds\n", LF_ADD ) ;
  103.      printf( "                   x          means add a character to the end of\n" ) ;
  104.      printf( "                              the line (any character you want)\n" ) ;
  105.      printf( "                              'EOLCon in out &' would put an\n" ) ;
  106.      printf( "                              ampersand at the end of each line.\n" ) ;
  107.      printf( "                   buffersize is the size (in bytes) of the I/O\n" ) ;
  108.      printf( "                              buffer.  Note that 3 times this may\n" ) ;
  109.      printf( "                              actually be allocated (optimized for\n" ) ;
  110.      printf( "                              speed rather than size)!\n" ) ;
  111.      printf( "                              Default size is 10000.\n\n" ) ;
  112.      printf( "        Note that you can keep the same filename if you put an\n" ) ;
  113.      printf( "             an asterisk (*) in place of <outputfile>.\n\n" ) ;
  114.       }
  115.    else
  116.       {
  117.      convert = (char) *argv[3] ;
  118.      infile = fopen( argv[1], "r" ) ;
  119.      if (infile == NULL)
  120.         printf( "\nError opening input file.\n" ) ;
  121.      else
  122.         {
  123.            if (argc == 5)
  124.           scan_test = sscanf( argv[4], "%u", &buffer ) ;
  125.            if ((scan_test == 0) || (buffer == 0))
  126.           {
  127.              buffer = 10000 ;
  128.              printf( "\nInvalid buffer size...using default.\n" ) ;
  129.           }
  130.            chstr = (char *) malloc( buffer ) ;
  131.            if (chstr == NULL)
  132.           {
  133.              printf( "\nError allocating primary memory!\n" ) ;
  134.              printf( "Try using a smaller buffer.\n" ) ;
  135.           }
  136.            else
  137.           {
  138.              if (convert != CR_DEL)
  139.             tempbuf = buffer * 2 ;
  140.              else
  141.             tempbuf = buffer ;
  142.              temp = (char *) malloc( tempbuf ) ;
  143.              if (temp == NULL)
  144.             {
  145.                printf( "\nError allocating secondary memory!\n" ) ;
  146.                printf( "Try using a smaller buffer.\n" ) ;
  147.             }
  148.              else
  149.             {
  150.                if (strcmp( argv[2], "*" ) == 0)
  151.                   {
  152.                  outfile = fopen( TMP_NAME, "w" ) ;
  153.                  replace = 1 ;
  154.                   }
  155.                else
  156.                   outfile = fopen( argv[2], "w" ) ;
  157.                if (outfile == NULL)
  158.                   printf( "Error opening output file.\n" ) ;
  159.                else
  160.                   {
  161.                  printf( "\nWorking...\n" ) ;
  162.                  ConvertFile( infile, outfile, convert,
  163.                           chstr, temp, buffer ) ;
  164.                  printf( "All done.\n" ) ;
  165.                  fclose( outfile ) ;
  166.                   }
  167.                free( temp ) ;
  168.             }
  169.              free( chstr ) ;
  170.           }
  171.            fclose( infile ) ;
  172.            if (replace)
  173.           {
  174.              remove( argv[1] ) ;
  175.              rename( TMP_NAME, argv[1] ) ;
  176.           }
  177.         }
  178.       }
  179. }
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  * ConvertFile
  186.  *
  187.  * Does the actual concatenation of
  188.  * the characters (reads/writes) to
  189.  * the file.
  190.  *
  191.  */
  192. void ConvertFile( FILE *in, FILE *out, char convert,
  193.           char *chstr, char *temp, unsigned int buffer )
  194. {
  195.    int i, j ;
  196.    int numread ;
  197.  
  198.    numread = fread( chstr, 1, buffer, in ) ;
  199.    while (numread)
  200.       {
  201.      j = 0 ;
  202.      for (i = 0; i < numread; ++i)
  203.         if ( (*(chstr + i) == CR) &&
  204.          ((convert == CR_ADD) || (convert == CR_DEL)) )
  205.            /* ignore it! */ ;
  206.         else
  207.            if ((*(chstr + i) == LF) && (convert == CR_ADD))
  208.           {
  209.              *(temp + (j++)) = CR ;
  210.              *(temp + (j++)) = *(chstr + i) ;
  211.           }
  212.            else
  213.           if ((*(chstr + i) == LF) && (convert != CR_DEL))
  214.              {
  215.             *(temp + (j++)) = convert ;
  216.             *(temp + (j++)) = *(chstr + i) ;
  217.              }
  218.           else
  219.              if ( (*(chstr + i) == CR) && (convert == LF_ADD) )
  220.             *(temp + (j++)) = LF ;
  221.              else
  222.             *(temp + (j++)) = *(chstr + i) ;
  223.      fwrite( temp, 1, j, out ) ;
  224.      numread = fread( chstr, 1, buffer, in ) ;
  225.       }
  226. }
  227.