forked from LASzip/LASzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytestreamout_file.hpp
265 lines (224 loc) · 7.11 KB
/
bytestreamout_file.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
===============================================================================
FILE: bytestreamout_file.hpp
CONTENTS:
Class for FILE*-based output streams with endian handling.
PROGRAMMERS:
[email protected] - https://rapidlasso.com
COPYRIGHT:
(c) 2007-2013, martin isenburg, rapidlasso - fast tools to catch reality
This is free software; you can redistribute and/or modify it under the
terms of the GNU Lesser General Licence as published by the Free Software
Foundation. See the COPYING file for more information.
This software is distributed WITHOUT ANY WARRANTY and without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CHANGE HISTORY:
1 October 2011 -- added 64 bit file support in MSVC 6.0 at McCafe at Hbf Linz
10 January 2011 -- licensing change for LGPL release and liblas integration
12 December 2010 -- created from ByteStreamOutFile after Howard got pushy (-;
===============================================================================
*/
#ifndef BYTE_STREAM_OUT_FILE_H
#define BYTE_STREAM_OUT_FILE_H
#include "bytestreamout.hpp"
#include <stdio.h>
#if defined(_MSC_VER) && (_MSC_VER < 1300)
extern "C" int _cdecl _fseeki64(FILE*, __int64, int);
extern "C" __int64 _cdecl _ftelli64(FILE*);
#endif
class ByteStreamOutFile : public ByteStreamOut
{
public:
ByteStreamOutFile(FILE* file);
/* replace a closed FILE* with a reopened FILE* in "ab" mode */
BOOL refile(FILE* file);
/* write a single byte */
BOOL putByte(U8 byte);
/* write an array of bytes */
BOOL putBytes(const U8* bytes, U32 num_bytes);
/* is the stream seekable (e.g. standard out is not) */
BOOL isSeekable() const;
/* get current position of stream */
I64 tell() const;
/* seek to this position in the stream */
BOOL seek(const I64 position);
/* seek to the end of the file */
BOOL seekEnd();
/* destructor */
~ByteStreamOutFile(){};
protected:
FILE* file;
};
class ByteStreamOutFileLE : public ByteStreamOutFile
{
public:
ByteStreamOutFileLE(FILE* file);
/* write 16 bit low-endian field */
BOOL put16bitsLE(const U8* bytes);
/* write 32 bit low-endian field */
BOOL put32bitsLE(const U8* bytes);
/* write 64 bit low-endian field */
BOOL put64bitsLE(const U8* bytes);
/* write 16 bit big-endian field */
BOOL put16bitsBE(const U8* bytes);
/* write 32 bit big-endian field */
BOOL put32bitsBE(const U8* bytes);
/* write 64 bit big-endian field */
BOOL put64bitsBE(const U8* bytes);
private:
U8 swapped[8];
};
class ByteStreamOutFileBE : public ByteStreamOutFile
{
public:
ByteStreamOutFileBE(FILE* file);
/* write 16 bit low-endian field */
BOOL put16bitsLE(const U8* bytes);
/* write 32 bit low-endian field */
BOOL put32bitsLE(const U8* bytes);
/* write 64 bit low-endian field */
BOOL put64bitsLE(const U8* bytes);
/* write 16 bit big-endian field */
BOOL put16bitsBE(const U8* bytes);
/* write 32 bit big-endian field */
BOOL put32bitsBE(const U8* bytes);
/* write 64 bit big-endian field */
BOOL put64bitsBE(const U8* bytes);
private:
U8 swapped[8];
};
inline ByteStreamOutFile::ByteStreamOutFile(FILE* file)
{
this->file = file;
}
inline BOOL ByteStreamOutFile::refile(FILE* file)
{
if (file == 0) return FALSE;
this->file = file;
return TRUE;
}
inline BOOL ByteStreamOutFile::putByte(U8 byte)
{
return (fputc(byte, file) == byte);
}
inline BOOL ByteStreamOutFile::putBytes(const U8* bytes, U32 num_bytes)
{
return (fwrite(bytes, 1, num_bytes, file) == num_bytes);
}
inline BOOL ByteStreamOutFile::isSeekable() const
{
return (file != stdout);
}
inline I64 ByteStreamOutFile::tell() const
{
#if defined _WIN32 && ! defined (__MINGW32__)
return _ftelli64(file);
#elif defined (__MINGW32__)
return (I64)ftello64(file);
#else
return (I64)ftello(file);
#endif
}
inline BOOL ByteStreamOutFile::seek(I64 position)
{
#if defined _WIN32 && ! defined (__MINGW32__)
return !(_fseeki64(file, position, SEEK_SET));
#elif defined (__MINGW32__)
return !(fseeko64(file, (off_t)position, SEEK_SET));
#else
return !(fseeko(file, (off_t)position, SEEK_SET));
#endif
}
inline BOOL ByteStreamOutFile::seekEnd()
{
#if defined _WIN32 && ! defined (__MINGW32__)
return !(_fseeki64(file, 0, SEEK_END));
#elif defined (__MINGW32__)
return !(fseeko64(file, (off_t)0, SEEK_END));
#else
return !(fseeko(file, (off_t)0, SEEK_END));
#endif
}
inline ByteStreamOutFileLE::ByteStreamOutFileLE(FILE* file) : ByteStreamOutFile(file)
{
}
inline BOOL ByteStreamOutFileLE::put16bitsLE(const U8* bytes)
{
return putBytes(bytes, 2);
}
inline BOOL ByteStreamOutFileLE::put32bitsLE(const U8* bytes)
{
return putBytes(bytes, 4);
}
inline BOOL ByteStreamOutFileLE::put64bitsLE(const U8* bytes)
{
return putBytes(bytes, 8);
}
inline BOOL ByteStreamOutFileLE::put16bitsBE(const U8* bytes)
{
swapped[0] = bytes[1];
swapped[1] = bytes[0];
return putBytes(swapped, 2);
}
inline BOOL ByteStreamOutFileLE::put32bitsBE(const U8* bytes)
{
swapped[0] = bytes[3];
swapped[1] = bytes[2];
swapped[2] = bytes[1];
swapped[3] = bytes[0];
return putBytes(swapped, 4);
}
inline BOOL ByteStreamOutFileLE::put64bitsBE(const U8* bytes)
{
swapped[0] = bytes[7];
swapped[1] = bytes[6];
swapped[2] = bytes[5];
swapped[3] = bytes[4];
swapped[4] = bytes[3];
swapped[5] = bytes[2];
swapped[6] = bytes[1];
swapped[7] = bytes[0];
return putBytes(swapped, 8);
}
inline ByteStreamOutFileBE::ByteStreamOutFileBE(FILE* file) : ByteStreamOutFile(file)
{
}
inline BOOL ByteStreamOutFileBE::put16bitsLE(const U8* bytes)
{
swapped[0] = bytes[1];
swapped[1] = bytes[0];
return putBytes(swapped, 2);
}
inline BOOL ByteStreamOutFileBE::put32bitsLE(const U8* bytes)
{
swapped[0] = bytes[3];
swapped[1] = bytes[2];
swapped[2] = bytes[1];
swapped[3] = bytes[0];
return putBytes(swapped, 4);
}
inline BOOL ByteStreamOutFileBE::put64bitsLE(const U8* bytes)
{
swapped[0] = bytes[7];
swapped[1] = bytes[6];
swapped[2] = bytes[5];
swapped[3] = bytes[4];
swapped[4] = bytes[3];
swapped[5] = bytes[2];
swapped[6] = bytes[1];
swapped[7] = bytes[0];
return putBytes(swapped, 8);
}
inline BOOL ByteStreamOutFileBE::put16bitsBE(const U8* bytes)
{
return putBytes(bytes, 2);
}
inline BOOL ByteStreamOutFileBE::put32bitsBE(const U8* bytes)
{
return putBytes(bytes, 4);
}
inline BOOL ByteStreamOutFileBE::put64bitsBE(const U8* bytes)
{
return putBytes(bytes, 8);
}
#endif