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

value_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_PARSERVALUE_PARSER_H
00021 #define GRAMMAR_TO_PARSERVALUE_PARSER_H
00022 
00023 #include <iostream>
00024 #include "default_type_parser.h"
00025 
00026 namespace grammar_to_parser {
00027 
00028 const char* VALUE_PARSER = "ValueParser";
00035 template< typename E, typename A >
00036 class basic_value_parser : public basic_default_type_parser<E,A>
00037 {
00038 public:
00039     basic_value_parser();
00040 
00041     ~basic_value_parser();
00042     virtual unsigned long parse( const E* buf, const unsigned long buf_length );
00043         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is );
00044 
00045 };
00046 
00047 template< typename E, typename A >
00048 basic_value_parser<E,A>::basic_value_parser() : 
00049         basic_default_type_parser<E,A>(VALUE_PARSER)
00050 {
00051 }
00052 
00053 template< typename E, typename A >
00054 basic_value_parser<E,A>::~basic_value_parser()
00055 {
00056 }
00057 
00058 template< typename E, typename A >
00059 unsigned long basic_value_parser<E,A>::parse( const E* buf, 
00060                                                                         const unsigned long buf_length )
00061 {
00062         prepare_for_parsing();
00063         
00064         std::auto_ptr< std::basic_stringbuf<E> > strbufptr(
00065                         new std::basic_stringbuf<E>( std::basic_string<E>( (const E*)buf, buf_length ) ) );
00066         std::basic_istream<E> is(strbufptr.get());
00067 
00068         std::streampos begin = is.tellg();
00069         is >> get_valid();
00070         if( !(m_is_parsed = ( !is.fail() && 
00071                 ( m_parsed_size = is.tellg() - begin ) > 0 ) ) )
00072         {
00073                 m_parsed_size = 0;
00074         }
00075         return m_parsed_size;
00076 }
00077 
00078 template< typename E, typename A >
00079 std::basic_istream<E>& basic_value_parser<E,A>::parse( std::basic_istream<E>& is )
00080 {
00081         prepare_for_parsing();
00082         
00083         std::streampos strPos = is.tellg();
00084         is >> get_valid();
00085 
00086         m_parsed_size = (std::streamoff)is.tellg() - (std::streamoff)strPos;
00087         m_is_parsed = !is.fail() && m_parsed_size > 0; 
00088 
00089         if( !m_is_parsed )
00090         {
00091                 is.clear();
00092                 is.seekg( strPos );
00093                 m_parsed_size = 0;
00094         }
00095         return is;
00096         
00097 }
00098 
00102 template< typename A >
00103 class value_parser : public basic_value_parser<char,A>
00104 {
00105 public:
00106     value_parser() : basic_value_parser<char,A>() {};
00107     ~value_parser () {};
00108 };
00109 
00113 template< typename A >
00114 class wvalue_parser : public basic_value_parser<wchar_t,A>
00115 {
00116 public:
00117     wvalue_parser() : basic_value_parser<wchar_t,A>() {};
00118     ~wvalue_parser () {};
00119 };
00120 
00121 };
00122 
00123 #endif

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