home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 11 Exotic Applications / ASCII.ZIP / TRUE_PET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-08  |  3.2 KB  |  103 lines

  1. /* Get True ASCII characters from the source file and convert them 
  2.    to Pet ASCII, then write them to the target file.  Stop when an 
  3.    EOF (End of File) is encountered.
  4.    
  5.    Called with:
  6.    
  7. TRUE_PET  true_ASCII_file   Pet_ASCII_file
  8.  
  9.    Noel Nyman 3/90
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <io.h>
  16.  
  17. char sourcebuf[BUFSIZ * 16];                   /* source file buffer */
  18. char targetbuf[BUFSIZ * 16];                   /* target file buffer */
  19.  
  20.  
  21. main(argc, argv)
  22.         int argc;
  23.         char *argv[];
  24.  
  25.  
  26. {
  27.         int c, i=0;
  28.         FILE *source, *target;
  29.         char response='Y';
  30.         char petfile[128], truefile[128];
  31.                 
  32.  
  33.         if (argc == 2)
  34.             {    
  35.             printf("TRUE_PET: must specify both source and target file\n");
  36.             exit(1);
  37.             }
  38.         else if (argc == 1)
  39.             {
  40.             printf("File name of TRUE-ASCII file: ");
  41.             gets(truefile);
  42.             printf("File name of destination file: ");
  43.             gets(petfile);
  44.             }
  45.         else 
  46.             {
  47.             strcpy(truefile,argv[1]);
  48.             strcpy(petfile,argv[2]);
  49.             }
  50.                 
  51.  
  52.         if (access(petfile, 0) == 0)   /* does target exist */
  53.                 do
  54.                     {
  55.                     printf("\n\"%s\" already exists - Overwrite (Y/N)? ", petfile);
  56.                     response = getchar();
  57.                     while (getchar() != '\n')
  58.                         { ; }
  59.                     response = toupper(response);
  60.                     }         /* loop until Y or N */
  61.                 while (response != 'Y' && response != 'N');
  62.  
  63.         if (response == 'Y')    /* overwrite the file */
  64.                 {
  65.                 if ((source = fopen(truefile, "r")) == NULL)
  66.                         printf("TRUE_PET: error opening \"%s\"\n", truefile);
  67.                 
  68.                 else if ((target = fopen(petfile, "w")) == NULL)
  69.                         printf("TRUE_PET: error opening \"%s\"\n", petfile);
  70.  
  71.                 else
  72.                     { 
  73.                     setvbuf( source, sourcebuf, _IOFBF, sizeof( sourcebuf ) );
  74.                     setvbuf( target, targetbuf, _IOFBF, sizeof( targetbuf ) );
  75.  
  76.                     printf("Working .");
  77.                     while ((c = getc(source)) != EOF)
  78.                        {
  79.                          if islower(c) 
  80.                                putc(toupper(c), target);
  81.                          else if isupper(c)
  82.                                putc(c += 128,target);
  83.                          else if (c == '\n')    /* don't pass line feeds */
  84.                                putc(13, target);
  85.                          else       
  86.                                putc(c,target);
  87.  
  88.  
  89.                          if (++i > 256)
  90.                                 {
  91.                                  i = 0;
  92.                                  printf(".");
  93.                                 }
  94.                         }
  95.                       fclose(source);
  96.                       fclose(target);
  97.                       printf("\n");
  98.                     }
  99.                 }
  100.    }
  101.  
  102.  
  103.