/// <summary>
        /// Parses a data object from the current parsing position.
        /// This can either be a NSData object or a GnuStep NSNumber or NSDate.
        /// The prerequisite for calling this method is, that a data begin token has been read.
        /// </summary>
        /// <returns>The data object found at the parsing position.</returns>
        NSObject ParseData()
        {
            NSObject obj = null;
            //Skip begin token
            Skip();
            if (Accept(DATA_GSOBJECT_BEGIN_TOKEN))
            {
                Skip();
                Expect(DATA_GSBOOL_BEGIN_TOKEN, DATA_GSDATE_BEGIN_TOKEN, DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN);
                if (Accept(DATA_GSBOOL_BEGIN_TOKEN))
                {
                    //Boolean
                    Skip();
                    Expect(DATA_GSBOOL_TRUE_TOKEN, DATA_GSBOOL_FALSE_TOKEN);
                    if (Accept(DATA_GSBOOL_TRUE_TOKEN))
                        obj = new NSNumber(true);
                    else
                        obj = new NSNumber(false);
                    //Skip the parsed boolean token
                    Skip();
                }
                else if (Accept(DATA_GSDATE_BEGIN_TOKEN))
                {
                    //Date
                    Skip();
                    string dateString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSDate(dateString);
                }
                else if (Accept(DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN))
                {
                    //Number
                    Skip();
                    string numberString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSNumber(numberString);
                }
                //parse data end token
                Read(DATA_END_TOKEN);
            }
            else
            {
                string dataString = ReadInputUntil(DATA_END_TOKEN);
                dataString = Regex.Replace(dataString, "\\s+", "");

                int numBytes = dataString.Length / 2;
                byte[] bytes = new byte[numBytes];
                for (int i = 0; i < bytes.Length; i++)
                {
                    string byteString = dataString.Substring(i * 2, 2);
                    int byteValue = Convert.ToInt32(byteString, 16);
                    bytes[i] = (byte)byteValue;
                }
                obj = new NSData(bytes);

                //skip end token
                Skip();
            }

            return obj;
        }
Example #2
0
 public static void ConstructorTest()
 {
     var actual = new NSDate("2000-01-01T00:00:00Z");
     var expected = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
     Assert.AreEqual(expected, actual.Date.ToUniversalTime());
 }
        /// <summary>
        ///     Parses a data object from the current parsing position.
        ///     This can either be a NSData object or a GnuStep NSNumber or NSDate.
        ///     The prerequisite for calling this method is, that a data begin token has been read.
        /// </summary>
        /// <returns>The data object found at the parsing position.</returns>
        NSObject ParseData()
        {
            NSObject obj = null;

            //Skip begin token
            Skip();
            if (Accept(DATA_GSOBJECT_BEGIN_TOKEN))
            {
                Skip();
                Expect(DATA_GSBOOL_BEGIN_TOKEN, DATA_GSDATE_BEGIN_TOKEN, DATA_GSINT_BEGIN_TOKEN,
                       DATA_GSREAL_BEGIN_TOKEN);
                if (Accept(DATA_GSBOOL_BEGIN_TOKEN))
                {
                    //Boolean
                    Skip();
                    Expect(DATA_GSBOOL_TRUE_TOKEN, DATA_GSBOOL_FALSE_TOKEN);
                    if (Accept(DATA_GSBOOL_TRUE_TOKEN))
                    {
                        obj = new NSNumber(true);
                    }
                    else
                    {
                        obj = new NSNumber(false);
                    }
                    //Skip the parsed boolean token
                    Skip();
                }
                else if (Accept(DATA_GSDATE_BEGIN_TOKEN))
                {
                    //Date
                    Skip();
                    string dateString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSDate(dateString);
                }
                else if (Accept(DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN))
                {
                    //Number
                    Skip();
                    string numberString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSNumber(numberString);
                }

                //parse data end token
                Read(DATA_END_TOKEN);
            }
            else
            {
                string dataString = ReadInputUntil(DATA_END_TOKEN);
                dataString = Regex.Replace(dataString, "\\s+", "");

                int    numBytes = dataString.Length / 2;
                byte[] bytes    = new byte[numBytes];
                for (int i = 0; i < bytes.Length; i++)
                {
                    string byteString = dataString.Substring(i * 2, 2);
                    int    byteValue  = Convert.ToInt32(byteString, 16);
                    bytes[i] = (byte)byteValue;
                }

                obj = new NSData(bytes);

                //skip end token
                Skip();
            }

            return(obj);
        }