Filestream.h 23 KB

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