TokenIter.h

00001 #ifndef TOKENITERATOR_H
00002 #define TOKENITERATOR_H
00003 
00004 //*************************************************************
00005 //
00006 // TokenIter class header.
00007 //
00008 // TokenIter is a template class that provides an iterator 
00009 // over a string, supplying the tokens indicated by the 
00010 // function object 'Finder'.
00011 // 
00012 // Finder is a function object that finds a token in a string 
00013 // by setting the supplied start and end pointers to the start, 
00014 // and just past the end, of the first token respectively.
00015 //
00016 // Finder should have a default constructor and a function 
00017 // signature of:
00018 //
00019 //      int operator()(const char*& start, const char*& end)
00020 //
00021 // It will be called with 'end' pointing to the beginning of 
00022 // the string and should set 'start' to point to the beginning 
00023 // of the first token, 'end' to point just past the end of the 
00024 // token, and return the token length.
00025 // 
00026 // See TokenFinder.h for a canonical example of Finder.
00027 //
00028 //*************************************************************
00029 
00030 #if _MSC_VER > 1000
00031 #pragma once
00032 #endif // _MSC_VER > 1000
00033 
00034 #include <iterator>
00035 #include <string>
00036 
00037 template<class TFinder>
00038 class TokenIter : public std::iterator<std::input_iterator_tag, 
00039                                                 std::string>
00040 {
00041 public:
00042         static const char EndOfString;
00043         static const TokenIter<TFinder> EOS;
00044 
00045         TokenIter() : start(&EndOfString), end(&EndOfString), length(0)
00046         {}
00047 
00048         TokenIter(const char* str, TFinder finder = TFinder()) 
00049                 : findToken(finder), end(str) 
00050         { length = findToken(start, end); }
00051 
00052         TokenIter(const std::string str, TFinder finder = TFinder()) 
00053                 : findToken(finder), end(str.c_str()) 
00054         { length = findToken(start, end); }
00055 
00056         TokenIter& operator=(const char* const str)
00057         { end = str; length = findToken(start, end); return *this; }
00058 
00059         TokenIter& operator=(const std::string str)
00060         { 
00061                 end = str.c_str(); length = findToken(start, end); 
00062                 return *this; 
00063         }
00064 
00065         std::string operator*() const 
00066         { return std::string(start, length); }
00067 
00068         bool operator == (TokenIter rhs) const
00069         {
00070                 bool retv(false);
00071                 if (*start == *rhs.start)
00072                         if (*start == EndOfString || start == rhs.start)
00073                                 retv = true;
00074                 return retv;
00075         }
00076 
00077         bool operator != (TokenIter rhs) const 
00078         { return !operator==(rhs); }
00079 
00080         TokenIter& operator ++ () 
00081         { length = findToken(start, end); return *this; }
00082 
00083         TokenIter& operator ++ (int) 
00084         { 
00085                 TokenIter retv(*this); 
00086                 length = findToken(start, end); 
00087                 return retv; 
00088         }
00089 
00090 private:
00091         TFinder         findToken;
00092         const char* start;
00093         const char* end;
00094         size_t          length;
00095 };
00096 
00097 // Define a string terminator for comparisons
00098 
00099 template<typename TFinder>
00100 const char TokenIter<TFinder>::EndOfString = '\0';
00101 
00102 
00103 // define default iterator for end of sequence
00104 
00105 template<typename TFinder>
00106 const TokenIter<TFinder> TokenIter<TFinder>::EOS;
00107 
00108 #endif // #ifndef TOKENITERATOR_H
00109 
00110 /* End of File */

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