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-2018, 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 #pragma once
10 
11 #include <mrpt/io/CStream.h>
12 #include <fstream>
13 
14 namespace mrpt
15 {
16 namespace io
17 {
18 /** File open modes are used in CFileStream
19  * Posible values are:
20  - fomRead
21  - fomWrite (creates the file if it didn't exist, otherwise truncates it).
22  - fomAppend (creates the file if it didn't exist)
23  */
24 using TFileOpenModes = int;
25 enum
26 {
27  fomRead = 1,
28  fomWrite = 2,
30 };
31 
32 /** This CStream derived class allow using a file as a read/write binary stream,
33  * creating it if the file didn't exist.
34  * The default behavior can be change to open as read, write, read and
35  * write,... in the constructor.
36  * \sa CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream
37  * \ingroup mrpt_io_grp
38  */
39 class CFileStream : public CStream
40 {
41  private:
42  /** The actual input file stream. */
43  std::fstream m_f;
44 
45  public:
46  /** Constructor and open a file
47  * \param fileName The file to be open in this stream
48  * \param mode The open mode: can be an or'd conbination of different
49  * 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
52  * found.
53  */
55  const std::string& fileName, TFileOpenModes mode = fomRead | fomWrite);
56  /** Constructor */
57  CFileStream();
58 
59  CFileStream(const CFileStream&) = delete;
60  CFileStream& operator=(const CFileStream&) = delete;
61 
62  /** Destructor */
63  virtual ~CFileStream();
64 
65  size_t Read(void* Buffer, size_t Count) override;
66  size_t Write(const void* Buffer, size_t Count) override;
67 
68  /** Opens the file, returning true on success.
69  * \param fileName The file to be open in this stream
70  * \param mode The open mode: can be an or'd conbination of different
71  * values.
72  * By default the file is opened for open and write and created if not
73  * found.
74  */
75  bool open(
76  const std::string& fileName, TFileOpenModes mode = fomRead | fomWrite);
77  /** Closes the file */
78  void close();
79  /** Returns true if the file was open without errors. */
80  bool fileOpenCorrectly() const;
81  /** Returns true if the file was open without errors. */
82  bool is_open() { return fileOpenCorrectly(); }
83  /** Will be true if EOF has been already reached. */
84  bool checkEOF();
85  /** Resets stream error status bits (e.g. after an EOF) */
86  void clearError();
87 
88  uint64_t Seek(
89  int64_t off, CStream::TSeekOrigin org = sFromBeginning) override;
90  uint64_t getTotalBytesCount() const override;
91  // See docs in base class
92  uint64_t getPosition() const override;
93  /** The current Input cursor position, where 0 is the first byte */
95  /** The current Input cursor position, where 0 is the first byte */
97 
98  /** Reads one string line from the file (until a new-line character)
99  * \return true if a line has been read, false on EOF or error */
100  bool readLine(std::string& str);
101 
102 }; // End of class def.
103 } // namespace io
104 } // namespace mrpt
mrpt::io::CFileStream::readLine
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
Definition: CFileStream.cpp:192
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
mrpt::io::CFileStream::~CFileStream
virtual ~CFileStream()
Destructor.
Definition: CFileStream.cpp:83
mrpt::io::CFileStream::is_open
bool is_open()
Returns true if the file was open without errors.
Definition: CFileStream.h:82
mrpt::io::CFileStream
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:39
mrpt::io::CFileStream::Seek
uint64_t Seek(int64_t off, CStream::TSeekOrigin org=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource.
Definition: CFileStream.cpp:112
mrpt::io::CFileStream::getPositionI
uint64_t getPositionI()
The current Input cursor position, where 0 is the first byte.
Definition: CFileStream.cpp:166
mrpt::io::CFileStream::close
void close()
Closes the file.
Definition: CFileStream.cpp:79
mrpt::io::CFileStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
Definition: CFileStream.cpp:141
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::io::CFileStream::fileOpenCorrectly
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
Definition: CFileStream.cpp:188
mrpt::io::CFileStream::getPosition
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
Definition: CFileStream.cpp:154
mrpt::io::CFileStream::checkEOF
bool checkEOF()
Will be true if EOF has been already reached.
Definition: CFileStream.cpp:204
mrpt::io::CFileStream::Read
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileStream.cpp:88
mrpt::io::CFileStream::getPositionO
uint64_t getPositionO()
The current Input cursor position, where 0 is the first byte.
Definition: CFileStream.cpp:177
mrpt::io::fomWrite
@ fomWrite
Definition: CFileStream.h:28
mrpt::io::fomAppend
@ fomAppend
Definition: CFileStream.h:29
mrpt::io::TFileOpenModes
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:24
mrpt::io::CFileStream::m_f
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:43
mrpt::io::CStream::sFromBeginning
@ sFromBeginning
Definition: io/CStream.h:36
mrpt::io::CFileStream::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CFileStream.cpp:99
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
CStream.h
mrpt::io::CFileStream::open
bool open(const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
Opens the file, returning true on success.
Definition: CFileStream.cpp:56
mrpt::io::CFileStream::CFileStream
CFileStream()
Constructor.
Definition: CFileStream.cpp:28
mrpt::io::CFileStream::clearError
void clearError()
Resets stream error status bits (e.g.
Definition: CFileStream.cpp:210
mode
GLint mode
Definition: glext.h:5669
mrpt::io::fomRead
@ fomRead
Definition: CFileStream.h:27
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::io::CFileStream::operator=
CFileStream & operator=(const CFileStream &)=delete
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST