using System; using System.Collections.Generic; using System.Text; namespace Pack89KData { public class ApplicationFile { #region Declarations private byte[] _signature; private ushort _revisionNumber; private byte _flags; private byte _objectType; private uint _fileDate; private string _applicationName; private byte _deviceType; private byte _dataType; private List _fields; #endregion #region Constructors/Teardown public ApplicationFile() { _signature = new byte[8]; _fields = new List(); } #endregion #region Public Properties public string ApplicationName { get { return _applicationName; } set { _applicationName = value; } } public List Fields { get { return _fields; } } #endregion #region Public Methods public void Load(string fileName) { const int BUFFER_SIZE = 9; byte[] buffer = new byte[BUFFER_SIZE]; var file = new System.IO.FileStream(fileName, System.IO.FileMode.Open); //Get the signature file.Read(_signature, 0, 8); //Get the revision number file.Read(buffer, 0, 2); _revisionNumber = (ushort)((buffer[0] & 0xFF) | (buffer[1] << 8)); //Get flags and object type _flags = (byte)file.ReadByte(); _objectType = (byte)file.ReadByte(); //Get date file.Read(buffer, 0, 4); _fileDate = (uint)((buffer[0] & 0xFF) | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24)); //Get the name char[] name = new char[9]; file.Read(buffer, 0, 9); for (int i = 0; i < 8; i++) name[i] = (char)buffer[i+1]; _applicationName = new string(name).Trim('\0'); //Get device/data types file.Seek(0x30, System.IO.SeekOrigin.Begin); _deviceType = (byte)file.ReadByte(); _dataType = (byte)file.ReadByte(); //Get total file size int fileSize; file.Seek(0x4A, System.IO.SeekOrigin.Begin); file.Read(buffer, 0, 4); fileSize = (buffer[0] & 0xFF) | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); //Read in the data byte[] data = new byte[fileSize]; file.Read(data, 0, fileSize); _fields = ApplicationField.ParseContents(data); file.Close(); } public void Save(string fileName) { const int BUFFER_SIZE = 9; byte[] buffer = new byte[BUFFER_SIZE]; var file = new System.IO.FileStream(fileName, System.IO.FileMode.Create); //Write signature file.Write(_signature, 0, _signature.Length); //Write revision number buffer[0] = (byte)(_revisionNumber & 0xFF); buffer[1] = (byte)(_revisionNumber >> 8); file.Write(buffer, 0, 2); //Write flags and object type file.WriteByte(_flags); file.WriteByte(_objectType); //Write date file.WriteByte((byte)(_fileDate & 0xFF)); file.WriteByte((byte)((_fileDate >> 8) & 0xFF)); file.WriteByte((byte)((_fileDate >> 16) & 0xFF)); file.WriteByte((byte)((_fileDate >> 24) & 0xFF)); //Write the name for (int i = 1; i < 9; i++) buffer[i] = 0x00; buffer[0] = 0x08; for (int i = 0; i < _applicationName.Length; i++) buffer[i + 1] = (byte)_applicationName[i]; file.Write(buffer, 0, 9); file.Seek(0x30, System.IO.SeekOrigin.Begin); //Write device/data types file.WriteByte(_deviceType); file.WriteByte(_dataType); file.Seek(0x4A, System.IO.SeekOrigin.Begin); //Write total file size int size = 0; for (int i = 0; i < _fields.Count; i++) size += _fields[i].Count; file.WriteByte((byte)(size & 0xFF)); file.WriteByte((byte)((size >> 8) & 0xFF)); file.WriteByte((byte)((size >> 16) & 0xFF)); file.WriteByte((byte)((size >> 24) & 0xFF)); //Write out the data for (int i = 0; i < _fields.Count; i++) { byte[] d = _fields[i].ToByteArray(); file.Write(d, 0, d.Length); } file.Close(); } #endregion } }