/// <summary> /// Constructs a dictionary /// </summary> /// <param name="istream">Stream to construct dictionary from</param> public Dictionary(IO.Stream istream) { if (istream.CanSeek) { this.position = (int)istream.Position - 1; } int c; while ((c = istream.ReadByte()) != 'e') { BEncode.String key = new BEncode.String(istream, c); // keys are always strings string strkey = key; Element element = BEncode.NextElement(istream); if (!this.map.Contains(key)) { this.map.Add(key, element); } } if (istream.CanSeek) { this.length = (int)istream.Position - this.position; } }
/// <summary>Constructs an integer from the bEncoded stream</summary> /// <param name="istream">Stream to construct integer from</param> public List(IO.Stream istream) { if (istream.CanSeek) { this.position = (int)istream.Position - 1; } int c; while ((c = istream.ReadByte()) != 'e') { BEncode.Element element = BEncode.NextElement(istream, c); string el = element.ToString(); this.list.Add(element); } if (istream.CanSeek) { this.length = (int)istream.Position - this.position; } }
/// <summary>Adds a key-value pair to the dictionary</summary> /// <param name="key">Key</param> /// <param name="val">Value</param> public void Add(BEncode.String key, BEncode.Element val) { this.map.Add(key, val); }
/// <summary>Checks if the key exists in the dictionary</summary> /// <param name="key">Key to check</param> /// <returns>True if the key exists</returns> public bool Contains(BEncode.String key) { return this.map.Contains(key); }
/// <summary>Removes a key-value pair from the dictionary</summary> /// <param name="key">Key to remove</param> public void Remove(BEncode.String key) { this.map.Remove(key); }
/// <summary>Retrieves the value associated with the specified key</summary> /// <param name="key">Key</param> /// <returns>Value</returns> public BEncode.Element this[BEncode.String key] { get { return (BEncode.Element)this.map[key]; } set { this.map[key] = value; } }
/// <summary> /// Gets the integer with the specified key. Same as GetElement(), except performs the casting automatically. /// </summary> /// <param name="key">Key name</param> /// <returns>Integer element</returns> public int GetInteger(BEncode.String key) { return (BEncode.Integer)this[key]; }
/// <summary> /// Gets the bytes with the specified key. Same as GetElement(), except performs the casting automatically. /// </summary> /// <param name="key">Key name</param> /// <returns>Bytes element</returns> public byte[] GetBytes(BEncode.String key) { return ((BEncode.String)this[key]).Data; }
/// <summary></summary> /// <param name="element">Object to add to list</param> /// <returns>Index of object added</returns> public int Add(BEncode.Element element) { return this.list.Add(element); }
/// <summary> /// Gets the list with the specified key. Same as GetElement(), except performs the casting automatically. /// </summary> /// <param name="key">Key name</param> /// <returns>List element</returns> public List GetList(BEncode.String key) { return (BEncode.List)this[key]; }
/// <summary> /// Gets the dictionary with the specified key. Same as GetElement(), except performs the casting automatically. /// </summary> /// <param name="key">Key name</param> /// <returns>Dictionary element</returns> public Dictionary GetDictionary(BEncode.String key) { return (BEncode.Dictionary)this[key]; }
/// <summary>Removes an element from the list</summary> /// <param name="obj">Element to remove</param> public void Remove(BEncode.Element element) { this.list.Remove(element); }
/// <summary>Inserts an element into the list at the specified index</summary> /// <param name="index">Index to insert element at</param> /// <param name="obj">Element to insert</param> public void Insert(int index, BEncode.Element element) { this.list.Insert(index, element); }
/// <summary>Finds the index at which the element resides</summary> /// <param name="obj">Element to check</param> /// <returns>Index of element, -1 if it does not exist</returns> public int IndexOf(BEncode.Element element) { return this.list.IndexOf(element); }
/// <summary></summary> /// <param name="element">Object to check if it exists in the list</param> /// <returns>True if object is in the list, false otherwise</returns> public bool Contains(BEncode.Element element) { return this.list.Contains(element); }
/// <summary> /// Gets the string with the specified key. Same as GetElement(), except performs the casting automatically. /// </summary> /// <param name="key">Key name</param> /// <returns>String element</returns> public string GetString(BEncode.String key) { return (BEncode.String)this[key]; }
/// <summary>Compares the two objects</summary> /// <param name="str">String to compare to</param> /// <returns>Zero if they are the same</returns> public int CompareTo(BEncode.String str) { return this.ToString().CompareTo(str); }