/// <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>Compares the object passed in to see if it is the same</summary> /// <param name="obj">Object to compare to</param> /// <returns>True if the same, false otherwise</returns> public override bool Equals(object obj) { if (obj is BEncode.String) { BEncode.String str = (BEncode.String)obj; if (str.data.Length != this.data.Length) { return(false); } for (int i = 0; i < str.data.Length; ++i) { if (str.data[i] != this.data[i]) { return(false); } } return(true); } else { return(false); } }
/// <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> /// 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> /// 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>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)); }
/// <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; }