diff --git a/plist-cil.test/NSArrayTests.cs b/plist-cil.test/NSArrayTests.cs new file mode 100644 index 0000000..7c8412d --- /dev/null +++ b/plist-cil.test/NSArrayTests.cs @@ -0,0 +1,73 @@ +using Claunia.PropertyList; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace plistcil.test +{ + [TestFixture] + public class NSArrayTests + { + /// + /// Tests the addition of a .NET object to the NSArray + /// + [Test] + public void AddAndContainsObjectTest() + { + NSArray array = new NSArray(); + array.Add(1); + + Assert.IsTrue(array.Contains(1)); + Assert.IsFalse(array.Contains(2)); + } + + /// + /// Tests the method for .NET objects. + /// + [Test] + public void IndexOfTest() + { + NSArray array = new NSArray(); + array.Add(1); + array.Add("test"); + + Assert.AreEqual(0, array.IndexOf(1)); + Assert.AreEqual(1, array.IndexOf("test")); + } + + /// + /// Tests the method for a + /// .NET object. + /// + [Test] + public void InsertTest() + { + NSArray array = new NSArray(); + array.Add(0); + array.Add(1); + array.Add(2); + + array.Insert(1, "test"); + + Assert.AreEqual(4, array.Count); + Assert.AreEqual("test", array.ObjectAtIndex(1).ToObject()); + } + + /// + /// Tests the method for a .NET object. + /// + [Test] + public void RemoveTest() + { + NSArray array = new NSArray(); + array.Add(0); + Assert.IsFalse(array.Remove((object)1)); + Assert.IsTrue(array.Remove((object)0)); + + Assert.AreEqual(0, array.Count); + } + } +} diff --git a/plist-cil.test/plist-cil.test.csproj b/plist-cil.test/plist-cil.test.csproj index 7171219..5df7326 100644 --- a/plist-cil.test/plist-cil.test.csproj +++ b/plist-cil.test/plist-cil.test.csproj @@ -1,4 +1,4 @@ - + Debug @@ -35,6 +35,7 @@ + @@ -119,7 +120,8 @@ plist-cil + - + \ No newline at end of file diff --git a/plist-cil/NSArray.IList.cs b/plist-cil/NSArray.IList.cs new file mode 100644 index 0000000..aa59ec6 --- /dev/null +++ b/plist-cil/NSArray.IList.cs @@ -0,0 +1,139 @@ +// plist-cil - An open source library to parse and generate property lists for .NET +// Copyright (C) 2016 Quamotion +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Claunia.PropertyList +{ + partial class NSArray : IList + { + /// + public NSObject this[int index] + { + get + { + return this.array[index]; + } + + set + { + this.array[index] = value; + } + } + + /// + public bool IsReadOnly + { + get + { + return false; + } + } + + public void Add(object item) + { + this.Add(NSObject.Wrap(item)); + } + + /// + public void Add(NSObject item) + { + this.array.Add(item); + } + + /// + public void Clear() + { + this.array.Clear(); + } + + public bool Contains(object item) + { + return this.Contains(NSObject.Wrap(item)); + } + + /// + public bool Contains(NSObject item) + { + return this.array.Contains(item); + } + + /// + public void CopyTo(NSObject[] array, int arrayIndex) + { + this.array.CopyTo(array, arrayIndex); + } + + /// + public IEnumerator GetEnumerator() + { + return this.GetEnumerator(); + } + + public int IndexOf(object item) + { + return this.array.IndexOf(NSObject.Wrap(item)); + } + + /// + public int IndexOf(NSObject item) + { + return this.array.IndexOf(item); + } + + public void Insert(int index, object item) + { + this.Insert(index, NSObject.Wrap(item)); + } + + /// + public void Insert(int index, NSObject item) + { + this.array.Insert(index, item); + } + + public bool Remove(object item) + { + return this.Remove(NSObject.Wrap(item)); + } + + /// + public bool Remove(NSObject item) + { + return this.array.Remove(item); + } + + /// + public void RemoveAt(int index) + { + this.array.RemoveAt(index); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return this.array.GetEnumerator(); + } + } +} diff --git a/plist-cil/NSArray.cs b/plist-cil/NSArray.cs index 044995b..466790d 100644 --- a/plist-cil/NSArray.cs +++ b/plist-cil/NSArray.cs @@ -23,6 +23,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Text; namespace Claunia.PropertyList @@ -32,9 +34,9 @@ namespace Claunia.PropertyList /// /// @author Daniel Dreibrodt /// @author Natalia Portillo - public class NSArray : NSObject + public partial class NSArray : NSObject { - NSObject[] array; + List array; /// /// Creates an empty array of the given length. @@ -42,7 +44,7 @@ namespace Claunia.PropertyList /// The number of elements this array will be able to hold. public NSArray(int length) { - array = new NSObject[length]; + array = new List(length); } /// @@ -51,7 +53,7 @@ namespace Claunia.PropertyList /// The array which should be wrapped by the NSArray. public NSArray(params NSObject[] a) { - array = a; + array = new List(a); } /// @@ -59,6 +61,7 @@ namespace Claunia.PropertyList /// /// The object at the given index. /// The index of the object. + [Obsolete] public NSObject ObjectAtIndex(int i) { return array[i]; @@ -69,14 +72,10 @@ namespace Claunia.PropertyList /// The array will be resized. /// /// The index of the object + [Obsolete] public void Remove(int i) { - if ((i >= array.Length) || (i < 0)) - throw new IndexOutOfRangeException("invalid index:" + i + ";the array length is " + array.Length); - NSObject[] newArray = new NSObject[array.Length - 1]; - Array.Copy(array, 0, newArray, 0, i); - Array.Copy(array, i + 1, newArray, i, array.Length - i - 1); - array = newArray; + this.array.RemoveAt(i); } /// @@ -85,6 +84,7 @@ namespace Claunia.PropertyList /// /// The index where to store the object. /// The object. + [Obsolete] public void SetValue(int key, Object value) { if (value == null) @@ -97,9 +97,10 @@ namespace Claunia.PropertyList /// Any changes to the values of this array will also affect the NSArray. /// /// The actual array represented by this NSArray. + [Obsolete] public NSObject[] GetArray() { - return array; + return array.ToArray(); } /// @@ -110,7 +111,7 @@ namespace Claunia.PropertyList { get { - return array.Length; + return array.Count; } } @@ -120,6 +121,7 @@ namespace Claunia.PropertyList /// /// true, when the object could be found. false otherwise. /// The object to look for. + [Obsolete] public bool ContainsObject(Object obj) { NSObject nso = NSObject.Wrap(obj); @@ -140,10 +142,11 @@ namespace Claunia.PropertyList /// /// The index of the object, if it was found. -1 otherwise. /// The object to look for. + [Obsolete] public int IndexOfObject(Object obj) { NSObject nso = NSObject.Wrap(obj); - for (int i = 0; i < array.Length; i++) + for (int i = 0; i < array.Count; i++) { if (array[i].Equals(nso)) { @@ -161,10 +164,11 @@ namespace Claunia.PropertyList /// /// The index of the object, if it was found. -1 otherwise. /// The object to look for. + [Obsolete] public int IndexOfIdenticalObject(Object obj) { NSObject nso = NSObject.Wrap(obj); - for (int i = 0; i < array.Length; i++) + for (int i = 0; i < array.Count; i++) { if (array[i] == nso) { @@ -180,7 +184,7 @@ namespace Claunia.PropertyList /// The value of the highest index in the array. public NSObject LastObject() { - return array[array.Length - 1]; + return array[array.Count - 1]; } /// @@ -208,14 +212,14 @@ namespace Claunia.PropertyList { if (obj.GetType().Equals(typeof(NSArray))) { - return ArrayEquals(((NSArray)obj).GetArray(), array); + return ArrayEquals(((NSArray)obj).GetArray(), this.GetArray()); } else { NSObject nso = NSObject.Wrap(obj); if (nso.GetType().Equals(typeof(NSArray))) { - return ArrayEquals(((NSArray)nso).GetArray(), array); + return ArrayEquals(((NSArray)nso).GetArray(), this.GetArray()); } } return false; @@ -258,7 +262,7 @@ namespace Claunia.PropertyList internal override void ToBinary(BinaryPropertyListWriter outPlist) { - outPlist.WriteIntHeader(0xA, array.Length); + outPlist.WriteIntHeader(0xA, array.Count); foreach (NSObject obj in array) { outPlist.WriteID(outPlist.GetID(obj)); @@ -308,7 +312,7 @@ namespace Claunia.PropertyList Indent(ascii, level); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); - for (int i = 0; i < array.Length; i++) + for (int i = 0; i < array.Count; i++) { Type objClass = array[i].GetType(); if ((objClass.Equals(typeof(NSDictionary)) || objClass.Equals(typeof(NSArray)) || objClass.Equals(typeof(NSData))) @@ -325,7 +329,7 @@ namespace Claunia.PropertyList array[i].ToASCII(ascii, 0); } - if (i != array.Length - 1) + if (i != array.Count - 1) ascii.Append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); if (ascii.Length - indexOfLastNewLine > ASCII_LINE_LENGTH) @@ -342,7 +346,7 @@ namespace Claunia.PropertyList Indent(ascii, level); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); - for (int i = 0; i < array.Length; i++) + for (int i = 0; i < array.Count; i++) { Type objClass = array[i].GetType(); if ((objClass.Equals(typeof(NSDictionary)) || objClass.Equals(typeof(NSArray)) || objClass.Equals(typeof(NSData))) @@ -359,7 +363,7 @@ namespace Claunia.PropertyList array[i].ToASCIIGnuStep(ascii, 0); } - if (i != array.Length - 1) + if (i != array.Count - 1) ascii.Append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); if (ascii.Length - indexOfLastNewLine > ASCII_LINE_LENGTH) @@ -382,10 +386,10 @@ namespace Claunia.PropertyList if (!(obj is NSArray)) return false; - if (array.Length != ((NSArray)obj).array.Length) + if (array.Count != ((NSArray)obj).array.Count) return false; - for (int i = 0; i < array.Length; i++) + for (int i = 0; i < array.Count; i++) if (!array[i].Equals(((NSArray)obj).ObjectAtIndex(i))) return false; diff --git a/plist-cil/plist-cil.csproj b/plist-cil/plist-cil.csproj index 7771674..d7faccb 100644 --- a/plist-cil/plist-cil.csproj +++ b/plist-cil/plist-cil.csproj @@ -144,6 +144,7 @@ False PreserveNewest +