StreamTokenizer.h

00001 #ifndef gridripper_lang_StreamTokenizer_h
00002 #define gridripper_lang_StreamTokenizer_h
00003 
00004 #include <string>
00005 #include <iostream>
00006  
00007 namespace gridripper {
00008 
00009 using namespace std;
00010 
00018 class StreamTokenizer
00019 {
00020 private:
00021     bool isMine;
00022     istream* is;
00023     string currentLine;
00024     string::size_type index;
00025     string spaceChars;
00026     string specialChars;
00027     string nonWordChars;
00028     char commentChar;
00029     int lineNumber;
00030 
00031 public:
00032     StreamTokenizer(istream& is);
00033     StreamTokenizer(string s);
00034     ~StreamTokenizer();
00035 
00036     string getSpecialChars() { return specialChars; }
00037 
00038     void setSpecialChars(string s) {
00039         specialChars = s;
00040         nonWordChars = s + " \t\n\r";
00041     }
00042 
00043     void setCommentChar(char c) { commentChar = c; }
00044 
00046     string next();
00047 
00049     int getLineNumber() const { return lineNumber; }
00050 
00051     static int countTokens(const string& s);
00052 
00053     static int countTokens(const string& s, const string& spec);
00054 
00055 private:
00057     string nextWithEols();
00058 
00059     bool isSpecialChar(char c) {
00060         return specialChars.find(c) != string::npos;
00061     }
00062     bool isWordChar(char c) {
00063         return nonWordChars.find(c) == string::npos;
00064     }
00065 };
00066 
00067 } // namespace gridripper
00068 
00069 #endif /* gridripper_lang_StreamTokenizer_h */