forked from dlang/phobos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.d
3334 lines (3096 loc) · 104 KB
/
io.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Standard I/O functions that extend $(B std.c.stdio). $(B std.c.stdio)
is $(D_PARAM public)ally imported when importing $(B std.stdio).
Source: $(PHOBOSSRC std/_stdio.d)
Macros:
WIKI=Phobos/StdStdio
Copyright: Copyright Digital Mars 2007-.
License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(WEB digitalmars.com, Walter Bright),
$(WEB erdani.org, Andrei Alexandrescu),
Steven Schveighoffer
*/
module std.io;
import std.format;
import std.string;
import core.memory, core.stdc.errno, core.stdc.stddef, core.stdc.stdlib,
core.stdc.string, core.stdc.wchar_, core.bitop;
import std.traits;
import std.range;
import std.utf;
import std.conv;
import std.typecons;
import std.exception;
// This uses inline utf decoding/encoding instead of calling runtime functions.
version = inlineutf;
version (DigitalMars) version (Windows)
{
// Specific to the way Digital Mars C does stdio
version = DIGITAL_MARS_STDIO;
import std.c.stdio : __fhnd_info, FHND_WCHAR, FHND_TEXT;
}
version (Posix)
{
import core.sys.posix.stdio;
import core.sys.posix.fcntl;
import core.sys.posix.unistd;
alias core.sys.posix.stdio.fileno fileno;
}
version (linux)
{
// Specific to the way Gnu C does stdio
version = GCC_IO;
extern(C) FILE* fopen64(const char*, const char*);
}
version (OSX)
{
version = GENERIC_IO;
alias core.stdc.stdio.fopen fopen64;
}
version (FreeBSD)
{
version = GENERIC_IO;
alias core.stdc.stdio.fopen fopen64;
}
version(Windows)
{
alias core.stdc.stdio.fopen fopen64;
}
version (DIGITAL_MARS_STDIO)
{
extern (C)
{
/* **
* Digital Mars under-the-hood C I/O functions.
* Use _iobuf* for the unshared version of FILE*,
* usable when the FILE is locked.
*/
int _fputc_nlock(int, _iobuf*);
int _fputwc_nlock(int, _iobuf*);
int _fgetc_nlock(_iobuf*);
int _fgetwc_nlock(_iobuf*);
int __fp_lock(FILE*);
void __fp_unlock(FILE*);
int setmode(int, int);
}
alias _fputc_nlock FPUTC;
alias _fputwc_nlock FPUTWC;
alias _fgetc_nlock FGETC;
alias _fgetwc_nlock FGETWC;
alias __fp_lock FLOCK;
alias __fp_unlock FUNLOCK;
alias setmode _setmode;
enum _O_BINARY = 0x8000;
int _fileno(FILE* f) { return f._file; }
alias _fileno fileno;
}
else version (GCC_IO)
{
/* **
* Gnu under-the-hood C I/O functions; see
* http://gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html
*/
extern (C)
{
int fputc_unlocked(int, _iobuf*);
int fputwc_unlocked(wchar_t, _iobuf*);
int fgetc_unlocked(_iobuf*);
int fgetwc_unlocked(_iobuf*);
void flockfile(FILE*);
void funlockfile(FILE*);
ssize_t getline(char**, size_t*, FILE*);
ssize_t getdelim (char**, size_t*, int, FILE*);
private size_t fwrite_unlocked(const(void)* ptr,
size_t size, size_t n, _iobuf *stream);
}
version (linux)
{
// declare fopen64 if not already
static if (!is(typeof(fopen64)))
extern (C) FILE* fopen64(in char*, in char*);
}
alias fputc_unlocked FPUTC;
alias fputwc_unlocked FPUTWC;
alias fgetc_unlocked FGETC;
alias fgetwc_unlocked FGETWC;
alias flockfile FLOCK;
alias funlockfile FUNLOCK;
}
else version (GENERIC_IO)
{
extern (C)
{
void flockfile(FILE*);
void funlockfile(FILE*);
}
int fputc_unlocked(int c, _iobuf* fp) { return fputc(c, cast(shared) fp); }
int fputwc_unlocked(wchar_t c, _iobuf* fp)
{
return fputwc(c, cast(shared) fp);
}
int fgetc_unlocked(_iobuf* fp) { return fgetc(cast(shared) fp); }
int fgetwc_unlocked(_iobuf* fp) { return fgetwc(cast(shared) fp); }
alias fputc_unlocked FPUTC;
alias fputwc_unlocked FPUTWC;
alias fgetc_unlocked FGETC;
alias fgetwc_unlocked FGETWC;
alias flockfile FLOCK;
alias funlockfile FUNLOCK;
}
else
{
static assert(0, "unsupported C I/O system");
}
enum PAGE = 4096;
/**
* Interface defining common stream functions
*/
interface StreamBase
{
/**
* Seek the stream. seekCurrent seeks from the current stream position,
* seekAboslute seeks to the given position offset from the beginning of
* the stream, and seekEnd seeks to the posision offset bytes from the end
* of the stream (backwards).
*
* Note, this throws an exception if seeking fails or isn't supported.
*
* params:
* offset = bytes to seek.
*
* returns: The position of the stream from the beginning of the stream
* after seeking, or ulong.max if this cannot be determined.
*/
ulong seekCurrent(long offset);
/// ditto
ulong seekAbsolute(ulong offset);
/// ditto
ulong seekEnd(ulong offset);
/**
* Determine the current file position. Calls seekCurrent(0).
*
* Returns ulong.max if the operation fails or is not supported.
*/
final ulong tell() {return seekCurrent(0); }
/**
* returns: false if the stream cannot be seeked, true if it can. True
* does not mean that a given seek call will succeed.
*/
@property bool seekable();
/**
* Close the stream. This releases any resources from the object.
*/
void close();
}
/**
* An input stream. This is the simplest interface to a stream. An
* InputStream provides no buffering or high-level constructs, it's simply an
* abstraction of a low-level stream mechanism.
*/
interface InputStream : StreamBase
{
/**
* Read data from the stream.
*
* Throws an exception if reading does not succeed.
*
* params: data = Location to store the data from the stream. Only the
* data read from the stream is filled in. It is valid for read to return
* less than the number of bytes requested *and* not be at EOF.
*
* returns: 0 on EOF, number of bytes read otherwise.
*/
size_t read(ubyte[] data);
}
/**
* simple output interface.
*/
interface OutputStream : StreamBase
{
/**
* Write a chunk of data to the output stream
*
* params:
* data = The buffer to write to the stream.
* returns: the number of bytes written on success. If 0 is returned, then
* the stream cannot be written to.
*/
size_t put(const(ubyte)[] data);
/// ditto
alias put write;
}
/**
* The basic device-based Input and Output stream. This uses the OS's native
* file handle to communicate using physical devices.
*/
class IODevice : InputStream, OutputStream
{
private
{
// the file descriptor
// fd is set to -1 when the stream is closed
int fd = -1;
// This is purely used only to close the file when this IODevice is
// using the same file descriptor as a FILE *.
FILE * _cfile;
// -1 = can't seek, 1 = can seek, 0 = uninitialized
byte _canSeek = 0;
// flag indicating the destructor should close the stream. Used
// when a File does not own the fd in question (set to false).
bool _closeOnDestroy;
}
/**
* Construct an input stream based on the file descriptor
*
* params:
* fd = The file descriptor to wrap
* closeOnDestroy = If set to true, the destructor will close the file
* descriptor. This does not affect the operation of close.
*/
this(int fd, bool closeOnDestroy = true)
{
this.fd = fd;
this._closeOnDestroy = closeOnDestroy;
}
/**
* Construct an input stream based on a FILE *. The only difference
* between this and the fd version is the close routine will close the
* FILE * if valid.
*
* params:
* fstream = The FILE * instance to use for initialization.
* closeOnDestroy = If set to true, the destructor will close the file
* descriptor. This does not affect the operation of close.
*/
this(FILE * fstream, bool closeOnDestroy = true)
{
assert(fstream);
this._cfile = fstream;
this.fd = .fileno(fstream);
this._closeOnDestroy = closeOnDestroy;
}
/**
* Open a file. the specification for mode is identical to the linux man
* page for fopen
*/
this(in char[] name, in char[] mode = "rb")
{
if(!mode.length)
throw new Exception("error in mode specification");
// first, parse the open mode
char m = mode[0];
switch(m)
{
case 'r': case 'a': case 'w':
break;
default:
throw new Exception("error in mode specification");
}
bool rw = false;
bool bflag = false;
foreach(i, c; mode[1..$])
{
if(i > 1)
throw new Exception("Error in mode specification");
switch(c)
{
case '+':
if(rw)
throw new Exception("Error in mode specification");
rw = true;
break;
case 'b':
// valid, but does nothing
if(bflag)
throw new Exception("Error in mode specification");
bflag = true;
break;
default:
throw new Exception("Error in mode specification");
}
}
// create the flags
int flags = rw ? O_RDWR : (m == 'r' ? O_RDONLY : O_WRONLY | O_CREAT);
if(!rw && m == 'w')
flags |= O_TRUNC;
this.fd = .open(toStringz(name), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(this.fd < 0)
throw new Exception("Error opening file, check errno");
// perform a seek if necessary
if(m == 'a')
{
seekEnd(0);
}
}
private ulong _seek(long delta, int whence)
{
auto retval = .lseek64(fd, delta, cast(int)whence);
if(retval < 0)
{
// TODO: probably need an I/O exception
throw new Exception("seek failed, check errno");
}
return retval;
}
/**
* Seek the stream. seekCurrent seeks from the current stream position,
* seekAboslute seeks to the given position offset from the beginning of
* the stream, and seekEnd seeks to the posision offset bytes from the end
* of the stream (backwards).
*
* Note, this throws an exception if seeking fails or isn't supported.
*
* params:
* offset = bytes to seek.
*
* returns: The position of the stream from the beginning of the stream
* after seeking, or ulong.max if this cannot be determined.
*/
ulong seekCurrent(long offset)
{
return _seek(offset, SEEK_CUR);
}
/// ditto
ulong seekAbsolute(ulong offset)
{
assert(offset <= long.max);
return _seek(offset, SEEK_SET);
}
/// ditto
ulong seekEnd(ulong offset)
{
assert(offset <= long.max);
return _seek(-cast(long)offset, SEEK_END);
}
/**
* returns: false if the stream cannot be seeked, true if it can. True
* does not mean that a given seek call will succeed, it depends on the
* implementation/environment.
*/
@property bool seekable()
{
// by default, we return true, because we cannot determine if a generic
// file descriptor can be seeked.
if(!_canSeek)
{
_canSeek = .lseek64(fd, 0, SEEK_CUR) == -1 ? -1 : 1;
}
return _canSeek > 0;
}
/**
* Read data from the stream.
*
* Throws an exception if reading does not succeed.
*
* params: data = Location to store the data from the stream. Only the
* data read from the stream is filled in. It is valid for read to return
* less than the number of bytes requested *and* not be at EOF.
*
* returns: 0 on EOF, number of bytes read otherwise.
*/
size_t read(ubyte[] data)
{
auto result = .read(fd, data.ptr, data.length);
if(result < 0)
{
// TODO: need an I/O exception
throw new Exception("read failed, check errno");
}
return cast(size_t)result;
}
/**
* Write a chunk of data to the output stream
*
* returns the number of bytes written on success.
*
* If 0 is returned, then the stream cannot be written to.
*/
size_t put(const(ubyte)[] data)
{
auto result = core.sys.posix.unistd.write(fd, data.ptr, data.length);
if(result < 0)
{
// Should we check for EPIPE? Not sure.
//if(errno == EPIPE)
// return 0;
throw new Exception("write failed, check errno");
}
return cast(size_t)result;
}
/// ditto
alias put write;
/**
* Close the stream. This releases any resources from the object.
*/
void close()
{
if(_cstream && .fclose(_cstream) == EOF)
throw new Exception("close failed, check errno");
else if(fd != -1 && .close(fd) < 0)
throw new Exception("close failed, check errno");
_cstream = null;
fd = -1;
}
/**
* Destructor. This is used as a safety net, in case the stream isn't
* closed before being destroyed in the GC. It is recommended to close
* deterministically using close, because there is no guarantee the GC will
* call this destructor.
*
* If the IODevice was designated not to close on destroy, the destructor
* does not close the underlying handle.
*/
~this()
{
if(_closeOnDestroy)
{
if(_cstream)
{
// can't check this for errors, because we can't throw in a
// destructor.
.fclose(_cstream);
}
else if(fd != -1)
{
// can't check this for errors, because we can't throw in a
// destructor.
.close(fd);
}
}
_cstream = null;
fd = -1;
}
/**
* Get the OS-specific handle for this File
*/
@property int handle()
{
return fd;
}
}
/**
* D buffered stream.
*
* This object wraps a source InputStream and/or OutputStream with a buffering
* system implemented purely in D.
*/
final class BufferedStream : StreamBase
{
private
{
// the source streams
InputStream _input;
OutputStream _output;
// the buffered data
ubyte[] buffer;
// the number of bytes to add when the buffer need to be extended.
size_t growSize;
// the minimum read size for reading from the OS. Attempting to read
// more than this will avoid using the buffer if possible.
size_t minReadSize;
// the current read/write position
size_t pos = 0;
// the position just beyond the last valid data. This is like the end
// of the valid data. If in write mode, this is set to size_t.max,
// as the number of valid bytes to write is in the pos member.
size_t valid = 0;
// a decoder function. When set, this decodes data coming in. This is
// useful for cases where for example byte-swapping must be done. It
// should return the number of bytes processed (for example, if you are
// byte-swapping every 2 bytes, and data contains an odd number of
// bytes, you cannot process the last byte.
size_t delegate(ubyte[] data) _decode;
// function to encode data for writing. This is called prior to
// writing the data to the underlying stream. When this is set, extra
// copies of the data may be made.
size_t delegate(ubyte[] data) _encode;
// function to check how much data should be flushed.
ptrdiff_t delegate(const(ubyte)[] data) _flushCheck;
// the number of bytes already decoded. not used in write mode
size_t decoded = 0;
}
/**
* Constructor. Wraps an input and output stream.
*
* Note that there is a convenience routine buffered which handles various
* flavors of input and output streams automatically.
*
* params:
*
*/
this(InputStream input, OutpuStream output, uint defGrowSize, size_t minReadSize)
{
this.minReadSize = minReadSize;
this._input = input;
this._output = output;
// make sure the default grow size is at least the minimum read size.
if(defGrowSize < minReadSize)
defGrowSize = minReadSize;
this.buffer = new ubyte[this.growSize = defGrowSize];
// ensure we aren't wasting any space.
this.buffer.length = this.buffer.capacity;
}
private setReadMode()
{
assert(_input !is null); // not valid to be in readmode unless input is valid.
if(valid == valid.max)
{
// currently in write mode. flush whatever data there is, then
// clear the buffer.
if(_output)
_flush_noModeCheck();
assert(pos == 0, "Dirty switch to read mode");
valid = decoded = 0; // reset the buffer
}
}
private setWriteMode()
{
assert(_output !is null); // not valid to be in write mode unless
// _output is valid.
if(valid != valid.max)
{
// in read mode. This is a bit trickier, since we haven't
// "technically" read the data that is still valid after pos. Use
// seeking to try and align the underlying stream back to our
// current position.
if(_input && pos != valid)
// do a seek
_input.seekCurrent(-cast(long)(valid - pos));
valid = valid.max;
pos = 0;
}
}
/* READ ROUTINES */
/**
* Read bytes into a buffer. Note that this may copy data to an internal
* buffer first before copying to the parameter. However, every attempt is
* made to minimize the double-buffering.
*
* params:
* data = The location to read the data to.
*
* Returns: the number of bytes read. 0 means EOF, nonzero but less than
* data.length does NOT indicate EOF.
*/
size_t readPartial(ubyte[] data)
{
setReadMode();
return _readPartial_noModeCheck(data);
}
private size_t _readPartial_noModeCheck(ubyte[] data)
{
setReadMode();
size_t result = 0;
// determine if there is any data in the buffer.
if(data.length)
{
// save this for later so we can decode the data.
auto origdata = data;
size_t origdata_decoded = decoded - pos;
if(decoded < valid)
{
// there's some leftover undecoded data in the buffer. the
// expectation is that this data is small in size, so it can be
// moved to the front of the buffer efficiently. We don't read
// data into a buffer without trying to decode it.
//
if(origdata_decoded >= data.length)
{
// already decoded data will satisfy the read request, just
// copy and move the read pointer.
data[] = buffer[pos..pos+data.length];
pos += data.length;
// shortcut, no need to deal with decoding anything.
return data.length;
}
// else, there is at least some non-decoded data we have to
// deal with. If it makes sense to read directly into the data
// buffer, then don't bother moving the data to the front of
// the buffer, we'll read directly into the data.
ptrdiff_t nleft = data.length - (valid - pos);
if(nleft >= minReadSize)
{
// read directly into data.
result = valid - pos;
data[0..result] = buffer[pos..valid];
result += _input.read(data[result..$]);
pos = decoded = valid = 0; // reset buffer
// we will decode the data later.
}
else
{
// too small to read into data directly, read into the
// buffer.
result = origdata_decoded;
data[0..origdata_decoded] = buffer[pos..decoded];
if(buffer.length - valid < minReadSize)
{
// move the undecoded data to the front of the buffer,
// then read
memmove(buffer.ptr, buffer.ptr + decoded, valid - decoded);
valid -= decoded;
decoded = 0;
}
// else no need to move, plenty of space in the buffer
pos = decoded;
valid += _input.read(buffer[valid..$]);
assert(valid <= buffer.length);
// encode the data
if(_decode)
decoded += _decode(buffer[decoded..valid]);
else
decoded = valid;
// copy as much decoded data as possible.
auto ncopy = decoded - pos;
if(ncopy > data.length)
ncopy = data.length;
data[origdata_decoded..origdata_decoded+ncopy] =
buffer[pos..pos+ncopy];
pos += ncopy;
// no more decoding needed, shortcut the execution.
return result + ncopy;
}
}
else
{
// no undecoded data.
if(pos < valid)
{
size_t nread = pos + data.length;
if(nread > valid)
nread = valid;
result = nread - pos;
data[0..result] = buffer[pos..nread];
pos = nread;
data = data[result..$];
}
// at this point, either there is no data left in the buffer, or we
// have filled up data.
if(data.length)
{
// still haven't filled it up. Try at least one read from the
// _input stream.
if(data.length >= minReadSize)
{
// data length is long enough to read into it directly.
result += _input.read(data);
}
else
{
// else, fill the buffer. Even though we will be copying
// data, it's probably more efficient than reading small
// chunks from the stream.
valid = _input.read(buffer);
if(valid > 0)
{
// decode the newly read data
if(_decode)
decoded = _decode(buffer[0..valid]);
else
decoded = valid;
pos = data.length > decoded ? decoded : data.length;
data[0..pos] = buffer[0..pos];
}
else
{
pos = decoded = valid = 0;
}
return result + pos;
}
}
}
// now, we need to possibly decode data that's ready to be
// returned.
if(_decode && origdata_decoded < result)
{
origdata_decoded += _decode(origdata[origdata_decoded..result]);
// any data that was not decoded needs to go back to the
// buffer.
if(origdata_decoded < result)
{
auto ntocopy = result - origdata_decoded;
if(pos != valid)
{
// this should only happen if the buffer has enough
// space to store the data that wasn't already decoded.
assert(pos >= ntocopy);
pos -= ntocopy;
buffer[pos..pos + ntocopy] =
origdata[origdata_decoded..result];
}
else
{
// no data in the buffer, reset everything
buffer[0..ntocopy] = origdata[origdata_decoded..result];
pos = decoded = 0;
valid = ntocopy;
}
result = origdata_decoded;
}
}
}
return result;
}
/**
* Reads as much data as possible from the stream, up to a given buffer
* length. This differs from read
* in that it will continue reading until either EOF is reached, or data is
* filled.
*
* This throws an exception on any error.
*
* params:
* data = The data buffer to fill.
*
* returns: the data read as a slice of the original buffer. If the length
* is less than the original data length, EOF was reached.
*/
ubyte[] read(ubyte[] data)
{
setReadMode();
size_t filled = 0;
while(filled < data.length)
{
auto nread = _readPartial_noModeCheck(data[filled..$]);
if(nread == 0)
break;
filled += nread;
}
return data[0..filled];
}
/**
* Read data until a condition is satisfied.
*
* Buffers data from the input stream until the delegate returns other than
* size_t.max. The delegate is passed the data read so far, and the start
* of the data just read. The deleate should return size_t.max if the
* condition is not satisfied, or the number of bytes that should be
* consumed otherwise.
*
* When no more bytes can be read, the delegate will be called with start
* == data.length. The delegate has the option of returning size_t.max,
* which means, return the data read so far. Or it can return a valid
* status, which means only that data will be read.
*
* If the delegate returns 0, then 0 bytes will be returned, and no data
* will be consumed. You can use this to peek at data.
*
* params: process = A delegate to determine satisfaction of a condition
* per the terms above.
*
* returns: the data identified by the delegate that satisfies the
* condition. Note that this data is owned by the buffer and so
* shouldn't be written to or stored for later use without duping.
*/
const(ubyte)[] readUntil(scope size_t delegate(const(ubyte)[] data, size_t start) process)
{
setReadMode();
debug(stdio) printf("readUntil, pos=%d, decoded=%d, valid=%d, buffer.length=%d\n", pos, decoded, valid, buffer.length);
// read data from the buffer/stream until the condition is met,
// expanding the buffer as necessary
auto d = buffer[pos..decoded];
auto status = d.length ? process(d, 0) : size_t.max;
// TODO: this simple version always moves data to the front
// of the buffer if the buffer needs to be filled, but a smarter
// version could probably only move data when it's most efficient.
if(status == size_t.max && pos > 0)
{
if((valid -= pos) > 0)
memmove(buffer.ptr, buffer.ptr + pos, valid);
decoded -= pos;
pos = 0;
}
while(status == size_t.max)
{
auto avail = buffer.length - valid;
if(avail < minReadSize)
{
buffer.length += growSize;
// always use up as much space as possible.
buffer.length = buffer.capacity;
}
auto nread = _input.read(buffer[valid..$]);
if(!nread)
{
// no more data available from the stream, process the EOF
if(decoded > 0)
status = process(buffer[0..decoded], decoded);
if(status == size_t.max)
status = decoded;
}
else
{
// record the new valid, then use nread to mean the number of
// newly decoded bytes.
valid += nread;
if(_decode)
nread = _decode(buffer[decoded..valid]);
else
nread = valid - decoded;
status = process(buffer[0..decoded + nread], decoded);
decoded += nread;
}
}
debug(stdio) printf("readUntil (after while), pos=%d, decoded=%d, valid=%d\n", pos, decoded, valid);
// adjust the read buffer, and return the data.
auto ep = pos + status;
d = buffer[pos..ep];
if(ep == valid)
valid = pos = decoded = 0;
else
pos = ep;
return d;
}
/**
* Read until a certain sequence of bytes is found.
*
* params:
* term = The byte sequence that terminates the read.
*
* returns: The data read. The data includes the termination sequence.
*/
const(ubyte)[] readUntil(const(ubyte)[] term)
{
immutable lastbyte = term[$-1];
auto ltend = term.ptr + term.length - 1;
size_t _checkDelim1(const(ubyte)[] data, size_t start)
{
auto ptr = data.ptr + start;
auto end = data.ptr + data.length;
for(;ptr < end; ++ptr)
{
if(*ptr == lastbyte)
break;
}
return ptr == end ? size_t.max : ptr - data.ptr + 1;
}
size_t _checkDelimN(const(ubyte)[] data, size_t start)
{
if(start <= term.length - 1)
start = term.length - 1;
// TODO; can try optimizing this
auto ptr = data.ptr + start;
auto end = data.ptr + data.length;
for(;ptr < end; ++ptr)
{
if(*ptr == lastbyte)
{
// triggers a check
auto ptr2 = ptr - term.length + 1;
auto ltptr = term.ptr;
for(; ltptr < ltend; ++ltptr, ++ptr2)
if(*ltptr != *ptr2)
break;
if(ltptr == ltend)
break;
}
}
return ptr >= end ? size_t.max : ptr - data.ptr + 1;
}
return term.length == 1 ? readUntil(&_checkDelim1) : readUntil(&_checkDelimN);
}
/// ditto
const(ubyte)[] readUntil(ubyte ub)
{
return readUntil((&ub)[0..1]);
}
/**
* Read data into a given buffer until a condition is satisfied.
*
* This performs just like readUntil, except the data is written to the
* given array. Since the caller owns this array, it controls the lifetime
* of the array. Any excess data will be pushed back into the stream's
* buffer.
*
* Note that more data may be written into the buffer than satisfies the
* condition, but that data is not considered to be consumed from the
* stream.
*
* params:
* process = A delegate to determine satisfaction of a condition
* per the terms above.
* arr = The array that will be written to. The array may be extended as
* necessary.
*
* returns: The number of bytes in arr that are valid.
*/
size_t appendUntil(scope size_t delegate(const(ubyte)[] data, size_t start) process,
ref ubyte[] arr)