00001
00002
00003
00004
00005
00006
00007 #ifndef MCFSBIFSTREAM_H
00008 #define MCFSBIFSTREAM_H
00009
00010 #ifdef USE_STD
00011 #include <iostream>
00012 #include <fstream>
00013 #include <string>
00014 using namespace std;
00015 #else
00016 #include <iostream.h>
00017 #include <fstream.h>
00018 #include <string.h>
00019 #endif
00020
00021 #include "MCFsbistream.h"
00022
00023 template < class Rtmra_t >
00024 class MCFsbifstream : public MCFsbistream<Rtmra_t> {
00025 public:
00026 MCFsbifstream( const string& = "_\0" );
00027 ~MCFsbifstream();
00028 void filenamebase( const string& );
00029 bool isopen();
00030 void open();
00031 void close();
00032 void initmfilepos();
00033 void findmpacket( MCFmpBase&, const char mcode, const int n );
00034 private:
00035 string _filenamebase;
00036 string _filenamesb;
00037 int* _mfilepos;
00038 };
00039
00040 template < class Rtmra_t >
00041 MCFsbifstream<Rtmra_t>::MCFsbifstream( const string& filenamebase )
00042 : MCFsbistream<Rtmra_t>( *( new ifstream ) )
00043 {
00044 _filenamebase = filenamebase;
00045 _mfilepos = new int[256];
00046 };
00047
00048 template < class Rtmra_t >
00049 MCFsbifstream<Rtmra_t>::~MCFsbifstream()
00050 {
00051 if ( isopen() ) close();
00052 if ( _mfilepos != 0 ) { delete [] _mfilepos; _mfilepos = 0; }
00053 };
00054
00055 template < class Rtmra_t >
00056 void MCFsbifstream<Rtmra_t>::filenamebase( const string& filenamebase )
00057 {
00058 _filenamebase = filenamebase;
00059 };
00060
00061 template < class Rtmra_t >
00062 bool MCFsbifstream<Rtmra_t>::isopen()
00063 {
00064 return (*static_cast<ifstream*>(_is)).is_open();
00065 }
00066
00067 template < class Rtmra_t >
00068 void MCFsbifstream<Rtmra_t>::open()
00069 {
00070 if ( isopen() ) close();
00071 mcfsbfilename( _sbn, _filenamebase, _filenamesb );
00072 cout << "attempting open " << _filenamesb << endl;
00073 (*static_cast<ifstream*>(_is)).open( _filenamesb.c_str() );
00074 cout << "attempt open " << _filenamesb << endl;
00075 if ( !(static_cast<ifstream*>(_is)->good()) ) {
00076 _openfail = true;
00077 } else {
00078 _openfail = false;
00079 MCFsbistream<Rtmra_t>::open();
00080 initmfilepos();
00081
00082 report();
00083
00084 }
00085 };
00086
00087 template < class Rtmra_t >
00088 void MCFsbifstream<Rtmra_t>::close()
00089 {
00090 (*static_cast<ifstream*>(_is)).close();
00091 }
00092
00093 template < class Rtmra_t >
00094 void MCFsbifstream<Rtmra_t>::initmfilepos()
00095 {
00096 for( int i = 0; i < 256; i++ ) { _mfilepos[i] = (*_is).tellg(); }
00097 #ifdef STORM_DEBUG
00098 cout << " initial seek pos " << _mfilepos[0] << '\n';
00099 #endif
00100 };
00101
00102 template < class Rtmra_t >
00103 void MCFsbifstream<Rtmra_t>::findmpacket(
00104 MCFmpBase& mp, const char mcode, const int n
00105 )
00106 {
00107 bool found = false;
00108 bool missing = false;
00109 bool eof = false;
00110 int nextfilepos;
00111 bytepack bp;
00112
00113 #ifdef STORM_DEBUG
00114 cout << "MCFsbifstream.findmpacket(): SEARCHING FOR : mcode "
00115 << int( (unsigned char)(mcode) ) << '\n';
00116 #endif
00117
00118 mp.clear();
00119 (*_is).seekg( _mfilepos[ int( (unsigned char)(mcode) ) ], ios::beg );
00120 int i0 = (*_is).tellg();
00121
00122 while ( (*_is).good() & !found & !missing & !eof ) {
00123 nextfilepos = (*_is).tellg();
00124 (*_is) >> mp;
00125 #ifdef STORM_DEBUG
00126 cout << "MCFsbifstream.findmpacket(): mp.mcode() mp.empty() "
00127 << int( (unsigned char)(mp.mcode()) ) << ' ' << mp.empty() << '\n';
00128 #endif
00129
00130 if ( mp.null() ) {
00131 (*_is) >> bp( 0, 4 );
00132 (*_is) >> bp( 0, 4 );
00133 (*_is) >> bp( 0, _bplen_n ); _finalstep = bp;
00134 #ifdef STORM_DEBUG
00135 cout << "FINALSTEP " << _finalstep << '\n';
00136 #endif
00137 eof = true;
00138 missing=true;
00139 mp.set( mcode, n );
00140 mp.clear();
00141 }
00142 else {
00143 if ( ( mp.mcode() == mcode ) && ( mp.n() == n ) ) {
00144 found=true;
00145 }
00146 if ( ( mp.mcode() == mcode ) && ( mp.n() > n ) ) {
00147 missing=true;
00148
00149 mp.clear();
00150 }
00151 }
00152 }
00153
00154 int i1=(*_is).tellg();
00155 if (found) {
00156 _mfilepos[ int( (unsigned char)(mcode) ) ] = (*_is).tellg();
00157 }
00158 else {
00159 _mfilepos[ int( (unsigned char)(mcode) ) ] = nextfilepos;
00160 }
00161
00162 #ifdef STORM_DEBUG
00163 cout << "MCFsbifstream.findmpacket(): mcode _result_ _mfilepos[mcode] "
00164 << int( (unsigned char)(mcode) );
00165 if (found) cout << " FOUND ";
00166 if (missing) cout << " MISSING ";
00167 if (eof) cout << " EOF ";
00168 cout << _mfilepos[ int( (unsigned char)(mcode) ) ] << ' ' << n << '\n';
00169 #endif
00170 };
00171
00172 #endif
00173