Main MRPT website > C++ reference
MRPT logo
CFileStream.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CFILESTREAM_H
29 #define CFILESTREAM_H
30 
31 #include <mrpt/utils/CStream.h>
32 #include <mrpt/utils/CUncopiable.h>
33 
34 #include <iostream>
35 
36 /*---------------------------------------------------------------
37  Class
38  ---------------------------------------------------------------*/
39 namespace mrpt
40 {
41  namespace utils
42  {
43  /** File open modes are used in CFileStream
44  * Posible values are:
45  - fomRead
46  - fomWrite (creates the file if it didn't exist, otherwise truncates it).
47  - fomAppend (creates the file if it didn't exist)
48  */
49  typedef int TFileOpenModes;
50  enum {
51  fomRead = 1,
52  fomWrite = 2,
54  };
55 
56  /** This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist.
57  * The default behavior can be change to open as read, write, read and write,... in the constructor.
58  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
59  * \ingroup mrpt_base_grp
60  */
61  class BASE_IMPEXP CFileStream : public CStream, public CUncopiable
62  {
63  protected:
64  /** Method responsible for reading from the stream.
65  */
66  size_t Read(void *Buffer, size_t Count);
67 
68  /** Method responsible for writing to the stream.
69  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
70  */
71  size_t Write(const void *Buffer, size_t Count);
72 
73  private:
74  std::fstream m_f; //!< The actual input file stream.
75 
76  public:
77  /** Constructor and open a file
78  * \param fileName The file to be open in this stream
79  * \param mode The open mode: can be an or'd conbination of different values.
80  * \exception std::exception On error creating or accessing the file.
81  * By default the file is opened for open and write and created if not found.
82  */
83  CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite);
84 
85  /** Constructor
86  */
87  CFileStream();
88 
89 
90  /** Opens the file, returning true on success.
91  * \param fileName The file to be open in this stream
92  * \param mode The open mode: can be an or'd conbination of different values.
93  * By default the file is opened for open and write and created if not found.
94  */
95  bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
96 
97  /** Closes the file
98  */
99  void close();
100 
101  /** Destructor
102  */
103  virtual ~CFileStream();
104 
105  /** Says if file was open successfully or not.
106  */
107  bool fileOpenCorrectly();
108 
109  /** Will be true if EOF has been already reached.
110  */
111  bool checkEOF();
112 
113  /** Method for moving to a specified position in the streamed resource.
114  * See documentation of CStream::Seek
115  */
116  uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
117 
118  /** Method for getting the total number of bytes writen to buffer.
119  */
120  uint64_t getTotalBytesCount();
121 
122  /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
123  */
124  uint64_t getPosition();
125 
126  /** The current Input cursor position, where 0 is the first byte.
127  */
128  uint64_t getPositionI();
129 
130  /** The current Input cursor position, where 0 is the first byte.
131  */
132  uint64_t getPositionO();
133 
134  /** Reads one string line from the file (until a new-line character)
135  * \return true if a line has been read, false on EOF or error.
136  */
137  bool readLine( std::string &str );
138 
139 
140  }; // End of class def.
141 
142  } // End of namespace
143 } // end of namespace
144 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013