Filestream.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // (c) 2007 by Basler Vision Technologies
3 // Section: Vision Components
4 // Project: GenApi
5 // Author: Thies Moeller
6 // $Header$
7 //
8 // License: This file is published under the license of the EMVA GenICam Standard Group.
9 // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
10 // If for some reason you are missing this file please contact the EMVA or visit the website
11 // (http://www.genicam.org) for a full copy.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
14 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
17 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 // POSSIBILITY OF SUCH DAMAGE.
24 //-----------------------------------------------------------------------------
30 #ifndef GENAPI_FILESTREAM_H_
31 #define GENAPI_FILESTREAM_H_
32 
33 // #include <algorithm>
34 // #include <iostream>
35 // #include <streambuf>
36 #include <iomanip>
37 #include <iosfwd>
38 #include <cstring> // memcpy
39 //#include <cstdlib>
40 //#include <memory>
41 
42 #include <Base/GCUtilities.h>
43 
44 #include <GenICamFwd.h>
45 
46 
47 #if (__GNUC__)
48 # include <unistd.h>
49 #endif
50 
51 // We cannot use std::streamsize because in VC90/Win32 this was int but in VC120 it is int64
52 // So in order to preserve compiler interoperability we need to use our own declaration
54 
55 
56 // FileProtocolAdapter was a header only class before.
57 // Since it has been moved to GenApi.dll the user now has to explicitly link against GenApi import library to use it
58 // To minimize the pain we add the genapi lib using pragmas
59 #if defined (_MSC_VER)
60 
61 # include <Base/GCLinkage.h>
62 
63 // you can define GENICAM_NO_AUTO_IMPLIB to turn off automatic linkage of genicam libs
64 // you can define GENICAM_FORCE_AUTO_IMPLIB to enforce automatic linkage of genicam libs
65 # if defined(GENICAM_FORCE_AUTO_IMPLIB) || (!defined(GENICAM_NO_AUTO_IMPLIB) && !defined(GENAPI_EXPORTS))
66 # if defined (_WIN32) && defined (_MT )
67 # pragma comment(lib, LIB_NAME( "GenApi" ))
68 # else
69 # error Invalid configuration
70 # endif
71 # endif
72 
73 #endif // _MSC_VER
74 
75 #if defined (_MSC_VER)
76 # pragma warning(push)
77 # pragma warning (disable: 4251) // class 'GenApi::CPointer<T>' needs to have dll-interface to be used by clients of class 'GenICam::FileProtocolAdapter'
78 #endif
79 
80 namespace GENAPI_NAMESPACE
81 {
86  interface GENAPI_DECL_ABSTRACT IFileProtocolAdapter
87  {
88  virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface) = 0;
89  virtual bool openFile(const char * pFileName, std::ios_base::openmode mode) = 0;
90  virtual bool closeFile(const char * pFileName) = 0;
91  virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName) = 0;
92  virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName) = 0;
93  virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode) = 0;
94  virtual bool deleteFile(const char * pFileName) = 0;
95 
96  };
105  class GENAPI_DECL FileProtocolAdapter : public IFileProtocolAdapter {
106  public:
113  virtual ~FileProtocolAdapter();
114  private:
115  FileProtocolAdapter(const FileProtocolAdapter&); // not implemented
116  FileProtocolAdapter& operator=(const FileProtocolAdapter&); // not implemented
117  public:
118 
129  virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface );
130 
131 
146  virtual bool openFile(const char * pFileName, std::ios_base::openmode mode);
147 
148 
159  virtual bool closeFile(const char * pFileName);
160 
161 
182  virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName);
183 
184 
205  virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName);
206 
207 
222  virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode);
223 
224 
235  virtual bool deleteFile(const char * pFileName);
236 
237  private:
238  void WaitUntilFileOperationExecuteDone( bool Validate = true );
239 
240  private:
241  // implementation details
242  struct FileProtocolAdapterImpl;
243  FileProtocolAdapterImpl* m_pImpl;
244  };
245 
246 
247  template<typename CharType, typename Traits> class IDevFileStreamBuf
248  : public std::basic_streambuf<CharType, Traits> {
249 
250  typedef Traits traits_type;
251  typedef typename Traits::int_type int_type;
252  typedef typename Traits::char_type char_type;
254 
255  // GET next ptr
256  using std::basic_streambuf<CharType, Traits>::gptr;
257  // GET end ptr
258  using std::basic_streambuf<CharType, Traits>::egptr;
259  // GET begin ptr
260  using std::basic_streambuf<CharType, Traits>::eback;
261  // increment next pointer
262  using std::basic_streambuf<CharType, Traits>::gbump;
263  // set buffer info
264  using std::basic_streambuf<CharType, Traits>::setg;
265 
266  public:
268  : m_pBuffer(0), m_BufSize(0), m_pAdapter(0), m_fpos(0) {
269  // This already handled by the base class constructor, right?
270  // std::basic_streambuf<CharType, Traits>::_Init();
271  };
272 
273 
275  {
276  // catch and dump all exceptions - we're in a destructor...
277  try
278  {
279  this->close();
280  }
281  catch(...)
282  {}
283  }
284 
285  filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::in ) {
286  // get file protocol adapter
287  m_pAdapter = new FileProtocolAdapter();
288 
289  // open file via Adapter
290  if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
291  delete m_pAdapter;
292  m_pAdapter = 0;
293  return 0;
294  }
295 
296  // open file via Adapter
297  try
298  {
299  if (!(m_pAdapter->openFile(pFileName, mode))){
300  delete m_pAdapter;
301  m_pAdapter = 0;
302  return 0;
303  }
304  }
305  catch (...)
306  {
307  delete m_pAdapter;
308  m_pAdapter = 0;
309  throw;
310  }
311 
312  m_file = pFileName;
313  // allocate buffer according to fileinfo
314  m_BufSize = (GenICam_streamsize)m_pAdapter->getBufSize(m_file.c_str(), mode);
315  m_pBuffer = new char_type[(unsigned int)m_BufSize / sizeof(char_type)];
316 
317  // setg(buffer+pbSize, buffer+pbSize, buffer+pbSize);
318  setg(m_pBuffer, m_pBuffer + m_BufSize,m_pBuffer + m_BufSize);
319 
320  #ifdef _MSC_VER
321  // is this reasonable?
322  std::basic_streambuf<CharType, Traits>::_Init();
323  #endif
324 
325  return this;
326  }
327 
328 
329 
330  bool
331  is_open() const
332  {
333  return m_pAdapter != 0;
334  }
335 
336  filebuf_type *close() {
337  filebuf_type * ret = 0;
338  if (this->is_open()) {
339  // close file
340  if(m_pAdapter->closeFile(m_file.c_str())){
341  // no error
342  ret = this;
343  }
344  delete m_pAdapter;
345  m_pAdapter = 0;
346  // buffer
347  delete[] m_pBuffer;
348  m_pBuffer = 0;
349  }
350  return ret;
351  }
352 
353 
354  protected:
355  int_type underflow() {
356  if (gptr() < egptr() )
357  return traits_type::to_int_type(*gptr());
358 
359  if (buffer_in() < 0)
360  return traits_type::eof();
361  else
362  return traits_type::to_int_type(*gptr());
363 
364  }
365 
366  int_type pbackfail(int_type c) {
367  if (gptr() != eback() || eback()<gptr()) {
368  gbump(-1);
369  if (!traits_type::eq_int_type(c, traits_type::eof() ) )
370  *(gptr()) = static_cast<char_type>(traits_type::not_eof(c));
371  return traits_type::not_eof(c);
372  } else
373  return traits_type::eof();
374  }
375 
376  private:
377  char_type * m_pBuffer;
379 
383 
384 
385 
386  int buffer_in() {
387 
388  GenICam_streamsize retval = m_pAdapter->read(m_pBuffer, m_fpos, m_BufSize, m_file.c_str());
389 
390  if (retval <= 0) {
391  setg(0, 0, 0);
392  return -1;
393  } else {
394  setg(m_pBuffer, m_pBuffer , m_pBuffer + retval);
395  m_fpos += retval;
396  return GENICAM_NAMESPACE::INTEGRAL_CAST2<int, GenICam_streamsize>(retval);
397  }
398  }
399 
400 
401  // prohibit copying and assignment
403  IDevFileStreamBuf& operator=(const IDevFileStreamBuf&);
404  };
405 
406 
407  template<typename CharType, typename Traits> class ODevFileStreamBuf
408  : public std::basic_streambuf<CharType, Traits> {
409  typedef Traits traits_type;
410 
411  typedef typename Traits::int_type int_type;
412  typedef typename Traits::char_type char_type;
413  typedef typename Traits::pos_type pos_type;
414  typedef typename Traits::off_type off_type;
415 
417 
418 
419  // PUT begin
420  using std::basic_streambuf<CharType, Traits>::pbase;
421  // PUT next
422  using std::basic_streambuf<CharType, Traits>::pptr;
423  // PUT end
424  using std::basic_streambuf<CharType, Traits>::epptr;
425  // increment next pointer
426  using std::basic_streambuf<CharType, Traits>::pbump;
427 
428  public:
430  : m_pBuffer(0), m_file(0), m_pAdapter(0), m_fpos(0) {
431  }
432 
434  this->close();
435  }
436 
437  filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode) {
438 
439  // create Genicam Access
440  m_pAdapter = new FileProtocolAdapter();
441 
442  // attach to nodemap
443  if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
444  delete m_pAdapter;
445  m_pAdapter = 0;
446  return 0;
447  }
448 
449 
450  // open file via Adapter
451  try
452  {
453  if (!(m_pAdapter->openFile(pFileName, mode))){
454  delete m_pAdapter;
455  m_pAdapter = 0;
456  return 0;
457  }
458  }
459  catch (...)
460  {
461  delete m_pAdapter;
462  m_pAdapter = 0;
463  throw;
464  }
465 
466  m_file = pFileName;
467  // allocate buffer according to fileinfo
468  const int64_t bufSize = m_pAdapter->getBufSize(m_file,mode);
469  m_pBuffer = new char_type[GENICAM_NAMESPACE::INTEGRAL_CAST<size_t>(bufSize) / sizeof(char_type)];
470  std::basic_streambuf<CharType, Traits>::setp(m_pBuffer, m_pBuffer + bufSize);
471 
472  return this;
473  }
474 
475  bool
476  is_open() const {
477  return m_pAdapter != 0;
478  }
479 
480  filebuf_type *close() {
481  filebuf_type * ret = 0;
482  bool syncFailed = false;
483  if (this->is_open()) {
484  if (sync()){
485  syncFailed = true;
486  };
487  // close file
488  if(m_pAdapter->closeFile(m_file)){
489  // no error
490  if ( syncFailed ){
491  ret = 0;
492  } else {
493  ret = this;
494  }
495  }
496  delete m_pAdapter;
497  m_pAdapter = 0;
498  // buffer
499  delete[] m_pBuffer;
500  m_pBuffer = 0;
501  }
502  return ret;
503  }
504 
505  protected:
506  GenICam_streamsize xsputn(const char_type * s, GenICam_streamsize n) {
507  if (n < epptr() - pptr() ) {
508  memcpy( pptr(), s, (size_t)(n * sizeof(char_type)));
509  pbump( GENICAM_NAMESPACE::INTEGRAL_CAST2<int>(n) );
510  return n;
511  } else {
512  for (GenICam_streamsize i = 0; i<n; ++i) {
513  if (traits_type::eq_int_type(std::basic_streambuf<CharType, Traits>::sputc(s[i]), traits_type::eof()))
514  return i;
515  }
516  return n;
517  }
518 
519  }
520  int_type overflow(int_type c = traits_type::eof()) {
521  if (buffer_out() < 0) {
522  return traits_type::eof();
523  } else {
524  if (!traits_type::eq_int_type (c, traits_type::eof() ) )
525  return std::basic_streambuf<CharType, Traits>::sputc(static_cast<char_type>(c));
526  else
527  return traits_type::not_eof(c);
528  }
529 
530  }
531  int sync() {
532  return GENICAM_NAMESPACE::INTEGRAL_CAST<int>(buffer_out());
533  }
534 
535  private:
536  char_type * m_pBuffer; // buffer[bufSize];
537  const char * m_file;
540 
542  int64_t cnt = pptr() - pbase();
543 
544  int64_t retval;
545  int64_t res = m_pAdapter->write(m_pBuffer, m_fpos, cnt, m_file);
546 
547  if (res != cnt) {
548  retval = -1;
549  } else {
550  retval = 0;
551  }
552  m_fpos += res;
553 
554  pbump(- GENICAM_NAMESPACE::INTEGRAL_CAST<int>(cnt));
555  return retval;
556  }
557 
558  // prohibit copying assignment
561  };
562 
563  template<typename CharType, typename Traits> class ODevFileStreamBase
564  : public std::basic_ostream<CharType, Traits> {
565  public:
566  // Non-standard types:
568  typedef std::basic_ios<CharType, Traits> ios_type;
569  typedef std::basic_ostream<CharType, Traits> ostream_type;
570 
571  private:
572  filebuf_type m_streambuf;
573  public:
574 
575 #if defined (_MSC_VER)
577  : ostream_type(std::_Noinit), m_streambuf() {
578  this->init(&m_streambuf);
579  }
580 
581  ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
582  : ostream_type(std::_Noinit), m_streambuf() {
583  this->init(&m_streambuf);
584  this->open(pInterface, pFileName, mode);
585  }
586  ;
587 
588 #elif defined (__GNUC__)
590  : ostream_type(), m_streambuf() {
591  this->init(&m_streambuf);
592  }
593 
594  ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
595  : ostream_type(), m_streambuf() {
596  this->init(&m_streambuf);
597  this->open(pInterface, pFileName, mode);
598  }
599  ;
600 
601 
602 #else
603 # error Unknown C++ library
604 #endif
605 
606 
607 
608  filebuf_type *rdbuf() const {
609  return const_cast<filebuf_type*>(&m_streambuf);
610  }
611 
612  /* bool is_open() {
613  return m_streambuf.is_open();
614  } */
615 
616  bool is_open() const {
617  return m_streambuf.is_open();
618  }
619 
629  void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
630  std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc) {
631  if (!m_streambuf.open(pInterface,pFileName, mode)){
632  this->setstate(std::ios_base::failbit);
633  }
634  else{
635  this->clear();
636  }
637  }
638 
643  void close() {
644  if (!m_streambuf.close())
645  this->setstate(std::ios_base::failbit);
646  }
647  };
648 
649  template<typename CharType, typename Traits> class IDevFileStreamBase
650  : public std::basic_istream<CharType, Traits> {
651  public:
652 
653  // Non-standard types:
655  typedef std::basic_ios<CharType, Traits> ios_type;
656  typedef std::basic_istream<CharType, Traits> istream_type;
657 
658  private:
659  filebuf_type m_streambuf;
660  public:
661 
662 #if defined (_MSC_VER)
664  : istream_type(std::_Noinit), m_streambuf() {
665  this->init(&m_streambuf);
666  }
667 
668  IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
669  std::ios_base::openmode mode = std::ios_base::in)
670  : istream_type(std::_Noinit), m_streambuf() {
671  this->init(&m_streambuf);
672  this->open(pInterface, pFileName, mode);
673  }
674  ;
675 
676 #elif defined (__APPLE__)
678  : istream_type(0), m_streambuf() {
679  this->init(&m_streambuf);
680  }
681 
682  IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
683  std::ios_base::openmode mode = std::ios_base::in)
684  : istream_type(0), m_streambuf() {
685  this->init(&m_streambuf);
686  this->open(pInterface, pFileName, mode);
687  }
688  ;
689 
690 #elif defined (__GNUC__)
692  : istream_type(), m_streambuf() {
693  this->init(&m_streambuf);
694  }
695 
696  IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
697  std::ios_base::openmode mode = std::ios_base::in)
698  : istream_type(), m_streambuf() {
699  this->init(&m_streambuf);
700  this->open(pInterface, pFileName, mode);
701  }
702  ;
703 
704 #else
705 # error Unknown C++ library
706 #endif
707 
708 
709  filebuf_type *rdbuf() const {
710  return const_cast<filebuf_type*>(&m_streambuf);
711  }
712 
713  /* bool is_open() {
714  return m_streambuf.is_open();
715  } */
716 
717  bool is_open() const {
718  return m_streambuf.is_open();
719  }
720 
721  /* @brief
722  * Open file on device in write mode
723  *
724  * @param pInterface NodeMap of the device to which the FileProtocolAdapter is attached
725  * @param pFileName Name of the file to open
726  * @param mode open mode
727  */
728  void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
729  std::ios_base::openmode mode = std::ios_base::in) {
730  if (!m_streambuf.open(pInterface,pFileName, mode))
731  this->setstate(std::ios_base::failbit);
732  else
733  this->clear();
734  }
735 
739  void close() {
740  if (!m_streambuf.close())
741  this->setstate(std::ios_base::failbit);
742  }
743  };
744 
747 }
748 
749 #if defined (_MSC_VER)
750 # pragma warning(pop)
751 #endif
752 
753 
754 #endif /*GENAPI_FILESTREAM_H_*/
void open(GENAPI_NAMESPACE::INodeMap *pInterface, const char *pFileName, std::ios_base::openmode mode=std::ios_base::out|std::ios_base::trunc)
Open file on device in write mode.
Definition: Filestream.h:629
ODevFileStreamBase< char, std::char_traits< char > > ODevFileStream
Definition: Filestream.h:745
std::basic_ostream< CharType, Traits > ostream_type
Definition: Filestream.h:569
virtual GenICam_streamsize read(char *buf, int64_t offs, GenICam_streamsize len, const char *pFileName)=0
void close()
Close the file on device.
Definition: Filestream.h:643
filebuf_type * open(GENAPI_NAMESPACE::INodeMap *pInterface, const char *pFileName, std::ios_base::openmode mode=std::ios_base::in)
Definition: Filestream.h:285
helpers for pragma linkage
filebuf_type * rdbuf() const
Definition: Filestream.h:608
std::basic_istream< CharType, Traits > istream_type
Definition: Filestream.h:656
GENICAM_NAMESPACE::gcstring m_file
Definition: Filestream.h:380
FileProtocolAdapter * m_pAdapter
Definition: Filestream.h:381
std::basic_ios< CharType, Traits > ios_type
Definition: Filestream.h:568
Adapter between the std::iostreambuf and the SFNC Features representing the device filesystem...
Definition: Filestream.h:105
virtual bool openFile(const char *pFileName, std::ios_base::openmode mode)=0
int_type overflow(int_type c=traits_type::eof())
Definition: Filestream.h:520
virtual void operator=(bool Value)
Set node value.
Definition: IBoolean.h:64
__int64 int64_t
Definition: config-win32.h:21
void open(GENAPI_NAMESPACE::INodeMap *pInterface, const char *pFileName, std::ios_base::openmode mode=std::ios_base::in)
Definition: Filestream.h:728
virtual GenICam_streamsize write(const char *buf, int64_t offs, int64_t len, const char *pFileName)
writes data into a file.
interface GENAPI_DECL_ABSTRACT INodeMap
Interface to access the node map.
Definition: INodeMap.h:56
IDevFileStreamBuf< CharType, Traits > filebuf_type
Definition: Filestream.h:253
#define GENAPI_DECL
Definition: GenApiDll.h:55
IDevFileStreamBuf< CharType, Traits > filebuf_type
Definition: Filestream.h:654
int_type pbackfail(int_type c)
Definition: Filestream.h:366
FileProtocolAdapter * m_pAdapter
Definition: Filestream.h:538
virtual bool closeFile(const char *pFileName)=0
GenICam_streamsize xsputn(const char_type *s, GenICam_streamsize n)
Definition: Filestream.h:506
virtual int64_t getBufSize(const char *pFileName, std::ios_base::openmode mode)=0
ODevFileStreamBuf< CharType, Traits > filebuf_type
Definition: Filestream.h:416
FileProtocolAdapterImpl * m_pImpl
Definition: Filestream.h:242
virtual GenICam_streamsize read(char *buf, int64_t offs, GenICam_streamsize len, const char *pFileName)
read data from the device into a buffer
Forward declarations for GenICam types.
filebuf_type * open(GENAPI_NAMESPACE::INodeMap *pInterface, const char *pFileName, std::ios_base::openmode mode)
Definition: Filestream.h:437
int64_t GenICam_streamsize
Definition: Filestream.h:53
virtual bool deleteFile(const char *pFileName)=0
std::basic_ios< CharType, Traits > ios_type
Definition: Filestream.h:655
A string class which is a clone of std::string.
Definition: GCString.h:52
void close()
Close the file on the device.
Definition: Filestream.h:739
ODevFileStreamBuf< CharType, Traits > filebuf_type
Definition: Filestream.h:567
filebuf_type * rdbuf() const
Definition: Filestream.h:709
IDevFileStreamBase< char, std::char_traits< char > > IDevFileStream
Definition: Filestream.h:746
virtual const char * c_str(void) const
Part of the generic device API.
Definition: Autovector.h:48
virtual GenICam_streamsize write(const char *buf, int64_t offs, int64_t len, const char *pFileName)=0
GenICam common utilities.


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 19:10:54