Main MRPT website > C++ reference for MRPT 1.3.2
CFileStream.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CFILESTREAM_H
10 #define CFILESTREAM_H
11 
12 #include <mrpt/utils/CStream.h>
13 #include <mrpt/utils/CUncopiable.h>
14 
15 #include <fstream>
16 
17 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace mrpt
21 {
22  namespace utils
23  {
24  /** File open modes are used in CFileStream
25  * Posible values are:
26  - fomRead
27  - fomWrite (creates the file if it didn't exist, otherwise truncates it).
28  - fomAppend (creates the file if it didn't exist)
29  */
30  typedef int TFileOpenModes;
31  enum {
32  fomRead = 1,
33  fomWrite = 2,
35  };
36 
37  /** This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist.
38  * The default behavior can be change to open as read, write, read and write,... in the constructor.
39  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
40  * \ingroup mrpt_base_grp
41  */
42  class BASE_IMPEXP CFileStream : public CStream, public CUncopiable
43  {
44  protected:
45  /** Method responsible for reading from the stream.
46  */
47  size_t Read(void *Buffer, size_t Count);
48 
49  /** Method responsible for writing to the stream.
50  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
51  */
52  size_t Write(const void *Buffer, size_t Count);
53 
54  private:
55  std::fstream m_f; //!< The actual input file stream.
56 
57  public:
58  /** Constructor and open a file
59  * \param fileName The file to be open in this stream
60  * \param mode The open mode: can be an or'd conbination of different values.
61  * \exception std::exception On error creating or accessing the file.
62  * By default the file is opened for open and write and created if not found.
63  */
64  CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite);
65 
66  /** Constructor
67  */
68  CFileStream();
69 
70 
71  /** Opens the file, returning true on success.
72  * \param fileName The file to be open in this stream
73  * \param mode The open mode: can be an or'd conbination of different values.
74  * By default the file is opened for open and write and created if not found.
75  */
76  bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
77 
78  /** Closes the file
79  */
80  void close();
81 
82  /** Destructor
83  */
84  virtual ~CFileStream();
85 
86  /** Says if file was open successfully or not.
87  */
88  bool fileOpenCorrectly();
89 
90  /** Will be true if EOF has been already reached.
91  */
92  bool checkEOF();
93 
94  /** Method for moving to a specified position in the streamed resource.
95  * See documentation of CStream::Seek
96  */
97  uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
98 
99  /** Method for getting the total number of bytes writen to buffer.
100  */
101  uint64_t getTotalBytesCount();
102 
103  /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
104  */
105  uint64_t getPosition();
106 
107  /** The current Input cursor position, where 0 is the first byte.
108  */
109  uint64_t getPositionI();
110 
111  /** The current Input cursor position, where 0 is the first byte.
112  */
113  uint64_t getPositionO();
114 
115  /** Reads one string line from the file (until a new-line character)
116  * \return true if a line has been read, false on EOF or error.
117  */
118  bool readLine( std::string &str );
119 
120 
121  }; // End of class def.
122 
123  } // End of namespace
124 } // end of namespace
125 #endif
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:55
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:42
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:30
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:30
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.8.12 for MRPT 1.3.2 SVN: at Thu Nov 10 13:22:34 UTC 2016