diff --git a/src/fart/bfile.d b/src/fart/bfile.d new file mode 100644 index 0000000..4706a2d --- /dev/null +++ b/src/fart/bfile.d @@ -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(); + } +}