home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d584 / textstat.lha / TextStat / TextStat.c < prev    next >
C/C++ Source or Header  |  1992-01-04  |  6KB  |  226 lines

  1. /*   TextStat v1.10
  2.  *
  3.  *   A text analysis and word count program.
  4.  *
  5.  *   Written by Stefan Zeiger - 08/02/91
  6.  *
  7.  *   © 1991 by ! WIZARD WORKS !
  8.  *
  9.  *   This program is Freeware. It may only be distributed for non-commercial
  10.  *   purposes, only for a little distribution fee (less or equal to the
  11.  *   distribution fee, that Fred Fish wants for his AmigaLibDisks) , only
  12.  *   with all files (TextStat, TextStat.doc, TextStat.c, LMKFile )
  13.  *   together and only in unmodified form. See TextStat.doc for further
  14.  *   information. Bug reports, donations, questions and suggestions to:
  15.  *
  16.  *    STANDARD MAIL : Stefan Zeiger
  17.  *                    Seligenstaedter Weg 24
  18.  *                    D-W-8756 Kahl
  19.  *                    West Germany
  20.  *    VOICE :         (49)-6188-2525
  21.  */
  22.  
  23.  
  24. #include <exec/types.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <ctype.h>
  31.  
  32.  
  33. #define EQUAL 0
  34.  
  35.  
  36. void cleanexit(int,ULONG),quicksort(int,int),quicksort2(int,int);
  37.  
  38.  
  39. ULONG field[512],field2[512];
  40. int index[512],index2[512];
  41.  
  42.  
  43. void main(int argc,char **argv)
  44. {
  45.   ULONG alnum=0,alpha=0,ascii=0,cntrl=0,csym=0,csymf=0,digit=0;
  46.   ULONG graph=0,lower=0,print=0,punct=0,upper=0,xdigit=0;
  47.   ULONG words=0,letters=0,checksum=0,whites=0,nonwhites=0;
  48.   int handle=-1,dummy=0,anzahl=1,i,flag=FALSE;
  49.   char wert=1,*wert_p=&wert;
  50.   UBYTE *message="\n\033[33mTextStat v1.10\033[31m\nWritten by Stefan Zeiger - ©1991 by ! WIZARD WORKS !\n";
  51.  
  52.   for(i=0;i<=255;i++) { field[i]=0; index[i]=i; }
  53.  
  54.   if(argc==1)
  55.   {
  56.     printf("No file specified.\n");
  57.     cleanexit(handle,15L);
  58.   }
  59.  
  60.   if(strcmp(argv[1],"?")==EQUAL)
  61.   {
  62.     printf(message);
  63.     printf("Usage : %s <filename> [-a]\n\n",argv[0]);
  64.     cleanexit(handle,15L);
  65.   }
  66.  
  67.   if(argc>3)
  68.   {
  69.     printf("Too many arguments.\n");
  70.     cleanexit(handle,15L);
  71.   }
  72.  
  73.   if((strcmp(argv[2],"-a")!=EQUAL) && (argc==3))
  74.   {
  75.     printf("Unknown option.\n");
  76.     cleanexit(handle,15L);
  77.   }
  78.  
  79.   handle=open(argv[1],0,dummy);
  80.   if(handle==-1)
  81.   {
  82.     printf("File not found.\n");
  83.     cleanexit(handle,15L);
  84.   }
  85.  
  86.   printf(message);
  87.   printf("Analyzing file \'\033[32m%s\033[31m\'\n",argv[1]);
  88.  
  89.   while (anzahl==1)    /* The main loop */
  90.   {
  91.     anzahl=read(handle,wert_p,1);
  92.     /* printf("%d\n",wert); */
  93.     checksum+=wert;
  94.     letters++;
  95.     field[wert]++;
  96.     if(wert==32)
  97.     {
  98.       whites++;
  99.       if(flag==TRUE) { flag=FALSE; words++; }
  100.     }
  101.     else
  102.     {
  103.       nonwhites++;
  104.       if(wert!=10) flag=TRUE;
  105.     }
  106.     if(wert==10) { if(flag==TRUE) { flag=FALSE; words++; } }
  107.  
  108.     if( isalnum (wert)) alnum  ++;
  109.     if( isalpha (wert)) alpha  ++;
  110.     if( isascii (wert)) ascii  ++;
  111.     if( iscntrl (wert)) cntrl  ++;
  112.     if( iscsym  (wert)) csym   ++;
  113.     if( iscsymf (wert)) csymf  ++;
  114.     if( isdigit (wert)) digit  ++;
  115.     if( isgraph (wert)) graph  ++;
  116.     if( islower (wert)) lower  ++;
  117.     if( isprint (wert)) print  ++;
  118.     if( ispunct (wert)) punct  ++;
  119.     if( isupper (wert)) upper  ++;
  120.     if( isxdigit(wert)) xdigit ++;
  121.  
  122.   }   /* End of the main loop */
  123.  
  124.   close(handle);
  125.  
  126.   if(strcmp(argv[2],"-a")==EQUAL)
  127.   {
  128.     printf("\n\033[32m DEC | ASC | COUNT  \n--------------------\033[31m\n");
  129.  
  130.     for(i=32;i<=127;i++)  printf("%3d  |  %c  |  %6lu\n",i,i,field[i]);
  131.     for(i=161;i<=255;i++) printf("%3d  |  %c  |  %6lu\n",i,i,field[i]);
  132.   }
  133.  
  134.   printf("\nLINES               : %9lu\n"  ,field[10]);
  135.   printf(  "WORDS               : %9lu\n"  ,words);
  136.   printf(  "CHARS               : %9lu\n"  ,letters);
  137.  
  138.   printf(  "ALPHANUMERIC CHARS  : %9lu\n"  ,alnum);
  139.   printf(  "ALPHABETIC CHARS    : %9lu\n"  ,alpha);
  140.   printf(  "ASCII CHARACTERS    : %9lu\n"  ,ascii);
  141.   printf(  "PRINTABLE CHARS     : %9lu\n"  ,print);
  142.  
  143.   printf(  "WHITESPACES         : %9lu\n"  ,whites);
  144.   printf(  "NON-WHITE CHARS     : %9lu\n"  ,nonwhites);
  145.  
  146.   printf(  "CONTROL CHARACTERS  : %9lu\n"  ,cntrl);
  147.   printf(  "C SYMBOL CHARS      : %9lu\n"  ,csym);
  148.   printf(  "C SYMBOL LEAD CHARS : %9lu\n"  ,csymf);
  149.   printf(  "GRAPHIC CHARACTERS  : %9lu\n"  ,graph);
  150.   printf(  "PUNCTUATION CHARS   : %9lu\n"  ,punct);
  151.  
  152.   printf(  "UPPER CASE CHARS    : %9lu\n"  ,upper);
  153.   printf(  "LOWER CASE CHARS    : %9lu\n"  ,lower);
  154.  
  155.   printf(  "DECIMAL DIGIT CHARS : %9lu\n"  ,digit);
  156.   printf(  "HEX DIGIT CHARS     : %9lu\n"  ,xdigit);
  157.  
  158.   printf(  "CHECKSUM            : %9lu\n\n",checksum);
  159.  
  160.   if(strcmp(argv[2],"-a")==EQUAL)
  161.   {
  162.     for(i=0;i<=255;i++) { field2[i]=field[i]; index2[i]=index[i]; }
  163.     quicksort(0,255);
  164.     printf("\033[32m DEC | ASC | COUNT  \n--------------------\033[31m\n");
  165.     for(i=0;i<=255;i++)
  166.     {
  167.       if((index[i]>31 && index[i]<128) || index[i]>160)
  168.         printf("%3d  |  %c  |  %6lu\n",index[i],index[i],field[i]);
  169.     }
  170.     for(i=0;i<=255;i++) { field[i]=field2[i]; index[i]=index2[i]; }
  171.     for(i='A';i<='z';i++)
  172.     {
  173.       if(i>='a' && i<='z') field[i-'a'+'A']+=field[i];
  174.     }
  175.     quicksort(0,255);
  176.     printf("\n\033[32m ASC | COUNT  \n---------------\033[31m\n");
  177.     for(i=0;i<=255;i++)
  178.     {
  179.      if(index[i]>='A' && index[i]<='Z')
  180.        printf("  %c  |  %6lu\n",index[i],field[i]);
  181.     }
  182.     printf("\n");
  183.   }
  184.   exit(0L);
  185. }
  186.  
  187.  
  188. void cleanexit(int handle,ULONG error)
  189. {
  190.   if(handle!=-1) close(handle);
  191.   exit(error);
  192. }
  193.  
  194.  
  195. void quicksort(int start,int stop)
  196. {
  197.   int m,l,r;
  198.   ULONG v,help;
  199.  
  200.   if(start<stop)
  201.   {
  202.     m=(start+stop)/2;
  203.     v=field[m];
  204.     l=start;
  205.     r=stop;
  206.     while(l<=r)
  207.     {
  208.       while(field[l]>v) l++;
  209.       while(field[r]<v) r--;
  210.       if(l<=r)
  211.       {
  212.         help=field[r];
  213.         field[r]=field[l];
  214.         field[l]=help;
  215.         help=index[r];
  216.         index[r]=index[l];
  217.         index[l]=help;
  218.         l++;
  219.         r--;
  220.       }
  221.     }
  222.     quicksort(start,r);
  223.     quicksort(l,stop);
  224.   }
  225. }
  226.