Main MRPT website > C++ reference
MRPT logo

CFileStream.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CFILESTREAM_H
00029 #define  CFILESTREAM_H
00030 
00031 #include <mrpt/utils/CStream.h>
00032 #include <mrpt/utils/CUncopiable.h>
00033 
00034 #include <iostream>
00035 
00036 /*---------------------------------------------------------------
00037         Class
00038   ---------------------------------------------------------------*/
00039 namespace mrpt
00040 {
00041         namespace utils
00042         {
00043                 /** File open modes are used in CFileStream
00044                   *  Posible values are:
00045                         - fomRead
00046                         - fomWrite (creates the file if it didn't exist, otherwise truncates it).
00047                         - fomAppend (creates the file if it didn't exist)
00048                   */
00049                 typedef int TFileOpenModes;
00050                 enum {
00051                         fomRead   = 1,
00052                         fomWrite  = 2,
00053                         fomAppend = 4
00054                 };
00055 
00056                 /** This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist.
00057                  *   The default behavior can be change to open as read, write, read and write,... in the constructor.
00058                  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
00059                  */
00060                 class BASE_IMPEXP  CFileStream : public CStream, public CUncopiable
00061                 {
00062                 protected:
00063                          /** Method responsible for reading from the stream.
00064                          */
00065                         size_t  Read(void *Buffer, size_t Count);
00066 
00067                         /** Method responsible for writing to the stream.
00068                          *  Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
00069                          */
00070                         size_t  Write(const void *Buffer, size_t Count);
00071 
00072                 private:
00073                         std::fstream    m_f;            //!< The actual input file stream.
00074 
00075                 public:
00076                          /** Constructor and open a file
00077                           * \param fileName The file to be open in this stream
00078                           * \param mode The open mode: can be an or'd conbination of different values.
00079                           * \exception std::exception On error creating or accessing the file.
00080                           *  By default the file is opened for open and write and created if not found.
00081                           */
00082                         CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite);
00083 
00084                         /** Constructor
00085                           */
00086                         CFileStream();
00087 
00088 
00089                         /** Opens the file, returning true on success.
00090                           * \param fileName The file to be open in this stream
00091                           * \param mode The open mode: can be an or'd conbination of different values.
00092                           *  By default the file is opened for open and write and created if not found.
00093                           */
00094                         bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
00095 
00096                         /** Closes the file
00097                           */
00098                         void close();
00099 
00100                          /** Destructor
00101                          */
00102                          virtual ~CFileStream();
00103 
00104                          /** Says if file was open successfully or not.
00105                           */
00106                          bool  fileOpenCorrectly();
00107 
00108                          /** Will be true if EOF has been already reached.
00109                            */
00110                          bool checkEOF();
00111 
00112                         /** Method for moving to a specified position in the streamed resource.
00113                          *   See documentation of CStream::Seek
00114                          */
00115                         uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
00116 
00117                         /** Method for getting the total number of bytes writen to buffer.
00118                          */
00119                         uint64_t getTotalBytesCount();
00120 
00121                         /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
00122                          */
00123                         uint64_t getPosition();
00124 
00125                         /** The current Input cursor position, where 0 is the first byte.
00126                          */
00127                         uint64_t getPositionI();
00128 
00129                         /** The current Input cursor position, where 0 is the first byte.
00130                          */
00131                         uint64_t getPositionO();
00132 
00133                         /** Reads one string line from the file (until a new-line character)
00134                           * \return true if a line has been read, false on EOF or error.
00135                           */
00136                         bool readLine( std::string &str );
00137 
00138 
00139                 }; // End of class def.
00140 
00141         } // End of namespace
00142 } // end of namespace
00143 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011