home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- // This example code is from the book:
- //
- // Object-Oriented Programming with C++ and OSF/Motif
- // by
- // Douglas Young
- // Prentice Hall, 1992
- // ISBN 0-13-630252-1
- //
- // Copyright 1991 by Prentice Hall
- // All Rights Reserved
- //
- // Permission to use, copy, modify, and distribute this software for
- // any purpose except publication and without fee is hereby granted, provided
- // that the above copyright notice appear in all copies of the software.
- ///////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
-
-
- ////////////////////////////////////////////////////////////////
- // CountWordsCmd.C: A simple test of the InterruptibleCmd class
- ///////////////////////////////////////////////////////////////
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include "CountWordsCmd.h"
- #include "InfoDialogManager.h"
-
- CountWordsCmd::CountWordsCmd ( char *name,
- int active,
- char *filename ) :
- InterruptibleCmd ( name, active )
- {
- struct stat statInfo;
-
- // Initialize data members
-
- _bytesRead = 0;
- _fileSize = 0;
- _numWords = 0;
- _list = NULL;
- _percentDone = 0;
-
- // Open the given file and post a warning in case of failure
-
- if( ( _fd = fopen ( filename, "r" ) ) == NULL )
- {
- char buf[BUFSIZ];
-
- sprintf ( buf, "Can't open %s", filename );
-
- theInfoDialogManager->post ( buf );
- }
- else
- {
- // Check the size of the file, to use as a basis for
- // reporting progress. Don't bother counting if the file
- // is empty.
-
- char buf[BUFSIZ];
-
- if ( stat ( filename, &statInfo ) == 0 )
- _fileSize = statInfo.st_size;
-
- if ( _fileSize == 0 )
- {
- sprintf ( buf, "%s is empty!", filename );
-
- theInfoDialogManager->post ( buf );
-
- fclose ( _fd );
- _fd = 0;
- }
- }
- }
-
- CountWordsCmd::~CountWordsCmd ()
- {
- // Only close the file if it was succesfully opened
-
- if ( _fd )
- fclose ( _fd );
-
- for ( int i=0; i < _numWords; i++ )
- delete _list[i];
-
- delete _list;
- }
-
- void CountWordsCmd::doit()
- {
- char buf[BUFSIZ];
- char *sep = " !@#$%^&*()_+=-}{][|\';:\"?></.,`~\\\n\t";
- int percent;
-
- // If the file has not been opened, indicate that the task
- // is finished.
-
- if ( !_fd )
- {
- _done = TRUE;
- return;
- }
-
- // Read a few lines each time doit() is called
-
- for ( int i = 0; i < 20; i++ )
- {
- char *result;
- char *word;
-
- // Read in one line of text, indicating that the
- // task is done if we reach end-of-file
-
- if ( ( result = fgets ( buf, BUFSIZ, _fd ) ) == NULL )
- {
- _done = TRUE;
- return;
- }
-
- // Compute the total characters read for progress report
-
- _bytesRead += strlen ( buf );
-
- // Extract the first full word and save it in the word list
-
- word = strtok ( buf, sep );
-
- saveWord ( word );
-
- // Continue to extract words until the line is exhausted
-
- while ( ( word = strtok ( NULL, sep ) ) != NULL )
- saveWord ( word );
- }
-
- // Update the busy dialog and report progress as
- // the percentage of the file read so far. Only report if the
- // percent done has changed.
-
- percent = (int) ((float) _bytesRead / (float) _fileSize * 100);
-
- if ( _percentDone != percent )
- {
-
- _percentDone = percent;
-
- sprintf ( buf, "Counting, Please Wait...\n %d %% Completed",
- _percentDone );
-
- updateMessage( buf );
-
- }
- }
-
- void CountWordsCmd::saveWord ( char * word )
- {
- // Check for valid input
-
- if ( !word )
- return;
-
- // Search for the word. Increment the count if found
-
- for ( int i = 0; i < _numWords; i++ )
- {
- if ( *_list[i] == word ) // Note use of overloaded operator==
- {
- ( *_list[i] )++;
- return;
- }
- }
-
- // If not found, create a new Word object and add it to the
- // list, increasing the size of the list for each new word.
-
- Word *obj = new Word ( word );
-
- Word **newList = new Word*[_numWords + 1];
-
- for ( i = 0; i < _numWords; i++ )
- newList[i] = _list[i];
-
- newList[_numWords] = obj;
- delete _list;
- _list = newList;
-
- _numWords++;
- }
-
-