-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.d
More file actions
517 lines (432 loc) · 13.5 KB
/
Copy pathapp.d
File metadata and controls
517 lines (432 loc) · 13.5 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
A simple version of the unix 'cat' program. It is used for performance testing.
Copyright (c) 2019, Jon Degenhardt
*/
module dcat;
import std.typecons : Flag, No, Yes, tuple;
auto helpText = q"EOS
Synopsis: dcat -t <test> [file]
dcat reads from a file or standard input and writes each line to standard
output. This program is used for performance testing.
Tests available (with input and output methods):
* byLineInRawOut
- Input: std.stdio.File.byLine
- Output: std.stdio.File.write
* byLineInBufOut
- Input: std.stdio.File.byLine
- Output: tsv_utils.common.utils.BufferedOutputRange
* bufferedByLineInBufOut
- Input: tsv_utils.common.utils.bufferedByLine
- Output: tsv_utils.common.utils.BufferedOutputRange
* iopipeByLineInRawOut
- Input: iopipe.byLine
- Output: std.stdio.File.write
* iopipeByLineInBufOut
- Input: iopipe.byLine
- Output: tsv_utils.common.utils.BufferedOutputRange
* iopipeByLineInIOOut
- Input: iopipe.byLine
- Output: std.io.file.File.write
* iopipeByLineInBufIOOut
- Input: iopipe.byLine
- Output: std.io.file.File.write (buffered)
* byChunkInRawOut
- Input: std.stdio.File.byChunk
- Output: std.stdio.File.rawWrite
* byChunkInBufOut
- Input: std.stdio.File.byChunk
- Output: tsv_utils.common.utils.BufferedOutputRange
* byChunkByLine
- Read chunk at a time, write line at a time, ignoring line boundaries
- Input: std.stdio.File.byChunk
- Output: tsv_utils.common.utils.BufferedOutputRange
Options:
EOS";
enum DCatTest
{
byLineInRawOut,
byLineInBufOut,
bufferedByLineInBufOut,
iopipeByLineInRawOut,
iopipeByLineInBufOut,
iopipeByLineInIOOut,
iopipeByLineInBufIOOut,
byChunkInBufOut,
byChunkInRawOut,
byChunkInByLineBufOut,
};
/** Container for command line options.
*/
struct CmdOptions
{
enum defaultHeaderString = "line";
string programName;
DCatTest dcatTest = DCatTest.byLineInRawOut; // --t|test
bool namesWanted = false; // --n|names
/* Returns a tuple. First value is true if command line arguments were successfully
* processed and execution should continue, or false if an error occurred or the user
* asked for help. If false, the second value is the appropriate exit code (0 or 1).
*/
auto processArgs (ref string[] cmdArgs)
{
import std.algorithm : any, each;
import std.getopt;
import std.path : baseName, stripExtension;
import std.stdio;
programName = (cmdArgs.length > 0) ? cmdArgs[0].stripExtension.baseName : "Unknown_program_name";
try
{
import std.traits : EnumMembers;
import std.conv : to;
auto dcatTestNames = [EnumMembers!DCatTest];
auto testOptionDescription = "One of: " ~ dcatTestNames.to!string;
auto r = getopt(
cmdArgs,
std.getopt.config.caseSensitive,
"t|test", testOptionDescription, &dcatTest,
"n|names", "Returns the list of test names.", &namesWanted,
);
if (r.helpWanted)
{
defaultGetoptPrinter(helpText, r.options);
return tuple(false, 0);
}
else if (namesWanted)
{
writefln("%(%s %)", dcatTestNames);
return tuple(false, 0);
}
}
catch (Exception e)
{
stderr.writefln("[%s] Error processing command line arguments: %s", programName, e.msg);
return tuple(false, 1);
}
return tuple(true, 0);
}
}
static if (__VERSION__ >= 2085) extern(C) __gshared string[] rt_options = [ "gcopt=cleanup:none" ];
/** Main program. */
int main(string[] cmdArgs)
{
/* When running in DMD code coverage mode, turn on report merging. */
version(D_Coverage) version(DigitalMars)
{
import core.runtime : dmd_coverSetMerge;
dmd_coverSetMerge(true);
}
CmdOptions cmdopt;
auto r = cmdopt.processArgs(cmdArgs);
if (!r[0]) return r[1];
version(LDC_Profile)
{
/* LDC profile build - reset data collection after command line arg processing. */
import ldc.profile : resetAll;
resetAll();
}
try dcat(cmdopt, cmdArgs[1..$]);
catch (Exception exc)
{
import std.stdio;
stderr.writefln("Error [%s]: %s", cmdopt.programName, exc.msg);
return 1;
}
return 0;
}
/** Primary routine, select the dcat test to run.
*/
void dcat(in CmdOptions cmdopt, in string[] inputFiles)
{
import std.stdio;
import std.conv : to;
import std.range;
import tsv_utils.common.utils : BufferedOutputRange;
auto bufferedOutput = BufferedOutputRange!(typeof(stdout))(stdout);
auto filename = inputFiles.length > 0 ? inputFiles[0] : "-";
final switch(cmdopt.dcatTest)
{
case DCatTest.byLineInRawOut:
useByLineInRawOut(cmdopt, filename);
break;
case DCatTest.byLineInBufOut:
useByLineInBufOut(cmdopt, filename, bufferedOutput);
break;
case DCatTest.bufferedByLineInBufOut:
useBufferedByLineInBufOut(cmdopt, filename, bufferedOutput);
break;
case DCatTest.iopipeByLineInRawOut:
useIopipeByLineInRawOut(cmdopt, filename);
break;
case DCatTest.iopipeByLineInBufOut:
useIopipeByLineInBufOut(cmdopt, filename, bufferedOutput);
break;
case DCatTest.iopipeByLineInIOOut:
useIopipeByLineInIOOut(cmdopt, filename);
break;
case DCatTest.iopipeByLineInBufIOOut:
useIopipeByLineInBufIOOut(cmdopt, filename);
break;
case DCatTest.byChunkInRawOut:
useByChunkInRawOut(cmdopt, filename);
break;
case DCatTest.byChunkInBufOut:
useByChunkInBufOut(cmdopt, filename, bufferedOutput);
break;
case DCatTest.byChunkInByLineBufOut:
useByChunkInByLineBufOut(cmdopt, filename, bufferedOutput);
break;
}
}
void useByLineInRawOut(CmdOptions cmdopt, string filename)
{
import std.stdio;
auto inputStream = (filename == "-") ? stdin : filename.File();
foreach (line; inputStream.byLine(Yes.keepTerminator))
{
stdout.write(line);
}
}
void useByLineInBufOut(OutputRange)(CmdOptions cmdopt, string filename, auto ref OutputRange outputStream)
{
import std.range;
import std.stdio;
auto inputStream = (filename == "-") ? stdin : filename.File();
foreach (line; inputStream.byLine(Yes.keepTerminator))
{
outputStream.put(line);
}
}
void useBufferedByLineInBufOut(OutputRange)(CmdOptions cmdopt, string filename, auto ref OutputRange outputStream)
{
import std.range;
import std.stdio;
import tsv_utils.common.utils : bufferedByLine;
auto inputStream = (filename == "-") ? stdin : filename.File();
foreach (ref line; inputStream.bufferedByLine!(Yes.keepTerminator))
{
outputStream.put(line);
}
}
void useIopipeByLineInRawOut(CmdOptions cmdopt, string filename)
{
import iopipe.textpipe;
import iopipe.bufpipe;
import std.stdio : writeln;
import std.io;
import std.typecons : refCounted;
version(Posix)
{
/* At present (std.io 0.2.2) supports access to stdin/stdout/stderr only via
* native handles. See: https://github.com/MartinNowak/io/issues/14
*/
auto inputStream = (filename == "-") ? 0.File().refCounted : filename.File().refCounted;
}
else
{
if (filename == "-")
{
import std.exception;
throw new Exception("useIoPipeByLineInBufOut supports reading from stdin only on Posix.");
}
auto inputStream = filename.File().refCounted;
}
foreach (ref line; inputStream.bufd.assumeText.byLineRange)
{
writeln(line);
}
}
void useIopipeByLineInBufOut(OutputRange)(CmdOptions cmdopt, string filename, auto ref OutputRange outputStream)
{
import std.range;
import iopipe.textpipe;
import iopipe.bufpipe;
import std.io;
import std.typecons : refCounted;
version(Posix)
{
/* At present (std.io 0.2.2) supports access to stdin/stdout/stderr only via
* native handles. See: https://github.com/MartinNowak/io/issues/14
*/
auto inputStream = (filename == "-") ? 0.File().refCounted : filename.File().refCounted;
}
else
{
if (filename == "-")
{
import std.exception;
throw new Exception("useIoPipeByLineInBufOut supports reading from stdin only on Posix.");
}
auto inputStream = filename.File().refCounted;
}
foreach (ref line; inputStream.bufd.assumeText.byLineRange)
{
outputStream.put(line);
outputStream.put('\n');
}
}
void useIopipeByLineInIOOut(CmdOptions cmdopt, string filename)
{
import iopipe.textpipe;
import iopipe.bufpipe;
import std.io;
import std.typecons : refCounted;
version(Posix)
{
/* At present (std.io 0.2.2) supports access to stdin/stdout/stderr only via
* native handles. See: https://github.com/MartinNowak/io/issues/14
*/
auto inputStream = (filename == "-") ? 0.File().refCounted : filename.File().refCounted;
auto ioStdout = 1.File().refCounted;
}
else
{
import std.exception;
throw new Exception("useIoPipeByLineInIOOut is available only on Posix.");
}
foreach (ref line; inputStream.bufd.assumeText.byLineRange)
{
ioStdout.write(cast(ubyte[])line, cast(ubyte[])"\n");
}
}
void useIopipeByLineInBufIOOut(CmdOptions cmdopt, string filename)
{
import iopipe.textpipe;
import iopipe.bufpipe;
import std.io;
import std.typecons : refCounted;
version(Posix)
{
/* At present (std.io 0.2.2) supports access to stdin/stdout/stderr only via
* native handles. See: https://github.com/MartinNowak/io/issues/14
*/
auto inputStream = (filename == "-") ? 0.File().refCounted : filename.File().refCounted;
auto outputStream = BufferedIOStdout!ubyte();
}
else
{
import std.exception;
throw new Exception("useIoPipeByLineInIOOut is available only on Posix.");
}
foreach (ref line; inputStream.bufd.assumeText.byLineRange)
{
outputStream.put(line);
outputStream.put('\n');
}
}
void useByChunkInRawOut(CmdOptions cmdopt, string filename)
{
import std.stdio;
auto ifile = (filename == "-") ? stdin : filename.File;
foreach (ref c; ifile.byChunk(1024 * 128)) stdout.rawWrite(c);
}
void useByChunkInBufOut(BufferedOutputRange)(CmdOptions cmdopt, string filename, auto ref BufferedOutputRange outputStream)
{
import std.stdio;
import tsv_utils.common.utils : bufferedByLine;
ubyte[1024 * 128] fileRawBuf;
auto ifile = (filename == "-") ? stdin : filename.File;
foreach (ref ubyte[] buffer; ifile.byChunk(fileRawBuf))
{
outputStream.append(buffer);
outputStream.flushIfFull;
}
outputStream.flush;
}
void useByChunkInByLineBufOut(OutputRange)(CmdOptions cmdopt, string filename, auto ref OutputRange outputStream)
{
import std.algorithm : splitter;
import std.stdio;
ubyte[1024 * 128] fileRawBuf;
auto ifile = (filename == "-") ? stdin : filename.File;
foreach (ref ubyte[] buffer; ifile.byChunk(fileRawBuf))
{
bool first = true;
foreach (ref line; buffer.splitter('\n'))
{
if (first) first = false;
else outputStream.put('\n');
outputStream.put(line);
}
}
}
/* Simple buffering of standard output using std.io. */
struct BufferedIOStdout(C = char)
if (is(C == char) || is(C == ubyte))
{
import std.array : appender;
import std.io;
private enum _reserveSize = 11264;
private enum _flushSize = 10240;
private File _ioStdout;
private auto _buffer = appender!(C[]);
static BufferedIOStdout opCall()
{
BufferedIOStdout x;
x._ioStdout = 1.File();
x._buffer.reserve(_reserveSize);
return x;
}
~this()
{
flush();
}
private void flush()
{
if (_buffer.data.length > 0)
{
_ioStdout.write(_buffer.data);
_buffer.clear;
}
}
private void appendFlush(T)(auto ref T stuff)
if (is(T : char[]) || is(T : ubyte[]))
{
if (_buffer.data.length > 0)
{
_ioStdout.write(cast(ubyte[]) _buffer.data, cast(ubyte[]) stuff);
_buffer.clear;
}
else
{
_ioStdout.write(cast(ubyte[]) stuff);
}
}
private void append(T)(auto ref T stuff)
{
import std.range : rangePut = put;
static if (is(T : char[]))
{
rangePut(_buffer, cast(ubyte[]) stuff);
}
else static if (is(T == char))
{
rangePut(_buffer, cast(ubyte) stuff);
}
else
{
rangePut(_buffer, stuff);
}
}
void put(T)(auto ref T stuff)
if (is(T : char[]) || is(T : ubyte[]) || is(T : char) || is(T : ubyte))
{
import std.traits;
static if (is(T : char[]) || is(T : ubyte[]))
{
if (_buffer.data.length + stuff.length > _reserveSize)
{
appendFlush(stuff);
}
else
{
append(stuff);
if (_buffer.data.length >= _flushSize) flush;
}
}
else
{
append(stuff);
if (_buffer.data.length >= _flushSize) flush;
}
}
}