home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / pcmag / v9n14.lzh / WC.C < prev    next >
Text File  |  1990-07-03  |  5KB  |  113 lines

  1. //  WC.C        Count characters, words, lines, and sentences in an
  2. //              ASCII text file.  32-bit application for OS/2 2.0 SDK.
  3. //              Word and sentence counting logic is simplistic and
  4. //              is intended for demonstration purposes only.
  5. //
  6. //  Compile:    CL386 WC.C
  7. //
  8. //  Usage:      WC filename.ext
  9. //
  10. //  Copyright (C) 1989 Ziff Davis Communications
  11. //  PC Magazine * Ray Duncan
  12.  
  13. #include <stdio.h>
  14.  
  15. #define INCL_DOS
  16. #include <os2.h>
  17.  
  18. #define TAB     '\x09'                      // ASCII tab character
  19. #define LF      '\x0A'                      // ASCII line feed
  20. #define FF      '\x0C'                      // ASCII form feed
  21. #define CR      '\x0D'                      // ASCII carriage return
  22. #define BLANK   '\x20'                      // ASCII blank
  23. #define EOFMK   '\x1A'                      // ASCII end-of-file mark
  24.  
  25. main(int argc, char *argv[])
  26. {
  27.     HFILE fhandle;                          // receives file handle
  28.     FILESTATUS fstatus;                     // receives file information
  29.     PBYTE fbuffer;                          // receives buffer address
  30.     ULONG fptr;                             // index to buffer
  31.     ULONG faction;                          // receives DosOpen action
  32.     ULONG frlen;                            // receives DosRead length
  33.     ULONG fchars;                           // chars. in file
  34.     ULONG fwords = 0;                       // words in file
  35.     ULONG flines = 0;                       // lines in file
  36.     ULONG fsentences = 0;                   // sentences in file
  37.     ULONG wflag = 0;                        // TRUE if inside word
  38.  
  39.                                             // try and open file...
  40.     if(DosOpen(argv[1],                     // filename from command line
  41.         &fhandle,                           // receives file handle
  42.         &faction,                           // receives DosOpen action
  43.         0,                                  // file size (ignored)
  44.         0,                                  // file attribute (ignored)
  45.         FILE_OPEN,                          // fail if doesn't exist
  46.         OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,    // access mode
  47.         NULL))                              // pointer to EAOP structure
  48.     {
  49.         printf("\nwc: can't open file %s", argv[1]);
  50.         exit(1);
  51.     }
  52.                                             // get file size...
  53.     if(DosQueryFileInfo(fhandle,            // file handle
  54.         FIL_STANDARD,                       // information level 1
  55.         (PBYTE) &fstatus,                   // address of info structure
  56.         sizeof(fstatus)))                   // size of info structure
  57.     {
  58.         printf("\nwc: can't get file size");
  59.         exit(2);
  60.     }
  61.  
  62.     fchars = fstatus.cbFile;                // save file size
  63.  
  64.                                             // allocate memory object...
  65.     if(DosAllocMem(&fbuffer,                // receives object offset
  66.         fchars,                             // size to allocate
  67.         PAG_COMMIT | PAG_READ | PAG_WRITE)) // commit backing store
  68.     {
  69.         printf("\nwc: can't allocate file buffer");
  70.         exit(3);
  71.     }
  72.                                             // read the entire file
  73.     DosRead(fhandle,                        // file handle
  74.         fbuffer,                            // buffer address
  75.         fchars,                             // length to read
  76.         &frlen);                            // receives actual length
  77.  
  78.     DosClose(fhandle);                      // close the file   
  79.  
  80.     for(fptr = 0; fptr < fchars; fptr++)    // scan the file
  81.     {
  82.         switch(fbuffer[fptr])               // check this character
  83.         {
  84.             case '.':                       // period found, count
  85.                 fsentences++;               // sentences (simplistic
  86.                 break;                      // assumption for demo)
  87.  
  88.             case LF:                        // line feed found, count
  89.                 flines++;                   // lines, fall through
  90.  
  91.             case CR:                        // if first whitespace
  92.             case BLANK:                     // character following
  93.             case FF:                        // text, count words
  94.             case TAB:
  95.             case EOFMK:
  96.                 if(wflag) fwords++;
  97.                 wflag = FALSE;
  98.                 break;
  99.  
  100.             default:                        // if not whitespace
  101.                 wflag = TRUE;               // character, assume we
  102.                 break;                      // are traversing a word
  103.         }
  104.     }
  105.  
  106.     if(wflag) fwords++;                     // in case word at EOF
  107.  
  108.     DosFreeMem(fbuffer);                    // release file buffer
  109.  
  110.     printf("\n%d characters, %d words, %d lines, %d sentences.\n",
  111.         fchars, fwords, flines, fsentences);
  112. }
  113.