Main MRPT website > C++ reference for MRPT 1.9.9
CFileInputStream.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-2018, 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 "io-precomp.h" // Precompiled headers
11 
13 #include <mrpt/core/exceptions.h>
14 
15 using namespace mrpt::io;
16 using namespace std;
17 
18 static_assert(
21  "Copy Check");
22 
23 CFileInputStream::CFileInputStream(const string& fileName) : m_if()
24 {
26 
27  // Try to open the file:
28  // Open for input:
29  if (!open(fileName))
31  "Error trying to open file: '%s'", fileName.c_str());
32 
33  MRPT_END
34 }
35 
36 /*---------------------------------------------------------------
37  Constructor
38  ---------------------------------------------------------------*/
40 /*---------------------------------------------------------------
41  open
42  ---------------------------------------------------------------*/
43 bool CFileInputStream::open(const string& fileName)
44 {
45  // Try to open the file:
46  // Open for input:
47  m_if.open(fileName.c_str(), ios_base::binary | ios_base::in);
48  return m_if.is_open();
49 }
50 
51 /*---------------------------------------------------------------
52  close
53  ---------------------------------------------------------------*/
55 {
56  if (m_if.is_open()) m_if.close();
57 }
58 
59 /*---------------------------------------------------------------
60  Destructor
61  ---------------------------------------------------------------*/
63 /*---------------------------------------------------------------
64  Read
65  Reads bytes from the stream into Buffer
66  ---------------------------------------------------------------*/
67 size_t CFileInputStream::Read(void* Buffer, size_t Count)
68 {
69  if (!m_if.is_open()) return 0;
70 
71  m_if.read(static_cast<char*>(Buffer), Count);
72  return m_if.fail() ? 0 : Count;
73 }
74 
75 /*---------------------------------------------------------------
76  Write
77  Writes a block of bytes to the stream.
78  ---------------------------------------------------------------*/
79 size_t CFileInputStream::Write(const void* Buffer, size_t Count)
80 {
81  MRPT_UNUSED_PARAM(Buffer);
82  MRPT_UNUSED_PARAM(Count);
83  THROW_EXCEPTION("Trying to write to a read file stream.");
84 }
85 
86 /*---------------------------------------------------------------
87  Seek
88  Method for moving to a specified position in the streamed resource.
89  See documentation of CStream::Seek
90  ---------------------------------------------------------------*/
92 {
93  if (!m_if.is_open()) return 0;
94 
95  ifstream::off_type offset = Offset;
96  ifstream::seekdir way;
97 
98  switch (Origin)
99  {
100  case sFromBeginning:
101  way = ios_base::beg;
102  break;
103  case sFromCurrent:
104  way = ios_base::cur;
105  break;
106  case sFromEnd:
107  way = ios_base::end;
108  break;
109  default:
110  THROW_EXCEPTION("Invalid value for 'Origin'");
111  }
112 
113  m_if.seekg(offset, way);
114 
115  return getPosition();
116 }
117 
118 /*---------------------------------------------------------------
119  getTotalBytesCount
120  ---------------------------------------------------------------*/
122 {
123  if (!fileOpenCorrectly()) return 0;
124 
125  auto& f = const_cast<std::ifstream&>(m_if);
126 
127  const uint64_t previousPos = f.tellg();
128  f.seekg(0, ios_base::end);
129  uint64_t fileSize = f.tellg();
130  f.seekg(previousPos, ios_base::beg);
131  return fileSize;
132 }
133 
134 /*---------------------------------------------------------------
135  getPosition
136  ---------------------------------------------------------------*/
138 {
139  auto& f = const_cast<std::ifstream&>(m_if);
140  if (m_if.is_open())
141  return f.tellg();
142  else
143  return 0;
144 }
145 
146 /*---------------------------------------------------------------
147  fileOpenCorrectly
148  ---------------------------------------------------------------*/
149 bool CFileInputStream::fileOpenCorrectly() const { return m_if.is_open(); }
150 /*---------------------------------------------------------------
151  readLine
152  ---------------------------------------------------------------*/
153 bool CFileInputStream::readLine(string& str)
154 {
155  str = string(); // clear() is not defined in VC6
156  if (!m_if.is_open()) return false;
157 
158  std::getline(m_if, str);
159  return !m_if.fail() && !m_if.eof();
160 }
161 
162 /*---------------------------------------------------------------
163  checkEOF
164  ---------------------------------------------------------------*/
166 {
167  if (!m_if.is_open()) return true;
168  return m_if.eof();
169 }
170 
172 {
173  if (m_if.is_open()) m_if.clear();
174 }
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
exceptions.h
mrpt::io::CFileInputStream::CFileInputStream
CFileInputStream()
Default constructor.
Definition: CFileInputStream.cpp:39
mrpt::io
Definition: img/CImage.h:22
mrpt::io::CFileInputStream::Read
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileInputStream.cpp:67
mrpt::io::CFileInputStream::fileOpenCorrectly
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
Definition: CFileInputStream.cpp:149
mrpt::io::CFileInputStream::~CFileInputStream
virtual ~CFileInputStream()
Definition: CFileInputStream.cpp:62
end
GLuint GLuint end
Definition: glext.h:3528
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int64_t
__int64 int64_t
Definition: rptypes.h:49
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt::io::CFileInputStream::m_if
std::ifstream m_if
The actual input file stream.
Definition: io/CFileInputStream.h:27
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
CFileInputStream.h
mrpt::io::CFileInputStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
Definition: CFileInputStream.cpp:121
mrpt::io::CFileInputStream::open
bool open(const std::string &fileName)
Open a file for reading.
Definition: CFileInputStream.cpp:43
mrpt::io::CFileInputStream::clearError
void clearError()
Resets stream error status bits (e.g.
Definition: CFileInputStream.cpp:171
offset
GLintptr offset
Definition: glext.h:3925
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::io::CFileInputStream::getPosition
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
Definition: CFileInputStream.cpp:137
mrpt::io::CStream::sFromBeginning
@ sFromBeginning
Definition: io/CStream.h:36
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::io::CFileInputStream::close
void close()
Close the stream.
Definition: CFileInputStream.cpp:54
mrpt::io::CFileInputStream::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CFileInputStream.cpp:79
value
GLsizei const GLfloat * value
Definition: glext.h:4117
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::io::CFileInputStream::checkEOF
bool checkEOF()
Will be true if EOF has been already reached.
Definition: CFileInputStream.cpp:165
in
GLuint in
Definition: glext.h:7274
mrpt::io::CStream::sFromEnd
@ sFromEnd
Definition: io/CStream.h:38
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::io::CFileInputStream::readLine
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
Definition: CFileInputStream.cpp:153
mrpt::io::CFileInputStream::Seek
uint64_t Seek(int64_t off, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource.
Definition: CFileInputStream.cpp:91
mrpt::io::CStream::sFromCurrent
@ sFromCurrent
Definition: io/CStream.h:37
io-precomp.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST