Filestream.h
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //  (c) 2007 by Basler Vision Technologies
00003 //  Section: Vision Components
00004 //  Project: GenApi
00005 //  Author:  Thies Moeller
00006 //  $Header$
00007 //
00008 //  License: This file is published under the license of the EMVA GenICam  Standard Group.
00009 //  A text file describing the legal terms is included in  your installation as 'GenICam_license.pdf'.
00010 //  If for some reason you are missing  this file please contact the EMVA or visit the website
00011 //  (http://www.genicam.org) for a full copy.
00012 //
00013 //  THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
00014 //  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00015 //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016 //  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD  GROUP
00017 //  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,
00018 //  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  LIMITED TO,
00019 //  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  DATA, OR PROFITS;
00020 //  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  THEORY OF LIABILITY,
00021 //  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)
00022 //  ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00023 //  POSSIBILITY OF SUCH DAMAGE.
00024 //-----------------------------------------------------------------------------
00030 #ifndef GENAPI_FILESTREAM_H_
00031 #define GENAPI_FILESTREAM_H_
00032 
00033 // #include <algorithm>
00034 // #include <iostream>
00035 // #include <streambuf>
00036 #include <iomanip>
00037 #include <iosfwd>
00038 //#include <cstdlib>
00039 //#include <memory>
00040 
00041 #include <Base/GCUtilities.h>
00042 
00043 #include <GenICamFwd.h>
00044 
00045 
00046 #if (__GNUC__)
00047 #  include <unistd.h>
00048 #endif
00049 
00050 // We cannot use std::streamsize because in VC90/Win32 this was int but in VC120 it is int64
00051 // So in order to preserve compiler interoperability we need to use our own declaration
00052 typedef int64_t GenICam_streamsize;
00053 
00054 
00055 // FileProtocolAdapter was a header only class before.
00056 // Since it has been moved to GenApi.dll the user now has to explicitly link against GenApi import library to use it
00057 // To minimize the pain we add the genapi lib  using pragmas
00058 #if defined (_MSC_VER)
00059 
00060 #   include <Base/GCLinkage.h>
00061 
00062 // you can define GENICAM_NO_AUTO_IMPLIB to turn off automatic linkage of genicam libs
00063 // you can define GENICAM_FORCE_AUTO_IMPLIB to enforce automatic linkage of genicam libs
00064 #   if defined(GENICAM_FORCE_AUTO_IMPLIB) || (!defined(GENICAM_NO_AUTO_IMPLIB) && !defined(GENAPI_EXPORTS))
00065 #       if defined (_WIN32) && defined (_MT )
00066 #           pragma comment(lib, LIB_NAME( "GenApi" ))
00067 #       else
00068 #           error Invalid configuration
00069 #       endif
00070 #   endif
00071 
00072 #endif // _MSC_VER
00073 
00074 #if defined (_MSC_VER)
00075 #   pragma warning(push)
00076 #   pragma warning (disable: 4251) //  class 'GenApi::CPointer<T>' needs to have dll-interface to be used by clients of class 'GenICam::FileProtocolAdapter'
00077 #endif
00078 
00079 namespace GENAPI_NAMESPACE
00080 {
00085     interface GENAPI_DECL_ABSTRACT IFileProtocolAdapter
00086     {
00087         virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface) = 0;
00088         virtual bool openFile(const char * pFileName, std::ios_base::openmode mode) = 0;
00089         virtual bool closeFile(const char * pFileName) = 0;
00090         virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName) = 0;
00091         virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName) = 0;
00092         virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode) = 0;
00093         virtual bool deleteFile(const char * pFileName) = 0;
00094 
00095     };
00104     class GENAPI_DECL FileProtocolAdapter : public IFileProtocolAdapter {
00105     public:
00111         FileProtocolAdapter();
00112         virtual ~FileProtocolAdapter();
00113     private:
00114         FileProtocolAdapter(const FileProtocolAdapter&); // not implemented
00115         FileProtocolAdapter& operator=(const FileProtocolAdapter&); // not implemented
00116     public:
00117         
00128         virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface );
00129 
00130 
00145         virtual bool openFile(const char * pFileName, std::ios_base::openmode mode);
00146 
00147 
00158         virtual bool closeFile(const char * pFileName);
00159 
00160 
00181         virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName);
00182 
00183 
00204         virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName);
00205 
00206 
00221         virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode);
00222 
00223 
00234         virtual bool deleteFile(const char * pFileName);
00235 
00236     private:
00237         void WaitUntilFileOperationExecuteDone( bool Validate = true );
00238 
00239     private:
00240         // implementation details
00241         struct FileProtocolAdapterImpl;
00242         FileProtocolAdapterImpl* m_pImpl;
00243     };
00244 
00245 
00246     template<typename CharType, typename Traits> class IDevFileStreamBuf
00247         : public std::basic_streambuf<CharType, Traits> {
00248 
00249             typedef Traits traits_type;
00250             typedef typename Traits::int_type int_type;
00251             typedef typename Traits::char_type char_type;
00252             typedef IDevFileStreamBuf<CharType, Traits> filebuf_type;
00253 
00254             // GET next ptr
00255             using std::basic_streambuf<CharType, Traits>::gptr;
00256             // GET end ptr
00257             using std::basic_streambuf<CharType, Traits>::egptr;
00258             // GET begin ptr
00259             using std::basic_streambuf<CharType, Traits>::eback;
00260             // increment next pointer
00261             using std::basic_streambuf<CharType, Traits>::gbump;
00262             // set buffer info
00263             using std::basic_streambuf<CharType, Traits>::setg;
00264 
00265     public:
00266         IDevFileStreamBuf()
00267             : m_pBuffer(0), m_BufSize(0), m_pAdapter(0), m_fpos(0) {
00268                 // This already handled by the base class constructor, right?
00269         // std::basic_streambuf<CharType, Traits>::_Init();
00270         };
00271 
00272 
00273         ~IDevFileStreamBuf()
00274         {
00275             // catch and dump all exceptions - we're in a destructor...
00276             try
00277             {
00278                 this->close();
00279             }
00280             catch(...)
00281             {}
00282         }
00283 
00284         filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::in ) {
00285             // get file protocol adapter
00286             m_pAdapter = new FileProtocolAdapter();
00287 
00288             // open file via Adapter
00289             if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
00290                 delete m_pAdapter;
00291                 m_pAdapter = 0;
00292                 return 0;
00293             }
00294 
00295             // open file via Adapter
00296             try
00297             {
00298                 if (!(m_pAdapter->openFile(pFileName, mode))){
00299                     delete m_pAdapter;
00300                     m_pAdapter = 0;
00301                     return 0;
00302                 }
00303             }
00304             catch (...)
00305             {
00306                 delete m_pAdapter;
00307                 m_pAdapter = 0;
00308                 throw;
00309             }
00310 
00311             m_file = pFileName;
00312             // allocate buffer according to fileinfo
00313             m_BufSize = (GenICam_streamsize)m_pAdapter->getBufSize(m_file.c_str(), mode);
00314             m_pBuffer = new char_type[(unsigned int)m_BufSize / sizeof(char_type)];
00315 
00316             // setg(buffer+pbSize, buffer+pbSize, buffer+pbSize);
00317             setg(m_pBuffer, m_pBuffer + m_BufSize,m_pBuffer + m_BufSize);
00318 
00319         #ifdef _MSC_VER
00320         // is this reasonable?
00321             std::basic_streambuf<CharType, Traits>::_Init();
00322         #endif
00323 
00324             return this;
00325         }
00326 
00327 
00328 
00329         bool
00330             is_open() const
00331         {
00332             return m_pAdapter != 0;
00333         }
00334 
00335         filebuf_type *close() {
00336             filebuf_type * ret = 0;
00337             if (this->is_open()) {
00338                 // close file
00339                 if(m_pAdapter->closeFile(m_file.c_str())){
00340                     // no error
00341                     ret = this;
00342                 }
00343                 delete m_pAdapter;
00344                 m_pAdapter = 0;
00345                 // buffer
00346                 delete[] m_pBuffer;
00347                 m_pBuffer = 0;
00348             }
00349             return ret;
00350         }
00351 
00352 
00353     protected:
00354         int_type underflow() {
00355             if (gptr() < egptr() )
00356                 return traits_type::to_int_type(*gptr());
00357 
00358             if (buffer_in() < 0)
00359                 return traits_type::eof();
00360             else
00361                 return traits_type::to_int_type(*gptr());
00362 
00363         }
00364 
00365         int_type pbackfail(int_type c) {
00366             if (gptr() != eback() || eback()<gptr()) {
00367                 gbump(-1);
00368                 if (!traits_type::eq_int_type(c, traits_type::eof() ) )
00369                     *(gptr()) = static_cast<char_type>(traits_type::not_eof(c));
00370                 return traits_type::not_eof(c);
00371             } else
00372                 return traits_type::eof();
00373         }
00374 
00375     private:
00376         char_type * m_pBuffer;
00377         GenICam_streamsize m_BufSize;
00378 
00379         GENICAM_NAMESPACE::gcstring m_file;
00380         FileProtocolAdapter * m_pAdapter;
00381         int64_t m_fpos;
00382 
00383 
00384 
00385         int buffer_in() {
00386 
00387             GenICam_streamsize retval = m_pAdapter->read(m_pBuffer, m_fpos, m_BufSize, m_file.c_str());
00388 
00389             if (retval <= 0) {
00390                 setg(0, 0, 0);
00391                 return -1;
00392             } else {
00393                 setg(m_pBuffer, m_pBuffer , m_pBuffer + retval);
00394                 m_fpos += retval;
00395                 return GENICAM_NAMESPACE::INTEGRAL_CAST2<int, GenICam_streamsize>(retval);
00396             }
00397         }
00398 
00399 
00400         // prohibit copying and assignment
00401         IDevFileStreamBuf(const IDevFileStreamBuf&);
00402         IDevFileStreamBuf& operator=(const IDevFileStreamBuf&);
00403     };
00404 
00405 
00406     template<typename CharType, typename Traits> class ODevFileStreamBuf
00407         : public std::basic_streambuf<CharType, Traits> {
00408             typedef Traits traits_type;
00409 
00410             typedef typename Traits::int_type int_type;
00411             typedef typename Traits::char_type char_type;
00412             typedef typename Traits::pos_type pos_type;
00413             typedef typename Traits::off_type off_type;
00414 
00415             typedef ODevFileStreamBuf<CharType, Traits> filebuf_type;
00416 
00417 
00418             // PUT begin
00419             using std::basic_streambuf<CharType, Traits>::pbase;
00420             // PUT next
00421             using std::basic_streambuf<CharType, Traits>::pptr;
00422             // PUT end
00423             using std::basic_streambuf<CharType, Traits>::epptr;
00424             // increment next pointer
00425             using std::basic_streambuf<CharType, Traits>::pbump;
00426 
00427     public:
00428         ODevFileStreamBuf()
00429             : m_pBuffer(0), m_file(0), m_pAdapter(0), m_fpos(0) {
00430         }
00431 
00432         ~ODevFileStreamBuf() {
00433             this->close();
00434         }
00435 
00436         filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode) {
00437 
00438             // create Genicam Access
00439             m_pAdapter = new FileProtocolAdapter();
00440 
00441             // attach to nodemap
00442             if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
00443                 delete m_pAdapter;
00444                 m_pAdapter = 0;
00445                 return 0;
00446             }
00447 
00448 
00449             // open file via Adapter
00450             try
00451             {
00452                 if (!(m_pAdapter->openFile(pFileName, mode))){
00453                     delete m_pAdapter;
00454                     m_pAdapter = 0;
00455                     return 0;
00456                 }
00457             }
00458             catch (...)
00459             {
00460                 delete m_pAdapter;
00461                 m_pAdapter = 0;
00462                 throw;
00463             }
00464 
00465             m_file = pFileName;
00466             // allocate buffer according to fileinfo
00467             const int64_t bufSize = m_pAdapter->getBufSize(m_file,mode);
00468             m_pBuffer = new char_type[GENICAM_NAMESPACE::INTEGRAL_CAST<size_t>(bufSize) / sizeof(char_type)];
00469             std::basic_streambuf<CharType, Traits>::setp(m_pBuffer, m_pBuffer + bufSize);
00470 
00471             return this;
00472         }
00473 
00474         bool
00475             is_open() const {
00476                 return m_pAdapter != 0;
00477         }
00478 
00479         filebuf_type *close() {
00480             filebuf_type * ret = 0;
00481             bool syncFailed = false;
00482             if (this->is_open()) {
00483                 if (sync()){
00484                     syncFailed = true;
00485                 };
00486                 // close file
00487                 if(m_pAdapter->closeFile(m_file)){
00488                     // no error
00489                     if ( syncFailed ){
00490                         ret = 0;
00491                     } else {
00492                     ret = this;
00493                 }
00494                 }
00495                 delete m_pAdapter;
00496                 m_pAdapter = 0;
00497                 // buffer
00498                 delete[] m_pBuffer;
00499                 m_pBuffer = 0;
00500             }
00501             return ret;
00502         }
00503 
00504     protected:
00505         GenICam_streamsize xsputn(const char_type * s, GenICam_streamsize n) {
00506             if (n < epptr() - pptr() ) {
00507                 memcpy( pptr(), s, (size_t)(n * sizeof(char_type)));
00508                 pbump( GENICAM_NAMESPACE::INTEGRAL_CAST2<int>(n) );
00509                 return n;
00510             } else {
00511                 for (GenICam_streamsize i = 0; i<n; ++i) {
00512                     if (traits_type::eq_int_type(std::basic_streambuf<CharType, Traits>::sputc(s[i]), traits_type::eof()))
00513                         return i;
00514                 }
00515                 return n;
00516             }
00517 
00518         }
00519         int_type overflow(int_type c = traits_type::eof()) {
00520             if (buffer_out() < 0) {
00521                 return traits_type::eof();
00522             } else {
00523                 if (!traits_type::eq_int_type (c, traits_type::eof() ) )
00524                     return std::basic_streambuf<CharType, Traits>::sputc(static_cast<char_type>(c));
00525                 else
00526                     return traits_type::not_eof(c);
00527             }
00528 
00529         }
00530         int sync() {
00531             return GENICAM_NAMESPACE::INTEGRAL_CAST<int>(buffer_out());
00532         }
00533 
00534     private:
00535         char_type * m_pBuffer; // buffer[bufSize];
00536         const char * m_file;
00537         FileProtocolAdapter * m_pAdapter;
00538         int64_t m_fpos;
00539 
00540         int64_t buffer_out() {
00541             int64_t cnt = pptr() - pbase();
00542 
00543             int64_t retval;
00544             int64_t res = m_pAdapter->write(m_pBuffer, m_fpos, cnt, m_file);
00545 
00546             if (res != cnt) {
00547                 retval = -1;
00548             } else {
00549                 retval = 0;
00550             }
00551             m_fpos += res;
00552 
00553             pbump(- GENICAM_NAMESPACE::INTEGRAL_CAST<int>(cnt));
00554             return retval;
00555         }
00556 
00557         // prohibit copying assignment
00558         ODevFileStreamBuf(const ODevFileStreamBuf&);
00559         ODevFileStreamBuf & operator =(const ODevFileStreamBuf&);
00560     };
00561 
00562     template<typename CharType, typename Traits> class ODevFileStreamBase
00563         : public std::basic_ostream<CharType, Traits> {
00564     public:
00565         // Non-standard types:
00566         typedef ODevFileStreamBuf<CharType, Traits> filebuf_type;
00567         typedef std::basic_ios<CharType, Traits> ios_type;
00568         typedef std::basic_ostream<CharType, Traits> ostream_type;
00569 
00570     private:
00571         filebuf_type m_streambuf;
00572     public:
00573 
00574 #if defined (_MSC_VER)
00575         ODevFileStreamBase()
00576             : ostream_type(std::_Noinit), m_streambuf() {
00577                 this->init(&m_streambuf);
00578         }
00579 
00580         ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
00581             : ostream_type(std::_Noinit), m_streambuf() {
00582                 this->init(&m_streambuf);
00583                 this->open(pInterface, pFileName, mode);
00584         }
00585         ;
00586 
00587 #elif defined (__GNUC__)
00588         ODevFileStreamBase()
00589             : ostream_type(), m_streambuf() {
00590                 this->init(&m_streambuf);
00591         }
00592 
00593         ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
00594             : ostream_type(), m_streambuf() {
00595                 this->init(&m_streambuf);
00596                 this->open(pInterface, pFileName, mode);
00597         }
00598         ;
00599 
00600 
00601 #else
00602 #   error Unknown C++ library
00603 #endif
00604 
00605 
00606 
00607         filebuf_type *rdbuf() const {
00608             return const_cast<filebuf_type*>(&m_streambuf);
00609         }
00610 
00611         /* bool is_open() {
00612             return m_streambuf.is_open();
00613         } */
00614 
00615         bool is_open() const {
00616             return m_streambuf.is_open();
00617         }
00618 
00628         void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
00629             std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc) {
00630                 if (!m_streambuf.open(pInterface,pFileName, mode)){
00631                     this->setstate(std::ios_base::failbit);
00632                 }
00633                 else{
00634                     this->clear();
00635                 }
00636         }
00637 
00642         void close() {
00643             if (!m_streambuf.close())
00644                 this->setstate(std::ios_base::failbit);
00645         }
00646     };
00647 
00648     template<typename CharType, typename Traits> class IDevFileStreamBase
00649         : public std::basic_istream<CharType, Traits> {
00650     public:
00651 
00652         // Non-standard types:
00653         typedef IDevFileStreamBuf<CharType, Traits> filebuf_type;
00654         typedef std::basic_ios<CharType, Traits> ios_type;
00655         typedef std::basic_istream<CharType, Traits> istream_type;
00656 
00657     private:
00658         filebuf_type m_streambuf;
00659     public:
00660 
00661 #if defined (_MSC_VER)
00662         IDevFileStreamBase()
00663             : istream_type(std::_Noinit), m_streambuf() {
00664                 this->init(&m_streambuf);
00665         }
00666 
00667         IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
00668             std::ios_base::openmode mode = std::ios_base::in)
00669             : istream_type(std::_Noinit), m_streambuf() {
00670                 this->init(&m_streambuf);
00671                 this->open(pInterface, pFileName, mode);
00672         }
00673         ;
00674 
00675 #elif defined (__APPLE__)
00676         IDevFileStreamBase()
00677             : istream_type(0), m_streambuf() {
00678             this->init(&m_streambuf);
00679         }
00680 
00681         IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
00682             std::ios_base::openmode mode = std::ios_base::in)
00683             : istream_type(0), m_streambuf() {
00684             this->init(&m_streambuf);
00685             this->open(pInterface, pFileName, mode);
00686         }
00687         ;
00688         
00689 #elif defined (__GNUC__)
00690         IDevFileStreamBase()
00691             : istream_type(), m_streambuf() {
00692                 this->init(&m_streambuf);
00693         }
00694 
00695         IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
00696             std::ios_base::openmode mode = std::ios_base::in)
00697             : istream_type(), m_streambuf() {
00698                 this->init(&m_streambuf);
00699                 this->open(pInterface, pFileName, mode);
00700         }
00701         ;
00702 
00703 #else
00704 #   error Unknown C++ library
00705 #endif
00706 
00707 
00708         filebuf_type *rdbuf() const {
00709             return const_cast<filebuf_type*>(&m_streambuf);
00710         }
00711 
00712         /* bool is_open() {
00713             return m_streambuf.is_open();
00714         } */
00715 
00716         bool is_open() const {
00717             return m_streambuf.is_open();
00718         }
00719 
00720         /* @brief
00721         *   Open file on device in write mode
00722         *
00723         *  @param  pInterface  NodeMap of the device to which the FileProtocolAdapter is attached
00724         *  @param  pFileName  Name of the file to open
00725         *  @param  mode open mode
00726         */
00727         void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
00728             std::ios_base::openmode mode = std::ios_base::in) {
00729                 if (!m_streambuf.open(pInterface,pFileName, mode))
00730                     this->setstate(std::ios_base::failbit);
00731                 else
00732                     this->clear();
00733         }
00734 
00738         void close() {
00739             if (!m_streambuf.close())
00740                 this->setstate(std::ios_base::failbit);
00741         }
00742     };
00743 
00744     typedef ODevFileStreamBase<char, std::char_traits<char> > ODevFileStream;
00745     typedef IDevFileStreamBase<char, std::char_traits<char> > IDevFileStream;
00746 }
00747 
00748 #if defined (_MSC_VER)
00749 #   pragma warning(pop)
00750 #endif
00751 
00752 
00753 #endif /*GENAPI_FILESTREAM_H_*/


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02