Main Page | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals

DCFsbifstream.h

Go to the documentation of this file.
00001 // DCFsbifstream.h
00002 //////////////////////////////////////////////////////////////////////
00003 //          (c) Copyright 2001-2002 Brown Deer Technology, LLC.
00004 //                        All rights reserved.
00005 //////////////////////////////////////////////////////////////////////
00006 
00007 #ifndef DCFSBIFSTREAM_H
00008 #define DCFSBIFSTREAM_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 "DCFsbistream.h"
00022 
00023 template < class Rtmra_t >
00024 class DCFsbifstream : public DCFsbistream<Rtmra_t> {
00025    public:
00026       DCFsbifstream( const string& = "_\0" );
00027       ~DCFsbifstream();
00028       void filenamebase( const string& );
00029       bool isopen();
00030       void open();
00031       void close();
00032       void initmfilepos();
00033       void findmpacket( DCFmpBase&,  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 DCFsbifstream<Rtmra_t>::DCFsbifstream( const string& filenamebase ) 
00042   : DCFsbistream<Rtmra_t>( *( new ifstream ) ) 
00043 {
00044     _filenamebase = filenamebase;
00045     _mfilepos = new int[256];
00046 };
00047 
00048 template < class Rtmra_t >
00049 DCFsbifstream<Rtmra_t>::~DCFsbifstream() 
00050 {
00051    if ( isopen() ) close();
00052    if ( _mfilepos != 0 ) { delete [] _mfilepos; _mfilepos = 0; }
00053 };
00054 
00055 template < class Rtmra_t >
00056 void DCFsbifstream<Rtmra_t>::filenamebase( const string& filenamebase ) 
00057 {
00058     _filenamebase = filenamebase;
00059 };
00060 
00061 template < class Rtmra_t >
00062 bool DCFsbifstream<Rtmra_t>::isopen() 
00063 { 
00064    return (*static_cast<ifstream*>(_is)).is_open(); 
00065 }
00066 
00067 template < class Rtmra_t >
00068 void DCFsbifstream<Rtmra_t>::open() 
00069 {
00070    if ( isopen() ) close();
00071    dcfsbfilename( _sbn, _filenamebase, _filenamesb );
00072    (*static_cast<ifstream*>(_is)).open( _filenamesb.c_str() );
00073    DCFsbistream<Rtmra_t>::open();
00074    initmfilepos();
00075 //#ifdef STORM_DEBUG
00076    report();
00077 //#endif
00078 };
00079 
00080 template < class Rtmra_t >
00081 void DCFsbifstream<Rtmra_t>::close()
00082 { 
00083    (*static_cast<ifstream*>(_is)).close(); 
00084 }
00085 
00086 template < class Rtmra_t >
00087 void DCFsbifstream<Rtmra_t>::initmfilepos() 
00088 {
00089    for( int i = 0; i < 256; i++ ) { _mfilepos[i] = (*_is).tellg(); }
00090 #ifdef STORM_DEBUG
00091    cout << " initial seek pos " << _mfilepos[0] << '\n';
00092 #endif
00093 };
00094 
00095 template < class Rtmra_t >
00096 void DCFsbifstream<Rtmra_t>::findmpacket( 
00097   DCFmpBase& mp, const char mcode, const int n
00098 ) 
00099 {
00100    bool found = false;
00101    bool missing = false;
00102    bool eof = false;
00103    int nextfilepos;
00104    bytepack bp;
00105 
00106 #ifdef STORM_DEBUG
00107    cout << "DCFsbifstream.findmpacket(): SEARCHING FOR : mcode "
00108         << int( (unsigned char)(mcode) ) << '\n';
00109 #endif
00110 
00111    mp.clear();
00112    (*_is).seekg( _mfilepos[ int( (unsigned char)(mcode) ) ], ios::beg );
00113    int i0 = (*_is).tellg();
00114 
00115    while ( (*_is).good() & !found & !missing & !eof ) {
00116       nextfilepos = (*_is).tellg();
00117       (*_is) >> mp; 
00118 #ifdef STORM_DEBUG
00119       cout << "DCFsbifstream.findmpacket(): mp.mcode() mp.empty() "
00120            << int( (unsigned char)(mp.mcode()) ) << ' ' << mp.empty() << '\n';
00121 #endif
00122 //      if ( mp.empty() ) { 
00123       if ( mp.null() ) { 
00124          (*_is) >> bp( 0, 4 ); 
00125          (*_is) >> bp( 0, 4 );
00126          (*_is) >> bp( 0, _bplen_n ); _finalstep = bp;
00127 #ifdef STORM_DEBUG
00128          cout << "FINALSTEP " << _finalstep << '\n';
00129 #endif
00130          eof = true; 
00131       }
00132       else {
00133          if ( ( mp.mcode() == mcode ) && ( mp.n() == n ) ) {
00134             found=true; 
00135          }
00136          if ( ( mp.mcode() == mcode ) && ( mp.n() > n ) ) {
00137             missing=true; 
00138 //            mp.reset();       HACK ?
00139             mp.clear(); 
00140          }
00141       }
00142    }
00143 
00144    int i1=(*_is).tellg();
00145    if (found) {
00146       _mfilepos[ int( (unsigned char)(mcode) ) ] = (*_is).tellg();
00147    }
00148    else {
00149       _mfilepos[ int( (unsigned char)(mcode) ) ] = nextfilepos;
00150    }
00151 
00152 #ifdef STORM_DEBUG
00153    cout << "DCFsbifstream.findmpacket(): mcode _result_ _mfilepos[mcode] "
00154         << int( (unsigned char)(mcode) );
00155    if (found)  cout << " FOUND ";
00156    if (missing) cout << " MISSING ";
00157    if (eof) cout << " EOF ";
00158    cout << _mfilepos[ int( (unsigned char)(mcode) ) ] << ' ' << n << '\n';
00159 #endif
00160 };
00161 
00162 #endif
00163 

Generated on Mon May 31 21:38:44 2004 for SR2k4 Assembler by doxygen 1.3.6