From e0df170a198f6b875fbbe37a7fa8a3c5fdf3aa5c Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 29 Apr 2012 23:05:51 +0000 Subject: Adding driver and test project. --- .../KeyboardDefinition.cs | 62 ++++++++++------------ 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs') diff --git a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs index 5ca3f47..10a1c51 100644 --- a/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs +++ b/src/Noddybox.Emulation.Keyboard.Schema/KeyboardDefinition.cs @@ -16,7 +16,7 @@ // Copyright (c) 2012 Ian Cowburn // using System; -using System.Xml; +using System.IO; using System.Collections.Generic; namespace Noddybox.Emulation.Keyboard.Schema @@ -26,6 +26,12 @@ namespace Noddybox.Emulation.Keyboard.Schema /// public class KeyboardDefinition { + #region Version + + private const string magic = "KEYBDEF1.0"; + + #endregion + #region Properties /// @@ -35,57 +41,45 @@ namespace Noddybox.Emulation.Keyboard.Schema #endregion - #region Xml functions + #region Load/Save /// - /// Create the XML for this keyboard definition. + /// Save this keybaord definition to the stream. /// - /// The created XML document. - public XmlDocument CreateXML() + /// The stream to write to. + public void Save(BinaryWriter stream) { - XmlDocument doc = new XmlDocument(); - - doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null)); - doc.AppendChild(doc.CreateElement("Keyboard")); - + stream.Write(magic); + stream.Write(Definitions.Count); + foreach (KeyboardKey k in Definitions) { - XmlElement e = doc.CreateElement("Key"); - k.SetElement(e); - doc.DocumentElement.AppendChild(e); + k.Save(stream); } - - return doc; } /// - /// Create an object from XML. + /// Load a keyboard definition from the passed stream. /// - /// The XML document to load from. - /// The created object. - public static KeyboardDefinition CreateFromXML(XmlDocument doc) + /// The stream to read from. + /// The KeyboardDefinition object. + public static KeyboardDefinition Load(BinaryReader stream) { - KeyboardDefinition definition = new KeyboardDefinition(); - - if (doc.DocumentElement.Name != "Keyboard") + if (stream.ReadString() != magic) { - throw new ArgumentException("Root element not a keyboard"); + throw new ArgumentException("Stream has wrong magic"); } - foreach (XmlNode n in doc.DocumentElement.ChildNodes) + KeyboardDefinition keyboard = new KeyboardDefinition(); + + int count = stream.ReadInt32(); + + for(int f = 0; f < count ; f++) { - if (n is XmlElement) - { - XmlElement e = (XmlElement)n; - - if (e.Name == "Key") - { - definition.Definitions.Add(KeyboardKey.CreateFromElement(e)); - } - } + keyboard.Definitions.Add(KeyboardKey.Load(stream)); } - return definition; + return keyboard; } #endregion -- cgit v1.2.3