Main MRPT website > C++ reference for MRPT 1.9.9
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 
14 #include <fstream>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20 /** File open modes are used in CFileStream
21  * Posible values are:
22  - fomRead
23  - fomWrite (creates the file if it didn't exist, otherwise truncates it).
24  - fomAppend (creates the file if it didn't exist)
25  */
26 typedef int TFileOpenModes;
27 enum
28 {
29  fomRead = 1,
30  fomWrite = 2,
32 };
33 
34 /** This CStream derived class allow using a file as a read/write binary stream,
35  * creating it if the file didn't exist.
36  * The default behavior can be change to open as read, write, read and
37  * write,... in the constructor.
38  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
39  * \ingroup mrpt_base_grp
40  */
41 class CFileStream : public CStream
42 {
43  protected:
44  size_t Read(void* Buffer, size_t Count) override;
45  size_t Write(const void* Buffer, size_t Count) override;
46 
47  private:
48  /** The actual input file stream. */
49  std::fstream m_f;
50 
51  public:
52  /** Constructor and open a file
53  * \param fileName The file to be open in this stream
54  * \param mode The open mode: can be an or'd conbination of different
55  * values.
56  * \exception std::exception On error creating or accessing the file.
57  * By default the file is opened for open and write and created if not
58  * found.
59  */
61  const std::string& fileName, TFileOpenModes mode = fomRead | fomWrite);
62  /** Constructor */
63  CFileStream();
64 
65  CFileStream(const CFileStream&) = delete;
66  CFileStream& operator=(const CFileStream&) = delete;
67 
68  /** Destructor */
69  virtual ~CFileStream();
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
74  * values.
75  * By default the file is opened for open and write and created if not
76  * found.
77  */
78  bool open(
79  const std::string& fileName, TFileOpenModes mode = fomRead | fomWrite);
80  /** Closes the file */
81  void close();
82  /** Returns true if the file was open without errors. */
83  bool fileOpenCorrectly();
84  /** Returns true if the file was open without errors. */
85  bool is_open() { return fileOpenCorrectly(); }
86  /** Will be true if EOF has been already reached. */
87  bool checkEOF();
88 
89  /** Method for moving to a specified position in the streamed resource.
90  * See documentation of CStream::Seek
91  */
92  uint64_t Seek(
93  uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
94 
95  /** Method for getting the total number of bytes writen to buffer. */
96  uint64_t getTotalBytesCount() override;
97  /** Method for getting the current cursor position, where 0 is the first
98  * byte and TotalBytesCount-1 the last one */
99  uint64_t getPosition() override;
100  /** The current Input cursor position, where 0 is the first byte */
102  /** The current Input cursor position, where 0 is the first byte */
104 
105  /** Reads one string line from the file (until a new-line character)
106  * \return true if a line has been read, false on EOF or error */
107  bool readLine(std::string& str);
108 
109 }; // End of class def.
110 static_assert(
113  "Copy Check");
114 } // End of namespace
115 } // end of namespace
116 #endif
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
bool fileOpenCorrectly()
Returns true if the file was open without errors.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:49
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:45
void close()
Closes the file.
Definition: CFileStream.cpp:82
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:41
uint64_t getPositionI()
The current Input cursor position, where 0 is the first byte.
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:26
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
uint64_t getPosition() override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
bool is_open()
Returns true if the file was open without errors.
Definition: CFileStream.h:85
uint64_t getPositionO()
The current Input cursor position, where 0 is the first byte.
CFileStream & operator=(const CFileStream &)=delete
GLsizei const GLchar ** string
Definition: glext.h:4101
virtual ~CFileStream()
Destructor.
Definition: CFileStream.cpp:86
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes writen to buffer.
CFileStream()
Constructor.
Definition: CFileStream.cpp:25
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileStream.cpp:91
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Method for moving to a specified position in the streamed resource.
GLsizei const GLfloat * value
Definition: glext.h:4117
bool checkEOF()
Will be true if EOF has been already reached.
bool open(const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
Opens the file, returning true on success.
Definition: CFileStream.cpp:55



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019