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

ascii_range_terminal.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_PARSERASCII_RANGE_TERMINAL_H
00021 #define GRAMMAR_TO_PARSERASCII_RANGE_TERMINAL_H
00022 
00023 #include <object_parser.h>
00024 
00025 namespace grammar_to_parser {
00026 
00031 template<typename E>
00032 class basic_ascii_range_terminal : public basic_object_parser<E,E>
00033 {
00034 typedef std::auto_ptr<E> std_auto_ptr;
00035 public:
00036     basic_ascii_range_terminal( E from_char, E to_char ) :
00037                 basic_object_parser<E,E>(),
00038                 m_from_char(from_char),
00039                 m_to_char(to_char) {};
00040     ~basic_ascii_range_terminal() {};
00041         
00050     unsigned long parse( const E *buf, const unsigned long buf_length );
00051         std::basic_istream<E>& parse( std::basic_istream<E>& is );
00052         
00056     std::basic_ostream<E>& format( std::basic_ostream<E>& os );
00057 private:
00058         E                       m_from_char;
00059         E                       m_to_char;
00060 };
00061 
00062 
00063 template< typename E >
00064 unsigned long basic_ascii_range_terminal<E>::parse( const E *buf, 
00065                                                                                 const unsigned long buf_length )
00066 {
00067         prepare_for_parsing();
00068 
00069         if( (*buf) >= m_from_char && (*buf) <= m_to_char )
00070         {
00071                 std_auto_ptr::operator = ( std_auto_ptr( new E(*buf) ) );
00072                 m_parsed_size = 1;
00073                 m_is_parsed = true;
00074         }
00075         return m_parsed_size;
00076 }
00077 
00078 template< typename E >
00079 std::basic_istream<E>& basic_ascii_range_terminal<E>::parse( 
00080                                                                                         std::basic_istream<E>& is )
00081 {
00082         prepare_for_parsing();
00083         if( !is.good() )
00084         {
00085                 return is;
00086         }
00087         E input = m_from_char;
00088         std::streampos str_pos = is.tellg();
00089 
00090         is.read( &input, 1 );
00091 
00092         if( is.good() && input >= m_from_char && input <= m_to_char )
00093         {
00094                 m_parsed_size = 1;
00095                 m_is_parsed = true;
00096                 get_valid() = input;
00097         }
00098         else 
00099         {
00100                 is.clear();
00101                 is.seekg( str_pos );
00102         }
00103         return is;
00104 }
00105 
00106 template< typename E >
00107 std::basic_ostream<E>& basic_ascii_range_terminal<E>::format(
00108                                                                                          std::basic_ostream<E>& os )
00109 {
00110         prepare_for_formatting();
00111 
00112         std::streampos strPos = os.tellp();
00113         if( (std::streamoff)strPos < 0 ) strPos = 0;
00114 
00115         int out = get_valid();
00116         if( os.good() ) 
00117         {
00118                 os.write( (const E*)&out,sizeof(E) );
00119                 m_formatted_size = sizeof(E);
00120                 m_is_formatted = true;
00121         }
00122         else 
00123         {
00124                 os.clear();
00125                 os.seekp( strPos );
00126         }
00127         return os;
00128 }
00129 
00130 typedef basic_ascii_range_terminal<char>        ascii_range_terminal;
00131 typedef basic_ascii_range_terminal<wchar_t>     wascii_range_terminal;
00132 };
00133 
00134 #endif

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