Filestream.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. \brief Definition of ODevFileStream and IDevFileStream
  28. */
  29. #ifndef GENAPI_FILESTREAM_H_
  30. #define GENAPI_FILESTREAM_H_
  31. // #include <algorithm>
  32. // #include <iostream>
  33. // #include <streambuf>
  34. #include <iomanip>
  35. #include <iosfwd>
  36. //#include <cstdlib>
  37. //#include <memory>
  38. #include <Base/GCUtilities.h>
  39. #include <GenICamFwd.h>
  40. #if (__GNUC__)
  41. # include <unistd.h>
  42. #endif
  43. // We cannot use std::streamsize because in VC90/Win32 this was int but in VC120 it is int64
  44. // So in order to preserve compiler interability we need to use our own declaration
  45. typedef int64_t GenICam_streamsize;
  46. // FileProtocolAdapter was a header only class before.
  47. // Since it has been moved to GenApi.dll the user now has to explicitly link against GenApi import library to use it
  48. // To minimize the pain we add the genapi lib using pragmas
  49. #if defined (_MSC_VER)
  50. # include <Base/GCLinkage.h>
  51. // you can define GENICAM_NO_AUTO_IMPLIB to turn off automatic linkage of genicam libs
  52. // you can define GENICAM_FORCE_AUTO_IMPLIB to enforce automatic linkage of genicam libs
  53. # if defined(GENICAM_FORCE_AUTO_IMPLIB) || (!defined(GENICAM_NO_AUTO_IMPLIB) && !defined(GENAPI_EXPORTS))
  54. # if defined (_WIN32) && defined (_MT )
  55. # pragma comment(lib, LIB_NAME( "GenApi" ))
  56. # else
  57. # error Invalid configuration
  58. # endif
  59. # endif
  60. #endif // _MSC_VER
  61. #if defined (_MSC_VER)
  62. # pragma warning(push)
  63. # pragma warning (disable: 4251) // class 'GenApi::CPointer<T>' needs to have dll-interface to be used by clients of class 'GenICam::FileProtocolAdapter'
  64. #endif
  65. namespace GENAPI_NAMESPACE
  66. {
  67. /**
  68. \brief Adapter between the std::iostreambuf and the SFNC Features representing the device filesystem
  69. \ingroup GenApi_PublicImpl
  70. */
  71. interface GENAPI_DECL_ABSTRACT IFileProtocolAdapter
  72. {
  73. virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface) = 0;
  74. virtual bool openFile(const char * pFileName, std::ios_base::openmode mode) = 0;
  75. virtual bool closeFile(const char * pFileName) = 0;
  76. virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName) = 0;
  77. virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName) = 0;
  78. virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode) = 0;
  79. virtual bool deleteFile(const char * pFileName) = 0;
  80. };
  81. /*!
  82. * @brief
  83. * Adapter between the std::iostreambuf and the SFNC Features representing the device filesystem
  84. *
  85. * The adapter assumes, that the features provide stdio fileaccess
  86. * compatible semantic
  87. *
  88. */
  89. class GENAPI_DECL FileProtocolAdapter : public IFileProtocolAdapter {
  90. public:
  91. /*!
  92. * @brief
  93. * Constructor
  94. *
  95. */
  96. FileProtocolAdapter();
  97. virtual ~FileProtocolAdapter();
  98. private:
  99. FileProtocolAdapter(const FileProtocolAdapter&); // not implemented
  100. FileProtocolAdapter& operator=(const FileProtocolAdapter&); // not implemented
  101. public:
  102. /*!
  103. * @brief
  104. * attach file protocol adapter to nodemap
  105. *
  106. * @param pInterface
  107. * NodeMap of the device to which the FileProtocolAdapter is attached
  108. *
  109. * @return true if attach was successful, false if not
  110. *
  111. */
  112. virtual bool attach(GENAPI_NAMESPACE::INodeMap * pInterface );
  113. /*!
  114. * @brief
  115. * open a file on the device
  116. *
  117. * @param pFileName
  118. * filename of the file to open. The filename must exist in the Enumeration FileSelector
  119. *
  120. * @param mode
  121. * mode to open the file. The mode must exist in the Enumeration FileOpenMode
  122. *
  123. * @returns
  124. * true on success, false on error
  125. *
  126. */
  127. virtual bool openFile(const char * pFileName, std::ios_base::openmode mode);
  128. /*!
  129. * @brief
  130. * close a file on the device
  131. *
  132. * @param pFileName
  133. * filename of the file to open. The filename must exist in the Enumeration FileSelector
  134. *
  135. * @returns
  136. * true on success, false on error
  137. */
  138. virtual bool closeFile(const char * pFileName);
  139. /*!
  140. * @brief
  141. * writes data into a file.
  142. *
  143. * @param buf
  144. * source buffer
  145. *
  146. * @param offs
  147. * offset into the device file
  148. *
  149. * @param len
  150. * count of bytes to write
  151. *
  152. * @param pFileName
  153. * filename of the file to write into The filename must exist in the Enumeration FileSelector
  154. *
  155. * @returns
  156. * count of bytes written
  157. *
  158. */
  159. virtual GenICam_streamsize write(const char * buf, int64_t offs, int64_t len, const char * pFileName);
  160. /*!
  161. * @brief
  162. * read data from the device into a buffer
  163. *
  164. * @param buf
  165. * target buffer
  166. *
  167. * @param offs
  168. * offset in the device file to read from
  169. *
  170. * @param len
  171. * count of bytes to read
  172. *
  173. * @param pFileName
  174. * filename of the file to write into The filename must exist in the Enumeration FileSelector
  175. *
  176. * @returns
  177. * count of bytes successfully read
  178. *
  179. */
  180. virtual GenICam_streamsize read(char * buf, int64_t offs, GenICam_streamsize len, const char * pFileName);
  181. /*!
  182. * @brief
  183. * fetch max FileAccessBuffer length for a file
  184. *
  185. * @param pFileName
  186. * filename of the file to open. The filename must exist in the Enumeration FileSelector
  187. *
  188. * @param mode
  189. * mode to open the file. The mode must exist in the Enunmeration FileOpenMode
  190. *
  191. * @returns
  192. * max length of FileAccessBuffer in the given mode on the given file
  193. *
  194. */
  195. virtual int64_t getBufSize(const char * pFileName, std::ios_base::openmode mode);
  196. /*!
  197. * @brief
  198. * Delete the content of the file.
  199. *
  200. * @param pFileName
  201. * filename of the file to open. The filename must exist in the Enumeration FileSelector
  202. *
  203. * @returns
  204. * true on success, false on error
  205. */
  206. virtual bool deleteFile(const char * pFileName);
  207. private:
  208. void WaitUntilFileOperationExecuteDone( bool Validate = true );
  209. private:
  210. // implementation details
  211. struct FileProtocolAdapterImpl;
  212. FileProtocolAdapterImpl* m_pImpl;
  213. };
  214. template<typename CharType, typename Traits> class IDevFileStreamBuf
  215. : public std::basic_streambuf<CharType, Traits> {
  216. typedef Traits traits_type;
  217. typedef typename Traits::int_type int_type;
  218. typedef typename Traits::char_type char_type;
  219. typedef IDevFileStreamBuf<CharType, Traits> filebuf_type;
  220. // GET next ptr
  221. using std::basic_streambuf<CharType, Traits>::gptr;
  222. // GET end ptr
  223. using std::basic_streambuf<CharType, Traits>::egptr;
  224. // GET begin ptr
  225. using std::basic_streambuf<CharType, Traits>::eback;
  226. // increment next pointer
  227. using std::basic_streambuf<CharType, Traits>::gbump;
  228. // set buffer info
  229. using std::basic_streambuf<CharType, Traits>::setg;
  230. public:
  231. IDevFileStreamBuf()
  232. : m_pBuffer(0), m_BufSize(0), m_pAdapter(0), m_fpos(0) {
  233. // This already handled by the base class constructor, right?
  234. // std::basic_streambuf<CharType, Traits>::_Init();
  235. };
  236. ~IDevFileStreamBuf()
  237. {
  238. // catch and dump all exceptions - we're in a destructor...
  239. try
  240. {
  241. this->close();
  242. }
  243. catch(...)
  244. {}
  245. }
  246. filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::in ) {
  247. // get file protocol adapter
  248. m_pAdapter = new FileProtocolAdapter();
  249. // open file via Adapter
  250. if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
  251. delete m_pAdapter;
  252. m_pAdapter = 0;
  253. return 0;
  254. }
  255. // open file via Adapter
  256. try
  257. {
  258. if (!(m_pAdapter->openFile(pFileName, mode))){
  259. delete m_pAdapter;
  260. m_pAdapter = 0;
  261. return 0;
  262. }
  263. }
  264. catch (...)
  265. {
  266. delete m_pAdapter;
  267. m_pAdapter = 0;
  268. throw;
  269. }
  270. m_file = pFileName;
  271. // allocate buffer according to fileinfo
  272. m_BufSize = (GenICam_streamsize)m_pAdapter->getBufSize(m_file.c_str(), mode);
  273. m_pBuffer = new char_type[(unsigned int)m_BufSize / sizeof(char_type)];
  274. // setg(buffer+pbSize, buffer+pbSize, buffer+pbSize);
  275. setg(m_pBuffer, m_pBuffer + m_BufSize,m_pBuffer + m_BufSize);
  276. #ifdef _MSC_VER
  277. // is this reasonable?
  278. std::basic_streambuf<CharType, Traits>::_Init();
  279. #endif
  280. return this;
  281. }
  282. bool
  283. is_open() const
  284. {
  285. return m_pAdapter != 0;
  286. }
  287. filebuf_type *close() {
  288. filebuf_type * ret = 0;
  289. if (this->is_open()) {
  290. // close file
  291. if(m_pAdapter->closeFile(m_file.c_str())){
  292. // no error
  293. ret = this;
  294. }
  295. delete m_pAdapter;
  296. m_pAdapter = 0;
  297. // buffer
  298. delete[] m_pBuffer;
  299. m_pBuffer = 0;
  300. }
  301. return ret;
  302. }
  303. protected:
  304. int_type underflow() {
  305. if (gptr() < egptr() )
  306. return traits_type::to_int_type(*gptr());
  307. if (buffer_in() < 0)
  308. return traits_type::eof();
  309. else
  310. return traits_type::to_int_type(*gptr());
  311. }
  312. int_type pbackfail(int_type c) {
  313. if (gptr() != eback() || eback()<gptr()) {
  314. gbump(-1);
  315. if (!traits_type::eq_int_type(c, traits_type::eof() ) )
  316. *(gptr()) = static_cast<char_type>(traits_type::not_eof(c));
  317. return traits_type::not_eof(c);
  318. } else
  319. return traits_type::eof();
  320. }
  321. private:
  322. char_type * m_pBuffer;
  323. GenICam_streamsize m_BufSize;
  324. GENICAM_NAMESPACE::gcstring m_file;
  325. FileProtocolAdapter * m_pAdapter;
  326. int64_t m_fpos;
  327. int buffer_in() {
  328. GenICam_streamsize retval = m_pAdapter->read(m_pBuffer, m_fpos, m_BufSize, m_file.c_str());
  329. if (retval <= 0) {
  330. setg(0, 0, 0);
  331. return -1;
  332. } else {
  333. setg(m_pBuffer, m_pBuffer , m_pBuffer + retval);
  334. m_fpos += retval;
  335. return GENICAM_NAMESPACE::INTEGRAL_CAST2<int, GenICam_streamsize>(retval);
  336. }
  337. }
  338. // prohibit copying and assignment
  339. IDevFileStreamBuf(const IDevFileStreamBuf&);
  340. IDevFileStreamBuf& operator=(const IDevFileStreamBuf&);
  341. };
  342. template<typename CharType, typename Traits> class ODevFileStreamBuf
  343. : public std::basic_streambuf<CharType, Traits> {
  344. typedef Traits traits_type;
  345. typedef typename Traits::int_type int_type;
  346. typedef typename Traits::char_type char_type;
  347. typedef typename Traits::pos_type pos_type;
  348. typedef typename Traits::off_type off_type;
  349. typedef ODevFileStreamBuf<CharType, Traits> filebuf_type;
  350. // PUT begin
  351. using std::basic_streambuf<CharType, Traits>::pbase;
  352. // PUT next
  353. using std::basic_streambuf<CharType, Traits>::pptr;
  354. // PUT end
  355. using std::basic_streambuf<CharType, Traits>::epptr;
  356. // increment next pointer
  357. using std::basic_streambuf<CharType, Traits>::pbump;
  358. public:
  359. ODevFileStreamBuf()
  360. : m_pBuffer(0), m_file(0), m_pAdapter(0), m_fpos(0) {
  361. }
  362. ~ODevFileStreamBuf() {
  363. this->close();
  364. }
  365. filebuf_type *open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode) {
  366. // create Genicam Access
  367. m_pAdapter = new FileProtocolAdapter();
  368. // attach to nodemap
  369. if (!m_pAdapter || !m_pAdapter->attach(pInterface)){
  370. delete m_pAdapter;
  371. m_pAdapter = 0;
  372. return 0;
  373. }
  374. // open file via Adapter
  375. try
  376. {
  377. if (!(m_pAdapter->openFile(pFileName, mode))){
  378. delete m_pAdapter;
  379. m_pAdapter = 0;
  380. return 0;
  381. }
  382. }
  383. catch (...)
  384. {
  385. delete m_pAdapter;
  386. m_pAdapter = 0;
  387. throw;
  388. }
  389. m_file = pFileName;
  390. // allocate buffer according to fileinfo
  391. const int64_t bufSize = m_pAdapter->getBufSize(m_file,mode);
  392. m_pBuffer = new char_type[GENICAM_NAMESPACE::INTEGRAL_CAST<size_t>(bufSize) / sizeof(char_type)];
  393. std::basic_streambuf<CharType, Traits>::setp(m_pBuffer, m_pBuffer + bufSize);
  394. return this;
  395. }
  396. bool
  397. is_open() const {
  398. return m_pAdapter != 0;
  399. }
  400. filebuf_type *close() {
  401. filebuf_type * ret = 0;
  402. bool syncFailed = false;
  403. if (this->is_open()) {
  404. if (sync()){
  405. syncFailed = true;
  406. };
  407. // close file
  408. if(m_pAdapter->closeFile(m_file)){
  409. // no error
  410. if ( syncFailed ){
  411. ret = 0;
  412. } else {
  413. ret = this;
  414. }
  415. }
  416. delete m_pAdapter;
  417. m_pAdapter = 0;
  418. // buffer
  419. delete[] m_pBuffer;
  420. m_pBuffer = 0;
  421. }
  422. return ret;
  423. }
  424. protected:
  425. GenICam_streamsize xsputn(const char_type * s, GenICam_streamsize n) {
  426. if (n < epptr() - pptr() ) {
  427. memcpy( pptr(), s, (size_t)(n * sizeof(char_type)));
  428. pbump( GENICAM_NAMESPACE::INTEGRAL_CAST2<int>(n) );
  429. return n;
  430. } else {
  431. for (GenICam_streamsize i = 0; i<n; ++i) {
  432. if (traits_type::eq_int_type(std::basic_streambuf<CharType, Traits>::sputc(s[i]), traits_type::eof()))
  433. return i;
  434. }
  435. return n;
  436. }
  437. }
  438. int_type overflow(int_type c = traits_type::eof()) {
  439. if (buffer_out() < 0) {
  440. return traits_type::eof();
  441. } else {
  442. if (!traits_type::eq_int_type (c, traits_type::eof() ) )
  443. return std::basic_streambuf<CharType, Traits>::sputc(static_cast<char_type>(c));
  444. else
  445. return traits_type::not_eof(c);
  446. }
  447. }
  448. int sync() {
  449. return GENICAM_NAMESPACE::INTEGRAL_CAST<int>(buffer_out());
  450. }
  451. private:
  452. char_type * m_pBuffer; // buffer[bufSize];
  453. const char * m_file;
  454. FileProtocolAdapter * m_pAdapter;
  455. int64_t m_fpos;
  456. int64_t buffer_out() {
  457. int64_t cnt = pptr() - pbase();
  458. int64_t retval;
  459. int64_t res = m_pAdapter->write(m_pBuffer, m_fpos, cnt, m_file);
  460. if (res != cnt) {
  461. retval = -1;
  462. } else {
  463. retval = 0;
  464. }
  465. m_fpos += res;
  466. pbump(- GENICAM_NAMESPACE::INTEGRAL_CAST<int>(cnt));
  467. return retval;
  468. }
  469. // prohibit copying assignment
  470. ODevFileStreamBuf(const ODevFileStreamBuf&);
  471. ODevFileStreamBuf & operator =(const ODevFileStreamBuf&);
  472. };
  473. template<typename CharType, typename Traits> class ODevFileStreamBase
  474. : public std::basic_ostream<CharType, Traits> {
  475. public:
  476. // Non-standard types:
  477. typedef ODevFileStreamBuf<CharType, Traits> filebuf_type;
  478. typedef std::basic_ios<CharType, Traits> ios_type;
  479. typedef std::basic_ostream<CharType, Traits> ostream_type;
  480. private:
  481. filebuf_type m_streambuf;
  482. public:
  483. #if defined (_MSC_VER)
  484. ODevFileStreamBase()
  485. : ostream_type(std::_Noinit), m_streambuf() {
  486. this->init(&m_streambuf);
  487. }
  488. ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
  489. : ostream_type(std::_Noinit), m_streambuf() {
  490. this->init(&m_streambuf);
  491. this->open(pInterface, pFileName, mode);
  492. }
  493. ;
  494. #elif defined (__GNUC__)
  495. ODevFileStreamBase()
  496. : ostream_type(), m_streambuf() {
  497. this->init(&m_streambuf);
  498. }
  499. ODevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName, std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc)
  500. : ostream_type(), m_streambuf() {
  501. this->init(&m_streambuf);
  502. this->open(pInterface, pFileName, mode);
  503. }
  504. ;
  505. #else
  506. # error Unknown C++ library
  507. #endif
  508. filebuf_type *rdbuf() const {
  509. return const_cast<filebuf_type*>(&m_streambuf);
  510. }
  511. /* bool is_open() {
  512. return m_streambuf.is_open();
  513. } */
  514. bool is_open() const {
  515. return m_streambuf.is_open();
  516. }
  517. /**
  518. * @brief
  519. * Open file on device in write mode
  520. *
  521. * @param pInterface NodeMap of the device to which the FileProtocolAdapter is attached
  522. * @param pFileName Name of the file to open
  523. * @param mode open mode
  524. *
  525. */
  526. void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
  527. std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc) {
  528. if (!m_streambuf.open(pInterface,pFileName, mode)){
  529. this->setstate(std::ios_base::failbit);
  530. }
  531. else{
  532. this->clear();
  533. }
  534. }
  535. /**
  536. * @brief Close the file on device
  537. *
  538. */
  539. void close() {
  540. if (!m_streambuf.close())
  541. this->setstate(std::ios_base::failbit);
  542. }
  543. };
  544. template<typename CharType, typename Traits> class IDevFileStreamBase
  545. : public std::basic_istream<CharType, Traits> {
  546. public:
  547. // Non-standard types:
  548. typedef IDevFileStreamBuf<CharType, Traits> filebuf_type;
  549. typedef std::basic_ios<CharType, Traits> ios_type;
  550. typedef std::basic_istream<CharType, Traits> istream_type;
  551. private:
  552. filebuf_type m_streambuf;
  553. public:
  554. #if defined (_MSC_VER)
  555. IDevFileStreamBase()
  556. : istream_type(std::_Noinit), m_streambuf() {
  557. this->init(&m_streambuf);
  558. }
  559. IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
  560. std::ios_base::openmode mode = std::ios_base::in)
  561. : istream_type(std::_Noinit), m_streambuf() {
  562. this->init(&m_streambuf);
  563. this->open(pInterface, pFileName, mode);
  564. }
  565. ;
  566. #elif defined (__APPLE__)
  567. IDevFileStreamBase()
  568. : istream_type(0), m_streambuf() {
  569. this->init(&m_streambuf);
  570. }
  571. IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
  572. std::ios_base::openmode mode = std::ios_base::in)
  573. : istream_type(0), m_streambuf() {
  574. this->init(&m_streambuf);
  575. this->open(pInterface, pFileName, mode);
  576. }
  577. ;
  578. #elif defined (__GNUC__)
  579. IDevFileStreamBase()
  580. : istream_type(), m_streambuf() {
  581. this->init(&m_streambuf);
  582. }
  583. IDevFileStreamBase(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
  584. std::ios_base::openmode mode = std::ios_base::in)
  585. : istream_type(), m_streambuf() {
  586. this->init(&m_streambuf);
  587. this->open(pInterface, pFileName, mode);
  588. }
  589. ;
  590. #else
  591. # error Unknown C++ library
  592. #endif
  593. filebuf_type *rdbuf() const {
  594. return const_cast<filebuf_type*>(&m_streambuf);
  595. }
  596. /* bool is_open() {
  597. return m_streambuf.is_open();
  598. } */
  599. bool is_open() const {
  600. return m_streambuf.is_open();
  601. }
  602. /* @brief
  603. * Open file on device in write mode
  604. *
  605. * @param pInterface NodeMap of the device to which the FileProtocolAdapter is attached
  606. * @param pFileName Name of the file to open
  607. * @param mode open mode
  608. */
  609. void open(GENAPI_NAMESPACE::INodeMap * pInterface, const char * pFileName,
  610. std::ios_base::openmode mode = std::ios_base::in) {
  611. if (!m_streambuf.open(pInterface,pFileName, mode))
  612. this->setstate(std::ios_base::failbit);
  613. else
  614. this->clear();
  615. }
  616. /**
  617. * @brief Close the file on the device
  618. */
  619. void close() {
  620. if (!m_streambuf.close())
  621. this->setstate(std::ios_base::failbit);
  622. }
  623. };
  624. typedef ODevFileStreamBase<char, std::char_traits<char> > ODevFileStream;
  625. typedef IDevFileStreamBase<char, std::char_traits<char> > IDevFileStream;
  626. }
  627. #if defined (_MSC_VER)
  628. # pragma warning(pop)
  629. #endif
  630. #endif /*GENAPI_FILESTREAM_H_*/