private void WriteData(TVal Data) { switch (Data.Type) { case DataType.Dictionary: WriteData((Dictionary <string, TVal>)Data); break; case DataType.List: WriteData((List <TVal>)Data); break; case DataType.Byte: WriteData((byte[])Data); break; case DataType.Int: WriteData((long)Data); break; default: WriteData((string)Data, Data.dEncoding); break; } }
public TorrentWriter(TVal val) { Debug.Assert(val.Type == DataType.Dictionary); Dictionary <string, TVal> Root = (Dictionary <string, TVal>)val; _root = Root; }
/// <summary> /// List handler /// </summary> /// <returns>The parsed List</returns> TVal ProcessList() { //Defining the new list List <TVal> TempList = new List <TVal>(); TVal val = new TVal(DataType.List, TempList); //Lopping while it's not the end of the list ("e") while (GetChar() != 'e') { //Adding the list value TempList.Add(ProcessVal()); } //Getting rid of the list end ("e") _torrent.Read(); //Returning the list return(val); }
/// <summary> /// Processing the dictionary starting form the file stream position /// </summary> /// <returns>The processed dictionary</returns> TVal ProcessDict() { //Defining the new dictionary Dictionary <string, TVal> TempDict = new Dictionary <string, TVal>(); TVal val = new TVal(DataType.Dictionary, TempDict); //Looping while it's not the end of the dictionary ('e' or in ASCII 101) while (_torrent.PeekChar() != 101) { //Getting the key string key = ReadString(); //Checking if this is the Info dictionary start if (key == "info") { _InfoStart = _torrent.BaseStream.Position; } //Checking for binary values if (key == "pieces") { //If this is a binary value, //call the binary value handler and add it to the dictionary TempDict.Add(key, ProcessBytes()); } else { //if not,Check which value is it and add to the dictionary TVal Val = ProcessVal(); TempDict.Add(key, Val); } //if this is the end of the info dictionary then this is the _InfoEnd if (key == "info") { _InfoEnd = _torrent.BaseStream.Position - _InfoStart; } } //Getting rid of the dictionary end ("e") _torrent.Read(); //Returning the dict. return(val); }