00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 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 BASE_IMPEXP 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 private: 00072 std::fstream m_f; //!< The actual input file stream. 00073 00074 public: 00075 /** Constructor and open a file 00076 * \param fileName The file to be open in this stream 00077 * \param mode The open mode: can be an or'd conbination of different values. 00078 * \exception std::exception On error creating or accessing the file. 00079 * By default the file is opened for open and write and created if not found. 00080 */ 00081 CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite); 00082 00083 /** Constructor 00084 */ 00085 CFileStream(); 00086 00087 00088 /** Opens the file, returning true on success. 00089 * \param fileName The file to be open in this stream 00090 * \param mode The open mode: can be an or'd conbination of different values. 00091 * By default the file is opened for open and write and created if not found. 00092 */ 00093 bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite ); 00094 00095 /** Closes the file 00096 */ 00097 void close(); 00098 00099 /** Destructor 00100 */ 00101 virtual ~CFileStream(); 00102 00103 /** Says if file was open successfully or not. 00104 */ 00105 bool fileOpenCorrectly(); 00106 00107 /** Will be true if EOF has been already reached. 00108 */ 00109 bool checkEOF(); 00110 00111 /** Method for moving to a specified position in the streamed resource. 00112 * See documentation of CStream::Seek 00113 */ 00114 size_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning); 00115 00116 /** Method for getting the total number of bytes writen to buffer. 00117 */ 00118 size_t getTotalBytesCount(); 00119 00120 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. 00121 */ 00122 size_t getPosition(); 00123 00124 /** The current Input cursor position, where 0 is the first byte. 00125 */ 00126 size_t getPositionI(); 00127 00128 /** The current Input cursor position, where 0 is the first byte. 00129 */ 00130 size_t getPositionO(); 00131 00132 /** Reads one string line from the file (until a new-line character) 00133 * \return true if a line has been read, false on EOF or error. 00134 */ 00135 bool readLine( std::string &str ); 00136 00137 00138 }; // End of class def. 00139 00140 } // End of namespace 00141 } // end of namespace 00142 #endif
| Page generated by Doxygen 1.6.1 for MRPT 0.9.0 SVN: at Mon Jun 7 06:47:58 UTC 2010 |
