contrib/mul/mbl/mbl_file_data_wrapper.txx
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_file_data_wrapper.txx
00002 #ifndef mbl_file_data_wrapper_txx_
00003 #define mbl_file_data_wrapper_txx_
00004 //:
00005 // \file
00006 
00007 #include "mbl_file_data_wrapper.h"
00008 
00009 #include <vcl_ios.h>
00010 #include <vcl_iostream.h>
00011 #include <vcl_cstdlib.h>
00012 
00013 // constructor
00014 template<class T>
00015 mbl_file_data_wrapper<T>::mbl_file_data_wrapper(vcl_string path)
00016 : bfs_(path)
00017 {
00018   path_ = path;
00019   if (!bfs_)
00020   {
00021     vcl_cerr<<"ERROR: mbl_file_data_wrapper::constructor\n"
00022             <<"file stream failed\n";
00023     vcl_abort();
00024   }
00025   
00026   calc_data_size();
00027   
00028   if ( size() > 0 )
00029     reset();
00030 }
00031 
00032 //: Count number of items in file
00033 template<class T>
00034 void mbl_file_data_wrapper<T>::calc_data_size()
00035 {
00036   unsigned long count=0;
00037 
00038   short version;
00039   if (!bfs_)
00040   {
00041      vcl_cerr<<"ERROR: mbl_file_data_wrapper::calc_data_size()\n"
00042              <<"file stream failed\n";
00043      vcl_abort();
00044   }
00045 
00046 
00047   //vcl_cout<<"count= "<<count<<vcl_endl;
00048   vsl_b_read(bfs_,version);
00049   T d;
00050   bool is_last;
00051   vsl_b_read(bfs_,is_last);
00052   while (!is_last)
00053   {
00054     vsl_b_read(bfs_,d);
00055     count++;
00056     //vcl_cout<<"count= "<<count<<vcl_endl;
00057     vsl_b_read(bfs_,is_last);
00058 #if 0
00059     vcl_cout << " ***Results1 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
00060              << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl
00061              << " ***Results2 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
00062              << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl;
00063 #endif
00064   }
00065 
00066   size_=count;
00067 }
00068 
00069 
00070 //: Default destructor
00071 template<class T>
00072 mbl_file_data_wrapper<T>::~mbl_file_data_wrapper()
00073 {
00074 }
00075 
00076 //: return number of items in file
00077 template<class T>
00078 unsigned long mbl_file_data_wrapper<T>::size() const
00079 {
00080   return size_;
00081 }
00082 
00083 
00084 //: Reset
00085 template<class T>
00086 void mbl_file_data_wrapper<T>::reset()
00087 {
00088   bfs_.is().seekg(vsl_b_ostream::header_length, vcl_ios_beg);
00089 #if 0
00090   vcl_cout << " ***Results3 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
00091            << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl
00092            << " ***Results4 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
00093            << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl;
00094 #endif
00095 
00096   if (!bfs_)
00097   {
00098     vcl_cerr<<"ERROR: mbl_file_data_wrapper::reset()\n"
00099             <<"file stream failed\n";
00100     vcl_abort();
00101   }
00102 
00103   short version;
00104   vsl_b_read(bfs_,version);
00105   bool is_last;
00106   vsl_b_read(bfs_,is_last);
00107   if (is_last)
00108   {
00109     vcl_cerr<<"ERROR: mbl_file_data_wrapper::reset()\n"
00110             <<"appears to be no data in file\n";
00111     vcl_abort();
00112   }
00113   else
00114    vsl_b_read(bfs_,d_);
00115 
00116   index_=0;
00117 }
00118 
00119 //: Return current object
00120 template<class T>
00121 const T& mbl_file_data_wrapper<T>::current()
00122 {
00123   // return current value
00124   return d_;
00125 }
00126 
00127 //: Read in next object
00128 template<class T>
00129 bool mbl_file_data_wrapper<T>::next()
00130 {
00131   if (!bfs_)
00132   {
00133     vcl_cerr<<"ERROR: mbl_file_data_wrapper::next()\n"
00134             <<"file stream failed\n";
00135     vcl_abort();
00136   }
00137 
00138   bool is_last;
00139   vsl_b_read(bfs_,is_last);
00140   if (is_last)
00141   {
00142     reset();
00143     //vcl_cout<<"WARNING: Reached end of file, so wrapper has been reset!\n";
00144     return false;
00145   }
00146   else
00147   {
00148     vsl_b_read(bfs_,d_);
00149     index_++;
00150     return true;
00151   }
00152 }
00153 
00154 //: Return current index
00155 //  First example has index 0
00156 template<class T>
00157 unsigned long mbl_file_data_wrapper<T>::index() const
00158 {
00159   return index_;
00160 }
00161 
00162 
00163 #if 0
00164 
00165 // an acceptable implementation is defined in mbl_data_wrapper base class!
00166 // maybe just use that??
00167 
00168 //: Move to element n
00169 //  First example has index 0
00170 template<class T>
00171 void mbl_file_data_wrapper<T>::set_index(unsigned long n)
00172 {
00173   // unsupported
00174   vcl_cout<<"mbl_file_data_wrapper<T>::set_index unsupported\n";
00175 }
00176 
00177 #endif
00178 
00179 
00180 //: Create copy on heap and return base pointer
00181 template<class T>
00182 mbl_data_wrapper< T >* mbl_file_data_wrapper<T>::clone() const
00183 {
00184   return new mbl_file_data_wrapper<T>(*this);
00185 }
00186 
00187 template <class T>
00188 bool mbl_file_data_wrapper<T>::is_class(vcl_string const& s) const
00189 {
00190   return s==is_a(); // no ref to parent's is_class() since that is pure virtual
00191 }
00192 
00193 
00194 #define MBL_FILE_DATA_WRAPPER_INSTANTIATE(T) \
00195 VCL_DEFINE_SPECIALIZATION vcl_string mbl_file_data_wrapper<T >::is_a() const \
00196 { return vcl_string("mbl_file_data_wrapper<" #T ">"); } \
00197 template class mbl_file_data_wrapper<T >
00198 
00199 #endif // mbl_file_data_wrapper_txx_