diff --git a/KaitaiWriter.cs b/KaitaiWriter.cs new file mode 100644 index 0000000..e6c2e4f --- /dev/null +++ b/KaitaiWriter.cs @@ -0,0 +1,268 @@ +using System; +using System.IO; + +namespace Kaitai +{ + /// + /// The base Kaitai writer which exposes an API for the Kaitai Struct framework. + /// It's based off a BinaryWriter, which is a little-endian writer. + /// + public partial class KaitaiWriter : BinaryWriter + { + #region Constructors + + /// + /// Creates a new KaitaiWriter backed by a stream + /// + public KaitaiWriter(Stream stream) : base(stream) + { + } + + /// + /// Creates a KaitaiWriter backed by a file + /// + public KaitaiWriter(string file) : base(File.Open(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) + { + } + + static readonly bool IsLittleEndian = BitConverter.IsLittleEndian; + + #endregion + + #region Integer types + + #region Signed + + /// + /// Write a signed byte to the stream + /// + /// + public void WriteS1(sbyte v) + { + Write(v); + } + + #region Big-endian + + /// + /// Write a signed short to the stream (big endian) + /// + /// + public void WriteS2be(short v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a signed int to the stream (big endian) + /// + /// + public void WriteS4be(int v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a signed long to the stream (big endian) + /// + /// + public void WriteS8be(long v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #region Little-endian + + /// + /// Write a signed short to the stream (little endian) + /// + /// + public void WriteS2le(short v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a signed int to the stream (little endian) + /// + /// + public void WriteS4le(int v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a signed long to the stream (little endian) + /// + /// + public void WriteS8le(long v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #endregion + + #region Unsigned + + /// + /// Write an unsigned byte to the stream + /// + /// + public void WriteU1(byte v) + { + Write(v); + } + + #region Big-endian + + /// + /// Write an unsigned short to the stream (big endian) + /// + /// + public void WriteU2be(ushort v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write an unsigned int to the stream (big endian) + /// + /// + public void WriteU4be(uint v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write an unsigned long to the stream (big endian) + /// + /// + public void WriteU8be(ulong v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #region Little-endian + + /// + /// Write an unsigned short to the stream (little endian) + /// + /// + public void WriteU2le(ushort v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write an unsigned int to the stream (little endian) + /// + /// + public void WriteU4le(uint v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write an unsigned long to the stream (little endian) + /// + /// + public void WriteU8le(ulong v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #endregion + + #endregion + + #region Floating point types + + #region Big-endian + + /// + /// Write a single-precision floating point value to the stream (big endian) + /// + /// + public void WriteF4be(float v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a double-precision floating point value to the stream (big endian) + /// + /// + public void WriteF8be(double v) + { + WriteBytesNormalisedBigEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #region Little-endian + + /// + /// Write a single-precision floating point value to the stream (little endian) + /// + /// + public void WriteF4le(float v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + /// + /// Write a double-precision floating point value to the stream (little endian) + /// + /// + public void WriteF8le(double v) + { + WriteBytesNormalisedLittleEndian(BitConverter.GetBytes(v)); + } + + #endregion + + #endregion + + #region Byte arrays + + /// + /// Write a fixed number of bytes to the stream + /// + /// The byte array to write + /// + public void WriteBytes(byte[] bytes) + { + Write(bytes); + } + + /// + /// Write bytes to the stream in little endian format. + /// + /// The byte array to write. + protected void WriteBytesNormalisedLittleEndian(byte[] bytes) + { + if (!IsLittleEndian) Array.Reverse(bytes); + Write(bytes); + } + + /// + /// Read bytes to the stream in big endian format. + /// + /// The byte array to write. + protected void WriteBytesNormalisedBigEndian(byte[] bytes) + { + if (IsLittleEndian) Array.Reverse(bytes); + Write(bytes); + } + + #endregion + } +} diff --git a/kaitai_struct_runtime_csharp.csproj b/kaitai_struct_runtime_csharp.csproj index 1ae4432..b743bd0 100644 --- a/kaitai_struct_runtime_csharp.csproj +++ b/kaitai_struct_runtime_csharp.csproj @@ -32,6 +32,7 @@ +