/
memconserve_stdfile.d
119 lines (108 loc) · 3.72 KB
/
memconserve_stdfile.d
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
module memconserve_stdfile;
import quickformat, std.file, std.date, std.string;
// std.file with more delete()
version(Win32) {
import std.c.windows.windows;
extern(C) void abort();
void getTimes(char[] name, out d_time ftc, out d_time fta, out d_time ftm)
{
HANDLE findhndl;
if (useWfuncs)
{
WIN32_FIND_DATAW filefindbuf;
auto nameptr = std.utf.toUTF16z(name);
scope(exit) delete nameptr;
findhndl = FindFirstFileW(nameptr, &filefindbuf);
ftc = std.date.FILETIME2d_time(&filefindbuf.ftCreationTime);
fta = std.date.FILETIME2d_time(&filefindbuf.ftLastAccessTime);
ftm = std.date.FILETIME2d_time(&filefindbuf.ftLastWriteTime);
}
else
{
WIN32_FIND_DATA filefindbuf;
auto nameptr = toMBSz(name);
scope(exit) delete nameptr;
findhndl = FindFirstFileA(nameptr, &filefindbuf);
ftc = std.date.FILETIME2d_time(&filefindbuf.ftCreationTime);
fta = std.date.FILETIME2d_time(&filefindbuf.ftLastAccessTime);
ftm = std.date.FILETIME2d_time(&filefindbuf.ftLastWriteTime);
}
if (findhndl == cast(HANDLE)-1)
{
throw new FileException(name, GetLastError());
}
FindClose(findhndl);
}
int exists(char[] name)
{
uint result;
if (useWfuncs) {
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fil
auto nameptr = std.utf.toUTF16z(name);
scope(exit) delete nameptr;
result = GetFileAttributesW(nameptr);
} else {
auto nameptr = toMBSz(name);
scope(exit) delete nameptr;
result = GetFileAttributesA(nameptr);
}
return result != 0xFFFFFFFF;
}
} else {
import std.c.unix.unix, std.c.stdlib;
void getTimes(string name, out d_time ftc, out d_time fta, out d_time ftm)
{
auto namez = cast(char*) std.c.stdlib.malloc(name.length + 1);
scope(success) std.c.stdlib.free(namez);
namez[0..name.length] = name;
namez[name.length] = cast(char) 0;
struct_stat statbuf;
if (unix.stat(namez, &statbuf))
{
throw new FileException(name, getErrno());
}
version (GNU)
{
ftc = cast(d_time)statbuf.st_ctime * std.date.TicksPerSecond;
fta = cast(d_time)statbuf.st_atime * std.date.TicksPerSecond;
ftm = cast(d_time)statbuf.st_mtime * std.date.TicksPerSecond;
}
else version (linux)
{
ftc = cast(d_time)statbuf.st_ctime * std.date.TicksPerSecond;
fta = cast(d_time)statbuf.st_atime * std.date.TicksPerSecond;
ftm = cast(d_time)statbuf.st_mtime * std.date.TicksPerSecond;
}
else version (OSX)
{ // BUG: should add in tv_nsec field
ftc = cast(d_time)statbuf.st_ctimespec.tv_sec * std.date.TicksPerSecond;
fta = cast(d_time)statbuf.st_atimespec.tv_sec * std.date.TicksPerSecond;
ftm = cast(d_time)statbuf.st_mtimespec.tv_sec * std.date.TicksPerSecond;
}
else version (FreeBSD)
{ // BUG: should add in tv_nsec field
ftc = cast(d_time)statbuf.st_ctimespec.tv_sec * std.date.TicksPerSecond;
fta = cast(d_time)statbuf.st_atimespec.tv_sec * std.date.TicksPerSecond;
ftm = cast(d_time)statbuf.st_mtimespec.tv_sec * std.date.TicksPerSecond;
}
else version (Solaris)
{ // BUG: should add in *nsec fields
ftc = cast(d_time)statbuf.st_ctime * std.date.TicksPerSecond;
fta = cast(d_time)statbuf.st_atime * std.date.TicksPerSecond;
ftm = cast(d_time)statbuf.st_mtime * std.date.TicksPerSecond;
}
else
{
static assert(0);
}
}
int exists(char[] name)
{
auto np = cast(char*) std.c.stdlib.malloc(name.length + 1);
np[0..name.length] = name;
np[name.length] = cast(char) 0;
auto res = access(np, 0) == 0;
std.c.stdlib.free(np);
return res;
}
}