home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / crlf / crlf.c next >
C/C++ Source or Header  |  1994-06-17  |  4KB  |  203 lines

  1. #include <stdio.h>
  2.  
  3. #define UNIX_BREAKS 1
  4. #define DOS_BREAKS  2
  5. #define MAC_BREAKS  3
  6.  
  7. /* Define WRITE_MODE as "wb" on PCs */
  8. #define WRITE_MODE  "w"
  9.  
  10. #define READ_LENGTH 4096;
  11.  
  12. #define CR 015
  13. #define LF 012
  14. #define HT 011
  15. #define FF 014
  16.  
  17. /* extern int getopt (int argc, char **argv, char *optstring); */
  18.  
  19. int main (argc, argv)
  20. int argc;
  21. char **argv;
  22. {
  23.   int  usage (char *, char *);
  24.   int  getopt (int, char **, char*);
  25.   int  lookahead (FILE *f);
  26.  
  27.   char *infn, *outfn;
  28.   FILE *infile, *outfile;
  29.   char *progname;
  30.   int  breaks = DEFAULT_BREAKS;
  31.   int  verify = 0;
  32.   int  quiet  = 0;
  33.   int  ch;
  34.  
  35.   char *brname[4] = { "ERROR", "UNIX", "DOS", "Mac" };
  36.  
  37.   unsigned char *inbuf, *outbuf;
  38.   int  inbuflen, outbuflen;
  39.   int  datalen, inplace, outplace;
  40.  
  41.   extern int  optind;
  42.  
  43.   progname = (char *) malloc (strlen(argv[0])+1);
  44.   strcpy(progname, argv[0]);
  45.  
  46.   while ((ch = getopt(argc, argv, "udmvqhH")) != -1) {
  47.     switch (ch) {
  48.     case 'u': 
  49.       breaks = UNIX_BREAKS;
  50.       break;
  51.     case 'd':
  52.       breaks = DOS_BREAKS;
  53.       break;
  54.     case 'm':
  55.       breaks = MAC_BREAKS;
  56.       break;
  57.     case 'v':
  58.       verify = 1;
  59.       break;
  60.     case 'q':
  61.       quiet = 1;
  62.       break;
  63.     case 'h':
  64.     case 'H':
  65.       usage(progname, NULL);
  66.       break;
  67.     default:
  68.       usage(progname, "unexpected option");
  69.     }
  70.   }
  71.  
  72.   if (optind < argc) {
  73.     infn = argv[optind++];
  74.     if (strcmp(infn, "-") == 0) {
  75.       infile = stdin;
  76.     } else {
  77.       infile = fopen(infn,"r");
  78.     if (infile == NULL)
  79.       usage(progname, "error opening input file");
  80.     }
  81.   } else {
  82.     infn = "-";
  83.     infile = stdin;
  84.   }
  85.  
  86.   inbuflen = READ_LENGTH;
  87.   outbuflen = 2*inbuflen;
  88.  
  89.   inbuf = (char *) malloc(inbuflen);
  90.   outbuf = (char *) malloc(outbuflen);
  91.  
  92.   if (verify) {
  93.     datalen = fread(inbuf, 1, inbuflen, infile);
  94.     ch = 0;
  95.     for (inplace = 0; inplace < datalen; inplace++) {
  96.       if ((inbuf[inplace] < 32) 
  97.       && (inbuf[inplace] != CR)
  98.       && (inbuf[inplace] != LF)
  99.       && (inbuf[inplace] != HT)
  100.       && (inbuf[inplace] != FF)) {
  101.     ch = 1;
  102.     break;
  103.       }
  104.     }
  105.  
  106.     if (ch) {
  107.       fprintf (stderr, 
  108.            "%s: %s does not appear to be a text file (%d).\n", 
  109.            progname, infn, inplace);
  110.       exit (2);
  111.     }
  112.  
  113.     fseek(infile, 0, 0);
  114.   }
  115.  
  116.   if (optind < argc) {
  117.     outfn = argv[optind++];
  118.     if (strcmp(outfn, "-") == 0) {
  119.       outfile = stdout;
  120.     } else {
  121.       outfile = fopen(outfn,WRITE_MODE);
  122.     if (outfile == NULL)
  123.       usage(progname, "error opening output file");
  124.     }
  125.   } else {
  126.     outfn = "-";
  127.     outfile = stdout;
  128.   }
  129.  
  130.   if (!quiet)
  131.     fprintf (stderr, 
  132.          "Converting %s to %s with %s line breaks.\n",
  133.          infn, outfn, brname[breaks]);
  134.  
  135.   while ((datalen = fread(inbuf, 1, inbuflen, infile)) > 0) {
  136.     outplace = 0;
  137.     for (inplace = 0; inplace < datalen; inplace++) {
  138.  
  139.       /* If this CR is the start of a CR/LF pair, skip it */
  140.       if (inbuf[inplace] == CR) {
  141.     if (inplace+1 == datalen) {
  142.       if (lookahead(infile) == LF) 
  143.         continue;
  144.     } else {
  145.       if (inbuf[inplace+1] == LF)
  146.         continue;
  147.     }
  148.       }
  149.         
  150.       if ((inbuf[inplace] == CR) || (inbuf[inplace] == LF)) {
  151.     if (breaks == UNIX_BREAKS)
  152.       outbuf[outplace++] = LF;
  153.     else
  154.       outbuf[outplace++] = CR;
  155.     if (breaks == DOS_BREAKS) 
  156.       outbuf[outplace++] = LF;
  157.       } else {
  158.     outbuf[outplace++] = inbuf[inplace];
  159.       }
  160.     }
  161.  
  162.     datalen = fwrite(outbuf, 1, outplace, outfile);
  163.     if (datalen != outplace) 
  164.       exit(1);
  165.   }
  166.  
  167.   fclose (infile);
  168.   fclose (outfile);
  169.  
  170.   exit (0);
  171. }
  172.  
  173. int usage (progname, msg)
  174. char *progname;
  175. char *msg;
  176. {
  177.   if (msg)
  178.     fprintf (stderr, "Error: %s\n", msg);
  179.  
  180.   fprintf (stderr, "Usage: %s [options] infile [outfile]\n", progname);
  181.   fprintf (stderr, "Where: infile = name of input file\n");
  182.   fprintf (stderr, "       outfile = name of output file\n");
  183.   fprintf (stderr, "       Use '-' for stdin or stdout, respectively.\n");
  184.   fprintf (stderr, "Options:\n");
  185.   fprintf (stderr, "  -d = output MS-DOS line breaks (CR/LF)\n");
  186.   fprintf (stderr, "  -m = output Mac line breaks (CR)\n");
  187.   fprintf (stderr, "  -u = output UNIX line breaks (LF)\n");
  188.   fprintf (stderr, "  -v = verify that input file is plain text\n");
  189.   fprintf (stderr, "  -q = suppress \"Converting...\" message\n");
  190.   fprintf (stderr, "  -h = display this help message\n");
  191.   exit (1);
  192. }
  193.  
  194. int lookahead (f)
  195. FILE *f;
  196. {
  197.   int pos = ftell(f);
  198.   int ch  = fgetc(f);
  199.   fseek(f, pos, 0);
  200.  
  201.   return ch;
  202. }
  203.