home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 109_01 / newchcnt.c < prev    next >
Text File  |  1985-03-10  |  2KB  |  90 lines

  1. /*
  2. This program reads an input ascii file and compiles statistics
  3. on all possible char. values (0-255).
  4. To run, type: chcnt filename.ext <cr>.
  5. The original program was from an earlier BDS Distribution
  6. disk.  I have cleaned up the existing bugs and added the
  7. BigFio buffered file input routines.
  8. The main bug was chcnt read past the EOF of a file to the
  9. physical end of the sector.  This added some extra counts!
  10. This thing is usefull in making sure that the # of left (,{,
  11. and [ equals the # of right ),} and ].
  12. */
  13. #define NB 32     /*  32*28 = 4K Input buffer  */
  14. #define CPMEOF -1
  15. #define EOF 26
  16. unsigned table[255];
  17. char ascy[4];
  18. main (argc,argv)
  19. int argc;
  20. char **argv;
  21. {
  22.     char ibuf[NB*128+8];
  23.     int ifd,c,i;
  24.     for (i=0; i<255; i++)
  25.         table[i] = 0;
  26.     ifd = FOpenBig(argv[1], ibuf, NB);
  27.     if (ifd == -1)    {
  28.         printf ("Can't open File!!");
  29.         exit ();
  30.     }
  31.     while ((c = GetcBig(ibuf)) != EOF && c != CPMEOF)
  32.         table[c]++;
  33.     table[c]++;        /*  count EOF also */
  34.  
  35.     close (ifd);
  36.     display ();
  37. }
  38. display ()
  39. {
  40.     int i,j,k;
  41.     char *cnvt();
  42.     unsigned count;
  43.     count = 0;
  44.     printf("\n\n  ");
  45.     for (i=1; i<=8; i++) printf ("ch  cnt   ");
  46.     printf("\n  ");
  47.     for (i=1; i<=8; i++) printf("--  ---   ");
  48.     printf("\n");
  49.  
  50.     for (i=0; i<=15; i++)  {
  51.         for (j=i,k=0; k<8; k++,j+=16) {
  52.             count += table[j];
  53.             printf ("%3s: %4u ",cnvt(j), table[j]);
  54.         }
  55.         printf ("\n");
  56.     }
  57.     printf ("\nTotal # of chars read = %u\n",count);
  58. }
  59. char *cnvt(byte)
  60. int byte;
  61. {
  62.     int c;
  63.     c=byte;
  64.     switch(c)
  65.     {
  66.         case 0:  return ("NU");
  67.         case 13: return ("CR");
  68.         case 10: return ("LF");
  69.         case 27: return ("ES");
  70.         case 28: return ("FS");
  71.         case 29: return ("GS");
  72.         case 30: return ("RS");
  73.         case 31: return ("US");
  74.         case 32: return ("SP");
  75.         case 8:  return ("BS");
  76.         case 127:return ("DL");
  77.         default:
  78.             if (c>=1 && c<=26) {
  79.                 ascy[0]='^';
  80.                 ascy[1]=c+64;
  81.                 ascy[2]='\0';
  82.                 return(ascy);
  83.             }
  84.             ascy[0]=' ';
  85.             ascy[1]=c;
  86.             ascy[2]= '\0';
  87.             return(ascy);
  88.     }
  89. }
  90.