00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00023
00030
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
00047 static const size_t item_count = 1024*16;
00048
00049
00050 static const size_t max_buffer_size = item_count + 1024;
00051
00052
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
00141 universe_.first.insert_stay( line_buffer );
00142 universe_.second.go_eof();
00143 assert( universe_.length() );
00144
00145 } else {
00146
00147 assert( feof( file_stream ) );
00148 fclose( file_stream );
00149 file_stream = NULL;
00150 }
00151 }
00152
00153 return universe_.first.offset();
00154 }
00155 };
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
00219 universe_.first.insert_stay( this->line_buffer );
00220 universe_.second.go_eof();
00221 assert( universe_.length() );
00222
00223 } else {
00224
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 };
00235
00236
00237 #endif //_XML_STREAM_POLICY_H_INCLUDED_