Пример #1
0
 public static void TestIssue18()
 {
     NSNumber x = new NSNumber(-999);
     PropertyListParser.SaveAsBinary(x, new FileInfo("test-files/out-testIssue18.plist"));
     NSObject y = PropertyListParser.Parse(new FileInfo("test-files/out-testIssue18.plist"));
     Assert.True(x.Equals(y));
 }
Пример #2
0
 public static void ParseNumberNlTest2()
 {
     // As seen in a real property list:
     // <key>TimeZoneOffsetFromUTC</key>
     // <real>7200.000000</real>
     var number = new NSNumber("7200.000000", NSNumber.REAL);
     Assert.IsTrue(number.isReal());
     Assert.AreEqual(7200d, number.ToDouble());
 }
Пример #3
0
 public static void ParseNumberEnTest()
 {
     var number = new NSNumber("7200.000001");
     Assert.IsTrue(number.isReal());
     Assert.AreEqual(7200.000001d, number.ToDouble());
 }
Пример #4
0
 public static void NSNumberConstructorTest()
 {
     var number = new NSNumber("10032936613", NSNumber.INTEGER);
     Assert.AreEqual(NSNumber.INTEGER, number.GetNSNumberType());
     Assert.AreEqual(10032936613, number.ToObject());
 }
Пример #5
0
        /// <summary>
        ///     Converts this NSObject into an equivalent object
        ///     of the .NET Runtime Environment.
        ///     <para><see cref="NSArray" /> objects are converted to arrays.</para>
        ///     <para>
        ///         <see cref="NSDictionary" /> objects are converted to objects extending the
        ///         <see cref="Dictionary{TKey, TValue}" /> class.
        ///     </para>
        ///     <para><see cref="NSSet" /> objects are converted to objects extending the <see cref="List{NSObject}" /> class.</para>
        ///     <para>
        ///         <see cref="NSNumber" /> objects are converted to primitive number values (<see cref="int" />,
        ///         <see cref="long" />, <see cref="double" /> or <see cref="bool" />).
        ///     </para>
        ///     <para><see cref="NSString" /> objects are converted to <see cref="string" /> objects.</para>
        ///     <para><see cref="NSData" /> objects are converted to <see cref="byte" /> arrays.</para>
        ///     <para><see cref="NSDate" /> objects are converted to <see cref="System.DateTime" /> objects.</para>
        ///     <para><see cref="UID" /> objects are converted to <see cref="byte" /> arrays.</para>
        /// </summary>
        /// <returns>A native .NET object representing this NSObject's value.</returns>
        public object ToObject()
        {
            if (this is NSArray)
            {
                NSArray  nsArray = (NSArray)this;
                object[] array   = new object[nsArray.Count];
                for (int i = 0; i < nsArray.Count; i++)
                {
                    array[i] = nsArray[i].ToObject();
                }
                return(array);
            }

            if (this is NSDictionary)
            {
                Dictionary <string, NSObject> dictA = ((NSDictionary)this).GetDictionary();
                Dictionary <string, object>   dictB = new Dictionary <string, object>(dictA.Count);
                foreach (KeyValuePair <string, NSObject> kvp in dictA)
                {
                    dictB.Add(kvp.Key, kvp.Value.ToObject());
                }
                return(dictB);
            }

            if (this is NSSet)
            {
                List <NSObject> setA = ((NSSet)this).GetSet();
                List <object>   setB = new List <object>();
                foreach (NSObject o in setA)
                {
                    setB.Add(o.ToObject());
                }
                return(setB);
            }

            if (this is NSNumber)
            {
                NSNumber num = (NSNumber)this;
                switch (num.GetNSNumberType())
                {
                case NSNumber.INTEGER:
                {
                    long longVal = num.ToLong();
                    if (longVal > int.MaxValue || longVal < int.MinValue)
                    {
                        return(longVal);
                    }

                    return(num.ToInt());
                }

                case NSNumber.REAL:    return(num.ToDouble());

                case NSNumber.BOOLEAN: return(num.ToBool());

                default:               return(num.ToDouble());
                }
            }

            if (this is NSString)
            {
                return(((NSString)this).Content);
            }

            if (this is NSData)
            {
                return(((NSData)this).Bytes);
            }

            if (this is NSDate)
            {
                return(((NSDate)this).Date);
            }

            if (this is UID)
            {
                return(((UID)this).Bytes);
            }

            return(this);
        }
Пример #6
0
        /// <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
            var origin = new BinaryOrigin(this.index, 0);

            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, origin);
                    }
                    else
                    {
                        obj = new NSNumber(false, origin);
                    }
                    //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, origin);
                }
                else if (Accept(DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN))
                {
                    //Number
                    Skip();
                    string numberString = ReadInputUntil(DATA_END_TOKEN);
                    obj = new NSNumber(numberString, origin);
                }
                //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, origin);

                //skip end token
                Skip();
            }

            origin.SetEndPosition(this.index);
            return(obj);
        }
Пример #7
0
 internal static bool IsSerializationPrimitive(NSNumber n)
 {
     return(n.isBoolean());
 }
        /// <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;
        }
Пример #9
0
        public static void SetBoolValue(IIgorModule ModuleInst, string PlistPath, string BoolKey, bool bValue)
        {
            if(IgorAssert.EnsureTrue(ModuleInst, File.Exists(PlistPath), "Plist " + PlistPath + " doesn't exist!"))
            {
                FileInfo PlistFileInfo = new FileInfo(PlistPath);

                NSObject PlistRoot = PropertyListParser.Parse(PlistFileInfo);

                if(IgorAssert.EnsureTrue(ModuleInst, PlistRoot != null, "Plist " + PlistPath + " could not be parsed!"))
                {
                    if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSDictionary).IsAssignableFrom(PlistRoot.GetType()), "Plist " + PlistPath + " root object is not a dictionary."))
                    {
                        NSDictionary RootDictionary = (NSDictionary)PlistRoot;

                        if(IgorAssert.EnsureTrue(ModuleInst, RootDictionary != null, "Plist root is not a dictionary."))
                        {
                            if(RootDictionary.ContainsKey(BoolKey))
                            {
                                RootDictionary[BoolKey] = new NSNumber(bValue);

                                IgorRuntimeUtils.DeleteFile(PlistPath);

                                PropertyListParser.SaveAsXml(RootDictionary, PlistFileInfo);

                                IgorDebug.Log(ModuleInst, "Plist key " + BoolKey + " updated to " + bValue);
                            }
                            else
                            {
                                RootDictionary.Add(BoolKey, new NSNumber(bValue));

                                IgorRuntimeUtils.DeleteFile(PlistPath);

                                PropertyListParser.SaveAsXml(RootDictionary, PlistFileInfo);

                                IgorDebug.Log(ModuleInst, "Plist key " + BoolKey + " added with value of " + bValue);
                            }
                        }
                    }
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Converts this NSObject into an equivalent object
        /// of the .NET Runtime Environment.
        /// <para><see cref="NSArray"/> objects are converted to arrays.</para>
        /// <para><see cref="NSDictionary"/> objects are converted to objects extending the <see cref="Dictionary{TKey, TValue}"/> class.</para>
        /// <para><see cref="NSSet"/> objects are converted to objects extending the <see cref="List{NSObject}"/> class.</para>
        /// <para><see cref="NSNumber"/> objects are converted to primitive number values (<see cref="int"/>, <see cref="long"/>, <see cref="double"/> or <see cref="bool"/>).</para>
        /// <para><see cref="NSString"/> objects are converted to <see cref="string"/> objects.</para>
        /// <para><see cref="NSData"/> objects are converted to <see cref="byte"/> arrays.</para>
        /// <para><see cref="NSDate"/> objects are converted to <see cref="System.DateTime"/> objects.</para>
        /// <para><see cref="UID"/> objects are converted to <see cref="byte"/> arrays.</para>
        /// </summary>
        /// <returns>A native .NET object representing this NSObject's value.</returns>
        public Object ToObject()
        {
            if (this is NSArray)
            {
                NSObject[] arrayA = ((NSArray)this).GetArray();
                Object[]   arrayB = new Object[arrayA.Length];
                for (int i = 0; i < arrayA.Length; i++)
                {
                    arrayB[i] = arrayA[i].ToObject();
                }
                return(arrayB);
            }
            if (this is NSDictionary)
            {
                Dictionary <string, NSObject> dictA = ((NSDictionary)this).GetDictionary();
                Dictionary <string, Object>   dictB = new Dictionary <string, Object>(dictA.Count);
                foreach (KeyValuePair <string, NSObject> kvp in dictA)
                {
                    dictB.Add(kvp.Key, kvp.Value.ToObject());
                }
                return(dictB);
            }
            if (this is NSSet)
            {
                List <NSObject> setA = ((NSSet)this).GetSet();
                List <Object>   setB = new List <Object>();
                foreach (NSObject o in setA)
                {
                    setB.Add(o.ToObject());
                }
                return(setB);
            }
            if (this is NSNumber)
            {
                NSNumber num = (NSNumber)this;
                switch (num.Type)
                {
                case NumberType.Integer:
                {
                    long longVal = num.ToLong();
                    if (longVal > int.MaxValue || longVal < int.MinValue)
                    {
                        return(longVal);
                    }
                    return(num.ToInt());
                }

                case NumberType.Real:
                    return(num.ToDouble());

                case NumberType.Boolean:
                    return(num.ToBool());

                default:
                    return(num.ToDouble());
                }
            }
            if (this is NSString)
            {
                return(((NSString)this).Content);
            }
            if (this is NSData)
            {
                return(((NSData)this).Bytes);
            }
            if (this is NSDate)
            {
                return(((NSDate)this).Date);
            }
            if (this is UID)
            {
                return(((UID)this).Bytes);
            }
            return(this);
        }