~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to extern/eltopo/common/bfstream.h

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef BFSTREAM_H
2
 
#define BFSTREAM_H
3
 
 
4
 
#include <cassert>
5
 
#include <cstdarg>
6
 
#include <cstdlib>
7
 
#include <fstream>
8
 
 
9
 
#ifdef __BIG_ENDIAN__
10
 
#ifdef __LITTLE_ENDIAN__
11
 
#error Cannot be both big and little endian
12
 
#endif
13
 
#else
14
 
#ifndef __LITTLE_ENDIAN__
15
 
#error Need to define either big or little endian
16
 
#endif
17
 
#endif
18
 
 
19
 
//=================================================================================
20
 
template<class T> inline void swap_endianity(T &x)
21
 
{
22
 
    assert(sizeof(T)<=8); // should not be called on composite types: instead specialize swap_endianity if needed.
23
 
    T old=x;
24
 
    for(unsigned int k=0; k<sizeof(T); ++k)
25
 
        ((char*)&x)[k] = ((char*)&old)[sizeof(T)-1-k];
26
 
}
27
 
 
28
 
 
29
 
//=================================================================================
30
 
struct bifstream
31
 
{
32
 
    std::ifstream input;
33
 
    bool big_endian;
34
 
    
35
 
    bifstream(void) :
36
 
    input(),
37
 
#ifdef __BIG_ENDIAN__
38
 
    big_endian(true)
39
 
#else
40
 
    big_endian(false)
41
 
#endif
42
 
    {
43
 
        assert_correct_endianity();
44
 
    }
45
 
    
46
 
    bifstream(const char *filename_format, ...) :
47
 
    input(),
48
 
#ifdef __BIG_ENDIAN__
49
 
    big_endian(true)
50
 
#else
51
 
    big_endian(false)
52
 
#endif
53
 
    {
54
 
#ifdef WIN32
55
 
        va_list ap;
56
 
        va_start(ap, filename_format);
57
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
58
 
        +1; // terminating '\0'
59
 
        char *filename=new char[len];
60
 
        vsprintf(filename, filename_format, ap);
61
 
        input.open(filename, std::ifstream::binary);
62
 
        delete[] filename;
63
 
        va_end(ap);
64
 
#else
65
 
        va_list ap;
66
 
        va_start(ap, filename_format);
67
 
        char *filename;
68
 
        vasprintf(&filename, filename_format, ap);
69
 
        input.open(filename, std::ifstream::binary);
70
 
        std::free(filename);
71
 
        va_end(ap);
72
 
#endif
73
 
    }
74
 
    
75
 
    void assert_correct_endianity(void)
76
 
    {
77
 
        int test=1;
78
 
#ifdef __BIG_ENDIAN__
79
 
        assert(*(char*)&test == 0); // if this fails, you should have defined __LITTLE_ENDIAN__ instead
80
 
#else
81
 
        assert(*(char*)&test == 1); // if this fails, you should have defined __BIG_ENDIAN__ instead
82
 
#endif
83
 
    }
84
 
    
85
 
    void open(const char *filename_format, ...)
86
 
    {
87
 
#ifdef WIN32
88
 
        va_list ap;
89
 
        va_start(ap, filename_format);
90
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
91
 
        +1; // terminating '\0'
92
 
        char *filename=new char[len];
93
 
        vsprintf(filename, filename_format, ap);
94
 
        input.open(filename, std::ifstream::binary);
95
 
        delete[] filename;
96
 
        va_end(ap);
97
 
#else
98
 
        va_list ap;
99
 
        va_start(ap, filename_format);
100
 
        char *filename;
101
 
        vasprintf(&filename, filename_format, ap);
102
 
        input.open(filename, std::ifstream::binary);
103
 
        std::free(filename);
104
 
        va_end(ap);
105
 
#endif
106
 
    }
107
 
    
108
 
    void vopen(const char *filename_format, va_list ap)
109
 
    {
110
 
#ifdef WIN32
111
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
112
 
        +1; // terminating '\0'
113
 
        char *filename=new char[len];
114
 
        vsprintf(filename, filename_format, ap);
115
 
        input.open(filename, std::ifstream::binary);
116
 
        delete[] filename;
117
 
#else
118
 
        char *filename;
119
 
        vasprintf(&filename, filename_format, ap);
120
 
        input.open(filename, std::ifstream::binary);
121
 
        std::free(filename);
122
 
#endif
123
 
    }
124
 
    
125
 
    bool good(void)
126
 
    { return input.good(); }
127
 
    
128
 
    bool fail(void)
129
 
    { return input.fail(); }
130
 
    
131
 
    void close(void)
132
 
    { input.close(); }
133
 
    
134
 
    void set_big_endian(void)
135
 
    { big_endian=true; }
136
 
    
137
 
    void set_little_endian(void)
138
 
    { big_endian=false; }
139
 
    
140
 
    void read_endianity(void)
141
 
    { (*this)>>big_endian; }
142
 
    
143
 
    void skip(long numbytes)
144
 
    { input.seekg(numbytes, std::ios_base::cur); }
145
 
    
146
 
    void seek(long position)
147
 
    { input.seekg(position); }
148
 
    
149
 
    int get(void)
150
 
    { return input.get(); }
151
 
    
152
 
private: // don't expose dangerous template
153
 
    template<class T>
154
 
    bifstream &templated_read(T &d)
155
 
    {
156
 
        input.read((char*)&d, sizeof(T));
157
 
#ifdef __BIG_ENDIAN__
158
 
        if(!big_endian)
159
 
#else
160
 
            if(big_endian)
161
 
#endif
162
 
                swap_endianity(d);
163
 
        return *this;
164
 
    }
165
 
public:
166
 
    
167
 
    template<class T>
168
 
    void read(T *d, unsigned int num)
169
 
    {
170
 
        assert(d!=0);
171
 
        for(unsigned int i=0; i<num; ++i) (*this)>>d[i];
172
 
    }
173
 
    
174
 
    friend bifstream &operator>>(bifstream &, bool &);
175
 
    friend bifstream &operator>>(bifstream &, unsigned char &);
176
 
    friend bifstream &operator>>(bifstream &, short int &);
177
 
    friend bifstream &operator>>(bifstream &, unsigned short int &);
178
 
    friend bifstream &operator>>(bifstream &, int &);
179
 
    friend bifstream &operator>>(bifstream &, unsigned int &);
180
 
    friend bifstream &operator>>(bifstream &, long int &);
181
 
    friend bifstream &operator>>(bifstream &, unsigned long int &);
182
 
    friend bifstream &operator>>(bifstream &, float &);
183
 
    friend bifstream &operator>>(bifstream &, double &);
184
 
};
185
 
 
186
 
 
187
 
bifstream &operator>>(bifstream &input, bool &d);
188
 
bifstream &operator>>(bifstream &input, char &d);
189
 
bifstream &operator>>(bifstream &input, signed char &d);
190
 
bifstream &operator>>(bifstream &input, unsigned char &d);
191
 
bifstream &operator>>(bifstream &input, short int &d);
192
 
bifstream &operator>>(bifstream &input, unsigned short int &d);
193
 
bifstream &operator>>(bifstream &input, int &d);
194
 
bifstream &operator>>(bifstream &input, unsigned int &d);
195
 
bifstream &operator>>(bifstream &input, long int &d);
196
 
bifstream &operator>>(bifstream &input, unsigned long int &d);
197
 
bifstream &operator>>(bifstream &input, float &d);
198
 
bifstream &operator>>(bifstream &input, double &d);
199
 
 
200
 
 
201
 
//=================================================================================
202
 
struct bofstream
203
 
{
204
 
    std::ofstream output;
205
 
    bool big_endian;
206
 
    
207
 
    bofstream(void) :
208
 
    output(),
209
 
#ifdef __BIG_ENDIAN__
210
 
    big_endian(true)
211
 
#else
212
 
    big_endian(false)
213
 
#endif
214
 
    {
215
 
        assert_correct_endianity();
216
 
    }
217
 
    
218
 
    bofstream(const char *filename_format, ...) :
219
 
    output(),
220
 
#ifdef __BIG_ENDIAN__
221
 
    big_endian(true)
222
 
#else
223
 
    big_endian(false)
224
 
#endif
225
 
    {
226
 
        assert_correct_endianity();
227
 
#ifdef WIN32
228
 
        va_list ap;
229
 
        va_start(ap, filename_format);
230
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
231
 
        +1; // terminating '\0'
232
 
        char *filename=new char[len];
233
 
        vsprintf(filename, filename_format, ap);
234
 
        output.open(filename, std::ofstream::binary);
235
 
        delete[] filename;
236
 
        va_end(ap);
237
 
#else
238
 
        va_list ap;
239
 
        va_start(ap, filename_format);
240
 
        char *filename;
241
 
        vasprintf(&filename, filename_format, ap);
242
 
        output.open(filename, std::ofstream::binary);
243
 
        std::free(filename);
244
 
        va_end(ap);
245
 
#endif
246
 
    }
247
 
    
248
 
    void assert_correct_endianity(void)
249
 
    {
250
 
        int test=1;
251
 
#ifdef __BIG_ENDIAN__
252
 
        assert(*(char*)&test == 0); // if this fails, you should have defined __LITTLE_ENDIAN__ instead
253
 
#else
254
 
        assert(*(char*)&test == 1); // if this fails, you should have defined __BIG_ENDIAN__ instead
255
 
#endif
256
 
    }
257
 
    
258
 
    void open(const char *filename_format, ...)
259
 
    {
260
 
#ifdef WIN32
261
 
        va_list ap;
262
 
        va_start(ap, filename_format);
263
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
264
 
        +1; // terminating '\0'
265
 
        char *filename=new char[len];
266
 
        vsprintf(filename, filename_format, ap);
267
 
        output.open(filename, std::ofstream::binary);
268
 
        delete[] filename;
269
 
        va_end(ap);
270
 
#else
271
 
        va_list ap;
272
 
        va_start(ap, filename_format);
273
 
        char *filename;
274
 
        vasprintf(&filename, filename_format, ap);
275
 
        output.open(filename, std::ofstream::binary);
276
 
        std::free(filename);
277
 
        va_end(ap);
278
 
#endif
279
 
    }
280
 
    
281
 
    void vopen(const char *filename_format, va_list ap)
282
 
    {
283
 
        
284
 
#ifdef WIN32
285
 
        int len=_vscprintf(filename_format, ap) // _vscprintf doesn't count
286
 
        +1; // terminating '\0'
287
 
        char *filename=new char[len];
288
 
        vsprintf(filename, filename_format, ap);
289
 
        output.open(filename, std::ofstream::binary);
290
 
        delete[] filename;
291
 
#else
292
 
        char *filename;
293
 
        vasprintf(&filename, filename_format, ap);
294
 
        output.open(filename, std::ofstream::binary);
295
 
        std::free(filename);
296
 
#endif
297
 
    }
298
 
    
299
 
    bool good(void)
300
 
    { return output.good(); }
301
 
    
302
 
    bool fail(void)
303
 
    { return output.fail(); }
304
 
    
305
 
    void close(void)
306
 
    { output.close(); }
307
 
    
308
 
    void set_big_endian(void)
309
 
    { big_endian=true; }
310
 
    
311
 
    void set_little_endian(void)
312
 
    { big_endian=false; }
313
 
    
314
 
    void write_endianity(void)
315
 
    { (*this)<<big_endian; }
316
 
    
317
 
    void write_zero(unsigned int numbytes)
318
 
    { for(unsigned int i=0; i<numbytes; ++i) output.put(0); }
319
 
    
320
 
    void put(char byte)
321
 
    { output.put(byte); }
322
 
    
323
 
private: // don't expose dangerous templates
324
 
    template<class T>
325
 
    bofstream &templated_write(const T &d)
326
 
    {
327
 
#ifdef __BIG_ENDIAN__
328
 
        if(!big_endian)
329
 
#else
330
 
            if(big_endian)
331
 
#endif
332
 
            {
333
 
                T swapped_copy=d;
334
 
                swap_endianity(swapped_copy);
335
 
                output.write((const char*)&swapped_copy, sizeof(T));
336
 
            }else
337
 
                output.write((const char*)&d, sizeof(T));
338
 
        return *this;
339
 
    }
340
 
public:
341
 
    
342
 
    template<class T>
343
 
    void write(const T *d, unsigned int num)
344
 
    {
345
 
        assert(d!=0);
346
 
        for(unsigned int i=0; i<num; ++i) (*this)<<d[i];
347
 
    }
348
 
    
349
 
    friend bofstream &operator<<(bofstream &, const bool &);
350
 
    friend bofstream &operator<<(bofstream &, const short int &);
351
 
    friend bofstream &operator<<(bofstream &, const unsigned short int &);
352
 
    friend bofstream &operator<<(bofstream &, const int &);
353
 
    friend bofstream &operator<<(bofstream &, const unsigned int &);
354
 
    friend bofstream &operator<<(bofstream &, const long int &);
355
 
    friend bofstream &operator<<(bofstream &, const unsigned long int &);
356
 
    friend bofstream &operator<<(bofstream &, const float &);
357
 
    friend bofstream &operator<<(bofstream &, const double &);
358
 
};
359
 
 
360
 
bofstream &operator<<(bofstream &output, const bool &d);
361
 
bofstream &operator<<(bofstream &output, const char &d);
362
 
bofstream &operator<<(bofstream &output, const signed char &d);
363
 
bofstream &operator<<(bofstream &output, const unsigned char &d);
364
 
bofstream &operator<<(bofstream &output, const short int &d);
365
 
bofstream &operator<<(bofstream &output, const unsigned short int &d);
366
 
bofstream &operator<<(bofstream &output, const int &d);
367
 
bofstream &operator<<(bofstream &output, const unsigned int &d);
368
 
bofstream &operator<<(bofstream &output, const long int &d);
369
 
bofstream &operator<<(bofstream &output, const unsigned long int &d);
370
 
bofstream &operator<<(bofstream &output, const float &d);
371
 
bofstream &operator<<(bofstream &output, const double &d);
372
 
 
373
 
#endif