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-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 
17 #include <mrpt/system/os.h>
18 #include <mrpt/system/os.h>
19 
20 using namespace mrpt::utils;
21 using namespace std;
22 
23 /*---------------------------------------------------------------
24  Constructor
25  ---------------------------------------------------------------*/
26 CFileInputStream::CFileInputStream(const string& fileName) : m_if()
27 {
29 
30  // Try to open the file:
31  // Open for input:
32  if (!open(fileName))
34  "Error trying to open file: '%s'", fileName.c_str());
35 
36  MRPT_END
37 }
38 
39 /*---------------------------------------------------------------
40  Constructor
41  ---------------------------------------------------------------*/
43 /*---------------------------------------------------------------
44  open
45  ---------------------------------------------------------------*/
46 bool CFileInputStream::open(const string& fileName)
47 {
48  // Try to open the file:
49  // Open for input:
50  m_if.open(fileName.c_str(), ios_base::binary | ios_base::in);
51  return m_if.is_open();
52 }
53 
54 /*---------------------------------------------------------------
55  close
56  ---------------------------------------------------------------*/
58 {
59  if (m_if.is_open()) m_if.close();
60 }
61 
62 /*---------------------------------------------------------------
63  Destructor
64  ---------------------------------------------------------------*/
66 /*---------------------------------------------------------------
67  Read
68  Reads bytes from the stream into Buffer
69  ---------------------------------------------------------------*/
70 size_t CFileInputStream::Read(void* Buffer, size_t Count)
71 {
72  if (!m_if.is_open()) return 0;
73 
74  m_if.read(static_cast<char*>(Buffer), Count);
75  return m_if.fail() ? 0 : Count;
76 }
77 
78 /*---------------------------------------------------------------
79  Write
80  Writes a block of bytes to the stream.
81  ---------------------------------------------------------------*/
82 size_t CFileInputStream::Write(const void* Buffer, size_t Count)
83 {
84  MRPT_UNUSED_PARAM(Buffer);
85  MRPT_UNUSED_PARAM(Count);
86  THROW_EXCEPTION("Trying to write to a read file stream.");
87 }
88 
89 /*---------------------------------------------------------------
90  Seek
91  Method for moving to a specified position in the streamed resource.
92  See documentation of CStream::Seek
93  ---------------------------------------------------------------*/
95 {
96  if (!m_if.is_open()) return 0;
97 
98  ifstream::off_type offset = Offset;
99  ifstream::seekdir way;
100 
101  switch (Origin)
102  {
103  case sFromBeginning:
104  way = ios_base::beg;
105  break;
106  case sFromCurrent:
107  way = ios_base::cur;
108  break;
109  case sFromEnd:
110  way = ios_base::end;
111  break;
112  default:
113  THROW_EXCEPTION("Invalid value for 'Origin'");
114  }
115 
116  m_if.seekg(offset, way);
117 
118  return getPosition();
119 }
120 
121 /*---------------------------------------------------------------
122  getTotalBytesCount
123  ---------------------------------------------------------------*/
125 {
126  if (!fileOpenCorrectly()) return 0;
127 
128  uint64_t previousPos = getPosition();
129  uint64_t fileSize = Seek(0, sFromEnd);
130  Seek(previousPos);
131  return fileSize;
132 }
133 
134 /*---------------------------------------------------------------
135  getPosition
136  ---------------------------------------------------------------*/
138 {
139  if (m_if.is_open())
140  return m_if.tellg();
141  else
142  return 0;
143 }
144 
145 /*---------------------------------------------------------------
146  fileOpenCorrectly
147  ---------------------------------------------------------------*/
148 bool CFileInputStream::fileOpenCorrectly() { return m_if.is_open(); }
149 /*---------------------------------------------------------------
150  readLine
151  ---------------------------------------------------------------*/
152 bool CFileInputStream::readLine(string& str)
153 {
154  str = string(); // clear() is not defined in VC6
155  if (!m_if.is_open()) return false;
156 
157  std::getline(m_if, str);
158  return !m_if.fail() && !m_if.eof();
159 }
160 
161 /*---------------------------------------------------------------
162  checkEOF
163  ---------------------------------------------------------------*/
165 {
166  if (!m_if.is_open()) return true;
167  return m_if.eof();
168 }
CFileInputStream()
Default constructor.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:45
std::ifstream m_if
The actual input file stream.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLintptr offset
Definition: glext.h:3925
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getPosition() override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
STL namespace.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
void close()
Close the stream.
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Method for moving to a specified position in the streamed resource.
GLuint GLuint end
Definition: glext.h:3528
GLsizei const GLchar ** string
Definition: glext.h:4101
bool checkEOF()
Will be true if EOF has been already reached.
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
GLuint in
Definition: glext.h:7274
bool open(const std::string &fileName)
Open a file for reading.
bool fileOpenCorrectly()
Returns true if the file was open without errors.
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes in the buffer.



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