This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfile.d
224 lines (192 loc) · 6.4 KB
/
file.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Written in the D programming language.
// This is derived from phobos/std/file.d, with a rewrite of the file reader.
/**
Utilities for manipulating files and scanning directories. Functions
in this module handle files as a unit, e.g., read or write one _file
at a time. For opening files and manipulating them via handles refer
to module $(LINK2 std_stdio.html,$(D std.stdio)).
Macros:
Copyright: Copyright Digital Mars 2007 - 2011.
License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(LINK2 http://digitalmars.com, Walter Bright),
$(LINK2 http://erdani.org, Andrei Alexandrescu),
Jonathan M Davis
*/
module file;
import std.file;
import core.memory;
import core.stdc.stdio, core.stdc.stdlib, core.stdc.string,
core.stdc.errno, std.algorithm, std.array, std.conv,
std.datetime, std.exception, std.format, std.path, std.process,
std.range, std.stdio, std.string, std.traits,
std.typecons, std.typetuple, std.utf;
version (Windows)
{
import core.sys.windows.windows, std.windows.syserror;
}
else version (Posix)
{
import core.sys.posix.dirent, core.sys.posix.fcntl, core.sys.posix.sys.stat,
core.sys.posix.sys.time, core.sys.posix.unistd, core.sys.posix.utime;
}
else
static assert(false, "Module " ~ .stringof ~ " not implemented for this OS.");
/**********************
* SPAD allows us to "look behind" the start of a buffer, to avoid the check
* EPAD ensures that buffers end in a \n
*/
enum SPAD = 16; // only need 2, the rest is to align the buffer
enum EPAD = 2;
/********************************************
Read entire contents of file $(D name) and returns it as an untyped
array. If the file size is larger than $(D upTo), only $(D upTo)
bytes are read.
Returns: Untyped array of bytes _read.
null if file doesn't exist.
*/
/* With or without this on Windows things might be faster - I get ambiguous results
*/
//version = onestat;
void[] myRead(in char[] name, size_t upTo = size_t.max)
{
void* result = null;
version(Windows)
{
auto namez = std.utf.toUTF16z(name);
/* Doing a stat to see if the file is there before attempting to read it
* turns out to be much faster.
*/
version (onestat)
{
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesExW(namez, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, &fad) == 0)
return null;
if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return null;
ULARGE_INTEGER li;
li.LowPart = fad.nFileSizeLow;
li.HighPart = fad.nFileSizeHigh;
auto size = cast(size_t)li.QuadPart;
}
else
{ // Two stats
auto attr = GetFileAttributesW(namez);
if (attr == 0xFFFF_FFFF || attr & FILE_ATTRIBUTE_DIRECTORY)
return null;
}
alias TypeTuple!(GENERIC_READ,
FILE_SHARE_READ, (SECURITY_ATTRIBUTES*).init, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
HANDLE.init)
defaults;
auto h = CreateFileW(namez, defaults);
if (h == INVALID_HANDLE_VALUE)
return null;
scope(exit) CloseHandle(h);
version (statsize)
{
}
else
{
auto size = GetFileSize(h, null);
if (size == INVALID_FILE_SIZE)
return null;
}
size = min(upTo, size);
auto buf = malloc(size + SPAD + EPAD);
assert(buf);
DWORD numread = void;
if (ReadFile(h, buf + SPAD, size, &numread, null) != 1
|| numread != size)
{
free(buf);
return null;
}
result = buf;
}
else version(Posix)
{
auto namez = toStringz(name);
/* Doing a stat to see if the file is there before attempting to read it
* turns out to be much faster.
*/
stat_t statbuf = void;
if (stat(namez, &statbuf) != 0 ||
(statbuf.st_mode & S_IFMT) != S_IFREG)
return null;
// A few internal configuration parameters {
enum size_t
minInitialAlloc = 1024 * 4,
maxInitialAlloc = size_t.max / 2,
sizeIncrement = 1024 * 16,
maxSlackMemoryAllowed = 1024;
// }
immutable fd = core.sys.posix.fcntl.open(namez,
core.sys.posix.fcntl.O_RDONLY);
if (fd == -1)
return null;
scope(exit) core.sys.posix.unistd.close(fd);
immutable initialAlloc = to!size_t(statbuf.st_size
? min(statbuf.st_size + 1, maxInitialAlloc)
: minInitialAlloc);
result = malloc(initialAlloc + SPAD + EPAD);
assert(result);
size_t result_length = initialAlloc;
size_t size = 0;
for (;;)
{
immutable actual = core.sys.posix.unistd.read(fd, result + size + SPAD,
min(result_length, upTo) - size);
if (actual == -1)
{
free(result);
return null;
}
if (actual == 0) break;
size += actual;
if (size < result_length) continue;
immutable newAlloc = size + sizeIncrement;
result = realloc(result, newAlloc + SPAD + EPAD);
assert(result);
result_length = newAlloc;
}
result = result_length - size >= maxSlackMemoryAllowed
? realloc(result, size + SPAD + EPAD)
: result;
}
else
static assert(0);
(cast(ubyte*)result)[SPAD - 2] = 0;
(cast(ubyte*)result)[SPAD - 1] = 0;
/* EPAD bytes are available past the end. Use to ensure file ends
* in \n. Need two in case file ends with a \ character.
*/
if (size)
{
if ((cast(ubyte*)result)[SPAD + size - 1] != '\n')
{
(cast(ubyte*)result)[SPAD + size] = '\n';
(cast(ubyte*)result)[SPAD + size + 1] = '\n';
size += 2;
}
}
else
{ // File is empty, so make it a one-liner
(cast(ubyte*)result)[SPAD] = '\n';
++size;
}
return result[SPAD .. SPAD + size];
}
/*****************************
* Free buffer allocated by myRead().
*/
void myReadFree(void[] buf)
{
free(buf.ptr - SPAD);
}
/*
* Local Variables:
* mode: d
* c-basic-offset: 4
* End:
*/