home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cpgms.zip / CCASE.C < prev    next >
C/C++ Source or Header  |  1985-11-17  |  3KB  |  139 lines

  1. #include "stdio.h"
  2. #define END 0xff
  3. #define ERROR 0
  4. #define CPMEOF 0X1A
  5. #define UPPER 0
  6. #define LOWER 1
  7. #define WORDS -1
  8. #define NULL 0
  9. #define DeSmet 1
  10.  
  11.  
  12. char    *documentation[] = {
  13. "ccase changes the case of a sequential file.",
  14. "   ccase [flags] [file] [<file] [>file]",
  15. "",
  16. "Flags are single characters preceeded by '-':",
  17. "   -w      1st letter of each word is upper case",
  18. "   -u      All letters are upper case",
  19. "   -l      All letters are lower case",
  20. "",
  21. "Input is from the given file or stdin, if none given.",
  22. "Output is to stdout.  To print add >PRN: to your command.",
  23. 0 };
  24.  
  25. int tflg = 0;
  26.  
  27.  
  28. main(argc, argv)
  29. char *argv[];
  30. {
  31.    register char   *p;
  32.    register int    c, i;
  33.     int nfile;
  34.     char tolower();
  35.    FILE        *f;
  36.  
  37.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  38.       help(documentation);
  39.       return;
  40.       }
  41.    nfile = argc-1;
  42.    for (i=1; i < argc; ++i) {
  43.       p = argv[i];
  44.       if (*p == '-') {
  45.      ++p;
  46.      while (c = *p++) {
  47.         switch(tolower(c)) {
  48.  
  49.         case '?':
  50.            help(documentation);
  51.            break;
  52.  
  53.         case 'W':
  54.         case 'w':
  55.            tflg = WORDS;
  56.            break;
  57.  
  58.         case 'U':
  59.         case 'u':
  60.            tflg = UPPER;
  61.            break;
  62.  
  63.         case 'L':
  64.         case 'l':
  65.            tflg = LOWER;
  66.            break;
  67.  
  68.  
  69.         default:
  70.            usage("Unknown flag");
  71.         }
  72.      }
  73.      argv[i] = 0;
  74.      --nfile;
  75.       }
  76.    }
  77.     if (nfile == 0)
  78.         ccase(stdin, 0);
  79.     else if (nfile > 1)
  80.         usage("Too many filenames");
  81.     else
  82.         if ((f=fopen(p, "r")) == NULL)
  83.            cant(p);
  84.         else {
  85.            ccase(f, p);
  86.            fclose(f);
  87.         }
  88. }
  89.  
  90.  
  91. /*******************************************************/
  92.  
  93. usage(s)
  94. char    *s;
  95. {
  96.    puts("?LIST-E-");
  97.    puts(s);
  98.    puts("\n");
  99.    puts("Usage: ccase [-wul] [file] [<file] [>file].  \n");
  100.    puts(" ccase ? for help\n");
  101.    exit(1);
  102. }
  103.  
  104.  
  105. /*******************************************************/
  106.  
  107. ccase(fp, fn)
  108. FILE       *fp;       /* File to process        */
  109. char       *fn;       /* File name (for -f option)  */
  110. /*
  111.  * change the case of the input file to upper, lower, or words.
  112.  */
  113. {
  114.     char c, getc(), putc(), toupper(), tolower();
  115.     int iflg, isalpha();
  116.  
  117.     iflg = 1;
  118.     while (((c = getc(fp)) != CPMEOF) && (c != END)) {
  119. #if DeSmet
  120.         if (c == '\r')
  121.             continue;
  122. #endif
  123.         if (tflg == UPPER)
  124.             putc(toupper(c),stdout);
  125.         else if (tflg == LOWER)
  126.             putc(tolower(c),stdout);
  127.         else {
  128.             if (iflg)
  129.                 putc(toupper(c),stdout);
  130.             else
  131.                 putc(tolower(c),stdout);
  132.             if (!isalpha(c))
  133.                 iflg = 1;
  134.             else
  135.                 iflg = 0;
  136.         }
  137.     }
  138. }
  139.