home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / lftocrlf.lzh / LFTOCRLF.C < prev    next >
Text File  |  1993-11-03  |  9KB  |  203 lines

  1. /************************************************************************
  2.  *    Program: LFtoCRLF.C                                               *
  3.  *                                                                      *
  4.  * Program to convert Unix files (each logical record terminated by LF) *
  5.  * to DOS and OS/2 files (each logical record terminated by CR/LF).     *
  6.  *                                                                      *
  7.  * This program is to be run from a command prompt, either in an OS/2   *
  8.  * full-screen session or an OS/2 Window.                               *
  9.  *                                                                      *
  10.  * The user can supply the input and output file paths/names on the     *
  11.  * command line, as follows:                                            *
  12.  *                                                                      *
  13.  *       LFtoCRLF <Input file> <Output file>                            *
  14.  *                                                                      *
  15.  * where <Input file> is the path and file name of the Unix delimited   *
  16.  * input file, and <Output file> is the path and file name of the OS/2  *
  17.  * file to be created. If the path information is omitted from the      *
  18.  * output file then it is created in the same directory as the input    *
  19.  * file.                                                                *
  20.  *                                                                      *
  21.  * If the user does not put any file specifications on the command line *
  22.  * the program will prompt for them individually. Again, if the output  *
  23.  * file has no path information it will be created in the same directory*
  24.  * as the input file currently resides.                                 *
  25.  *                                                                      *
  26.  * This program was compiled and tested using the IBM CSet/++ compiler. *
  27.  * I have kept the C fairly standard and so it should port to any other *
  28.  * compiler with a minimum of reworking.                                *
  29.  *                                                                      *
  30.  * The author retains all copyrights on the code, but expressly permits *
  31.  * its distribution provided no charge is levied beyond that of the     *
  32.  * medium of its distribution. (I.e. it's a freebie!)                   *
  33.  *                                                                      *
  34.  *                                                                      *
  35.  *                                                                      *
  36.  * (C) 1993 Copyright  David W. Noon                                    *
  37.  *                     13583 Lynn St.                                   *
  38.  *                     Woodbridge VA 22191-2121                         *
  39.  *                     Phone: (703) 490-5652                            *
  40.  *                     Fax:   (703) 491-6390                            *
  41.  *                     Compuserve: 72172,431                            *
  42.  *                                                                      *
  43.  ************************************************************************/
  44.  
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48.  
  49. #define Buffer_size 8192
  50.  
  51. /* Internal subroutine to merge the drive and directory of the input file
  52.    into the output file specification if they were omitted. */
  53.  
  54. void merge_path(char *Input_file, char *Output_file)
  55. {
  56.    char Input_drive[_MAX_DRIVE], Input_dir[_MAX_DIR],
  57.       Input_fname[_MAX_FNAME], Input_ext[_MAX_EXT],
  58.       Output_drive[_MAX_DRIVE], Output_dir[_MAX_DIR],
  59.       Output_fname[_MAX_FNAME], Output_ext[_MAX_EXT];
  60.  
  61.    _splitpath(Input_file, Input_drive, Input_dir, Input_fname, Input_ext);
  62.  
  63.    _splitpath(Output_file, Output_drive, Output_dir, Output_fname, Output_ext);
  64.  
  65.    if (strcmp(Output_drive, "") == 0) /* No drive given */
  66.       strcpy(Output_drive, Input_drive);
  67.  
  68.    if (strcmp(Output_dir, "") == 0) /* No directory given */
  69.       strcpy(Output_dir, Input_dir);
  70.  
  71.    _makepath(Output_file, Output_drive, Output_dir, Output_fname, Output_ext);
  72.  
  73. }
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77.    unsigned long int Input_byte_count = 0L, Output_byte_count = 0L;
  78.    unsigned char current_byte;
  79.    short int Add_CR = 1;
  80.    static const unsigned char CR = 0x0D, LF = 0x0A;
  81.    char Input_file[_MAX_PATH] = "", Output_file[_MAX_PATH] = "";
  82.  
  83.    FILE *ifp = NULL, *ofp = NULL; 
  84.  
  85.    setvbuf(stdout, NULL, _IONBF, 0); /* Make stdout unbuffered */
  86.  
  87.    printf("\nUnix-to-OS/2 File Conversion\n");
  88.  
  89.    switch(argc)
  90.    {
  91.    case 1: /* Only the command name was given */
  92.       while(ifp == NULL)
  93.       {
  94.          /* Obtain the path/name of the Unix file to be read */
  95.          printf("\nEnter path/name of input file: ");
  96.          fflush(stdin); /* Clear out any junk from last read */
  97.          scanf("%[^\r\n]", &Input_file);
  98.  
  99.          if (strlen(Input_file) == 0) /* A null response will mean stop */
  100.             exit(EXIT_FAILURE); /* Take a quick way out */
  101.  
  102.          ifp = fopen(Input_file, "rb"); /* The file must exist or else! */
  103.  
  104.          if (ifp == NULL) /* File not found? */
  105.             printf("Unable to open file %s. Please try again.", Input_file);
  106.       }
  107.  
  108.       while(ofp == NULL)
  109.       {
  110.          /* Obtain the path/name of the OS/2 file to be written */
  111.          printf("\nEnter path/name of output file: ");
  112.          fflush(stdin); /* Clear out any junk from last read */
  113.          scanf("%[^\r\n]", &Output_file);
  114.  
  115.          if (strlen(Output_file) == 0) /* A null response will mean stop */
  116.          {
  117.             fclose(ifp); /* close what we've opened */
  118.             exit(EXIT_FAILURE); /* Take a quick way out */
  119.          }
  120.  
  121.          merge_path(Input_file, Output_file);
  122.  
  123.          ofp = fopen(Output_file, "wb"); /* Open will create it, if necessary */
  124.  
  125.          if (ofp == NULL) /* Unable to create file? */
  126.             printf("Unable to open file %s. Please try again.", Output_file);
  127.       }
  128.       break;
  129.  
  130.    case 3: /* Both input and output files were given */
  131.       strcpy(Input_file,argv[1]);
  132.       ifp = fopen(Input_file,"rb");
  133.       if (ifp == NULL) /* File not found? */
  134.       {
  135.          printf("Unable to open file %s.\n", Input_file);
  136.          exit(EXIT_FAILURE);
  137.       }
  138.  
  139.       strcpy(Output_file,argv[2]);
  140.  
  141.       merge_path(Input_file, Output_file);
  142.  
  143.       ofp = fopen(Output_file,"wb");
  144.       if (ofp == NULL) /* Unable to create file? */
  145.       {
  146.          printf("Unable to open file %s.\n", Output_file);
  147.          fclose(ifp); /* Clean up what we've opened */
  148.          exit(EXIT_FAILURE);
  149.       }
  150.       break;
  151.  
  152.    default : /* No other combinations are valid */
  153.       printf("Invalid parameter list. Your command line should be:"
  154.          "\n\tLFtoCRLF <Input file path/name> <Output file path/name>");
  155.       exit(EXIT_FAILURE);
  156.       break;
  157.    }
  158.  
  159.    #if Buffer_size > 0
  160.    setvbuf(ifp, NULL, _IOFBF, Buffer_size); /* Establish buffering for input */
  161.    setvbuf(ofp, NULL, _IOFBF, Buffer_size); /* Establish buffering for output */
  162.    #endif
  163.  
  164.    fread(¤t_byte, 1, 1, ifp); /* Read first byte of file. Beware EOF! */
  165.    while(feof(ifp) == 0) /* Check for EOF */
  166.    {
  167.       Input_byte_count++; /* keep the accounting straight */
  168.  
  169.       if (current_byte == LF) /* LF is Unix delimiter */
  170.       {
  171.          if (Add_CR) /* then a CR is needed */
  172.          {
  173.             /* Compatibilty note: some editors require that each LF
  174.                should have a CR before it. There is no such requirement
  175.                in the DOS or OS/2 specifications, but if you want a CR
  176.                before each and every LF then the Add_CR flag should always
  177.                be non-zero, so comment out the next statement. */
  178.  
  179.             Add_CR = 0; /* suppress further CR's until non-LF is read */
  180.  
  181.             fwrite(&CR, 1, 1, ofp); /* Insert a CR */
  182.             Output_byte_count++;
  183.          }
  184.       }
  185.       else
  186.          Add_CR = 1; /* a non-LF means next LF needs a CR */
  187.  
  188.       fwrite(¤t_byte, 1, 1, ofp); /* write out the byte read from input */
  189.       Output_byte_count++;
  190.  
  191.       fread(¤t_byte, 1, 1, ifp); /* get the next byte and check for EOF */
  192.    }
  193.  
  194.    /* Close both files.  */
  195.    fclose(ifp);
  196.    fclose(ofp);
  197.  
  198.    printf("\nNo. of bytes read from %s was: %lu\nNo. of bytes written to %s was: %lu\n",
  199.       Input_file, Input_byte_count, Output_file, Output_byte_count);
  200.  
  201.    return 0;
  202. }
  203.