home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / NLCNVRT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  198 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  NLCNVRT.C - A utility to convert newlines in text files
  5. **
  6. **  public domain by Bob Stout
  7. **  Mac support by Norman Dodge and Bob Stout
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "errors.h"
  14. #include "sniptype.h"
  15.  
  16. FILE *infile;
  17. FILE *outfile;
  18.  
  19. static void usage(void);
  20. static void dos2unix(void);
  21. static void dos2mac(void);
  22. static void unix2dos(void);
  23. static void unix2mac(void);
  24. static void mac2dos(void);
  25. static void mac2unix(void);
  26. static void Cout(int ch, FILE *outfile);
  27. static void Sout(char *str, FILE *outfile);
  28.  
  29. main(int argc, char *argv[])
  30. {
  31.       char outname[L_tmpnam] = "";
  32.  
  33.       if (argc < 3)
  34.             usage();
  35.       if (NULL == strchr("-/", argv[1][0]))
  36.             usage();
  37.  
  38.       infile = cant(argv[2], "rb");
  39.  
  40.       if (3 == argc || NULL == (outfile = fopen(argv[3], "wb")))
  41.       {
  42.             tmpnam(outname);
  43.             outfile = cant(outname, "wb");
  44.       }
  45.  
  46.       switch (argv[1][1])
  47.       {
  48.       case 'd':
  49.             unix2dos();
  50.             break;
  51.  
  52.       case 'D':
  53.             mac2dos();
  54.             break;
  55.  
  56.       case 'u':
  57.             dos2unix();
  58.             break;
  59.  
  60.       case 'U':
  61.             mac2unix();
  62.             break;
  63.  
  64.       case 'm':
  65.             dos2mac();
  66.             break;
  67.  
  68.       case 'M':
  69.             unix2mac();
  70.             break;
  71.  
  72.       default:
  73.             usage();
  74.       }
  75.       fclose(infile);
  76.       fclose(outfile);
  77.       if (*outname)
  78.       {
  79.             if (Success_ != remove(argv[2]))
  80.             {
  81.                   remove(outname);
  82.                   ErrExit("Can't remove %s", argv[2]);
  83.             }
  84.             rename(outname, argv[2]);
  85.       }
  86.       return EXIT_SUCCESS;
  87. }
  88.  
  89. void usage(void)
  90. {
  91.       puts("Usage: NLCNVRT -[d | u | m | D | U | M] infile [outfile]");
  92.       puts("Switches: -d  Convert to DOS  from Unix - converts LF   => CRLF");
  93.       puts("        : -u  Convert to Unix from DOS  - converts CRLF => LF");
  94.       puts("        : -m  Convert to Mac  from DOS  - converts CRLF => CR");
  95.       puts("        : -D  Convert to DOS  from Mac  - converts CR   => CRLF");
  96.       puts("        : -U  Convert to Unix from Mac  - converts CR   => LF");
  97.       puts("        : -M  Convert to Mac  from Unix - converts LF   => CR");
  98.       exit(EXIT_FAILURE);
  99. }
  100.  
  101. void unix2dos(void)
  102. {
  103.       int ch, lastch = 0;
  104.  
  105.       while (EOF != (ch = fgetc(infile)))
  106.       {
  107.             if ('\n' == ch && '\r' != lastch)
  108.                   fputc('\r', outfile);
  109.             fputc(lastch = ch, outfile);
  110.       }
  111.       if ('\n' != lastch)
  112.             fputs("\r\n", outfile);
  113. }
  114.  
  115. void dos2unix(void)
  116. {
  117.       int ch, lastch = 0;
  118.  
  119.       while (EOF != (ch = fgetc(infile)) && '\x1a' != ch)
  120.       {
  121.             if ('\r' != ch)
  122.                   fputc(lastch = ch, outfile);
  123.       }
  124.       if ('\n' != lastch)
  125.             fputc('\n', outfile);
  126. }
  127.  
  128. void mac2dos(void)
  129. {
  130.       int ch, lastch = 0;
  131.  
  132.       while (EOF != (ch = fgetc(infile)))
  133.       {
  134.             fputc(lastch = ch, outfile);
  135.             if ('\r' == ch)
  136.                   fputc('\n', outfile);
  137.       }
  138.       if ('\r' != lastch)
  139.             fputs("\r\n", outfile);
  140. }
  141.  
  142. static void dos2mac(void)
  143. {
  144.       int ch, lastch = 0;
  145.  
  146.       while (EOF != (ch = fgetc(infile)) && '\x1a' != ch)
  147.       {
  148.             if ('\r' != ch)
  149.             {
  150.                   if ('\n' == ch)
  151.                         ch = '\r';
  152.                   fputc(lastch = ch, outfile);
  153.             }
  154.       }
  155.       if ('\r' != lastch)
  156.             fputc('\r', outfile);
  157. }
  158.  
  159. static void unix2mac(void)
  160. {
  161.       int ch, lastch = 0;
  162.  
  163.       while (EOF != (ch = fgetc(infile)))
  164.       {
  165.             if ('\n' == ch)
  166.                   ch = '\r';
  167.             fputc(lastch = ch, outfile);
  168.       }
  169.       if ('\r' != lastch)
  170.             fputc('\r', outfile);
  171. }
  172.  
  173. static void mac2unix(void)
  174. {
  175.       int ch, lastch = 0;
  176.  
  177.       while (EOF != (ch = fgetc(infile)))
  178.       {
  179.             if ('\r' == ch)
  180.                   ch = '\n';
  181.             fputc(lastch = ch, outfile);
  182.       }
  183.       if ('\n' != lastch)
  184.             fputc('\n', outfile);
  185. }
  186.  
  187. void Cout(int ch, FILE *outfile)
  188. {
  189.       if (EOF == fputc(ch, outfile))
  190.             exit(EXIT_FAILURE);
  191. }
  192.  
  193. void Sout(char *str, FILE *outfile)
  194. {
  195.       if (EOF == fputs(str, outfile))
  196.             exit(EXIT_FAILURE);
  197. }
  198.