Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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>
14
15#include <fstream>
16
17/*---------------------------------------------------------------
18 Class
19 ---------------------------------------------------------------*/
20namespace 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 {
34 fomAppend = 4
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 */
43 {
44 protected:
45 size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
46 size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
47 private:
48 std::fstream m_f; //!< The actual input file stream.
49 public:
50 /** Constructor and open a file
51 * \param fileName The file to be open in this stream
52 * \param mode The open mode: can be an or'd conbination of different values.
53 * \exception std::exception On error creating or accessing the file.
54 * By default the file is opened for open and write and created if not found.
55 */
56 CFileStream( const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite);
57 /** Constructor */
59 virtual ~CFileStream(); //!< Destructor
60
61 /** Opens the file, returning true on success.
62 * \param fileName The file to be open in this stream
63 * \param mode The open mode: can be an or'd conbination of different values.
64 * By default the file is opened for open and write and created if not found.
65 */
66 bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite );
67 void close(); //!< Closes the file
68 bool fileOpenCorrectly(); //!< Returns true if the file was open without errors.
69 bool is_open() { return fileOpenCorrectly(); } //!< Returns true if the file was open without errors.
70 bool checkEOF(); //!< Will be true if EOF has been already reached.
71
72 /** Method for moving to a specified position in the streamed resource.
73 * See documentation of CStream::Seek
74 */
75 uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE;
76
77 uint64_t getTotalBytesCount() MRPT_OVERRIDE; //!< Method for getting the total number of bytes writen to buffer.
78 uint64_t getPosition() MRPT_OVERRIDE; //!< Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one
79 uint64_t getPositionI(); //!< The current Input cursor position, where 0 is the first byte
80 uint64_t getPositionO(); //!< The current Input cursor position, where 0 is the first byte
81
82 /** Reads one string line from the file (until a new-line character)
83 * \return true if a line has been read, false on EOF or error */
84 bool readLine( std::string &str );
85
86 }; // End of class def.
87
88 } // End of namespace
89} // end of namespace
90#endif
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:43
bool is_open()
Returns true if the file was open without errors.
Definition: CFileStream.h:69
bool fileOpenCorrectly()
Returns true if the file was open without errors.
virtual ~CFileStream()
Destructor.
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
bool checkEOF()
Will be true if EOF has been already reached.
void close()
Closes the file.
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:48
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Method for moving to a specified position in the streamed resource.
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Method for getting the total number of bytes writen to buffer.
bool open(const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
Opens the file, returning true on success.
CFileStream()
Constructor.
CFileStream(const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
Constructor and open a file.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:43
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:31
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:30
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
STL namespace.



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 06:31:24 UTC 2023