-
Notifications
You must be signed in to change notification settings - Fork 1
/
w3Fd.cpp
104 lines (85 loc) · 1.44 KB
/
w3Fd.cpp
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
// A WebAssembly codebase by Jay Krell
//
// https://webassembly.github.io/spec/core/binary/index.html
// https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
#include "w3.h"
#include "w3Fd.h"
#ifndef _WIN32
uint64_t w3Fd::get_file_size (PCSTR file_name)
{
#if __CYGWIN__
struct stat st = { 0 }; // TODO test more systems
if (fstat (fd, &st))
#else
struct stat64 st = { 0 }; // TODO test more systems
if (fstat64 (fd, &st))
#endif
ThrowErrno (StringFormat ("fstat (%s)", file_name).c_str ());
return st.st_size;
}
#endif
#if 0 // C++11
w3Fd::operator bool ()
{
return valid ();
}
#else
w3Fd::operator explicit_operator_bool::T () const
{
return valid () ? &explicit_operator_bool::True : 0;
}
#endif
bool w3Fd::operator ! ()
{
return !valid ();
}
w3Fd::operator int ()
{
return get ();
}
bool w3Fd::static_valid (int fd)
{
return fd != -1;
}
int w3Fd::get () const
{
return fd;
}
bool w3Fd::valid () const
{
return static_valid (fd);
}
void w3Fd::static_cleanup (int fd)
{
if (!static_valid (fd))
return;
#if _WIN32
_close (fd);
#else
close (fd);
#endif
}
int w3Fd::detach ()
{
int const a = fd;
fd = -1;
return a;
}
void w3Fd::cleanup ()
{
static_cleanup (detach ());
}
w3Fd::w3Fd (int a) : fd (a)
{
}
w3Fd& w3Fd::operator = (int a)
{
if (fd == a) return *this;
cleanup ();
fd = a;
return *this;
}
w3Fd::~w3Fd ()
{
cleanup ();
}