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>
13 #include <mrpt/utils/CUncopiable.h>
14 
15 #include <fstream>
16 
17 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace 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 {
32  fomRead = 1,
33  fomWrite = 2,
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  */
42  class BASE_IMPEXP CFileStream : public CStream, public CUncopiable
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 */
58  CFileStream();
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
mrpt::utils::CFileStream::m_f
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:48
mrpt::utils::fomRead
Definition: CFileStream.h:32
mrpt::utils::CFileStream
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:42
mrpt::utils::TFileOpenModes
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:30
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:16
CStream.h
mrpt::utils::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
mrpt::utils::CUncopiable
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:30
mrpt::utils::fomWrite
Definition: CFileStream.h:33
mrpt::utils::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
CUncopiable.h
mrpt::utils::fomAppend
Definition: CFileStream.h:34
MRPT_OVERRIDE
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
mrpt::utils::CFileStream::is_open
bool is_open()
Returns true if the file was open without errors.
Definition: CFileStream.h:69



Page generated by Doxygen 1.8.16 for MRPT 1.4.0 SVN: at Mon Oct 14 22:32:58 UTC 2019