#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/IO/BinFileIO.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_DPBINIO_HEADER #define RAVL_DPBINIO_HEADER 1 ////////////////////////////////////////////////////////// //! docentry="Ravl.Core.Data Processing.IO" //! example=exDataProc.cc //! lib=RavlIO //! author="Charles Galambos" //! date="04/07/1998" //! rcsid="$Id: BinFileIO.hh,v 1.3 2002/06/25 15:53:02 craftit Exp $" //! file="Ravl/Core/IO/BinFileIO.hh" //! userlevel=Default #include "Ravl/DP/Port.hh" #include "Ravl/String.hh" #include "Ravl/TypeName.hh" #include "Ravl/BinStream.hh" namespace RavlN { static const UInt16T RAVLBinaryID = 0x4143; // AB Ravl Binary file id. ///////////////////////////////////////////// //! userlevel=Develop //: Save objects in binary to a file. // Object must have a binary stream output function. template class DPOBinFileBodyC : public DPOPortBodyC { public: DPOBinFileBodyC() {} //: Default constructor. DPOBinFileBodyC(const StringC &nfname,bool useHeader=false) : out(nfname), version(0) { #ifdef RAVL_CHECK if(!out.Stream().good()) cerr << "DPOBinFileBodyC::DPOBinFileBodyC(StringC,bool), Failed to open file.\n"; #endif if(useHeader) out << RavlN::RAVLBinaryID << TypeName(typeid(DataT)) << version; } //: Construct from a filename. inline DPOBinFileBodyC(BinOStreamC &strmout,bool useHeader=false) : out(strmout), version(0) { #ifdef RAVL_CHECK if(!out.Stream().good()) cerr << "DPOBinFileBodyC::DPOBinFileBodyC(OStreamC,bool), Passed bad output stream. \n"; #endif if(useHeader) { out << RavlN::RAVLBinaryID << TypeName(typeid(DataT)) << version; #ifdef RAVL_CHECK if(!out.Stream().good()) cerr << "DPOBinFileBodyC::DPOBinFileBodyC(BinOStreamC,bool), Bad stream after writting header! \n"; #endif } } //: Stream constructor. virtual bool Put(const DataT &dat) { #ifdef RAVL_CHECK if(!out.Stream().good()) { cerr << "DPOBinFileBodyC::Put(), Failed because of bad output stream (before write!). \n"; return false; } #endif out << dat; #ifdef RAVL_CHECK if(!out.Stream().good()) cerr << "DPOBinFileBodyC::Put(), Failed because of bad output stream. \n"; #endif return out.Stream().good(); } //: Put data. virtual IntT PutArray(const SArray1dC &data) { if(!out.Stream().good()) return data.Size(); for(SArray1dIterC it(data);it;it++) { out << *it; if(!out.Stream().good()) { #ifdef RAVL_CHECK cerr << "DPOBinFileBodyC::PutArray(), Endded early because of bad output stream. \n"; #endif return it.Index().V(); } } return data.Size(); } //: Put an array of data to stream. // returns the number of elements processed. virtual bool IsPutReady() const { return out.Stream().good(); } //: Is port ready for data ? virtual bool Save(ostream &sout) const { sout << out.Name(); return true; } //: Save to ostream. private: BinOStreamC out; UIntT version; }; ///////////////////////////////////// //! userlevel=Develop //: Load objects in binary from a file. // Object must have a binary stream input function // and a default constructor. template class DPIBinFileBodyC : public DPIPortBodyC { public: DPIBinFileBodyC() {} //: Default constructor. DPIBinFileBodyC(const StringC &nfname,bool useHeader = false) : in(nfname), version(0) { if(useHeader) { StringC classname; UInt16T id; in >> id; if(id != RavlN::RAVLBinaryID) cerr << "DPIBinFileC ERROR: Bad file id. \n"; in >> classname >> version; // Read class name & version if(classname != TypeName(typeid(DataT))) cerr << "DPIBinFileC ERROR: Bad file type: " << classname << " Expected:" << TypeName(typeid(DataT)) << " \n"; } } //: Construct from a filename. inline DPIBinFileBodyC(BinIStreamC &strmin,bool useHeader = false) : in(strmin), version(0) { if(useHeader) { StringC classname; UInt16T id; in >> id; if(id != RavlN::RAVLBinaryID) cerr << "DPIBinFileC ERROR: Bad file id. \n"; in >> classname >> version; // Read class name & version if(classname != TypeName(typeid(DataT))) cerr << "DPIBinFileC ERROR: Bad file type. " << classname << " Expected:" << TypeName(typeid(DataT)) << " \n"; } } //: Stream constructor. virtual bool IsGetEOS() const { return (!in.Stream().good() || in.Stream().eof()); } //: Is valid data ? virtual DataT Get() { DataT ret; in >> ret; return ret; } //: Get next piece of data. virtual bool Get(DataT &buff) { if(in.IsEndOfStream()) return false; in >> buff; return true; } //: Get next piece of data. virtual IntT GetArray(SArray1dC &data) { for(SArray1dIterC it(data);it;it++) { in >> *it; if(!in.Stream().good()) { #ifdef RAVL_CHECK cerr << "DPIBinFileBodyC::GetArray(), Ended early because of bad input stream. \n"; #endif return it.Index().V(); } } return data.Size(); } //: Get multiple pieces of input data. virtual bool Save(ostream &out) const { out << in.Name(); return true; } //: Save to ostream. private: BinIStreamC in; UIntT version; }; /////////////////////////////// //! userlevel=Normal //: Binary file output stream. // Object must have a binary stream output function. template class DPOBinFileC : public DPOPortC { public: inline DPOBinFileC() {} //: Default constructor. inline DPOBinFileC(BinOStreamC &strm,bool useHeader=false) : DPEntityC(*new DPOBinFileBodyC(strm,useHeader)) {} //: Stream constructor. inline DPOBinFileC(const StringC &fname,bool useHeader=false) : DPEntityC(*new DPOBinFileBodyC(fname,useHeader)) {} //: Filename constructor. }; ////////////////////////////////// //! userlevel=Normal //: Binary file input stream. // Object must have a binary stream input function // and a default constructor. template class DPIBinFileC : public DPIPortC { public: inline DPIBinFileC() {} //: Default constructor. inline DPIBinFileC(BinIStreamC &strm,bool useHeader=false) : DPEntityC(*new DPIBinFileBodyC(strm,useHeader)) {} //: Stream constructor. inline DPIBinFileC(const StringC &afname,bool useHeader=false) : DPEntityC(*new DPIBinFileBodyC(afname,useHeader)) {} //: Filename constructor. }; } #endif