-
Notifications
You must be signed in to change notification settings - Fork 1
/
rwfile.h
76 lines (57 loc) · 1.63 KB
/
rwfile.h
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
#ifndef _rwfile_h
#define _rwfile_h
#include <string>
#include <cstdio>
#include <byteswap.h>
#include "rwexception.h"
#include "rwtypes.h"
namespace rws
{
inline void bswap(uint8_t&) { /* no-op*/ }
extern void bswap(uint32_t&);
//---------------------------------------------------------------------------
class file
{
public:
file(const std::string& filename, io_mode = io_mode::read);
~file();
template <typename T> void read(T& data);
template <typename T> void read_be(T& data);
template <typename T> void write(T data);
void read(uint8_t* buffer, size_t size);
void write(void const* data, size_t size);
void seek(off64_t pos, int mode = SEEK_SET);
void skip(off64_t off);
template <typename T> void skip();
inline off64_t size() const { return m_size; }
off64_t pos() const;
protected:
FILE* m_handle;
off64_t m_size;
};
template <> void file::read(std::string&);
}
//-----------------------------------------------------------------------------
template <typename T>
void rws::file::read(T& data)
{
if (fread(&data, sizeof(T), 1, m_handle) != 1)
raise("read error");
}
//-----------------------------------------------------------------------------
template <typename T> void rws::file::read_be(T& data)
{
read(data);
bswap(data);
}
//-----------------------------------------------------------------------------
template <typename T> void rws::file::write(T data)
{
write(&data, sizeof(data));
}
//-----------------------------------------------------------------------------
template <typename T> void rws::file::skip()
{
skip(sizeof(T));
}
#endif