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



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018