// WordFinder.h #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef WordFinderh #define WordFinderh #include //You must include this file (write #include "WordFinder.h") before any THIS_FILE defines if you're using MFC /* This class is intended to pick out imortant sub-strings of data from a string. It was designed for a spell-checker and so currently returns words from sentences. Its perception of what letters can make up a "word" comes from a table. This table is currently loaded with characters from dictionaries downloaded from the internet. Apostrophe (') is considered part of a word to allow "don't" and "o'clock" etc. Single letters are ignored. There are two versions of the class, one uses the Standard Template Library string class, the other uses the MFC CString class. The std::string version is the default. To use MFC CString Version comment out the line: #define UseSTLstring Usage: std::string Version: CSTLWordFinder WordFinder("S=WordFinder.GetFirstWord();"); MFC CString Version: CWordFinder WordFinder("S=WordFinder.GetFirstWord();"); ASSERT( WordFinder.GetFirstWord() =="WordFinder"); // "S" is length 1, therefore, not a "word" but a letter. ASSERT( WordFinder.GetNextWord() =="GetFirstWord"); ASSERT( WordFinder.GetNextWord() ==""); //(no next word) ASSERT( WordFinder.GetFirstWord() =="WordFinder"); ASSERT( WordFinder.GetWord() =="WordFinder"); ASSERT( WordFinder.GetWordCapitalised()=="Wordfinder"); ASSERT( *WordFinder =="WordFinder"); //You can use the class as if it were the data ASSERT( WordFinder++ =="WordFinder"); // "S" is length 1, therefore, not a "word" but a letter. ASSERT( WordFinder++ =="GetFirstWord"); ASSERT( WordFinder++ ==""); //(no next word) ASSERT( WordFinder.GetFirstWord() =="WordFinder"); ASSERT( *WordFinder =="WordFinder"); ASSERT(++WordFinder =="GetFirstWord"); ASSERT( *WordFinder =="GetFirstWord"); ASSERT(++WordFinder ==""); //(no next word) */ struct LetterType { static bool _fastcall IsLetter(BYTE Letter) { static const bool chars[]={ // 0 1 2 3 4 5 6 7 8 9 A B C D E F 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0         . . .   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 1                 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0, // 2 ! " # $ % & ' ( ) * + - . / 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 3 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 4 @ A B C D E F G H I J K L M N O 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, // 5 P Q R S T U V W X Y Z [ \ ] ^ _ 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 6 ` a b c d e f g h i j k l m n o 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, // 7 p q r s t u v w x y z { | } ~  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 8 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 9 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // A 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // B 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // C 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, // D 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // E 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, // F }; return chars[Letter]; } }; template class CWordFinderBase { protected: const char* Msg; const char* stt; const char* end; int Len; public: CWordFinderBase(const char* Message) { end=Msg=Message; Len=strlen(Msg); } virtual ~CWordFinderBase() {} T operator *() const {return GetWord();} T operator ++() {return GetNextWord();} T operator ++(int) {T S(GetWord()); GetNextWord(); return S;} T GetFirstWord() { end=Msg; return GetNextWord(); } T GetNextWord() { char c; if(Len > end-Msg) { stt=end; while(c=*stt++) { if(IsLetter(c)) { end=stt; do { if(!LetterType::IsLetter(c=*end++)) { if(end-stt==1) break; return GetWord(); } }while(c); } } } return ""; } virtual T GetWord()=0; }; struct CSTLWordFinder : public CWordFinderBase { CSTLWordFinder(const char* Message) : CWordFinderBase(Message) {} virtual ~CSTLWordFinder() {} std::string GetWord() {return std::string(stt-1).substr(0, end-stt);} std::string GetWordCapitalised() { std::string S(GetWord()); char* ptr=&S[0]; *ptr++=*ptr & ~0x20; while(char c=*ptr) *ptr++=c | 0x20; return S; } }; #ifdef _AFX // Microsoft Application Framework Classes struct CWordFinder : public CWordFinderBase { CWordFinder(const char* Message) : CWordFinderBase(Message) {} virtual ~CWordFinder() {} CString GetWord() {return CString(Msg).Mid(stt-Msg-1, end-stt);} CString GetWordCapitalised() { CString S(GetWord()); char* ptr=S.GetBuffer(0); *ptr++=*ptr & ~0x20; while(char c=*ptr) *ptr++=c | 0x20; return S; } }; #endif //def _AFX // Microsoft Application Framework Classes #endif //ndef WordFinderh