MRPT  1.9.9
CFileInputStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
14 
15 using namespace mrpt::io;
16 using namespace std;
17 
18 static_assert(
19  !std::is_copy_constructible_v<CFileInputStream> &&
20  !std::is_copy_assignable_v<CFileInputStream>,
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 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
void close()
Close the stream.
bool checkEOF()
Will be true if EOF has been already reached.
#define MRPT_START
Definition: exceptions.h:241
bool open(const std::string &fileName)
Open a file for reading.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void clearError()
Resets stream error status bits (e.g.
GLintptr offset
Definition: glext.h:3936
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...
STL namespace.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
__int64 int64_t
Definition: rptypes.h:52
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
GLuint GLuint end
Definition: glext.h:3532
std::ifstream m_if
The actual input file stream.
GLsizei const GLchar ** string
Definition: glext.h:4116
unsigned __int64 uint64_t
Definition: rptypes.h:53
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
CFileInputStream()
Default constructor.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019