-- $Date: 2004/02/14 09:40:45 $
-- $Revision: 1.10 $
-- $Author: jcrocholl $

with Token_Readers; use Token_Readers;
with Integer_Strings; use Integer_Strings;
with Readers; use Readers;

package body Keys.MusicXML is

   -- Read key data from an XML reader.
   function Read_Key
     (XML : access XML_Reader-- Use this XML reader.
     return Key_Access         -- The newly created key.
   is
      Fifths : Integer := 0;
      Mode   : Mode_Enum := Default;
   begin
      while Descend(XML) loop
         Exit_Tag(XML);

         if Found(XML, "fifths") then
            Fifths := To_Number(Read_Data(XML));
         elsif Found(XML, "mode") then
            Mode := To_Mode_Enum(Read_Data(XML));
         else
            XML_Expect_Error(XML, "/key",
              "fifths" / "mode");
         end if;
         Exit_Element(XML);
      end loop;
      Exit_Tag(XML);

      return Create(Fifths, Mode);
   end Read_Key;

   -- Write key data to an XML writer.
   procedure Write_Key
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Key)        -- Write this key.
   is
   begin
      Start_Element(XML, "key");
      Write_Element(XML, "fifths", To_String(Get_Fifths(This)));
      if Get_Mode(This) /= Default then
         Write_Element(XML, "mode", To_XML(Get_Mode(This)'Img));
      end if;
      Close_Element(XML, "key");
   end Write_Key;

end Keys.MusicXML;