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

AbstractFile.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------+
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  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 licence. |
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 __ABSTRACTFILE_H_INCLUDED__
00029 #define __ABSTRACTFILE_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file   AbstractFile.H
00034  * @brief  Header file of class qgar::AbstractFile.
00035  *
00036  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00037  * @date   Jul 02, 2001  17:13
00038  * @since  Qgar 1.0
00039  */
00040 
00041 
00042 // For RCS/CVS use: Do not delete
00043 /* $Id: AbstractFile.H,v 1.15 2005/07/13 16:29:00 masini Exp $ */
00044 
00045 
00046 
00047 // STL
00048 #include <fstream>
00049 // QGAR
00050 #include <qgarlib/QgarErrorIO.H>
00051 #include <qgarlib/QgarErrorInvalidArg.H>
00052 #include <qgarlib/QgarErrorUser.H>
00053 
00054 
00055 
00056 namespace qgar
00057 {
00058 
00059 
00060 // -------------------------------------------------------------------
00061 // T Y P E S   R E L A T E D   T O   F I L E S
00062 // -------------------------------------------------------------------
00063 
00064 /**
00065  * @name Types related to files
00066  * @ingroup IO_FILE
00067  */
00068 
00069 /**
00070  * @brief File status.
00071  */
00072 enum QGEfileStatus
00073 {
00074 QGE_FILE_STATUS_CLOSED,
00075 QGE_FILE_STATUS_READ_ONLY,
00076 QGE_FILE_STATUS_WRITE_ONLY,
00077 QGE_FILE_STATUS_APPEND,
00078 QGE_FILE_STATUS_READ_WRITE,
00079 QGE_FILE_STATUS_READ_APPEND
00080 };
00081 
00082 //@}
00083 
00084 // -------------------------------------------------------------------
00085 // E N D   O F   T Y P E S   R E L A T E D   T O   F I L E S
00086 // -------------------------------------------------------------------
00087 
00088 
00089 
00090 
00091 /**
00092  * @class AbstractFile AbstractFile.H "qgarlib/AbstractFile.H"
00093  * @ingroup IO_FILE
00094  * @brief Base class implementing common tools to conveniently
00095  *        define and use files in the Qgar way.
00096  * 
00097  * @warning
00098  * This is an abstract class.
00099  * Pure virtual functions:
00100  * - qgar::AbstractFile::readHeader
00101  * - qgar::AbstractFile::writeHeader
00102  * - qgar::AbstractFile::writeFooter
00103  *
00104  * @author   <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00105  * @date     Jul 02, 2001  17:13
00106  * @since    Qgar 1.0
00107  */
00108 class AbstractFile
00109 {
00110 // -------------------------------------------------------------------
00111 // P U B L I C    M E M B E R S
00112 // -------------------------------------------------------------------
00113 public:
00114 
00115   /** @name Destructor */
00116   //        ==========
00117   //@{
00118 
00119   /**
00120    * @brief Virtual destructor.
00121    *
00122    * Free space allocated to character string representing
00123    * the name of the file
00124    */
00125   virtual ~AbstractFile();
00126 
00127   //@}
00128 
00129 
00130   /** @name Access to file characteristics */
00131   //        ==============================
00132   //@{
00133 
00134   /**
00135    * @brief Get file stream.
00136    *
00137    * When saving the stream, usual I/O operations apply to the
00138    * associated file, depending on the opening mode.
00139    */
00140   inline std::fstream& fileStream();
00141 
00142   /**
00143    * @brief Get file name.
00144    */
00145   inline const char* const fileName() const;
00146 
00147   /**
00148    * @brief Get current file status.
00149    */
00150   inline QGEfileStatus fileStatus() const;
00151 
00152   //@}
00153 
00154 
00155   /** @name Opening */
00156   //        =======
00157   //@{
00158 
00159   /**
00160    * @brief Open in <b>read-only</b> mode.
00161    *
00162    * The file pointer is set to the first character after the header.
00163    *
00164    * @warning The file header is read using function
00165    * qgar::AbstractFile::readHeader.
00166    *
00167    * @exception qgar::QgarErrorIO (file cannot be opened)
00168    */
00169   virtual void openRONLY() throw(QgarErrorIO);
00170 
00171   /**
00172    * @brief Open in <b>write-only</b> mode.
00173    *
00174    * @warning The file is new and a header is written into the
00175    * file using function qgar::AbstractFile::writeHeader.
00176    * If a file with given name already exists, its content is lost.
00177    *
00178    * @exception qgar::QgarErrorIO (file cannot be opened)
00179    */
00180   virtual void openWONLY() throw(QgarErrorIO);
00181 
00182   /**
00183    * @brief Open in <b>append</b> mode.
00184    *
00185    * The eventual content of the file is preserved and the file pointer
00186    * is set to the end of the file.
00187    *
00188    * @warning The file header is read using function
00189    * qgar::AbstractFile::readHeader.
00190    *
00191    * @exception qgar::QgarErrorIO (file cannot be opened)
00192    */
00193   virtual void openAPPEND() throw(QgarErrorIO);
00194 
00195   /**
00196    * @brief Open in <b>read-write</b> mode.
00197    *
00198    * @warning The file is new and a header is written into the
00199    * file using function qgar::AbstractFile::writeHeader.
00200    * If a file with given name already exists, its content is lost.
00201    *
00202    * @exception qgar::QgarErrorIO (file cannot be opened)
00203    */
00204   virtual void openRW() throw(QgarErrorIO);
00205 
00206   /**
00207    * @brief Open in <b>read-append</b> mode.
00208    *
00209    * The file pointer is set to the end of the file.
00210    *
00211    * @warning The file header is read using function
00212    * qgar::AbstractFile::readHeader.
00213    *
00214    * @exception qgar::QgarErrorIO (file cannot be opened)
00215    */
00216   virtual void openRA() throw(QgarErrorIO);
00217 
00218   //@}
00219 
00220 
00221   /** @name Closing */
00222   //        =======
00223   //@{
00224 
00225   /**
00226    * @brief Close the file.
00227    *
00228    * No effect is the file is already closed.
00229    *
00230    * @warning If the file is currently open in <b>write-only</b>
00231    * or <b>read-write</b> modes, a footer is written into the file
00232    * using function qgar::AbstractFile::writeFooter.
00233    */
00234   virtual void close();
00235 
00236   //@}
00237 
00238 
00239   /** @name Checking status */
00240   //        ===============
00241   //@{
00242 
00243   /**
00244    * @brief Abort if the file is not open
00245    *   in a mode allowing reading.
00246    *
00247    * @exception qgar::QgarErrorUser (file not open in read mode)
00248    */
00249   virtual void isOpenR() throw(QgarErrorUser);
00250  
00251  /**
00252    * @brief Abort if the file is not open
00253    *   in a mode allowing writing.
00254    *
00255    * @exception qgar::QgarErrorUser (file not open in write mode)
00256    */
00257   virtual void isOpenW() throw(QgarErrorUser);
00258 
00259   /**
00260    * @brief Abort if the file is not open
00261    *  in a mode allowing appending.
00262    *
00263    * @exception qgar::QgarErrorUser (file not open in append mode)
00264    */
00265   virtual void isOpenA() throw(QgarErrorUser);
00266 
00267   /**
00268    * @brief Abort if the file is not open
00269    *   in <b>read-write</b> mode.
00270    *
00271    * @exception qgar::QgarErrorUser (file not open in read/write mode)
00272    */
00273   virtual void isOpenRW() throw(QgarErrorUser);
00274 
00275   /**
00276    * @brief Abort if the file is not open
00277    *   in <b>readi-append</b> mode.
00278    *
00279    * @exception qgar::QgarErrorUser (file not open in read/append mode)
00280    */
00281   virtual void isOpenRA() throw(QgarErrorUser);
00282 
00283   //@}
00284 
00285 
00286 // -------------------------------------------------------------------
00287 // P R O T E C T E D    M E M B E R S
00288 // -------------------------------------------------------------------
00289 protected:
00290 
00291   //  ============================================
00292   /** @name Constructors
00293       Constructors belong to the protected section
00294       so that the class cannot be instantiated.
00295    */
00296   //  ============================================
00297   //@{
00298 
00299   /**
00300    * @brief Create a file of given name.
00301    * @param aFileName name of the file
00302    *
00303    * @warning The file is closed and must be opened to be used.
00304    *
00305    * @exception qgar::QgarErrorInvalidArg (incorrect file name)
00306    */
00307   AbstractFile(const char* aFileName) throw(QgarErrorInvalidArg);
00308 
00309   //@}
00310 
00311 
00312   /** @name Headers & footers */
00313   //        =================
00314   //@{
00315 
00316   /**
00317    * @brief Read header of the file (pure virtual function).
00318    *
00319    * Automatically performed when the file is opened for reading,
00320    * appending or reading/appending.
00321    */
00322   virtual void readHeader() = 0;
00323 
00324   /**
00325    * @brief Write header of the file (pure virtual function).
00326    *
00327    * Automatically performed when the file is opened for writing only
00328    * or reading/writing.
00329    */
00330   virtual void writeHeader() = 0;
00331 
00332   /**
00333    * @brief Write footer of the file (pure virtual function).
00334    *
00335    * Automatically performed when the file is closed.
00336    */
00337   virtual void writeFooter() = 0;
00338 
00339   //@}
00340 
00341 
00342   /** @name Representation of a file */
00343   //        ========================
00344   //@{
00345 
00346   /**
00347    * @brief File name.
00348    */
00349   char* _fileName;
00350 
00351   /**
00352    * @brief File status.
00353    */
00354   QGEfileStatus _fileStatus;
00355 
00356   /**
00357    * @brief Associated file stream.
00358    */
00359   std::fstream _fileStream;
00360 
00361   //@}
00362 
00363 
00364 // -------------------------------------------------------------------
00365 
00366 }; // class AbstractFile
00367 
00368 
00369 
00370 
00371 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00372 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00373 // I N L I N E    F U N C T I O N S    I M P L E M E N T A T I O N
00374 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00375 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00376 
00377 
00378 // ==============================
00379 // ACCESS TO FILE CHARACTERISTICS
00380 // ==============================
00381 
00382 
00383 // GET FILE STREAM
00384 
00385 inline std::fstream&
00386 AbstractFile::fileStream()
00387 {
00388   return _fileStream;
00389 }
00390 
00391 
00392 // GET FILE NAME
00393 
00394 inline const char* const
00395 AbstractFile::fileName() const
00396 {
00397   return _fileName;
00398 }
00399 
00400 
00401 // GET CURRENT FILE STATUS
00402 
00403 inline QGEfileStatus
00404 AbstractFile::fileStatus() const
00405 {
00406   return _fileStatus;
00407 }
00408 
00409 
00410 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00411 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00412 
00413 
00414 } // namespace qgar 
00415 
00416 
00417 #endif /* __ABSTRACTFILE_H_INCLUDED__ */