* plist-cil/ASCIIPropertyListParser.cs:

Remove redundant items.
	Correct same ASCII exceptionfallback issue that appeared in
	  NSString.

	* plist-cil/UID.cs:
	* plist-cil/NSSet.cs:
	* plist-cil/NSDate.cs:
	* plist-cil/NSData.cs:
	* plist-cil/NSArray.cs:
	* plist-cil/NSObject.cs:
	* plist-cil/NSNumber.cs:
	* plist-cil/NSString.cs:
	* plist-cil/NSDictionary.cs:
	* plist-cil/PropertyListParser.cs:
	* plist-cil/XmlPropertyListParser.cs:
	* plist-cil/BinaryPropertyListWriter.cs:
	* plist-cil/BinaryPropertyListParser.cs:
	  Remove redundant items.
This commit is contained in:
2015-02-20 00:42:40 +00:00
parent 49afbe3bdc
commit 3836b3549b
15 changed files with 225 additions and 319 deletions

View File

@@ -26,7 +26,6 @@ using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text;
using System.Runtime.CompilerServices;
@@ -135,11 +134,11 @@ namespace Claunia.PropertyList
/**
* Property list source data
*/
private byte[] data;
byte[] data;
/**
* Current parsing index
*/
private int index;
int index;
/**
* Only allow subclasses to change instantiation.
@@ -153,7 +152,7 @@ namespace Claunia.PropertyList
/// Creates a new parser for the given property list content.
/// </summary>
/// <param name="propertyListContent">The content of the property list that is to be parsed.</param>
private ASCIIPropertyListParser(byte[] propertyListContent)
ASCIIPropertyListParser(byte[] propertyListContent)
{
data = propertyListContent;
}
@@ -163,7 +162,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>Whether the given tokens occur at the current parsing position.</returns>
/// <param name="sequence">The sequence of tokens to look for.</param>
private bool AcceptSequence(params char[] sequence)
bool AcceptSequence(params char[] sequence)
{
for (int i = 0; i < sequence.Length; i++)
{
@@ -179,14 +178,11 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="acceptableSymbols">The symbols to check.</param>
/// <returns>Whether one of the symbols can be accepted or not.</returns>
private bool Accept(params char[] acceptableSymbols)
bool Accept(params char[] acceptableSymbols)
{
bool symbolPresent = false;
foreach (char c in acceptableSymbols)
{
if (data[index] == c)
symbolPresent = true;
}
symbolPresent |= data[index] == c;
return symbolPresent;
}
@@ -196,7 +192,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="acceptableSymbol">The symbol to check.</param>
/// <returns>Whether the symbol can be accepted or not.</returns>
private bool Accept(char acceptableSymbol)
bool Accept(char acceptableSymbol)
{
return data[index] == acceptableSymbol;
}
@@ -206,15 +202,14 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="expectedSymbols">The expected symbols.</param>
/// <exception cref="FormatException">If none of the expected symbols could be found.</exception>
private void Expect(params char[] expectedSymbols)
void Expect(params char[] expectedSymbols)
{
if (!Accept(expectedSymbols))
{
String excString = "Expected '" + expectedSymbols[0] + "'";
for (int i = 1; i < expectedSymbols.Length; i++)
{
excString += " or '" + expectedSymbols[i] + "'";
}
excString += " but found '" + (char)data[index] + "'";
throw new FormatException(String.Format("{0} at {1}", excString, index));
}
@@ -225,7 +220,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="expectedSymbol">The expected symbol.</param>
/// <exception cref="FormatException">If the expected symbol could be found.</exception>
private void Expect(char expectedSymbol)
void Expect(char expectedSymbol)
{
if (!Accept(expectedSymbol))
throw new FormatException(String.Format("Expected '{0}' but found '{1}' at {2}", expectedSymbol, data[index], index));
@@ -236,7 +231,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="symbol">The symbol to read.</param>
/// <exception cref="FormatException">If the expected symbol could not be read.</exception>
private void Read(char symbol)
void Read(char symbol)
{
Expect(symbol);
index++;
@@ -245,7 +240,7 @@ namespace Claunia.PropertyList
/**
* Skips the current symbol.
*/
private void Skip()
void Skip()
{
index++;
}
@@ -254,7 +249,7 @@ namespace Claunia.PropertyList
/// Skips several symbols
/// </summary>
/// <param name="numSymbols">The amount of symbols to skip.</param>
private void Skip(int numSymbols)
void Skip(int numSymbols)
{
index += numSymbols;
}
@@ -262,7 +257,7 @@ namespace Claunia.PropertyList
/**
* Skips all whitespaces and comments from the current parsing position onward.
*/
private void SkipWhitespacesAndComments()
void SkipWhitespacesAndComments()
{
bool commentSkipped;
do
@@ -306,7 +301,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The input until one the given symbols.</returns>
/// <param name="symbols">The symbols that can occur after the string to read.</param>
private string ReadInputUntil(params char[] symbols)
string ReadInputUntil(params char[] symbols)
{
string s = "";
while (!Accept(symbols))
@@ -322,7 +317,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The input until the given symbol.</returns>
/// <param name="symbol">The symbol that can occur after the string to read.</param>
private string ReadInputUntil(char symbol)
string ReadInputUntil(char symbol)
{
String s = "";
while (!Accept(symbol))
@@ -360,7 +355,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The parsed NSObject.</returns>
/// <seealso cref="ASCIIPropertyListParser.index"/>
private NSObject ParseObject()
NSObject ParseObject()
{
switch (data[index])
{
@@ -392,10 +387,7 @@ namespace Claunia.PropertyList
return new NSString(quotedString);
}
}
else
{
return new NSString(quotedString);
}
return new NSString(quotedString);
}
default:
{
@@ -420,7 +412,7 @@ namespace Claunia.PropertyList
/// The prerequisite for calling this method is, that an array begin token has been read.
/// </summary>
/// <returns>The array found at the parsing position.</returns>
private NSArray ParseArray()
NSArray ParseArray()
{
//Skip begin token
Skip();
@@ -450,7 +442,7 @@ namespace Claunia.PropertyList
/// The prerequisite for calling this method is, that a dictionary begin token has been read.
/// </summary>
/// <returns>The dictionary found at the parsing position.</returns>
private NSDictionary ParseDictionary()
NSDictionary ParseDictionary()
{
//Skip begin token
Skip();
@@ -461,13 +453,9 @@ namespace Claunia.PropertyList
//Parse key
string keyString;
if (Accept(QUOTEDSTRING_BEGIN_TOKEN))
{
keyString = ParseQuotedString();
}
else
{
keyString = ParseString();
}
SkipWhitespacesAndComments();
//Parse assign token
@@ -491,7 +479,7 @@ namespace Claunia.PropertyList
/// 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>
private NSObject ParseData()
NSObject ParseData()
{
NSObject obj = null;
//Skip begin token
@@ -506,13 +494,9 @@ namespace Claunia.PropertyList
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();
}
@@ -559,7 +543,7 @@ namespace Claunia.PropertyList
/// Attempts to parse a plain string as a date if possible.
/// </summary>
/// <returns>A NSDate if the string represents such an object. Otherwise a NSString is returned.</returns>
private NSObject ParseDateString()
NSObject ParseDateString()
{
string numericalString = ParseString();
if (numericalString.Length > 4 && numericalString[4] == DATE_DATE_FIELD_DELIMITER)
@@ -581,7 +565,7 @@ namespace Claunia.PropertyList
/// The string is made up of all characters to the next whitespace, delimiter token or assignment token.
/// </summary>
/// <returns>The string found at the current parsing position.</returns>
private string ParseString()
string ParseString()
{
return ReadInputUntil(WHITESPACE_SPACE, WHITESPACE_TAB, WHITESPACE_NEWLINE, WHITESPACE_CARRIAGE_RETURN,
ARRAY_ITEM_DELIMITER_TOKEN, DICTIONARY_ITEM_DELIMITER_TOKEN, DICTIONARY_ASSIGN_TOKEN, ARRAY_END_TOKEN);
@@ -593,7 +577,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The quoted string found at the parsing method with all special characters unescaped.</returns>
/// <exception cref="FormatException">If an error occured during parsing.</exception>
private string ParseQuotedString()
string ParseQuotedString()
{
//Skip begin token
Skip();
@@ -626,7 +610,7 @@ namespace Claunia.PropertyList
/**
* Used to encode the parsed strings
*/
private static Encoding asciiEncoder;
static Encoding asciiEncoder;
/// <summary>
/// Parses a string according to the format specified for ASCII property lists.
@@ -670,22 +654,14 @@ namespace Claunia.PropertyList
}
//Build string
string result = Encoding.BigEndianUnicode.GetString(bytArr);
//emoryStream charBuf = CharBuffer.wrap(result);
//If the string can be represented in the ASCII codepage
// --> use ASCII encoding
try
{
if (asciiEncoder == null)
asciiEncoder = Encoding.GetEncoding("ascii", EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback);
return asciiEncoder.GetString(Encoding.Convert(Encoding.BigEndianUnicode, asciiEncoder, bytArr));
}
catch
{
//The string contains characters outside the ASCII codepage
// --> use the UTF-8 encoded string
return result;
}
if(IsASCIIEncodable(result))
return Encoding.ASCII.GetString(Encoding.Convert(Encoding.BigEndianUnicode, Encoding.ASCII, bytArr));
//The string contains characters outside the ASCII codepage
// --> use the UTF-8 encoded string
return result;
}
/// <summary>
@@ -752,6 +728,14 @@ namespace Claunia.PropertyList
return Encoding.UTF8.GetString(stringBytes);
}
}
internal static bool IsASCIIEncodable(string text)
{
foreach (char c in text)
if ((int)c > 0x7F)
return false;
return true;
}
}
}

View File

@@ -40,43 +40,43 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class BinaryPropertyListParser
{
private int majorVersion, minorVersion;
int majorVersion, minorVersion;
/**
* property list in bytes *
*/
private byte[] bytes;
byte[] bytes;
/**
* Length of an offset definition in bytes *
*/
private int offsetSize;
int offsetSize;
/**
* Length of an object reference in bytes *
*/
private int objectRefSize;
int objectRefSize;
/**
* Number of objects stored in this property list *
*/
private int numObjects;
int numObjects;
/**
* Reference to the top object of the property list *
*/
private int topObject;
int topObject;
/**
* Offset of the offset table from the beginning of the file *
*/
private int offsetTableOffset;
int offsetTableOffset;
/**
* The table holding the information at which offset each object is found *
*/
private int[] offsetTable;
int[] offsetTable;
/// <summary>
/// Protected constructor so that instantiation is fully controlled by the
/// static parse methods.
/// </summary>
/// <see cref="Parse(byte[])"/>
public BinaryPropertyListParser()
protected BinaryPropertyListParser()
{
/** empty **/
}
@@ -99,11 +99,11 @@ namespace Claunia.PropertyList
/// <returns>The root object of the property list. This is usually a NSDictionary but can also be a NSArray.</returns>
/// <param name="data">The binary property list's data.</param>
/// <exception cref="PropertyListFormatException">When the property list's format could not be parsed.</exception>
private NSObject DoParse(byte[] data)
NSObject DoParse(byte[] data)
{
bytes = data;
string magic = Encoding.ASCII.GetString(CopyOfRange(bytes, 0, 8));
if (!magic.StartsWith("bplist"))
if (!magic.StartsWith("bplist", StringComparison.Ordinal))
{
throw new PropertyListFormatException("The given data is no binary property list. Wrong magic bytes: " + magic);
}
@@ -199,7 +199,7 @@ namespace Claunia.PropertyList
/// <returns>The parsed object.</returns>
/// <param name="obj">The object ID.</param>
/// <exception cref="PropertyListFormatException">When the property list's format could not be parsed.</exception>
private NSObject ParseObject(int obj)
NSObject ParseObject(int obj)
{
int offset = offsetTable[obj];
byte type = bytes[offset];
@@ -399,7 +399,7 @@ namespace Claunia.PropertyList
/// <returns>An array with the length two. First entry is the length, second entry the offset at which the content starts.</returns>
/// <param name="objInfo">Object information byte.</param>
/// <param name="offset">Offset in the byte array at which the object is located.</param>
private int[] ReadLengthAndOffset(int objInfo, int offset)
int[] ReadLengthAndOffset(int objInfo, int offset)
{
int length = objInfo;
int stroffset = 1;
@@ -431,7 +431,7 @@ namespace Claunia.PropertyList
length = (int)new System.Numerics.BigInteger(litEBigInteger);
}
}
return new int[]{ length, stroffset };
return new []{ length, stroffset };
}
/// <summary>
@@ -512,17 +512,10 @@ namespace Claunia.PropertyList
public static double ParseDouble(byte[] bytes)
{
if (bytes.Length == 8)
{
return BitConverter.Int64BitsToDouble(ParseLong(bytes));
}
else if (bytes.Length == 4)
{
if (bytes.Length == 4)
return BitConverter.ToSingle(BitConverter.GetBytes(ParseLong(bytes)), 0);
}
else
{
throw new ArgumentException("bad byte array length " + bytes.Length);
}
throw new ArgumentException("bad byte array length " + bytes.Length);
}
/// <summary>
@@ -535,17 +528,10 @@ namespace Claunia.PropertyList
public static double ParseDouble(byte[] bytes, int startIndex, int endIndex)
{
if (endIndex - startIndex == 8)
{
return BitConverter.Int64BitsToDouble(ParseLong(bytes, startIndex, endIndex));
}
else if (endIndex - startIndex == 4)
{
if (endIndex - startIndex == 4)
return BitConverter.ToSingle(BitConverter.GetBytes(ParseLong(bytes, startIndex, endIndex)), 0);
}
else
{
throw new ArgumentException("endIndex (" + endIndex + ") - startIndex (" + startIndex + ") != 4 or 8");
}
throw new ArgumentException("endIndex (" + endIndex + ") - startIndex (" + startIndex + ") != 4 or 8");
}
/// <summary>

View File

@@ -49,7 +49,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>Version code</returns>
/// <param name="root">Object root.</param>
private static int GetMinimumRequiredVersion(NSObject root)
static int GetMinimumRequiredVersion(NSObject root)
{
int minVersion = VERSION_00;
if (root == null)
@@ -137,17 +137,17 @@ namespace Claunia.PropertyList
return bout.ToArray();
}
private int version = VERSION_00;
int version = VERSION_00;
// raw output stream to result file
private Stream outStream;
Stream outStream;
// # of bytes written so far
private long count;
long count;
// map from object to its ID
private Dictionary<NSObject, int> idMap = new Dictionary<NSObject, int>();
private int idSizeInBytes;
Dictionary<NSObject, int> idMap = new Dictionary<NSObject, int>();
int idSizeInBytes;
/// <summary>
/// Creates a new binary property list writer
@@ -168,29 +168,29 @@ namespace Claunia.PropertyList
void Write(NSObject root)
{
// magic bytes
Write(new byte[]{ (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' });
Write(new []{ (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' });
//version
switch (version)
{
case VERSION_00:
{
Write(new byte[]{ (byte)'0', (byte)'0' });
Write(new []{ (byte)'0', (byte)'0' });
break;
}
case VERSION_10:
{
Write(new byte[]{ (byte)'1', (byte)'0' });
Write(new []{ (byte)'1', (byte)'0' });
break;
}
case VERSION_15:
{
Write(new byte[]{ (byte)'1', (byte)'5' });
Write(new []{ (byte)'1', (byte)'5' });
break;
}
case VERSION_20:
{
Write(new byte[]{ (byte)'2', (byte)'0' });
Write(new []{ (byte)'2', (byte)'0' });
break;
}
}
@@ -264,24 +264,20 @@ namespace Claunia.PropertyList
return ID;
}
private static int ComputeIdSizeInBytes(int numberOfIds)
static int ComputeIdSizeInBytes(int numberOfIds)
{
if (numberOfIds < 256)
return 1;
if (numberOfIds < 65536)
return 2;
return 4;
return numberOfIds < 65536 ? 2 : 4;
}
private int ComputeOffsetSizeInBytes(long maxOffset)
static int ComputeOffsetSizeInBytes(long maxOffset)
{
if (maxOffset < 256)
return 1;
if (maxOffset < 65536)
return 2;
if (maxOffset < 4294967296L)
return 4;
return 8;
return maxOffset < 4294967296L ? 4 : 8;
}
internal void WriteIntHeader(int kind, int value)

View File

@@ -1,3 +1,25 @@
2015-02-20 Natalia Portillo <claunia@claunia.com>
* ASCIIPropertyListParser.cs:
Remove redundant items.
Correct same ASCII exceptionfallback issue that appeared in
NSString.
* UID.cs:
* NSSet.cs:
* NSDate.cs:
* NSData.cs:
* NSArray.cs:
* NSObject.cs:
* NSNumber.cs:
* NSString.cs:
* NSDictionary.cs:
* PropertyListParser.cs:
* XmlPropertyListParser.cs:
* BinaryPropertyListWriter.cs:
* BinaryPropertyListParser.cs:
Remove redundant items.
2015-02-20 Natalia Portillo <claunia@claunia.com>
* UID.cs:

View File

@@ -88,7 +88,7 @@ namespace Claunia.PropertyList
public void SetValue(int key, Object value)
{
if (value == null)
throw new ArgumentNullException("Cannot add null values to an NSArray!");
throw new ArgumentNullException("value", "Cannot add null values to an NSArray!");
array[key] = NSObject.Wrap(value);
}
@@ -202,14 +202,14 @@ namespace Claunia.PropertyList
{
if (obj.GetType().Equals(typeof(NSArray)))
{
return ArrayEquals(((NSArray)obj).GetArray(), this.array);
return ArrayEquals(((NSArray)obj).GetArray(), array);
}
else
{
NSObject nso = NSObject.Wrap(obj);
if (nso.GetType().Equals(typeof(NSArray)))
{
return ArrayEquals(((NSArray)nso).GetArray(), this.array);
return ArrayEquals(((NSArray)nso).GetArray(), array);
}
}
return false;
@@ -289,7 +289,7 @@ namespace Claunia.PropertyList
{
Indent(ascii, level);
ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal);
for (int i = 0; i < array.Length; i++)
{
Type objClass = array[i].GetType();
@@ -323,7 +323,7 @@ namespace Claunia.PropertyList
{
Indent(ascii, level);
ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal);
for (int i = 0; i < array.Length; i++)
{
Type objClass = array[i].GetType();

View File

@@ -35,7 +35,7 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class NSData : NSObject
{
byte[] bytes;
readonly byte[] bytes;
/// <summary>
/// Creates the NSData object from the binary representation of it.
@@ -162,7 +162,7 @@ namespace Claunia.PropertyList
{
Indent(ascii, level);
ascii.Append(ASCIIPropertyListParser.DATA_BEGIN_TOKEN);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal);
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i] & 0xFF;

View File

@@ -34,7 +34,7 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class NSDate : NSObject
{
DateTime date;
readonly DateTime date;
static readonly DateTime EPOCH = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc);
@@ -49,7 +49,7 @@ namespace Claunia.PropertyList
/// <returns>The parsed Date</returns>
/// <param name="textRepresentation">The date string as found in the XML property list</param>
/// <exception cref="FormatException">Given string cannot be parsed</exception>
private static DateTime ParseDateString(string textRepresentation)
static DateTime ParseDateString(string textRepresentation)
{
try
{
@@ -67,7 +67,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="date">The date which should be represented.</param>
/// <returns>The string representation of the date.</returns>
private static string MakeDateString(DateTime date)
static string MakeDateString(DateTime date)
{
return date.ToString(sdfDefault);
}
@@ -79,7 +79,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="date">The date which should be represented.</param>
/// <returns>The string representation of the date.</returns>
private static string MakeDateStringGnuStep(DateTime date)
static string MakeDateStringGnuStep(DateTime date)
{
return date.ToString(sdfGnuStep);
}

View File

@@ -40,7 +40,7 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class NSDictionary : NSObject, IDictionary<string, NSObject>
{
Dictionary<string, NSObject> dict;
readonly Dictionary<string, NSObject> dict;
/// <summary>
/// Creates a new empty NSDictionary.
@@ -262,12 +262,6 @@ namespace Claunia.PropertyList
return false;
}
public override bool Equals(Object obj)
{
bool foo = this.Equals((NSDictionary)obj);
return (obj.GetType().Equals(GetType()) && ((NSDictionary)obj).dict.Equals(dict));
}
public override bool Equals(NSObject obj)
{
if (!(obj is NSDictionary))
@@ -294,7 +288,7 @@ namespace Claunia.PropertyList
public override int GetHashCode()
{
int hash = 7;
hash = 83 * hash + (this.dict != null ? this.dict.GetHashCode() : 0);
hash = 83 * hash + (dict != null ? dict.GetHashCode() : 0);
return hash;
}

View File

@@ -55,11 +55,11 @@ namespace Claunia.PropertyList
public const int BOOLEAN = 2;
//Holds the current type of this number
int type;
readonly int type;
long longValue;
double doubleValue;
bool boolValue;
readonly long longValue;
readonly double doubleValue;
readonly bool boolValue;
/// <summary>
/// Parses integers and real numbers from their binary representation.
@@ -225,8 +225,7 @@ namespace Claunia.PropertyList
{
if (type == BOOLEAN)
return boolValue;
else
return longValue != 0;
return longValue != 0;
}
/// <summary>
@@ -285,8 +284,8 @@ namespace Claunia.PropertyList
public override int GetHashCode()
{
int hash = type;
hash = 37 * hash + (int)(this.longValue ^ ((uint)this.longValue >> 32));
hash = 37 * hash + (int)(BitConverter.DoubleToInt64Bits(this.doubleValue) ^ ((uint)(BitConverter.DoubleToInt64Bits(this.doubleValue) >> 32)));
hash = 37 * hash + (int)(longValue ^ ((uint)longValue >> 32));
hash = 37 * hash + (int)(BitConverter.DoubleToInt64Bits(doubleValue) ^ ((uint)(BitConverter.DoubleToInt64Bits(doubleValue) >> 32)));
hash = 37 * hash + (ToBool() ? 1 : 0);
return hash;
}
@@ -448,15 +447,12 @@ namespace Claunia.PropertyList
y = num.ToDouble();
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
else if (IsNumber(o))
if (IsNumber(o))
{
y = GetDoubleFromObject(o);
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
else
{
return -1;
}
return -1;
}
/// <summary>

View File

@@ -70,7 +70,7 @@ namespace Claunia.PropertyList
/// <summary>
/// Assigns IDs to all the objects in this NSObject subtree.
/// </summary>
/// <param name="out">The writer object that handles the binary serialization.</param>
/// <param name="outPlist">The writer object that handles the binary serialization.</param>
internal virtual void AssignIDs(BinaryPropertyListWriter outPlist)
{
outPlist.AssignID(this);
@@ -79,7 +79,7 @@ namespace Claunia.PropertyList
/// <summary>
/// Generates the binary representation of the object.
/// </summary>
/// <param name="out">The output stream to serialize the object to.</param>
/// <param name="outPlist">The output stream to serialize the object to.</param>
internal abstract void ToBinary(BinaryPropertyListWriter outPlist);
/// <summary>
@@ -124,7 +124,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="xml">The string builder for the XML document.</param>
/// <param name="level">The level of identation.</param>
internal void Indent(StringBuilder xml, int level)
internal static void Indent(StringBuilder xml, int level)
{
for (int i = 0; i < level; i++)
xml.Append(INDENT);
@@ -258,7 +258,7 @@ namespace Claunia.PropertyList
}
if (typeof(long).IsAssignableFrom(c))
{
return Wrap((long)(long)o);
return Wrap((long)o);
}
if (typeof(float).Equals(c))
{
@@ -266,7 +266,7 @@ namespace Claunia.PropertyList
}
if (typeof(double).IsAssignableFrom(c))
{
return Wrap((double)(Double)o);
return Wrap((double)o);
}
if (typeof(string).Equals(c))
{
@@ -283,7 +283,7 @@ namespace Claunia.PropertyList
{
return Wrap((byte[])o);
}
else if (cc.Equals(typeof(bool)))
if (cc.Equals(typeof(bool)))
{
bool[] array = (bool[])o;
NSArray nsa = new NSArray(array.Length);
@@ -291,7 +291,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else if (cc.Equals(typeof(float)))
if (cc.Equals(typeof(float)))
{
float[] array = (float[])o;
NSArray nsa = new NSArray(array.Length);
@@ -299,7 +299,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else if (cc.Equals(typeof(double)))
if (cc.Equals(typeof(double)))
{
double[] array = (double[])o;
NSArray nsa = new NSArray(array.Length);
@@ -307,7 +307,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else if (cc.Equals(typeof(short)))
if (cc.Equals(typeof(short)))
{
short[] array = (short[])o;
NSArray nsa = new NSArray(array.Length);
@@ -315,7 +315,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else if (cc.Equals(typeof(int)))
if (cc.Equals(typeof(int)))
{
int[] array = (int[])o;
NSArray nsa = new NSArray(array.Length);
@@ -323,7 +323,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else if (cc.Equals(typeof(long)))
if (cc.Equals(typeof(long)))
{
long[] array = (long[])o;
NSArray nsa = new NSArray(array.Length);
@@ -331,10 +331,7 @@ namespace Claunia.PropertyList
nsa.SetValue(i, Wrap(array[i]));
return nsa;
}
else
{
return Wrap((Object[])o);
}
}
if (typeof(Dictionary<string,Object>).IsAssignableFrom(c))
{
@@ -347,9 +344,7 @@ namespace Claunia.PropertyList
return dict;
}
if (typeof(List<Object>).IsAssignableFrom(c))
{
return Wrap(((List<Object>)o).ToArray());
}
return WrapSerialized(o);
}
@@ -404,7 +399,7 @@ namespace Claunia.PropertyList
}
return arrayB;
}
else if (this is NSDictionary)
if (this is NSDictionary)
{
Dictionary<string, NSObject> dictA = ((NSDictionary)this).GetDictionary();
Dictionary<string, Object> dictB = new Dictionary<string, Object>(dictA.Count);
@@ -414,7 +409,7 @@ namespace Claunia.PropertyList
}
return dictB;
}
else if (this is NSSet)
if (this is NSSet)
{
List<NSObject> setA = ((NSSet)this).GetSet();
List<Object> setB = new List<Object>();
@@ -424,7 +419,7 @@ namespace Claunia.PropertyList
}
return setB;
}
else if (this is NSNumber)
if (this is NSNumber)
{
NSNumber num = (NSNumber)this;
switch (num.GetNSNumberType())
@@ -433,48 +428,34 @@ namespace Claunia.PropertyList
{
long longVal = num.ToLong();
if (longVal > int.MaxValue || longVal < int.MinValue)
{
return longVal;
}
else
{
return num.ToInt();
}
return num.ToInt();
}
case NSNumber.REAL:
{
return num.ToDouble();
}
case NSNumber.BOOLEAN:
{
return num.ToBool();
}
default :
{
return num.ToDouble();
}
}
}
else if (this is NSString)
if (this is NSString)
{
return ((NSString)this).GetContent();
}
else if (this is NSData)
if (this is NSData)
{
return ((NSData)this).Bytes;
}
else if (this is NSDate)
if (this is NSDate)
{
return ((NSDate)this).Date;
}
else if (this is UID)
if (this is UID)
{
return ((UID)this).Bytes;
}
else
{
return this;
}
}
internal static bool ArrayEquals(byte[] arrayA, byte[] arrayB)
@@ -482,12 +463,8 @@ namespace Claunia.PropertyList
if (arrayA.Length == arrayB.Length)
{
for (int i = 0; i < arrayA.Length; i++)
{
if (arrayA[i] != arrayB[i])
{
return false;
}
}
return true;
}
return false;

View File

@@ -24,7 +24,6 @@
// SOFTWARE.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Collections;
using System.Text;
@@ -39,9 +38,9 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class NSSet : NSObject, IEnumerable
{
List<NSObject> set;
readonly List<NSObject> set;
private bool ordered = false;
bool ordered;
/// <summary>
/// Creates an empty unordered set.
@@ -114,34 +113,40 @@ namespace Claunia.PropertyList
/// Adds an object to the set.
/// </summary>
/// <param name="obj">The object to add.</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void AddObject(NSObject obj)
{
set.Add(obj);
if (ordered)
set.Sort();
lock (set)
{
set.Add(obj);
if (ordered)
set.Sort();
}
}
/// <summary>
/// Removes an object from the set.
/// </summary>
/// <param name="obj">The object to remove.</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void RemoveObject(NSObject obj)
{
set.Remove(obj);
if (ordered)
set.Sort();
lock (set)
{
set.Remove(obj);
if (ordered)
set.Sort();
}
}
/// <summary>
/// Returns all objects contained in the set.
/// </summary>
/// <returns>An array of all objects in the set.</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public NSObject[] AllObjects()
{
return set.ToArray();
lock (set)
{
return set.ToArray();
}
}
/// <summary>
@@ -149,13 +154,12 @@ namespace Claunia.PropertyList
/// if the set contains no objects.
/// </summary>
/// <returns>The first object in the set, or <code>null</code> if the set is empty.</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public NSObject AnyObject()
{
if (set.Count == 0)
return null;
else
return set[0];
lock (set)
{
return set.Count == 0 ? null : set[0];
}
}
/// <summary>
@@ -174,15 +178,17 @@ namespace Claunia.PropertyList
/// </summary>
/// <param name="obj">The object to look for.</param>
/// <returns>The object if it is present, <code>null</code> otherwise.</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public NSObject Member(NSObject obj)
{
foreach (NSObject o in set)
lock (set)
{
if (o.Equals(obj))
return o;
foreach (NSObject o in set)
{
if (o.Equals(obj))
return o;
}
return null;
}
return null;
}
/// <summary>
@@ -190,15 +196,17 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns><c>true</c> if the intersection of both sets is empty, <c>false</c> otherwise.</returns>
/// <param name="otherSet">The other set.</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public bool IntersectsSet(NSSet otherSet)
{
foreach (NSObject o in set)
lock (set)
{
if (otherSet.ContainsObject(o))
return true;
foreach (NSObject o in set)
{
if (otherSet.ContainsObject(o))
return true;
}
return false;
}
return false;
}
/// <summary>
@@ -206,15 +214,17 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns><c>true</c> if all elements in this set are also present in the other set, <c>false</c>otherwise.</returns>
/// <param name="otherSet">The other set.</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public bool IsSubsetOfSet(NSSet otherSet)
{
foreach (NSObject o in set)
lock (set)
{
if (!otherSet.ContainsObject(o))
return false;
foreach (NSObject o in set)
{
if (!otherSet.ContainsObject(o))
return false;
}
return true;
}
return true;
}
/// <summary>
@@ -223,10 +233,12 @@ namespace Claunia.PropertyList
/// of NSSet.
/// </summary>
/// <returns>The iterator for the set.</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public IEnumerator GetEnumerator()
{
return set.GetEnumerator();
lock (set)
{
return set.GetEnumerator();
}
}
/// <summary>
@@ -241,7 +253,7 @@ namespace Claunia.PropertyList
public override int GetHashCode()
{
int hash = 7;
hash = 29 * hash + (this.set != null ? this.set.GetHashCode() : 0);
hash = 29 * hash + (set != null ? set.GetHashCode() : 0);
return hash;
}
@@ -256,7 +268,7 @@ namespace Claunia.PropertyList
return false;
}
NSSet other = (NSSet)obj;
return !(this.set != other.set && (this.set == null || !this.set.Equals(other.set)));
return !(set != other.set && (set == null || !set.Equals(other.set)));
}
/// <summary>
@@ -337,7 +349,7 @@ namespace Claunia.PropertyList
set.Sort();
NSObject[] array = AllObjects();
ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal);
for (int i = 0; i < array.Length; i++)
{
Type objClass = array[i].GetType();
@@ -381,7 +393,7 @@ namespace Claunia.PropertyList
set.Sort();
NSObject[] array = AllObjects();
ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE);
int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal);
for (int i = 0; i < array.Length; i++)
{
Type objClass = array[i].GetType();

View File

@@ -51,7 +51,7 @@ namespace Claunia.PropertyList
/// <summary>
/// Creates a NSString from a string.
/// </summary>
/// <param name="string">The string that will be contained in the NSString.</param>
/// <param name="text">The string that will be contained in the NSString.</param>
public NSString(string text)
{
content = text;
@@ -228,9 +228,8 @@ namespace Claunia.PropertyList
{
string outString = "";
char[] cArray = s.ToCharArray();
for (int i = 0; i < cArray.Length; i++)
foreach (char c in cArray)
{
char c = cArray[i];
if (c > 127)
{
//non-ASCII Unicode
@@ -275,17 +274,10 @@ namespace Claunia.PropertyList
public int CompareTo(Object o)
{
if (o is NSString)
{
return GetContent().CompareTo(((NSString)o).GetContent());
}
else if (o is String)
{
return GetContent().CompareTo(((String)o));
}
else
{
return -1;
}
return string.Compare(GetContent(), ((NSString)o).GetContent(), StringComparison.Ordinal);
if (o is String)
return string.Compare(GetContent(), ((String)o), StringComparison.Ordinal);
return -1;
}
public override bool Equals(NSObject obj)

View File

@@ -49,7 +49,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The type of the property list</returns>
/// <param name="dataBeginning">The very first bytes of data of the property list (minus any whitespace) as a string</param>
private static int DetermineType(string dataBeginning)
static int DetermineType(string dataBeginning)
{
dataBeginning = dataBeginning.Trim();
if (dataBeginning.Length == 0)
@@ -64,11 +64,7 @@ namespace Claunia.PropertyList
{
return TYPE_ASCII;
}
if (dataBeginning.StartsWith("<"))
{
return TYPE_XML;
}
return TYPE_ERROR_UNKNOWN;
return dataBeginning.StartsWith("<") ? TYPE_XML : TYPE_ERROR_UNKNOWN;
}
/// <summary>
@@ -76,7 +72,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The very first bytes of data of the property list (minus any whitespace)</returns>
/// <param name="bytes">The type of the property list</param>
private static int DetermineType(byte[] bytes)
static int DetermineType(byte[] bytes)
{
//Skip any possible whitespace at the beginning of the file
int offset = 0;
@@ -94,7 +90,7 @@ namespace Claunia.PropertyList
/// <param name="fs">An input stream pointing to the beginning of the property list data.
/// The stream will be reset to the beginning of the property
/// list data after the type has been determined.</param>
private static int DetermineType(Stream fs)
static int DetermineType(Stream fs)
{
//Skip any possible whitespace at the beginning of the file
byte[] magicBytes = new byte[8];
@@ -246,7 +242,7 @@ namespace Claunia.PropertyList
/// Saves a property list with the given object as root into a binary file.
/// </summary>
/// <param name="root">The root object.</param>
/// <param name="outFile"></param>The output file.</param>
/// <param name="outFile">The output file.</param>
/// <exception cref="IOException">When an error occurs during the writing process.</exception>
public static void SaveAsBinary(NSObject root, FileInfo outFile)
{
@@ -264,7 +260,7 @@ namespace Claunia.PropertyList
/// <exception cref="IOException">When an error occurs during the writing process.</exception>
public static void SaveAsBinary(NSObject root, Stream outStream)
{
//BinaryPropertyListWriter.write(outStream, root);
BinaryPropertyListWriter.Write(outStream, root);
}
/// <summary>

View File

@@ -34,8 +34,8 @@ namespace Claunia.PropertyList
/// @author Natalia Portillo
public class UID : NSObject
{
byte[] bytes;
string name;
readonly byte[] bytes;
readonly string name;
public UID(String name, byte[] bytes)
{
@@ -69,11 +69,8 @@ namespace Claunia.PropertyList
{
Indent(xml, level);
xml.Append("<string>");
for (int i = 0; i < bytes.Length; i++)
{
byte b = bytes[i];
foreach (byte b in bytes)
xml.Append(String.Format("{0:x2}", b));
}
xml.Append("</string>");
}
@@ -87,11 +84,8 @@ namespace Claunia.PropertyList
{
Indent(ascii, level);
ascii.Append("\"");
for (int i = 0; i < bytes.Length; i++)
{
byte b = bytes[i];
foreach (byte b in bytes)
ascii.Append(String.Format("{0:x2}", b));
}
ascii.Append("\"");
}

View File

@@ -22,7 +22,6 @@
// 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.Xml;
using System.Collections.Generic;
using System.IO;
@@ -78,7 +77,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The root NSObject of the property list contained in the XML document.</returns>
/// <param name="doc">The XML document.</param>
private static NSObject ParseDocument(XmlDocument doc)
static NSObject ParseDocument(XmlDocument doc)
{
XmlDocumentType docType = doc.DocumentType;
if (docType == null)
@@ -100,23 +99,15 @@ namespace Claunia.PropertyList
//Root element wrapped in plist tag
List<XmlNode> rootNodes = FilterElementNodes(doc.DocumentElement.ChildNodes);
if (rootNodes.Count == 0)
{
throw new PropertyListFormatException("The given XML property list has no root element!");
}
else if (rootNodes.Count == 1)
{
if (rootNodes.Count == 1)
rootNode = rootNodes[0];
}
else
{
throw new PropertyListFormatException("The given XML property list has more than one root element!");
}
}
else
{
//Root NSObject not wrapped in plist-tag
rootNode = doc.DocumentElement;
}
return ParseObject(rootNode);
}
@@ -126,7 +117,7 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The corresponding NSObject.</returns>
/// <param name="n">The XML node.</param>
private static NSObject ParseObject(XmlNode n)
static NSObject ParseObject(XmlNode n)
{
if (n.Name.Equals("dict"))
{
@@ -143,7 +134,7 @@ namespace Claunia.PropertyList
}
return dict;
}
else if (n.Name.Equals("array"))
if (n.Name.Equals("array"))
{
List<XmlNode> children = FilterElementNodes(n.ChildNodes);
NSArray array = new NSArray(children.Count);
@@ -153,35 +144,19 @@ namespace Claunia.PropertyList
}
return array;
}
else if (n.Name.Equals("true"))
{
if (n.Name.Equals("true"))
return new NSNumber(true);
}
else if (n.Name.Equals("false"))
{
if (n.Name.Equals("false"))
return new NSNumber(false);
}
else if (n.Name.Equals("integer"))
{
if (n.Name.Equals("integer"))
return new NSNumber(GetNodeTextContents(n));
}
else if (n.Name.Equals("real"))
{
if (n.Name.Equals("real"))
return new NSNumber(GetNodeTextContents(n));
}
else if (n.Name.Equals("string"))
{
if (n.Name.Equals("string"))
return new NSString(GetNodeTextContents(n));
}
else if (n.Name.Equals("data"))
{
if (n.Name.Equals("data"))
return new NSData(GetNodeTextContents(n));
}
else if (n.Name.Equals("date"))
{
return new NSDate(GetNodeTextContents(n));
}
return null;
return n.Name.Equals("date") ? new NSDate(GetNodeTextContents(n)) : null;
}
/// <summary>
@@ -189,16 +164,12 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The sublist containing only nodes representing actual elements.</returns>
/// <param name="list">The list of nodes to search.</param>
private static List<XmlNode> FilterElementNodes(XmlNodeList list)
static List<XmlNode> FilterElementNodes(XmlNodeList list)
{
List<XmlNode> result = new List<XmlNode>();
foreach (XmlNode child in list)
{
if (child.NodeType == XmlNodeType.Element)
{
result.Add(child);
}
}
return result;
}
@@ -209,44 +180,30 @@ namespace Claunia.PropertyList
/// </summary>
/// <returns>The node's text content.</returns>
/// <param name="n">The node.</param>
private static string GetNodeTextContents(XmlNode n)
static string GetNodeTextContents(XmlNode n)
{
if (n.NodeType == XmlNodeType.Text || n.NodeType == XmlNodeType.CDATA)
{
string content = n.Value; //This concatenates any adjacent text/cdata/entity nodes
if (content == null)
return "";
else
return content;
return content ?? "";
}
else
if (n.HasChildNodes)
{
if (n.HasChildNodes)
{
XmlNodeList children = n.ChildNodes;
XmlNodeList children = n.ChildNodes;
foreach (XmlNode child in children)
foreach (XmlNode child in children)
{
//Skip any non-text nodes, like comments or entities
if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA)
{
//Skip any non-text nodes, like comments or entities
if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA)
{
string content = child.Value; //This concatenates any adjacent text/cdata/entity nodes
if (content == null)
return "";
else
return content;
}
string content = child.Value; //This concatenates any adjacent text/cdata/entity nodes
return content ?? "";
}
return "";
}
else
{
return "";
}
return "";
}
return "";
}
}
}