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:
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
ByteStreamInFileBE::get16bitsBE
void get16bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:241
ByteStreamInFile::tell
I64 tell() const
Definition: bytestreamin_file.hpp:133
ByteStreamInFileLE::get64bitsBE
void get64bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:195
I64
long long I64
Definition: mydefs.hpp:48
ByteStreamInFile::getBytes
void getBytes(U8 *bytes, const U32 num_bytes)
Definition: bytestreamin_file.hpp:120
ByteStreamInFileLE::get64bitsLE
void get64bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:174
ByteStreamInFileBE::get32bitsLE
void get32bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:219
ByteStreamInFile::seekEnd
BOOL seekEnd(const I64 distance=0)
Definition: bytestreamin_file.hpp:151
ByteStreamInFileLE::get32bitsLE
void get32bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:169
ByteStreamInFileBE::get64bitsLE
void get64bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:228
ByteStreamInFileBE::get64bitsBE
void get64bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:251
ByteStreamInFile::getByte
U32 getByte()
Definition: bytestreamin_file.hpp:110
ByteStreamInFileLE::ByteStreamInFileLE
ByteStreamInFileLE(FILE *file)
Definition: bytestreamin_file.hpp:160
ByteStreamInFile::isSeekable
BOOL isSeekable() const
Definition: bytestreamin_file.hpp:128
ByteStreamInFile::file
FILE * file
Definition: bytestreamin_file.hpp:60
ByteStreamInFile::seek
BOOL seek(const I64 position)
Definition: bytestreamin_file.hpp:142
ByteStreamInFileLE::get16bitsLE
void get16bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:164
bytestreamin.hpp
ByteStreamInFile::~ByteStreamInFile
~ByteStreamInFile()
Definition: bytestreamin_file.hpp:60
ByteStreamInFileBE::get16bitsLE
void get16bitsLE(U8 *bytes)
Definition: bytestreamin_file.hpp:212
ByteStreamInFile
Definition: bytestreamin_file.hpp:43
ByteStreamIn
Definition: bytestreamin.hpp:36
ByteStreamInFileLE::swapped
U8 swapped[8]
Definition: bytestreamin_file.hpp:82
ByteStreamInFileLE::get16bitsBE
void get16bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:179
U8
unsigned char U8
Definition: mydefs.hpp:41
ByteStreamInFileBE::ByteStreamInFileBE
ByteStreamInFileBE(FILE *file)
Definition: bytestreamin_file.hpp:208
BOOL
int BOOL
Definition: mydefs.hpp:57
ByteStreamInFileBE::swapped
U8 swapped[8]
Definition: bytestreamin_file.hpp:102
ByteStreamInFileBE::get32bitsBE
void get32bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:246
file
FILE * file
Definition: arithmeticencoder.cpp:77
ByteStreamInFileLE
Definition: bytestreamin_file.hpp:65
ByteStreamInFile::ByteStreamInFile
ByteStreamInFile(FILE *file)
Definition: bytestreamin_file.hpp:105
U32
unsigned int U32
Definition: mydefs.hpp:39
ByteStreamInFileLE::get32bitsBE
void get32bitsBE(U8 *bytes)
Definition: bytestreamin_file.hpp:186
ByteStreamInFileBE
Definition: bytestreamin_file.hpp:85


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 Wed Mar 2 2022 00:37:22