home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 620.lha / EOLCon_v1.2 / EOLCon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  5.6 KB  |  206 lines

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