Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

white_space_parser.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Radko Mihal                                *
00003  *   rmihal@pobox.sk                                                       *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program 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         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
00019  ***************************************************************************/
00020 #ifndef GRAMMAR_TO_PARSERWHITE_SPACE_PARSER_H
00021 #define GRAMMAR_TO_PARSERWHITE_SPACE_PARSER_H
00022 
00023 #include "default_type_parser.h"
00024 
00025 namespace grammar_to_parser {
00026 
00031 template< typename E >
00032 class basic_white_space_parser : 
00033         public basic_default_type_parser< E,std::basic_string<E> >
00034 {
00035 typedef std::basic_string<E> std_string;
00036 typedef std::basic_istream<E> std_istream;
00037 typedef std::ctype<E> std_ctype;
00038 typedef std::char_traits<E> std_char_traits;
00039 public:
00040     basic_white_space_parser() : 
00041                 basic_default_type_parser< E,std::basic_string<E> >() {};
00042         
00043         basic_white_space_parser( const basic_white_space_parser &rhs ) :
00044                 basic_default_type_parser< E,std::basic_string<E> >(rhs) {};
00045         
00046         basic_white_space_parser( const std::basic_string<E> &space ) :
00047                 basic_default_type_parser< E,std::basic_string<E> >(space) {};
00048     
00049         virtual ~basic_white_space_parser() {};
00050     virtual unsigned long parse( const E *buf, const unsigned long buf_length )
00051         {
00052                 prepare_for_parsing();
00053         
00054                 typename std_string::iterator it = get_valid().begin();
00055                 
00056                 while( m_parsed_size < buf_length && is_space( buf[m_parsed_size] ) )
00057                 {
00058                         m_parsed_size++;
00059                         get_valid() += buf[m_parsed_size];
00060                 }
00061                 m_is_parsed = true;
00062                 return m_parsed_size;
00063         }
00064 
00065         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is )
00066         {
00067                 prepare_for_parsing();
00068         
00069                 typename std_string::iterator it = get_valid().begin();
00070                 std::streampos strPos = is.tellg();
00071                 E input = 0;
00072         
00073                 while( is.good() && is_space( is ) )
00074                 {
00075                         is.read( &input, 1 );
00076                         m_parsed_size++;
00077                         get_valid() += input;
00078                         strPos = is.tellg();
00079                 }
00080                 m_is_parsed = true;
00081                 is.clear();
00082                 is.seekg( strPos );
00083                 return is;
00084         }
00085 protected:
00086         bool is_space( E input )
00087         {
00088                 const std::locale loc;
00089                 const std_ctype& fac = std::use_facet( loc, (std::ctype<E>*)0, true );
00090                 return fac.is( std_ctype::space, input );
00091         }
00092         bool is_space( std::basic_istream<E>& is )
00093         {
00094                 E input = 0;
00095                 is.read( &input, 1 );
00096                 if( is.good() )
00097                 {
00098                         const std::locale loc;
00099                         const std_ctype& fac = std::use_facet( loc, (std::ctype<E>*)0, true );
00100                         return fac.is( std_ctype::space, input );
00101                 }
00102                 return false;
00103         }
00104 };
00105 
00106 typedef basic_white_space_parser<char> white_space_parser;
00107 typedef basic_white_space_parser<wchar_t> wwhite_space_parser;
00108 
00109 };
00110 
00111 #endif

Generated on Sun Jul 2 18:39:43 2006 for grammar2parser.kdevelop by  doxygen 1.4.1