src/Utils/StreamTokenizer.h

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2005 Norman Casagrande
00003 * For informations write to nova77@gmail.com
00004 *
00005 * This library is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU Lesser General Public
00007 * License as published by the Free Software Foundation; either
00008 * version 2.1 of the License, or (at your option) any later version.
00009 *
00010 * This library is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 * Lesser General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU Lesser General Public
00016 * License along with this library; if not, write to the Free Software
00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 *
00019 */
00020 
00025 #ifndef __STREAM_TOKENIZER_H
00026 #define __STREAM_TOKENIZER_H
00027 
00028 #include <string>
00029 #include <iterator>
00030 
00031 using namespace std;
00032 
00035 
00036 namespace nor_utils {
00037 
00044 class StreamTokenizer
00045 {
00046    typedef istream_iterator<char> sIt; 
00047 
00048 public:
00049 
00057    StreamTokenizer(istream& is, const string delim = " \t")
00058       : p(is), end_of_stream(), delimiters(delim) 
00059    { is.unsetf(std::ios::skipws); }
00060 
00066    string next_token()
00067    {
00068       string result;
00069       char ch;
00070       insert_iterator<string> iIt(result, result.begin());
00071 
00072       if ( p != end_of_stream)
00073       {
00074          // move until the delimiter
00075          while ( p != end_of_stream && is_delimiter(*p) )
00076          {
00077             ch = *p;
00078             p++;
00079          }
00080 
00081          // move until the next delimiter, and build
00082          // the string.
00083          while ( p != end_of_stream && !is_delimiter(*p) )
00084          {
00085             ch = *p;
00086             *iIt++ = *p++;
00087          }
00088       }
00089 
00090       return result;  
00091    }
00092 
00107    bool has_token()
00108    { return (! (p == end_of_stream) ); }
00109 
00110 private:
00111 
00112    sIt p; 
00113    sIt end_of_stream; 
00114 
00115    const string delimiters; 
00116 
00123    bool is_delimiter(char c)
00124    { 
00125       return delimiters.find(c) != string::npos; 
00126    }
00127 };
00128 
00129 } // end of namespace nor_utils
00130 
00131 #endif //__STREAM_TOKENIZER_H

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5