Main MRPT website > C++ reference for MRPT 1.5.7
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 
13 #ifdef _MSC_VER
14 # define _SCL_SECURE_NO_WARNINGS
15 #endif
16 
18 #include <mrpt/system/os.h>
19 #include <mrpt/system/os.h>
20 
21 using namespace mrpt::utils;
22 using namespace std;
23 
24 /*---------------------------------------------------------------
25  Constructor
26  ---------------------------------------------------------------*/
27 CFileInputStream::CFileInputStream( const string &fileName ) : m_if()
28 {
30 
31  // Try to open the file:
32  // Open for input:
33  if (!open(fileName))
34  THROW_EXCEPTION_FMT( "Error trying to open file: '%s'",fileName.c_str() );
35 
36  MRPT_END
37 }
38 
39 /*---------------------------------------------------------------
40  Constructor
41  ---------------------------------------------------------------*/
43 {
44 }
45 
46 /*---------------------------------------------------------------
47  open
48  ---------------------------------------------------------------*/
49 bool CFileInputStream::open( const string &fileName )
50 {
51  // Try to open the file:
52  // Open for input:
53  m_if.open(fileName.c_str(), ios_base::binary | ios_base::in );
54  return m_if.is_open();
55 }
56 
57 /*---------------------------------------------------------------
58  close
59  ---------------------------------------------------------------*/
61 {
62  if (m_if.is_open()) m_if.close();
63 }
64 
65 /*---------------------------------------------------------------
66  Destructor
67  ---------------------------------------------------------------*/
69 {
70  close();
71 }
72 
73 /*---------------------------------------------------------------
74  Read
75  Reads bytes from the stream into Buffer
76  ---------------------------------------------------------------*/
77 size_t CFileInputStream::Read(void *Buffer, size_t Count)
78 {
79  if (!m_if.is_open()) return 0;
80 
81  m_if.read(static_cast<char*>(Buffer),Count);
82  return m_if.fail() ? 0:Count;
83 }
84 
85 /*---------------------------------------------------------------
86  Write
87  Writes a block of bytes to the stream.
88  ---------------------------------------------------------------*/
89 size_t CFileInputStream::Write(const void *Buffer, size_t Count)
90 {
91  MRPT_UNUSED_PARAM(Buffer); MRPT_UNUSED_PARAM(Count);
92  THROW_EXCEPTION("Trying to write to a read file stream.");
93 }
94 
95 /*---------------------------------------------------------------
96  Seek
97  Method for moving to a specified position in the streamed resource.
98  See documentation of CStream::Seek
99  ---------------------------------------------------------------*/
101 {
102  if (!m_if.is_open()) return 0;
103 
104  ifstream::off_type offset = Offset;
105  ifstream::seekdir way;
106 
107  switch(Origin)
108  {
109  case sFromBeginning: way = ios_base::beg; break;
110  case sFromCurrent: way = ios_base::cur; break;
111  case sFromEnd: way = ios_base::end; break;
112  default: THROW_EXCEPTION("Invalid value for 'Origin'");
113  }
114 
115  m_if.seekg(offset, way);
116 
117  return getPosition();
118 }
119 
120 /*---------------------------------------------------------------
121  getTotalBytesCount
122  ---------------------------------------------------------------*/
124 {
125  if (!fileOpenCorrectly()) return 0;
126 
127  uint64_t previousPos = getPosition();
128  uint64_t fileSize = Seek(0,sFromEnd);
129  Seek( previousPos );
130  return fileSize;
131 }
132 
133 /*---------------------------------------------------------------
134  getPosition
135  ---------------------------------------------------------------*/
137 {
138  if (m_if.is_open())
139  return m_if.tellg();
140  else return 0;
141 }
142 
143 /*---------------------------------------------------------------
144  fileOpenCorrectly
145  ---------------------------------------------------------------*/
147 {
148  return m_if.is_open();
149 }
150 
151 /*---------------------------------------------------------------
152  readLine
153  ---------------------------------------------------------------*/
154 bool CFileInputStream::readLine( string &str )
155 {
156  str = string(); // clear() is not defined in VC6
157  if (!m_if.is_open()) return false;
158 
159  std::getline( m_if, str );
160  return !m_if.fail() && !m_if.eof();
161 }
162 
163 /*---------------------------------------------------------------
164  checkEOF
165  ---------------------------------------------------------------*/
167 {
168  if (!m_if.is_open())
169  return true;
170  return m_if.eof();
171 }
172 
174 {
175  if (m_if.is_open())
176  m_if.clear();
177 }
CFileInputStream()
Default constructor.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Method for moving to a specified position in the streamed resource.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
std::ifstream m_if
The actual input file stream.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLintptr offset
Definition: glext.h:3780
STL namespace.
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.
__int64 int64_t
Definition: rptypes.h:51
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
GLuint GLuint end
Definition: glext.h:3512
GLsizei const GLchar ** string
Definition: glext.h:3919
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:52
uint64_t getPosition() MRPT_OVERRIDE
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
GLuint in
Definition: glext.h:6301
bool open(const std::string &fileName)
Open a file for reading.
void clearError()
Resets stream error status bits (e.g. after an EOF)
bool fileOpenCorrectly()
Returns true if the file was open without errors.
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Method for getting the total number of bytes in the buffer.
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019