TokenFinder.h

00001 #ifndef TOKENFINDER_H
00002 #define TOKENFINDER_H
00003 
00004 //***************************************************************************
00005 //
00006 // TokenFinder class header.
00007 //
00008 // TokenFinder is a function object type that finds a token in a string, given 
00009 // a delimiting string. It sets the start pointer to the beginning of the token,
00010 // and the end pointer to one character past the end of the token.
00011 //
00012 //***************************************************************************
00013 
00014 #if _MSC_VER > 1000
00015 #pragma once
00016 #endif // _MSC_VER > 1000
00017 
00018 #include <string> 
00019 
00020 class TokenFinder
00021 {
00022 public:
00023         TokenFinder() {}                // Default ctor - delimiter is end-of-string
00024         TokenFinder(const char* delim) : delimiter(delim) {}
00025 
00026         int operator()(const char*& start, const char*& end)
00027         {
00028                 // end initially points to beginning of string
00029                 char* dPos = 0;
00030 
00031                 if (!delimiter.empty())
00032                 {
00033                         // Skip leading delimiters to start of token 
00034                         dPos = strstr(end, delimiter.c_str());
00035 
00036                         while (dPos == end)
00037                         {
00038                                 end += delimiter.size();
00039                                 dPos = strstr(end, delimiter.c_str());
00040                         }
00041                 }
00042 
00043                 start = end;            // start of token found
00044 
00045                 // Set end to just past end of token
00046                 end = (dPos) ? dPos : end + strlen(start);
00047 
00048                 // Return token length
00049                 return (end - start);
00050         }
00051 
00052 private:
00053         std::string delimiter;
00054 };
00055 
00056 #endif // #ifndef TOKENFINDER_H
00057 
00058 
00059 /* End of File */

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3