Main MRPT website > C++ reference for MRPT 1.9.9
CFileStream.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #ifdef _MSC_VER
13 #define _SCL_SECURE_NO_WARNINGS
14 #endif
15 
16 #include <mrpt/utils/CFileStream.h>
17 #include <mrpt/system/os.h>
18 
19 using namespace mrpt::utils;
20 using namespace std;
21 
22 /*---------------------------------------------------------------
23  Constructor
24  ---------------------------------------------------------------*/
26 /*---------------------------------------------------------------
27  Constructor
28  ---------------------------------------------------------------*/
29 CFileStream::CFileStream(const string& fileName, TFileOpenModes mode_) : m_f()
30 {
32 
33  std::ios_base::openmode mode = std::ios_base::in;
34  if (mode_ == fomRead)
36  else if (mode_ == fomWrite)
37  mode = std::ios_base::out | std::ios_base::trunc;
38  else if (mode_ == fomAppend)
39  mode = std::ios_base::app | std::ios_base::out;
40  else if (mode_ == (fomRead | fomWrite))
41  mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc;
42  else if (mode_ == (fomAppend | fomWrite))
43  mode = std::ios_base::in | std::ios_base::out | std::ios_base::app;
44 
45  // Try to open the file:
46  m_f.open(fileName.c_str(), mode);
47  if (!m_f.is_open())
48  THROW_EXCEPTION_FMT("Error creating/opening: '%s'", fileName.c_str());
49  MRPT_END
50 }
51 
52 /*---------------------------------------------------------------
53  open
54  ---------------------------------------------------------------*/
55 bool CFileStream::open(const std::string& fileName, TFileOpenModes mode_)
56 {
58 
59  std::ios_base::openmode mode = std::ios_base::in;
60  if (mode_ == fomRead)
62  else if (mode_ == fomWrite)
63  mode = std::ios_base::out | std::ios_base::trunc;
64  else if (mode_ == fomAppend)
65  mode = std::ios_base::app | std::ios_base::out;
66  else if (mode_ == (fomRead | fomWrite))
67  mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc;
68  else if (mode_ == (fomAppend | fomWrite))
69  mode = std::ios_base::in | std::ios_base::out | std::ios_base::app;
70 
71  if (m_f.is_open()) m_f.close();
72 
73  m_f.open(fileName.c_str(), ios_base::binary | mode);
74  return m_f.is_open();
75 
76  MRPT_END
77 }
78 
79 /*---------------------------------------------------------------
80  close
81  ---------------------------------------------------------------*/
82 void CFileStream::close() { m_f.close(); }
83 /*---------------------------------------------------------------
84  Destructor
85  ---------------------------------------------------------------*/
87 /*---------------------------------------------------------------
88  Read
89  Reads bytes from the stream into Buffer
90  ---------------------------------------------------------------*/
91 size_t CFileStream::Read(void* Buffer, size_t Count)
92 {
93  if (!m_f.is_open()) return 0;
94  m_f.read(static_cast<char*>(Buffer), Count);
95  return m_f.fail() ? 0 : Count;
96 }
97 
98 /*---------------------------------------------------------------
99  Write
100  Writes a block of bytes to the stream.
101  ---------------------------------------------------------------*/
102 size_t CFileStream::Write(const void* Buffer, size_t Count)
103 {
104  if (!m_f.is_open()) return 0;
105 
106  m_f.write(static_cast<const char*>(Buffer), Count);
107  return m_f.fail() ? 0 : Count;
108 }
109 
110 /*---------------------------------------------------------------
111  Seek
112  Method for moving to a specified position in the streamed resource.
113  See documentation of CStream::Seek
114  ---------------------------------------------------------------*/
116 {
117  if (!m_f.is_open()) return 0;
118 
119  fstream::off_type offset = Offset;
120  fstream::seekdir way;
121 
122  switch (Origin)
123  {
124  case sFromBeginning:
125  way = ios_base::beg;
126  break;
127  case sFromCurrent:
128  way = ios_base::cur;
129  break;
130  case sFromEnd:
131  way = ios_base::end;
132  break;
133  default:
134  THROW_EXCEPTION("Invalid value for 'Origin'");
135  }
136 
137  m_f.seekp(offset, way);
138  m_f.seekg(offset, way);
139 
140  return getPosition();
141 }
142 
143 /*---------------------------------------------------------------
144  getTotalBytesCount
145  ---------------------------------------------------------------*/
147 {
148  if (!fileOpenCorrectly()) return 0;
149 
150  uint64_t previousPos = getPosition();
151  uint64_t fileSize = Seek(0, sFromEnd);
152  Seek(previousPos);
153  return fileSize;
154 }
155 
156 /*---------------------------------------------------------------
157  getPosition
158  ---------------------------------------------------------------*/
160 {
161  if (m_f.is_open())
162  return m_f.tellg();
163  else
164  return 0;
165 }
166 
167 /*---------------------------------------------------------------
168  getPositionI
169  ---------------------------------------------------------------*/
171 {
172  if (m_f.is_open())
173  return m_f.tellg();
174  else
175  return 0;
176 }
177 
178 /*---------------------------------------------------------------
179  getPositionO
180  ---------------------------------------------------------------*/
182 {
183  if (m_f.is_open())
184  return m_f.tellp();
185  else
186  return 0;
187 }
188 
189 /*---------------------------------------------------------------
190  fileOpenCorrectly
191  ---------------------------------------------------------------*/
192 bool CFileStream::fileOpenCorrectly() { return m_f.is_open(); }
193 /*---------------------------------------------------------------
194  readLine
195  ---------------------------------------------------------------*/
196 bool CFileStream::readLine(string& str)
197 {
198  str = string(); // clear() is not defined in VC6
199  if (!m_f.is_open()) return false;
200 
201  std::getline(m_f, str);
202  return !m_f.fail() && !m_f.eof();
203 }
204 
205 /*---------------------------------------------------------------
206  checkEOF
207  ---------------------------------------------------------------*/
209 {
210  if (!m_f.is_open()) return true;
211  return m_f.eof();
212 }
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.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
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
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLintptr offset
Definition: glext.h:3925
uint64_t getPositionI()
The current Input cursor position, where 0 is the first byte.
STL namespace.
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:26
uint64_t getPosition() override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
#define MRPT_END
GLuint GLuint end
Definition: glext.h:3528
uint64_t getPositionO()
The current Input cursor position, where 0 is the first byte.
GLsizei const GLchar ** string
Definition: glext.h:4101
virtual ~CFileStream()
Destructor.
Definition: CFileStream.cpp:86
#define MRPT_START
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
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
GLuint in
Definition: glext.h:7274
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Method for moving to a specified position in the streamed resource.
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