00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 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 std::ios_base::openmode TFileOpenModes; 00050 00051 static const TFileOpenModes fomRead = std::ios_base::in; 00052 static const TFileOpenModes fomWrite = std::ios_base::out | std::ios_base::trunc; 00053 static const TFileOpenModes fomAppend = std::ios_base::app | std::ios_base::out; 00054 00055 /** This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist. 00056 * The default behavior can be change to open as read, write, read and write,... in the constructor. 00057 * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream 00058 */ 00059 class MRPTDLLIMPEXP CFileStream : public CStream, public CUncopiable 00060 { 00061 protected: 00062 /** Method responsible for reading from the stream. 00063 */ 00064 size_t Read(void *Buffer, size_t Count); 00065 00066 /** Method responsible for writing to the stream. 00067 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00068 */ 00069 size_t Write(const void *Buffer, size_t Count); 00070 00071 DECLARE_UNCOPIABLE( CFileStream ) 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 size_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning); 00117 00118 /** Method for getting the total number of bytes writen to buffer. 00119 */ 00120 size_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 size_t getPosition(); 00125 00126 /** The current Input cursor position, where 0 is the first byte. 00127 */ 00128 size_t getPositionI(); 00129 00130 /** The current Input cursor position, where 0 is the first byte. 00131 */ 00132 size_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.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:20:53 EDT 2009 |