Add BFile struct
This commit is contained in:
parent
4c57df0774
commit
9f39cddc24
54
src/fart/bfile.d
Normal file
54
src/fart/bfile.d
Normal file
@ -0,0 +1,54 @@
|
||||
module fart.bfile;
|
||||
|
||||
import std.stdio;
|
||||
|
||||
/**
|
||||
* Struct to handle easily writing data to a binary file.
|
||||
*/
|
||||
public struct BFile
|
||||
{
|
||||
/**
|
||||
* Underlying file handle.
|
||||
*/
|
||||
private File m_file;
|
||||
|
||||
/**
|
||||
* Create a binary file.
|
||||
*/
|
||||
this(string filename)
|
||||
{
|
||||
m_file = File(filename, "wb");
|
||||
}
|
||||
|
||||
/**
|
||||
* Write arbitrary byte data to the file.
|
||||
*/
|
||||
public void write(const(void) * data, size_t length)
|
||||
{
|
||||
write((cast(const(ubyte) *)data)[0..length]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an array to the file.
|
||||
*/
|
||||
public void write(T)(const(T)[] data)
|
||||
{
|
||||
m_file.rawWrite(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an arbitrary object to the file.
|
||||
*/
|
||||
public void writeObject(T)(ref const(T) obj)
|
||||
{
|
||||
write(&obj, obj.sizeof);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the file.
|
||||
*/
|
||||
public void close()
|
||||
{
|
||||
m_file.close();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user