1 2 // Copyright Luna & Cospec 2019. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 module wsf.streams.stream; 8 import std.stdio : SEEK_SET, SEEK_END, SEEK_CUR; 9 10 /** 11 Origin for stream seek operations 12 */ 13 enum StreamOrigin { 14 Start = SEEK_SET, 15 Cursor = SEEK_CUR, 16 End = SEEK_END 17 } 18 19 /** 20 A stream 21 */ 22 abstract class Stream { 23 public: 24 /** 25 Gets the position of the stream 26 27 Convenience property for tell() 28 */ 29 @property 30 size_t position() { 31 return tell(); 32 } 33 34 abstract: 35 36 /** 37 Gets wether the stream can be read from 38 */ 39 @property 40 bool canRead(); 41 42 /** 43 Gets wether the stream can be written to 44 */ 45 @property 46 bool canWrite(); 47 /** 48 Gets wether the stream is seekable 49 */ 50 @property 51 bool canSeek(); 52 53 /** 54 Gets wether the stream is tellable 55 */ 56 @property 57 bool canTell(); 58 59 /** 60 Gets wether the stream knows its length 61 */ 62 @property 63 bool knowsLength(); 64 65 /** 66 Gets the length of the stream (if possible) 67 */ 68 @property 69 size_t length(); 70 71 /** 72 Gets wether the end has been reached 73 */ 74 @property 75 bool eof(); 76 77 /** 78 Reads the specified amount out to the buffer 79 80 Returns the amount read, -1 if at end of stream 81 */ 82 int read(ref ubyte[] buffer); 83 84 /** 85 Reads the specified amount out to the buffer 86 87 Returns the amount read, -1 if at end of stream 88 */ 89 int read(size_t amount, ref ubyte[] buffer); 90 91 /** 92 Peek the X amount of bytes from the current position 93 */ 94 int peek(ref ubyte[] data); 95 96 /** 97 Writes the buffer to the stream 98 */ 99 void write(ubyte[] buffer); 100 101 /** 102 Writes the buffer to the stream from the start position 103 */ 104 void write(ubyte[] buffer, size_t start); 105 106 /** 107 Writes the buffer to the stream from the start position to the specified length 108 */ 109 void write(ubyte[] buffer, size_t start, size_t length); 110 111 /** 112 Seek to a specified position in the stream 113 */ 114 void seek(long position, StreamOrigin origin); 115 116 /** 117 Returns the position of the stream the cursor is at 118 */ 119 ulong tell(); 120 121 /** 122 Flushes and syncronizes the stream 123 */ 124 void flush(); 125 126 /** 127 Seeks back to start of stream 128 */ 129 void rewind(); 130 131 /** 132 Closes the stream 133 */ 134 void close(); 135 }