Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

BinInputStreamAdaptor.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------*
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2003  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the license. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 #ifndef __BININPUTSTREAMADAPTOR_H_INCLUDED__
00029 #define __BININPUTSTREAMADAPTOR_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file     BinInputStreamAdaptor.H
00034  * @brief    Header file of class qgxml::BinInputStreamAdaptor
00035  *
00036  * @author   <a href="mailto:qgar-contact@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00037  * @date     March 31, 2003  18:16
00038  * @since    Qgar 2.1
00039  */
00040 
00041 
00042 // For RCS/CVS use: Do not delete
00043 /* $Id: BinInputStreamAdaptor.H,v 1.2 2004/06/21 15:50:53 rendek Exp $ */
00044 
00045 
00046 // XERCES
00047 #include <xercesc/util/BinInputStream.hpp>
00048 
00049 // STD
00050 #include <iostream>
00051 #include <fstream>
00052 
00053 
00054 namespace qgxml
00055 {
00056 
00057 
00058 /**
00059  * @class BinInputStreamAdaptor BinInputStreamAdaptor.H
00060  * 
00061  * @author <a href="mailto:qgar-contact@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00062  * @date   March 31, 2003  18:16
00063  * @since  Qgar 2.1
00064  *
00065  * @bug qgxml::BinInputStreamAdaptor::readBytes() has a different
00066  * implementation according to the dynamic type of _stream. This is
00067  * a necessary workaround to the bug #6746 of the libstd of gcc.
00068  * Future versions will clean this as soon as gcc will be upgraded.
00069  *
00070  * @todo Comment this class!
00071  *
00072  * @author <a href="mailto:qgar-contact@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00073  * @date   March 31, 2003  18:16
00074  * @since  Qgar 2.1
00075  */
00076 class BinInputStreamAdaptor
00077 
00078   : public xercesc::BinInputStream
00079 
00080 {
00081 // -------------------------------------------------------------------
00082 // P U B L I C    M E M B E R S
00083 // -------------------------------------------------------------------
00084 public:
00085 
00086 
00087 /** @name Constructors */
00088 //        ============
00089 //@{
00090 /**
00091  * @brief Default constructor
00092  */
00093   BinInputStreamAdaptor(std::istream * stream)
00094   {
00095     _stream = stream;
00096   }
00097 
00098 //@}
00099 
00100 
00101 /** @name Destructor */
00102 //        ==========
00103 //@{
00104 /**
00105  * @brief Destructor
00106  */
00107   virtual ~BinInputStreamAdaptor()
00108   { /* NOP */ }
00109 //@}
00110 
00111 
00112   virtual unsigned int curPos() const
00113   {
00114     return (unsigned int)(_stream->tellg());
00115   }
00116 
00117   virtual unsigned int readBytes (XMLByte * const toFill, 
00118                                   const unsigned int maxToRead)
00119   {
00120 
00121     // ---
00122     // @bug: Due to gcc bug #6746, readsome does not work properly
00123     // with ifstream instances. The following line will fix it.
00124     // ---
00125     if (std::ifstream * fs = dynamic_cast<std::ifstream *>(_stream)) {
00126 
00127       // --
00128       // Read bytes one by one until the end of file or maxToRead
00129       // reached 
00130       // --
00131       unsigned int count = 0;
00132       char * bufferPtr = (char *)toFill;
00133 
00134       while (fs->good() && (count < maxToRead)) {
00135 
00136         *(bufferPtr++) = _stream->get();
00137         ++count;
00138       }
00139 
00140       return count;
00141     }
00142 
00143     else
00144       return _stream->readsome((char *)toFill, maxToRead);
00145   }
00146 
00147 // -------------------------------------------------------------------
00148 // P R O T E C T E D    M E M B E R S
00149 // -------------------------------------------------------------------
00150 protected:
00151 
00152   std::istream * _stream;
00153 
00154 // -------------------------------------------------------------------
00155 
00156 }; // class BinInputStreamAdaptor
00157 
00158 } // namespace qgxml
00159 
00160 
00161 #endif /* __BININPUTSTREAMADAPTOR_H_INCLUDED__ */