bytestreamin_file.hpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: bytestreamin_file.hpp
5 
6  CONTENTS:
7 
8  PROGRAMMERS:
9 
10  martin.isenburg@gmail.com
11 
12  COPYRIGHT:
13 
14  (c) 2010-2011, Martin Isenburg, LASSO - tools to catch reality
15 
16  This is free software; you can redistribute and/or modify it under the
17  terms of the GNU Lesser General Licence as published by the Free Software
18  Foundation. See the COPYING file for more information.
19 
20  This software is distributed WITHOUT ANY WARRANTY and without even the
21  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 
23  CHANGE HISTORY:
24 
25  1 October 2011 -- added 64 bit file support in MSVC 6.0 at McCafe at Hbf Linz
26  10 January 2011 -- licensing change for LGPL release and liblas integration
27  12 December 2010 -- created from ByteStreamOutFile after Howard got pushy (-;
28 
29 ===============================================================================
30 */
31 #ifndef BYTE_STREAM_IN_FILE_H
32 #define BYTE_STREAM_IN_FILE_H
33 
34 #include "bytestreamin.hpp"
35 
36 #include <stdio.h>
37 
38 #if defined(_MSC_VER) && (_MSC_VER < 1300)
39 extern "C" __int64 _cdecl _ftelli64(FILE*);
40 extern "C" int _cdecl _fseeki64(FILE*, __int64, int);
41 #endif
42 
44 {
45 public:
46  ByteStreamInFile(FILE* file);
47 /* read a single byte */
48  U32 getByte();
49 /* read an array of bytes */
50  void getBytes(U8* bytes, const U32 num_bytes);
51 /* is the stream seekable (e.g. stdin is not) */
52  BOOL isSeekable() const;
53 /* get current position of stream */
54  I64 tell() const;
55 /* seek to this position in the stream */
56  BOOL seek(const I64 position);
57 /* seek to the end of the file */
58  BOOL seekEnd(const I64 distance=0);
59 /* destructor */
61 protected:
62  FILE* file;
63 };
64 
66 {
67 public:
68  ByteStreamInFileLE(FILE* file);
69 /* read 16 bit low-endian field */
70  void get16bitsLE(U8* bytes);
71 /* read 32 bit low-endian field */
72  void get32bitsLE(U8* bytes);
73 /* read 64 bit low-endian field */
74  void get64bitsLE(U8* bytes);
75 /* read 16 bit big-endian field */
76  void get16bitsBE(U8* bytes);
77 /* read 32 bit big-endian field */
78  void get32bitsBE(U8* bytes);
79 /* read 64 bit big-endian field */
80  void get64bitsBE(U8* bytes);
81 private:
82  U8 swapped[8];
83 };
84 
86 {
87 public:
88  ByteStreamInFileBE(FILE* file);
89 /* read 16 bit low-endian field */
90  void get16bitsLE(U8* bytes);
91 /* read 32 bit low-endian field */
92  void get32bitsLE(U8* bytes);
93 /* read 64 bit low-endian field */
94  void get64bitsLE(U8* bytes);
95 /* read 16 bit big-endian field */
96  void get16bitsBE(U8* bytes);
97 /* read 32 bit big-endian field */
98  void get32bitsBE(U8* bytes);
99 /* read 64 bit big-endian field */
100  void get64bitsBE(U8* bytes);
101 private:
102  U8 swapped[8];
103 };
104 
106 {
107  this->file = file;
108 }
109 
111 {
112  int byte = getc(file);
113  if (byte == EOF)
114  {
115  throw EOF;
116  }
117  return (U32)byte;
118 }
119 
120 inline void ByteStreamInFile::getBytes(U8* bytes, const U32 num_bytes)
121 {
122  if (fread(bytes, 1, num_bytes, file) != num_bytes)
123  {
124  throw EOF;
125  }
126 }
127 
129 {
130  return (file != stdin);
131 }
132 
134 {
135 #ifdef _WIN32
136  return _ftelli64(file);
137 #else
138  return (I64)ftello(file);
139 #endif
140 }
141 
142 inline BOOL ByteStreamInFile::seek(const I64 position)
143 {
144 #ifdef _WIN32
145  return !(_fseeki64(file, position, SEEK_SET));
146 #else
147  return !(fseeko(file, (off_t)position, SEEK_SET));
148 #endif
149 }
150 
151 inline BOOL ByteStreamInFile::seekEnd(const I64 distance)
152 {
153 #ifdef _WIN32
154  return !(_fseeki64(file, -distance, SEEK_END));
155 #else
156  return !(fseeko(file, (off_t)-distance, SEEK_END));
157 #endif
158 }
159 
161 {
162 }
163 
165 {
166  getBytes(bytes, 2);
167 }
168 
170 {
171  getBytes(bytes, 4);
172 }
173 
175 {
176  getBytes(bytes, 8);
177 }
178 
180 {
181  getBytes(swapped, 2);
182  bytes[0] = swapped[1];
183  bytes[1] = swapped[0];
184 }
185 
187 {
188  getBytes(swapped, 4);
189  bytes[0] = swapped[3];
190  bytes[1] = swapped[2];
191  bytes[2] = swapped[1];
192  bytes[3] = swapped[0];
193 }
194 
196 {
197  getBytes(swapped, 8);
198  bytes[0] = swapped[7];
199  bytes[1] = swapped[6];
200  bytes[2] = swapped[5];
201  bytes[3] = swapped[4];
202  bytes[4] = swapped[3];
203  bytes[5] = swapped[2];
204  bytes[6] = swapped[1];
205  bytes[7] = swapped[0];
206 }
207 
209 {
210 }
211 
213 {
214  getBytes(swapped, 2);
215  bytes[0] = swapped[1];
216  bytes[1] = swapped[0];
217 }
218 
220 {
221  getBytes(swapped, 4);
222  bytes[0] = swapped[3];
223  bytes[1] = swapped[2];
224  bytes[2] = swapped[1];
225  bytes[3] = swapped[0];
226 }
227 
229 {
230  getBytes(swapped, 8);
231  bytes[0] = swapped[7];
232  bytes[1] = swapped[6];
233  bytes[2] = swapped[5];
234  bytes[3] = swapped[4];
235  bytes[4] = swapped[3];
236  bytes[5] = swapped[2];
237  bytes[6] = swapped[1];
238  bytes[7] = swapped[0];
239 }
240 
242 {
243  getBytes(bytes, 2);
244 }
245 
247 {
248  getBytes(bytes, 4);
249 }
250 
252 {
253  getBytes(bytes, 8);
254 }
255 
256 #endif
void get64bitsLE(U8 *bytes)
int BOOL
Definition: mydefs.hpp:57
void get16bitsBE(U8 *bytes)
void getBytes(U8 *bytes, const U32 num_bytes)
void get64bitsBE(U8 *bytes)
void get32bitsBE(U8 *bytes)
unsigned int U32
Definition: mydefs.hpp:39
void get32bitsBE(U8 *bytes)
virtual void get32bitsBE(U8 *bytes)=0
void get64bitsBE(U8 *bytes)
void get16bitsLE(U8 *bytes)
long long I64
Definition: mydefs.hpp:48
unsigned char U8
Definition: mydefs.hpp:41
BOOL seekEnd(const I64 distance=0)
virtual void get64bitsLE(U8 *bytes)=0
void get32bitsLE(U8 *bytes)
void get32bitsLE(U8 *bytes)
void get16bitsLE(U8 *bytes)
BOOL seek(const I64 position)
virtual void get32bitsLE(U8 *bytes)=0
ByteStreamInFileBE(FILE *file)
virtual void get64bitsBE(U8 *bytes)=0
void get16bitsBE(U8 *bytes)
ByteStreamInFileLE(FILE *file)
void get64bitsLE(U8 *bytes)
virtual void get16bitsLE(U8 *bytes)=0
virtual void get16bitsBE(U8 *bytes)=0
ByteStreamInFile(FILE *file)


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06