diff --git a/plist-cil/ChangeLog b/plist-cil/ChangeLog index 79bf772..9d3d0e5 100644 --- a/plist-cil/ChangeLog +++ b/plist-cil/ChangeLog @@ -1,3 +1,10 @@ +2015-02-18 Natalia Portillo + + * NSArray.cs: + * NSObject.cs: + * plist-cil.csproj: + Implements NSArray + 2015-02-18 Natalia Portillo * NSDate.cs: diff --git a/plist-cil/NSArray.cs b/plist-cil/NSArray.cs new file mode 100644 index 0000000..08c4a9e --- /dev/null +++ b/plist-cil/NSArray.cs @@ -0,0 +1,334 @@ +// plist-cil - An open source library to parse and generate property lists for .NET +// Copyright (C) 2015 Natalia Portillo +// +// This code is based on: +// plist - An open source library to parse and generate property lists +// Copyright (C) 2014 Daniel Dreibrodt +// +// 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.Text; + +namespace Claunia.PropertyList +{ + /// + /// Represents an Array. + /// + /// @author Daniel Dreibrodt + public class NSArray : NSObject + { + NSObject[] array; + + /// + /// Creates an empty array of the given length. + /// + /// The number of elements this array will be able to hold. + public NSArray(int length) { + array = new NSObject[length]; + } + + /// + /// Creates a array from an existing one + /// + /// The array which should be wrapped by the NSArray + public NSArray(params NSObject[] a) { + array = a; + } + + /// + /// Returns the object stored at the given index. + /// + /// The object at the given index. + /// The index of the object. + public NSObject ObjectAtIndex(int i) { + return array[i]; + } + + /// + /// Remove the i-th element from the array. + /// The array will be resized. + /// + /// The index of the object + 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; + } + + /// + /// Stores an object at the specified index. + /// If there was another object stored at that index it will be replaced. + /// + /// The index where to store the object. + /// The object. + public void SetValue(int key, Object value) { + // TODO: Implement NSObject.Wrap(Object) + /* + if(value == null) + throw new ArgumentNullException("Cannot add null values to an NSArray!"); + array[key] = NSObject.Wrap(value); + */ + } + + /// + /// Returns the array of NSObjects represented by this NSArray. + /// Any changes to the values of this array will also affect the NSArray. + /// + /// The actual array represented by this NSArray. + public NSObject[] GetArray() { + return array; + } + + /// + /// Returns the size of the array. + /// + /// The number of elements that this array can store. + public int Count { + get + { + return array.Length; + } + } + + /// + /// Checks whether an object is present in the array or whether it is equal + /// to any of the objects in the array. + /// + /// true, when the object could be found. false otherwise. + /// The object to look for. + public bool ContainsObject(Object obj) { + // TODO: Implement NSObject.Wrap(Object) + /* + NSObject nso = NSObject.Wrap(obj); + for (NSObject elem : array) { + if (elem.equals(nso)) { + return true; + } + }*/ + return false; + } + + /// + /// Searches for an object in the array. If it is found its index will be + /// returned. This method also returns an index if the object is not the same + /// as the one stored in the array but has equal contents. + /// + /// The index of the object, if it was found. -1 otherwise. + /// The object to look for. + public int IndexOfObject(Object obj) { + // TODO: Implement NSObject.Wrap(Object) + /* + NSObject nso = NSObject.wrap(obj); + for (int i = 0; i < array.length; i++) { + if (array[i].equals(nso)) { + return i; + } + }*/ + return -1; + } + + /// + /// Searches for an object in the array. If it is found its index will be + /// returned. This method only returns the index of an object that is + /// identical to the given one. Thus objects that might contain the + /// same value as the given one will not be considered. + /// + /// The index of the object, if it was found. -1 otherwise. + /// The object to look for. + public int IndexOfIdenticalObject(Object obj) { + // TODO: Implement NSObject.Wrap(Object) + /* + NSObject nso = NSObject.Wrap(obj); + for (int i = 0; i < array.length; i++) { + if (array[i] == nso) { + return i; + } + }*/ + return -1; + } + + /// + /// Returns the last object contained in this array. + /// + /// The value of the highest index in the array. + public NSObject LastObject() { + return array[array.Length - 1]; + } + + /// + /// Returns a new array containing only the values stored at the given + /// indices. The values are sorted by their index. + /// + /// The new array containing the objects stored at the given indices. + /// The indices of the objects. + public NSObject[] objectsAtIndexes(params int[] indexes) { + NSObject[] result = new NSObject[indexes.Length]; + Array.Sort(indexes); + for (int i = 0; i < indexes.Length; i++) + result[i] = array[indexes[i]]; + return result; + } + + public override bool Equals(Object obj) { + // TODO: Implement NSObject.Wrap(Object) + /* + + if(obj.GetType().Equals(NSArray.GetType())) { + return Array.Equals(((NSArray) obj).GetArray(), this.array); + } else { + NSObject nso = NSObject.Wrap(obj); + if(nso.GetType().Equals(NSArray.GetType())) { + return Array.Equals(((NSArray) nso).GetArray(), this.array); + } + }*/ + return false; + } + + public override int GetHashCode() { + int hash = 7; + hash = 89 * hash + array.GetHashCode(); + return hash; + } + + internal override void ToXml(StringBuilder xml, int level) { + Indent(xml, level); + xml.Append(""); + xml.Append(NSObject.NEWLINE); + foreach (NSObject o in array) { + o.ToXml(xml, level + 1); + xml.Append(NSObject.NEWLINE); + } + Indent(xml, level); + xml.Append(""); + } + + // TODO: Implement BinaryPropertyListWriter + /* + override void assignIDs(BinaryPropertyListWriter out) { + super.assignIDs(out); + for (NSObject obj : array) { + obj.assignIDs(out); + } + } + + override void toBinary(BinaryPropertyListWriter out) throws IOException { + out.writeIntHeader(0xA, array.length); + for (NSObject obj : array) { + out.writeID(out.getID(obj)); + } + }*/ + + + /** + * Generates a valid ASCII property list which has this NSArray as its + * root object. The generated property list complies with the format as + * described in + * Property List Programming Guide - Old-Style ASCII Property Lists. + * + * @return ASCII representation of this object. + * + public string toASCIIPropertyList() { + StringBuilder ascii = new StringBuilder(); + ToASCII(ascii, 0); + ascii.Append(NEWLINE); + return ascii.ToString(); + } + + /** + * Generates a valid ASCII property list in GnuStep format which has this + * NSArray as its root object. The generated property list complies with + * the format as described in + * GnuStep - NSPropertyListSerialization class documentation + * + * + * @return GnuStep ASCII representation of this object. + * + public string ToGnuStepASCIIPropertyList() { + StringBuilder ascii = new StringBuilder(); + ToASCIIGnuStep(ascii, 0); + ascii.Append(NEWLINE); + return ascii.ToString(); + }*/ + + protected override void ToASCII(StringBuilder ascii, int level) { + // TODO: Implement ASCIIPropertyListParser + /* + + indent(ascii, level); + ascii.append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); + int indexOfLastNewLine = ascii.lastIndexOf(NEWLINE); + for (int i = 0; i < array.length; i++) { + Class objClass = array[i].getClass(); + if ((objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) + && indexOfLastNewLine != ascii.length()) { + ascii.append(NEWLINE); + indexOfLastNewLine = ascii.length(); + array[i].toASCII(ascii, level + 1); + } else { + if (i != 0) + ascii.append(" "); + array[i].toASCII(ascii, 0); + } + + if (i != array.length - 1) + ascii.append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); + + if (ascii.length() - indexOfLastNewLine > ASCII_LINE_LENGTH) { + ascii.append(NEWLINE); + indexOfLastNewLine = ascii.length(); + } + } + ascii.append(ASCIIPropertyListParser.ARRAY_END_TOKEN);*/ + } + + protected override void ToASCIIGnuStep(StringBuilder ascii, int level) { + // TODO: Implement ASCIIPropertyListParser + /* + indent(ascii, level); + ascii.append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); + int indexOfLastNewLine = ascii.lastIndexOf(NEWLINE); + for (int i = 0; i < array.length; i++) { + Class objClass = array[i].getClass(); + if ((objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) + && indexOfLastNewLine != ascii.length()) { + ascii.append(NEWLINE); + indexOfLastNewLine = ascii.length(); + array[i].toASCIIGnuStep(ascii, level + 1); + } else { + if (i != 0) + ascii.append(" "); + array[i].toASCIIGnuStep(ascii, 0); + } + + if (i != array.length - 1) + ascii.append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); + + if (ascii.length() - indexOfLastNewLine > ASCII_LINE_LENGTH) { + ascii.append(NEWLINE); + indexOfLastNewLine = ascii.length(); + } + } + ascii.append(ASCIIPropertyListParser.ARRAY_END_TOKEN);*/ + } + } +} + diff --git a/plist-cil/NSObject.cs b/plist-cil/NSObject.cs index d72472c..5d9cadd 100644 --- a/plist-cil/NSObject.cs +++ b/plist-cil/NSObject.cs @@ -171,7 +171,7 @@ namespace Claunia.PropertyList /// The value to represent as a NSObject. /// A NSObject representing the given value. /// When one of the objects contained in the array cannot be represented by a NSObject. - // TODO: Implement NSArray class + // TODO: Implement this.Wrap(Object) /* public static NSArray Wrap(Object[] value) { NSArray arr = new NSArray(value.length); @@ -202,7 +202,7 @@ namespace Claunia.PropertyList /// The value to represent as a NSObject. /// A NSObject representing the given value. /// When one of the values contained in the map cannot be represented by a NSObject. - /// TODO: Implement this.Wrap(Object) + // TODO: Implement this.Wrap(Object) /* public static NSSet Wrap(List value) { NSSet set = new NSSet(); foreach (Object o in value) diff --git a/plist-cil/plist-cil.csproj b/plist-cil/plist-cil.csproj index 382a922..1d52ce6 100644 --- a/plist-cil/plist-cil.csproj +++ b/plist-cil/plist-cil.csproj @@ -39,6 +39,7 @@ +