-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrwfile.cpp
More file actions
93 lines (76 loc) · 2.24 KB
/
rwfile.cpp
File metadata and controls
93 lines (76 loc) · 2.24 KB
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
#include "rwfile.h"
#include "rwexception.h"
#include <cstring>
#include <byteswap.h>
#include <sys/stat.h>
//-----------------------------------------------------------------------------
void rws::bswap(uint32_t& data)
{
data = bswap_32(data);
}
//-----------------------------------------------------------------------------
rws::file::file(const std::string& filename, io_mode mode)
{
m_handle = fopen64(filename.c_str(), mode == io_mode::write ? "w+b" : "rb");
if (!m_handle)
raise("unable to open input file '", filename, "'");
struct stat64 si;
if (fstat64(fileno(m_handle), &si))
raise("failed to stat input file '", filename, "'");
m_size = si.st_size;
}
//-----------------------------------------------------------------------------
rws::file::~file()
{
if (m_handle)
fclose(m_handle);
}
//-----------------------------------------------------------------------------
void rws::file::seek(off64_t pos, int mode)
{
if (fseeko64(m_handle, pos, mode) != 0)
raise("seek error");
}
//-----------------------------------------------------------------------------
void rws::file::skip(off64_t off)
{
seek(off, SEEK_CUR);
}
//-----------------------------------------------------------------------------
off64_t rws::file::pos() const
{
return ftello64(m_handle);
}
//-----------------------------------------------------------------------------
void rws::file::read(uint8_t* buffer, size_t size)
{
if (fread(buffer, 1, size, m_handle) != size)
raise("read error");
}
//-----------------------------------------------------------------------------
template <> void rws::file::read(std::string& str)
{
char buffer[16];
size_t len = 0;
str.clear();
for(;;)
{
if (fread(buffer, 1, 16, m_handle) != 16)
raise("read error");
auto const end =
reinterpret_cast<char const*>(memchr(buffer, '\0', sizeof(buffer)));
if (end)
{
str.insert(len, buffer, static_cast<size_t>(end - buffer));
return;
}
str.insert(len, buffer, sizeof(buffer));
len += sizeof(buffer);
}
}
//-----------------------------------------------------------------------------
void rws::file::write(void const* data, size_t size)
{
if (fwrite(data, 1, size, m_handle) != size)
raise("write error");
}