D programming Language



Download 1.66 Mb.
Page39/47
Date08.01.2017
Size1.66 Mb.
#7507
1   ...   35   36   37   38   39   40   41   42   ...   47

stream


class Stream

Stream is the base abstract class from which the other stream classes derive. Stream's byte order is the format native to the computer.

bit readable

Indicates whether this stream can be read from.

bit writeable

Indicates whether this stream can be written to.

bit seekable

Indicates whether this stream can be seeked within.

Reading


These methods require that the readable flag be set. Problems with reading result in a ReadError being thrown.

uint readBlock(void* buffer, uint size)

Read up to size bytes into the buffer and return the number of bytes actually read.

void readExact(void* buffer, uint size)

Read exactly size bytes into the buffer, throwing a ReadError if it is not correct.

uint read(ubyte[] buffer)

Read a block of data big enough to fill the given array and return the actual number of bytes read. Unfilled bytes are not modified.

void read(out byte x)


void read(out ubyte x)
void read(out short x)
void read(out ushort x)
void read(out int x)
void read(out uint x)
void read(out long x)
void read(out ulong x)
void read(out float x)
void read(out double x)
void read(out real x)
void read(out ireal x)
void read(out creal x)
void read(out char x)
void read(out wchar x)
void read(out char[] s)
void read(out wchar[] s)

Read a basic type or counted string, throwing a ReadError if it could not be read. Outside of byte, ubyte, and char, the format is implementation-specific and should not be used except as opposite actions to write.

char[] readLine()
wchar[] readLineW()

Read a line that is terminated with some combination of carriage return and line feed or end-of-file. The terminators are not included. The wchar version is identical.

char[] readString(uint length)

Read a string of the given length, throwing ReadError if there was a problem.

wchar[] readStringW(uint length)

Read a string of the given length, throwing ReadError if there was a problem. The file format is implementation-specific and should not be used except as opposite actions to write.

char getc()
wchar getcw()

Read and return the next character in the stream. This is the only method that will handle ungetc properly. getcw's format is implementation-specific.

char ungetc(char c)
wchar ungetcw(wchar c)

Push a character back onto the stream. They will be returned in first-in last-out order from getc/getcw.

int scanf(char[] fmt, ...)
int vscanf(char[] fmt, va_list args)

Scan a string from the input using a similar form to C's scanf.


Writing


These methods require that the writeable flag be set. Problems with writing result in a WriteError being thrown.

uint writeBlock(void* buffer, uint size)

Write up to size bytes from buffer in the stream, returning the actual number of bytes that were written.

void writeExact(void* buffer, uint size)

Write exactly size bytes from buffer, or throw a WriteError if that could not be done.

uint write(ubyte[] buffer)

Write as much of the buffer as possible, returning the number of bytes written.

void write(byte x)


void write(ubyte x)
void write(short x)
void write(ushort x)
void write(int x)
void write(uint x)
void write(long x)
void write(ulong x)
void write(float x)
void write(double x)
void write(real x)
void write(ireal x)
void write(creal x)
void write(char x)
void write(wchar x)
void write(char[] s)
void write(wchar[] s)

Write a basic type or counted string. Outside of byte, ubyte, and char, the format is implementation-specific and should only be used in conjunction with read.

void writeLine(char[] s)

Write a line of text, appending the line with an operating-system-specific line ending.

void writeLineW(wchar[] s)

Write a line of text, appending the line with an operating-system-specific line ending. The format is implementation-specific.

void writeString(char[] s)

Write a string of text, throwing WriteError if it could not be fully written.

void writeStringW(wchar[] s)

Write a string of text, throwing WriteError if it could not be fully written. The format is implementation-dependent.

uint printf(char[] format, ...)

uint vprintf(char[] format, va_list args)

Print a formatted string into the stream using printf-style syntax, returning the number of bytes written.

void copyFrom(Stream s)

Copies all data from s into this stream. This may throw ReadError or WriteError on failure. This restores the file position of s so that it is unchanged.

void copyFrom(Stream s, uint count)

Copy a specified number of bytes from the given stream into this one. This may throw ReadError or WriteError on failure. Unlike the previous form, this doesn't restore the file position of s.

Seeking


These methods require that the seekable flag be set. Problems with seeking result in a SeekError being thrown.

ulong seek(long offset, SeekPos whence)

Change the current position of the stream. whence is either SeekPos.Set, in which case the offset is an absolute index from the beginning of the stream, SeekPos.Current, in which case the offset is a delta from the current position, or SeekPos.End, in which case the offset is a delta from the end of the stream (negative or zero offsets only make sense in that case). This returns the new file position.

ulong seekSet(long offset)

ulong seekCur(long offset)

ulong seekEnd(long offset)

Aliases for their normal seek counterparts.

ulong position()

void position(ulong pos)

Retrieve or set the file position, identical to calling seek(0, SeekPos.Current) or seek(pos, SeekPos.Set) respectively.

ulong size()

Retrieve the size of the stream in bytes.

bit eof()

Return whether the current file position is the same as the end of the file. This does not require actually reading past the end of the file, as with stdio.

char[] toString()

Read the entire stream and return it as a string.

uint toHash()

Get a hash of the stream by reading each byte and using it in a CRC-32 checksum.



class File : Stream

This subclass is for file system streams.

this()

this(char[] filename)

this(char[] filename, FileMode mode)

Create the stream with no open file, an open file in read and write mode, or an open file with explicit file mode. mode, if given, is a combination of FileMode.In (indicating a file that can be read) and FileMode.Out (indicating a file that can be written). If the file does not exist, it is created.

void open(char[] filename)

void open(char[] filename, FileMode mode)

Open a file for the stream, in an identical manner to the constructors.

void create(char[] filename)

void create(char[] filename, FileMode mode)

Create a file for the stream.

void close()

Close the current file if it is open; otherwise it does nothing.

uint readBlock(void* buffer, uint size)

uint writeBlock(void* buffer, uint size)

ulong seek(long offset, SeekPos rel)

Overrides of the Stream methods.



class MemoryStream : Stream

This subclass reads and constructs an array of bytes in memory.

this()

this(ubyte[] data)

Create the output buffer and setup for reading, writing, and seeking. The second constructor loads it with specific input data.

ubyte[] data()

Get the current memory data in total.

uint readBlock(void* buffer, uint size)

uint writeBlock(void* buffer, uint size)

ulong seek(long offset, SeekPos rel)

char[] toString()

Overrides of Stream methods.

class SliceStream : Stream

This subclass slices off a portion of another stream, making seeking relative to the boundaries of the slice. It could be used to section a large file into a set of smaller files, such as with tar archives.

this(Stream base, int low)

Indicate both the base stream to use for reading from and the low part of the slice. The high part of the slice is dependent upon the end of the base stream, so that if you write beyond the end it resizes the stream normally.



this(Stream base, int low, int high)

Indicate the high index as well. Attempting to read or write past the high index results in the end being clipped off.

uint readBlock(void* buffer, uint size)

uint writeBlock(void* buffer, uint size)

ulong seek(long offset, SeekPos rel)

Overrides of Stream methods.






Download 1.66 Mb.

Share with your friends:
1   ...   35   36   37   38   39   40   41   42   ...   47




The database is protected by copyright ©ininet.org 2024
send message

    Main page