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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020