ruby写的BT种子解析器
ruby写的BT种子解析器
class MyFile < File
ruby写的BT种子解析器  
def peek(len)
ruby写的BT种子解析器    str 
= self.read(len)
ruby写的BT种子解析器    self.pos 
-= len
ruby写的BT种子解析器    str
ruby写的BT种子解析器  end  
ruby写的BT种子解析器end
ruby写的BT种子解析器
ruby写的BT种子解析器
#字段信息
ruby写的BT种子解析器
class TVal
ruby写的BT种子解析器  attr_accessor :Type
ruby写的BT种子解析器  attr_accessor :Value
ruby写的BT种子解析器  
ruby写的BT种子解析器  
def to_s()
ruby写的BT种子解析器    @Value
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
ruby写的BT种子解析器end
ruby写的BT种子解析器
ruby写的BT种子解析器
#文件信息
ruby写的BT种子解析器
class TFile
ruby写的BT种子解析器  attr_reader :Length
ruby写的BT种子解析器  attr_reader :Path
ruby写的BT种子解析器  attr_reader :Name
ruby写的BT种子解析器  attr_reader :FirstPiece
ruby写的BT种子解析器  attr_reader :PieceLength
ruby写的BT种子解析器  
ruby写的BT种子解析器  
def initialize(size, fullPath, pStart, pLen)
ruby写的BT种子解析器    @Length 
= Size
ruby写的BT种子解析器    @FirstPiece 
= pStart
ruby写的BT种子解析器    @PieceLength 
= pLen
ruby写的BT种子解析器    fileNamePos 
= fullPath.rindex('\\')
ruby写的BT种子解析器    
if fileNamePos.nil? then
ruby写的BT种子解析器      @Path 
= ""
ruby写的BT种子解析器      @Name 
= fullPath
ruby写的BT种子解析器    
else
ruby写的BT种子解析器      @Path 
= fullPath[0..fileNamePos]
ruby写的BT种子解析器      @Name 
= fullPath[fileNamePos..fullPath.length-1]
ruby写的BT种子解析器    end
ruby写的BT种子解析器  end
ruby写的BT种子解析器end
ruby写的BT种子解析器  
ruby写的BT种子解析器
ruby写的BT种子解析器
class TorrentParser
ruby写的BT种子解析器  attr_accessor :Root
ruby写的BT种子解析器  
def initialize(tPath)
ruby写的BT种子解析器    
ruby写的BT种子解析器    @Path 
= tPath
ruby写的BT种子解析器    @Size 
= nil
ruby写的BT种子解析器    
ruby写的BT种子解析器    @Root 
= nil
ruby写的BT种子解析器    @Files 
= nil
ruby写的BT种子解析器    
ruby写的BT种子解析器    @Torrent 
= nil
ruby写的BT种子解析器    @TotalValues 
= 0
ruby写的BT种子解析器    
ruby写的BT种子解析器    @InfoStart 
= nil
ruby写的BT种子解析器    @InfoEnd 
= nil
ruby写的BT种子解析器    
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
#主函数
ruby写的BT种子解析器
  def Process()
ruby写的BT种子解析器    
raise "File doesn't exist"  if !MyFile.exist? @Path #检测文件路径
ruby写的BT种子解析器
    @TotalValues = 0
ruby写的BT种子解析器    
ruby写的BT种子解析器    @Torrent 
= MyFile.new @Path
ruby写的BT种子解析器    @Torrent.binmode
ruby写的BT种子解析器
ruby写的BT种子解析器    
raise "Torrent File Error" if @Torrent.read(1!= 'd'
ruby写的BT种子解析器    
ruby写的BT种子解析器    @Root 
= ProcessDict()
ruby写的BT种子解析器    @Files 
= GetFiles()
ruby写的BT种子解析器    
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
#读取一个字典
ruby写的BT种子解析器
  def ProcessDict()
ruby写的BT种子解析器    tempDict 
= Hash.new
ruby写的BT种子解析器
ruby写的BT种子解析器    
while @Torrent.peek(1!= 'e' #向后读一个字符 看看是不是e 如果是e的话 说明dictionary结束了
ruby写的BT种子解析器
      key = ProcessString() #读取一个Key Name
ruby写的BT种子解析器

ruby写的BT种子解析器      
if key == 'info' then #如果是名字为info的key
ruby写的BT种子解析器
        @InfoStart = @Torrent.pos #设置info start的index
ruby写的BT种子解析器
      end
ruby写的BT种子解析器      
if key == 'pieces' then #如果是pieces的key 则按照特殊的byte方式读取
ruby写的BT种子解析器
        val = TVal.new
ruby写的BT种子解析器        val.Type 
= "BYTE"
ruby写的BT种子解析器        val.Value 
= ProcessByte()
ruby写的BT种子解析器        tempDict[key] 
= val
ruby写的BT种子解析器      
else
ruby写的BT种子解析器        val 
= ProcessVal() #否则读该key的Val
ruby写的BT种子解析器
        tempDict[key] = val
ruby写的BT种子解析器      end
ruby写的BT种子解析器      
if key == "info" then
ruby写的BT种子解析器        @InfoEnd 
= @Torrent.pos - @InfoStart
ruby写的BT种子解析器      end
ruby写的BT种子解析器    end
ruby写的BT种子解析器    @Torrent.read 
1 #读掉结尾的"e"
ruby写的BT种子解析器
    
ruby写的BT种子解析器    
return tempDict
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
ruby写的BT种子解析器  
ruby写的BT种子解析器
ruby写的BT种子解析器  
#读取一个字串
ruby写的BT种子解析器
  def ProcessString()
ruby写的BT种子解析器    tempLen 
= ''
ruby写的BT种子解析器    tempLen 
+= @Torrent.read(1while @Torrent.peek(1!= ':' #获取字串长度
ruby写的BT种子解析器
    
ruby写的BT种子解析器    @Torrent.read(
1# 将后面的冒号读掉    
ruby写的BT种子解析器

ruby写的BT种子解析器    content 
= @Torrent.read(tempLen.to_i) #然后读指定长度的字符串 并返回 
ruby写的BT种子解析器

ruby写的BT种子解析器    
return content
ruby写的BT种子解析器  end
ruby写的BT种子解析器    
ruby写的BT种子解析器
ruby写的BT种子解析器  
#读取字节串
ruby写的BT种子解析器
  def ProcessByte()
ruby写的BT种子解析器    tempLen 
= ''
ruby写的BT种子解析器    tempLen 
+= @Torrent.read(1while @Torrent.peek(1!= ':' #获取字串长度
ruby写的BT种子解析器
    
ruby写的BT种子解析器    @Torrent.read(
1# 将后面的冒号读掉    
ruby写的BT种子解析器
    
ruby写的BT种子解析器    chrs 
= Array.new
ruby写的BT种子解析器    (
1..tempLen.to_i).each{|idx|
ruby写的BT种子解析器      byte 
= @Torrent.readchar
ruby写的BT种子解析器      chrs 
<< "%02X" % byte
ruby写的BT种子解析器    }
ruby写的BT种子解析器    chrs
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
ruby写的BT种子解析器
ruby写的BT种子解析器  
#读一个数字
ruby写的BT种子解析器
  def ProcessInt()
ruby写的BT种子解析器    tempInt 
= ''
ruby写的BT种子解析器    tempInt 
+= @Torrent.read(1while @Torrent.peek(1!= 'e'
ruby写的BT种子解析器
ruby写的BT种子解析器    @Torrent.read(
1#把最后的e读掉
ruby写的BT种子解析器
    tempInt.to_i
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
#读一个列表
ruby写的BT种子解析器
  def ProcessList()
ruby写的BT种子解析器    tempList 
= Array.new
ruby写的BT种子解析器    
while @Torrent.peek(1!= 'e'
ruby写的BT种子解析器      tempList 
<< ProcessVal
ruby写的BT种子解析器    end
ruby写的BT种子解析器    tempList
ruby写的BT种子解析器  end
ruby写的BT种子解析器    
ruby写的BT种子解析器  
#读取一个Val
ruby写的BT种子解析器
  def ProcessVal()
ruby写的BT种子解析器    @TotalValues 
+= 1
ruby写的BT种子解析器    
ruby写的BT种子解析器    val 
= TVal.new
ruby写的BT种子解析器    
ruby写的BT种子解析器    
ruby写的BT种子解析器    case @Torrent.peek(
1)
ruby写的BT种子解析器      when 
'd'
ruby写的BT种子解析器        @Torrent.read(
1)  #把d读掉
ruby写的BT种子解析器
        val.Type = "DICT"
ruby写的BT种子解析器        val.Value 
= ProcessDict()
ruby写的BT种子解析器      when 
'l'
ruby写的BT种子解析器        @Torrent.read(
1#把l读掉
ruby写的BT种子解析器
        val.Type = "LIST" 
ruby写的BT种子解析器        val.Value 
= ProcessList()
ruby写的BT种子解析器      when 
'i'
ruby写的BT种子解析器        @Torrent.read(
1#把i读掉
ruby写的BT种子解析器
        val.Type = "INT"
ruby写的BT种子解析器        val.Value 
= ProcessInt()
ruby写的BT种子解析器      
else
ruby写的BT种子解析器        val.Type 
= "STRING"
ruby写的BT种子解析器        val.Value 
= ProcessString()
ruby写的BT种子解析器    end
ruby写的BT种子解析器    val
ruby写的BT种子解析器  end
ruby写的BT种子解析器  
ruby写的BT种子解析器end
ruby写的BT种子解析器
ruby写的BT种子解析器begin
ruby写的BT种子解析器  _torrent 
= TorrentParser.new ARGV.shift
ruby写的BT种子解析器  _torrent.Process
ruby写的BT种子解析器  
ruby写的BT种子解析器  _torrent.Root.map{
|key,value|
ruby写的BT种子解析器    case value.Type
ruby写的BT种子解析器      when 
'LIST'
ruby写的BT种子解析器        puts 
"#{key}(#{value.Type})\t\t#{value.Value}"
ruby写的BT种子解析器      when 
'BYTE'
ruby写的BT种子解析器        puts 
"#{key}(#{value.Type})\t\t%0X", value.Value
ruby写的BT种子解析器      when 
'DICT'
ruby写的BT种子解析器        value.Value.map{
|skey,svalue|
ruby写的BT种子解析器          puts 
"--#{skey}(#{svalue.Type})\t\t#{svalue.Value}"
ruby写的BT种子解析器        }
ruby写的BT种子解析器      
else
ruby写的BT种子解析器        puts 
"#{key}(#{value.Type})\t\t#{value.Value}"
ruby写的BT种子解析器    end
ruby写的BT种子解析器  }
ruby写的BT种子解析器  
ruby写的BT种子解析器rescue
ruby写的BT种子解析器  puts $!
ruby写的BT种子解析器  puts $@
ruby写的BT种子解析器end

相关文章: