Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 namespace mrpt
18 {
19  namespace utils
20  {
21  /** File open modes are used in CFileStream
22  * Posible values are:
23  - fomRead
24  - fomWrite (creates the file if it didn't exist, otherwise truncates it).
25  - fomAppend (creates the file if it didn't exist)
26  */
27  typedef int TFileOpenModes;
28  enum {
29  fomRead = 1,
30  fomWrite = 2,
32  };
33 
34  /** This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist.
35  * The default behavior can be change to open as read, write, read and write,... in the constructor.
36  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
37  * \ingroup mrpt_base_grp
38  */
39  class BASE_IMPEXP CFileStream : public CStream, public CUncopiable
40  {
41  protected:
42  size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
43  size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
44  private:
45  std::fstream m_f; //!< The actual input file stream.
46  public:
47  /** Constructor and open a file
48  * \param fileName The file to be open in this stream
49  * \param mode The open mode: can be an or'd conbination of different values.
50  * \exception std::exception On error creating or accessing the file.
51  * By default the file is opened for open and write and created if not found.
52  */
54  /** Constructor */
55  CFileStream();
56  virtual ~CFileStream(); //!< Destructor
57 
58  /** Opens the file, returning true on success.
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  * By default the file is opened for open and write and created if not found.
62  */
63  bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
64  void close(); //!< Closes the file
65  bool fileOpenCorrectly(); //!< Returns true if the file was open without errors.
66  bool is_open() { return fileOpenCorrectly(); } //!< Returns true if the file was open without errors.
67  bool checkEOF(); //!< Will be true if EOF has been already reached.
68  void clearError(); //!< Resets stream error status bits (e.g. after an EOF)
69 
70  /** Method for moving to a specified position in the streamed resource.
71  * See documentation of CStream::Seek
72  */
73  uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE;
74 
75  uint64_t getTotalBytesCount() MRPT_OVERRIDE; //!< Method for getting the total number of bytes writen to buffer.
76  uint64_t getPosition() MRPT_OVERRIDE; //!< Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one
77  uint64_t getPositionI(); //!< The current Input cursor position, where 0 is the first byte
78  uint64_t getPositionO(); //!< The current Input cursor position, where 0 is the first byte
79 
80  /** Reads one string line from the file (until a new-line character)
81  * \return true if a line has been read, false on EOF or error */
82  bool readLine( std::string &str );
83 
84  }; // End of class def.
85 
86  } // End of namespace
87 } // end of namespace
88 #endif
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:45
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:39
STL namespace.
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:27
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
__int64 int64_t
Definition: rptypes.h:51
bool is_open()
Returns true if the file was open without errors.
Definition: CFileStream.h:66
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:30
GLsizei const GLchar ** string
Definition: glext.h:3919
GLint mode
Definition: glext.h:5078
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019