home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / rtfprsr / rtfwc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  2.2 KB  |  132 lines

  1. /*
  2.     rtfwc - read rtf input, write word count (actually, char, word
  3.         and paragraph counts).
  4.  
  5.     This installs callbacks for the ascii and control token classes.
  6.     The control class is necessary so that special characters such as
  7.     \par, \tab, \sect, etc.  can be counted.
  8.  
  9.     Counts paragraphs instead of lines, since the concept of "line"
  10.     is relatively meaningless.
  11.  
  12.     It's problematic how to count text in headers and footers, and
  13.     what to do about tables.
  14.  
  15.     04 Feb 91    Paul DuBois    dubois@primate.wisc.edu
  16.  
  17.     04 Feb 91 V1.0. Created.
  18.     27 Feb 91 V1.01. Updated for distribution 1.05.
  19.     16 Mar 91 V1.02. Updated for distribution 1.06.  Multiple files
  20.         allowed.
  21. */
  22.  
  23. # include    <stdio.h>
  24. # include    "rtf.h"
  25.  
  26.  
  27. static long    chars, tchars = 0;
  28. static long    words, twords = 0;
  29. static long    paras, tparas = 0;
  30. static long    wchars = 0;    /* chars in current word */
  31.  
  32. static void    Count ();
  33. static void    Text ();
  34. static void    Control ();
  35.  
  36. int main (argc, argv)
  37. int    argc;
  38. char    **argv;
  39. {
  40. int    i;
  41.  
  42.     --argc;
  43.     ++argv;
  44.  
  45.     if (argc == 0)
  46.     {
  47.         Count ();
  48.         printf ("\n");
  49.     }
  50.     else
  51.     {
  52.         for (i = 0; i < argc; i++)
  53.         {
  54.             if (freopen (argv[i], "r", stdin) == NULL)
  55.             {
  56.                 fprintf (stderr, "Can't open \"%s\"\n",
  57.                                 argv[i]);
  58.                 exit (1);
  59.             }
  60.             Count ();
  61.             printf ("\t%s\n", argv[i]);
  62.         }
  63.         if (argc > 1)    /* multiple files, print totals */
  64.             printf ("%ld chars\t%ld words\t%ld paragraphs\ttotal\n",
  65.                         tchars, twords, tparas);
  66.     }
  67.  
  68.     exit (0);
  69. }
  70.  
  71.  
  72. static void Count ()
  73. {
  74.     chars = words = paras = wchars = 0;
  75.  
  76.     RTFInit ();
  77.  
  78.     /* install counting hooks and process the input stream */
  79.  
  80.     RTFSetClassCallback (rtfText, Text);
  81.     RTFSetClassCallback (rtfControl, Control);
  82.     RTFRead ();
  83.     printf ("%ld chars\t%ld words\t%ld paragraphs", chars, words, paras);
  84.     tchars += chars;
  85.     twords += words;
  86.     tparas += paras;
  87. }
  88.  
  89. static void Text ()
  90. {
  91.     ++chars;
  92.     if (rtfMajor != ' ')
  93.         ++wchars;
  94.     else if (wchars > 0)
  95.         ++words;
  96. }
  97.  
  98.  
  99. static void Control ()
  100. {
  101.     if (rtfMajor != rtfSpecialChar)
  102.         return;
  103.     switch (rtfMinor)
  104.     {
  105.     case rtfPage:
  106.     case rtfSect:
  107.     case rtfLine:
  108.     case rtfPar:
  109.         ++paras;
  110.         ++chars;
  111.         if (wchars > 0)
  112.         {
  113.             ++words;
  114.             wchars = 0;
  115.         }
  116.         break;
  117.     case rtfNoBrkSpace:
  118.     case rtfTab:
  119.         ++chars;
  120.         if (wchars > 0)
  121.         {
  122.             ++words;
  123.             wchars = 0;
  124.         }
  125.         break;
  126.     case rtfNoBrkHyphen:
  127.         ++chars;
  128.         ++wchars;
  129.         break;
  130.     }
  131. }
  132.