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

xml_stream_policy.h

Go to the documentation of this file.
00001 
00002 // Common Text Transformation Library
00003 // Copyright (C) 1997-2006 by Igor Kholodov. 
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
00017 // Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 // mailto:cttl@users.sourceforge.net
00021 // http://sourceforge.net/projects/cttl/
00023 
00030 // xml_stream_policy.h
00031 
00032 #ifndef _XML_STREAM_POLICY_H_INCLUDED_
00033 #define _XML_STREAM_POLICY_H_INCLUDED_
00034 
00043 class policy_strict_stream : public policy_default
00044 {
00045 public:
00046     // how many bytes to read: anywhere from 1 byte and up...
00047     static const size_t item_count = 1024*16;
00048 
00049     // input buffer size
00050     static const size_t max_buffer_size = item_count + 1024;
00051 
00052     // read items of 1 byte in size
00053     static const size_t item_size = 1;
00054 
00055 protected:
00057     char line_buffer[ max_buffer_size ];
00058 
00064     FILE *file_stream;
00065 
00070     edge<>& consumed_data;
00071 
00072 public:
00074     policy_strict_stream( edge<>& consumed_data_ )
00075         :
00076         file_stream( NULL ),
00077         consumed_data( consumed_data_ )
00078     {
00079     }
00080 
00082     void operator=( policy_strict_stream const& ) const
00083     {
00084     }
00085 
00086 public:
00094     bool init( char* file_ )
00095     {
00096         assert( file_stream == NULL );
00097         return( ( file_stream = fopen( file_, "r" ) ) != NULL );
00098     }
00099 
00126     template< typename UniverseT >
00127     size_t match( UniverseT& universe_ )
00128     {
00129         if ( file_stream && !universe_.length() ) {
00130             size_t numread = 0;
00131             if ( ( numread = fread( line_buffer, item_size, item_count, file_stream ) ) ) {
00132                 line_buffer[ numread ] = 0x00;
00133 
00134 #ifdef DISPOSE_CONSUMED_DATA
00135                 assert( !consumed_data.first.offset() );
00136                 if ( consumed_data.length() )
00137                     consumed_data.text( "" );
00138 #endif // DISPOSE_CONSUMED_DATA
00139 
00140                 // inject new data into the universe
00141                 universe_.first.insert_stay( line_buffer );
00142                 universe_.second.go_eof();
00143                 assert( universe_.length() );
00144 
00145             } else {
00146                 // feof returns 0 if the current position is not end of file
00147                 assert( feof( file_stream ) );
00148                 fclose( file_stream );
00149                 file_stream = NULL;
00150             }
00151         }
00152 
00153         return universe_.first.offset();
00154     }
00155 };  // struct policy_strict_stream
00156 
00157 
00166 class policy_relaxed_stream : public policy_strict_stream
00167 {
00168 public:
00170     policy_relaxed_stream( edge<>& consumed_data_ )
00171         :
00172         policy_strict_stream( consumed_data_ )
00173     {
00174     }
00175 
00202     template< typename UniverseT >
00203     size_t match( UniverseT& universe_ )
00204     {
00205         ( true ^ ( entity( isspace ) | true ) ).match( universe_ );
00206 
00207         if ( this->file_stream && !universe_.length() ) {
00208             size_t numread = 0;
00209             if ( ( numread = fread( this->line_buffer, this->item_size, this->item_count, this->file_stream ) ) ) {
00210                 this->line_buffer[ numread ] = 0x00;
00211 
00212 #ifdef DISPOSE_CONSUMED_DATA
00213                 assert( !consumed_data.first.offset() );
00214                 if ( consumed_data.length() )
00215                     consumed_data.text( "" );
00216 #endif // DISPOSE_CONSUMED_DATA
00217 
00218                 // inject new data into the universe
00219                 universe_.first.insert_stay( this->line_buffer );
00220                 universe_.second.go_eof();
00221                 assert( universe_.length() );
00222 
00223             } else {
00224                 // feof returns 0 if the current position is not end of file
00225                 assert( feof( file_stream ) );
00226                 fclose( this->file_stream );
00227                 this->file_stream = NULL;
00228             }
00229             return match( universe_ );
00230         }
00231 
00232         return universe_.first.offset();
00233     }
00234 };  // struct policy_relaxed_stream
00235 
00236 
00237 #endif //_XML_STREAM_POLICY_H_INCLUDED_

Generated on Thu Nov 2 17:43:27 2006 for CTTL XML stream parser sample by  doxygen 1.3.9.1