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://www.mrpt.org/                                |
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                  * \ingroup mrpt_base_grp
00060                  */
00061                 class BASE_IMPEXP  CFileStream : public CStream, public CUncopiable
00062                 {
00063                 protected:
00064                          /** Method responsible for reading from the stream.
00065                          */
00066                         size_t  Read(void *Buffer, size_t Count);
00067 
00068                         /** Method responsible for writing to the stream.
00069                          *  Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
00070                          */
00071                         size_t  Write(const void *Buffer, size_t Count);
00072 
00073                 private:
00074                         std::fstream    m_f;            //!< The actual input file stream.
00075 
00076                 public:
00077                          /** Constructor and open a file
00078                           * \param fileName The file to be open in this stream
00079                           * \param mode The open mode: can be an or'd conbination of different values.
00080                           * \exception std::exception On error creating or accessing the file.
00081                           *  By default the file is opened for open and write and created if not found.
00082                           */
00083                         CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite);
00084 
00085                         /** Constructor
00086                           */
00087                         CFileStream();
00088 
00089 
00090                         /** Opens the file, returning true on success.
00091                           * \param fileName The file to be open in this stream
00092                           * \param mode The open mode: can be an or'd conbination of different values.
00093                           *  By default the file is opened for open and write and created if not found.
00094                           */
00095                         bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
00096 
00097                         /** Closes the file
00098                           */
00099                         void close();
00100 
00101                          /** Destructor
00102                          */
00103                          virtual ~CFileStream();
00104 
00105                          /** Says if file was open successfully or not.
00106                           */
00107                          bool  fileOpenCorrectly();
00108 
00109                          /** Will be true if EOF has been already reached.
00110                            */
00111                          bool checkEOF();
00112 
00113                         /** Method for moving to a specified position in the streamed resource.
00114                          *   See documentation of CStream::Seek
00115                          */
00116                         uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
00117 
00118                         /** Method for getting the total number of bytes writen to buffer.
00119                          */
00120                         uint64_t getTotalBytesCount();
00121 
00122                         /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
00123                          */
00124                         uint64_t getPosition();
00125 
00126                         /** The current Input cursor position, where 0 is the first byte.
00127                          */
00128                         uint64_t getPositionI();
00129 
00130                         /** The current Input cursor position, where 0 is the first byte.
00131                          */
00132                         uint64_t getPositionO();
00133 
00134                         /** Reads one string line from the file (until a new-line character)
00135                           * \return true if a line has been read, false on EOF or error.
00136                           */
00137                         bool readLine( std::string &str );
00138 
00139 
00140                 }; // End of class def.
00141 
00142         } // End of namespace
00143 } // end of namespace
00144 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011