[matroska] r781 - in trunk/RbMatroska: . ebml

[email protected]
Newsgroups gmane.comp.multimedia.matroska.cvs
Message-ID <[email protected]>
Author: mosu
Date: 2004-09-11 21:48:34 +0400 (Sat, 11 Sep 2004)
New Revision: 781

Added:
   trunk/RbMatroska/ebml/
   trunk/RbMatroska/ebml/binary.rb
   trunk/RbMatroska/ebml/date.rb
   trunk/RbMatroska/ebml/file.rb
   trunk/RbMatroska/ebml/float.rb
   trunk/RbMatroska/ebml/master.rb
   trunk/RbMatroska/ebml/sinteger.rb
   trunk/RbMatroska/ebml/string.rb
   trunk/RbMatroska/ebml/uinteger.rb
   trunk/RbMatroska/ebml/unicodestring.rb
Modified:
   trunk/RbMatroska/ebml.rb
Log:
Split the EBML classes into seperate files. This should be more concise, especially when I add support and a lot of code for writing stuff.

Added: trunk/RbMatroska/ebml/binary.rb
===================================================================
--- trunk/RbMatroska/ebml/binary.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/binary.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,32 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class Binary < Element
+    def read(ef)
+      @value = ef.io.read(@data_size)
+      raise EOFError if (@value.size < @data_size)
+
+      return self
+    end
+
+    def to_s
+      @value ||= Array.new
+      return "[Binary size " + @data_size.to_s + "]"
+    end
+  end                           # class Binary
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/binary.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/date.rb
===================================================================
--- trunk/RbMatroska/ebml/date.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/date.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,27 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+require "ebml/uinteger"
+
+module Ebml
+  class Date < UInteger
+    def to_s
+      @value ||= 0
+      return Time.at(@value / 1000000000).to_s
+    end
+  end                           # class Date
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/date.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/file.rb
===================================================================
--- trunk/RbMatroska/ebml/file.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/file.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,123 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class File
+    attr_reader :io
+
+    def initialize(io)
+      @io = io
+    end
+
+    def entity_size?(byte, mask_bits = false)
+      size = 0
+      mask = 0
+      if ((byte & 0x80) == 0x80)
+        size = 1
+        mask = 0x7f
+      elsif ((byte & 0xc0) == 0x40)
+        size = 2
+        mask = 0x3f
+      elsif ((byte & 0xe0) == 0x20)
+        size = 3
+        mask = 0x1f
+      elsif ((byte & 0xf0) == 0x10)
+        size = 4
+        mask = 0x0f
+      elsif ((byte & 0xf8) == 0x08)
+        size = 5
+        mask = 0x07
+      elsif ((byte & 0xfc) == 0x04)
+        size = 6
+        mask = 0x03
+      elsif ((byte & 0xfe) == 0x02)
+        size = 7
+        mask = 0x01
+      elsif (byte == 1)
+        size = 8
+        mask = 0x00
+      else
+        raise DataError
+      end
+      byte = byte & mask if (mask_bits)
+      return [size, byte]
+    end
+
+    def read_id
+      id = 0
+
+      byte = @io.getc
+      return nil if (byte == nil)
+
+      esize = entity_size?(byte)
+      raise IdError if (esize[0] > 4)
+      id = esize[1]
+
+      1.upto(esize[0] - 1) do |i|
+        byte = @io.readchar
+        id = id * 256 + byte
+      end
+      return Id.new(id, esize[0])
+    end
+
+    def read_size
+      byte = @io.getc
+      return nil if (byte == nil)
+
+      esize = entity_size?(byte, true)
+
+      size = esize[1]
+      1.upto(esize[0] - 1) do
+        byte = @io.readchar
+        size = size * 256 + byte
+      end
+      return [size, esize[0]]
+    end
+
+    def read_element_head
+      pos = @io.pos
+      id = read_id
+      return nil if (id.class == NilClass)
+      size = read_size
+      return nil if (size.class == NilClass)
+      return find_class_for_id(id).new(id, pos, size[0], size[1])
+    end
+
+    def map_ids(namespace = nil)
+      @id_mapping = Hash.new
+
+      rebml = Regexp.new("^Ebml::")
+      rns = nil
+      if (namespace != nil)
+        require namespace.downcase
+        rns = Regexp.new("^#{namespace}::")
+      end
+      ObjectSpace.each_object(Class) do |c|
+        if (((rebml =~ c.name) or ((rns != nil) and (rns =~ c.name))) and
+           (c.respond_to?("global_id")))
+          @id_mapping[c.global_id.value] = c
+        end
+      end
+    end
+
+    def find_class_for_id(id)
+      raise Error, "IDs not mapped" unless (@id_mapping)
+      return @id_mapping[id.value] if (@id_mapping.has_key?(id.value))
+      return Element
+    end
+  end                           # class File
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/file.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/float.rb
===================================================================
--- trunk/RbMatroska/ebml/float.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/float.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,37 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class Float < Element
+    def read(ef)
+      ins = ef.io.read(@data_size)
+      raise EOFError if (ins.size < @data_size)
+      @value = ins.unpack(@data_size == 4 ? "g" : "G")[0]
+
+      return self
+    end
+
+    def to_f
+      @value ||= 0.0
+      return @value
+    end
+
+    def to_s
+      return sprintf("%f", to_f)
+    end
+  end                           # class Float
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/float.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/master.rb
===================================================================
--- trunk/RbMatroska/ebml/master.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/master.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,69 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class Master < Element
+    attr_reader :children
+
+    def initialize(id, pos, data_size, size_length)
+      super(id, pos, data_size, size_length)
+      @children = Array.new
+    end
+
+    def read(ef)
+      @children = Array.new
+
+      size_left = data_size
+      while (size_left > 0)
+        e = ef.read_element_head
+        if (e.class != NilClass)
+          e.read(ef)
+          @children.push(e)
+          size_left -= e.element_size
+          e.skip(ef)
+        else
+          break
+        end
+      end
+
+      return self
+    end
+
+    def each(filter = Id.none)
+      @children.each do |c|
+        yield(c) if ((filter == Id.none) or (filter == c.id))
+      end
+    end
+
+    def find(filter)
+      id = nil
+      if (filter.class == Class)
+        id = filter.global_id
+      else
+        id = filter
+      end
+      @children.each { |c| return c if (c.id == id) }
+      return nil
+    end
+
+    def to_s
+      s = "{" + self.class.name + " " + super + " "
+      @children.each { |c| s += c.to_s + " " }
+      return s + "}"
+    end
+  end                           # class Master
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/master.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/sinteger.rb
===================================================================
--- trunk/RbMatroska/ebml/sinteger.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/sinteger.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,45 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class SInteger < Element
+    def read(ef)
+      byte = ef.io.readchar
+      if ((byte & 0x80) == 0x80)
+        @value = -1 << 8
+      else
+        @value = 0
+      end
+      @value |= byte
+      2.upto(@data_size) do |i|
+        byte = ef.io.readchar
+        @value = (@value << 1) | byte
+      end
+
+      return self
+    end
+
+    def to_i
+      @value ||= 0
+      return @value
+    end
+
+    def to_s
+      return to_i.to_s
+    end
+  end                           # class SInteger
+end                             # module Ebml
+


Property changes on: trunk/RbMatroska/ebml/sinteger.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/string.rb
===================================================================
--- trunk/RbMatroska/ebml/string.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/string.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,32 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class String < Element
+    def read(ef)
+      @value = ef.io.read(@data_size)
+      raise DataError if (@value.size < @data_size)
+
+      return self
+    end
+
+    def to_s
+      @value ||= ""
+      return @value
+    end
+  end                           # class String
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/string.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/uinteger.rb
===================================================================
--- trunk/RbMatroska/ebml/uinteger.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/uinteger.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,39 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class UInteger < Element
+    def read(ef)
+      @value = 0
+      1.upto(@data_size) do
+        byte = ef.io.readchar
+        @value = (@value << 8) | byte
+      end
+
+      return self
+    end
+
+    def to_i
+      @value ||= 0
+      return @value
+    end
+
+    def to_s
+      return to_i.to_s
+    end
+  end                           # class UInteger
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/uinteger.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/RbMatroska/ebml/unicodestring.rb
===================================================================
--- trunk/RbMatroska/ebml/unicodestring.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml/unicodestring.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -0,0 +1,32 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A simple EBML parser based loosely on libebml and other
+# EBML implementations. It also contains classes for the global EBML
+# elements mentioned on # http://www.matroska.org/technical/specs/index.html
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+module Ebml
+  class UnicodeString < String
+    def read(ef)
+      @value = ef.io.read(@data_size)
+      raise DataError if (@value.size < @data_size)
+
+      return self
+    end
+
+    def to_s
+      @value ||= ""
+      return @value
+    end
+  end                           # class UnicodeString
+
+end                             # module Ebml


Property changes on: trunk/RbMatroska/ebml/unicodestring.rb
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: trunk/RbMatroska/ebml.rb
===================================================================
--- trunk/RbMatroska/ebml.rb	2004-09-11 17:42:24 UTC (rev 780)
+++ trunk/RbMatroska/ebml.rb	2004-09-11 17:48:34 UTC (rev 781)
@@ -108,279 +108,17 @@
     end
   end
 
-  class SInteger < Element
-    def read(ef)
-      byte = ef.io.readchar
-      if ((byte & 0x80) == 0x80)
-        @value = -1 << 8
-      else
-        @value = 0
-      end
-      @value |= byte
-      2.upto(@data_size) do |i|
-        byte = ef.io.readchar
-        @value = (@value << 8) | byte
-      end
+  require "ebml/binary"
+  require "ebml/date"
+  require "ebml/float"
+  require "ebml/master"
+  require "ebml/sinteger"
+  require "ebml/string"
+  require "ebml/uinteger"
+  require "ebml/unicodestring"
 
-      return self
-    end
+  require "ebml/file"
 
-    def to_i
-      @value ||= 0
-      return @value
-    end
-
-    def to_s
-      return to_i.to_s
-    end
-  end
-
-  class UInteger < Element
-    def read(ef)
-      @value = 0
-      1.upto(@data_size) do
-        byte = ef.io.readchar
-        @value = (@value << 8) | byte
-      end
-
-      return self
-    end
-
-    def to_i
-      @value ||= 0
-      return @value
-    end
-
-    def to_s
-      return to_i.to_s
-    end
-  end
-
-  class String < Element
-    def read(ef)
-      @value = ef.io.read(@data_size)
-      raise DataError if (@value.size < @data_size)
-
-      return self
-    end
-
-    def to_s
-      @value ||= ""
-      return @value
-    end
-  end
-
-  class UnicodeString < String
-    def read(ef)
-      @value = ef.io.read(@data_size)
-      raise DataError if (@value.size < @data_size)
-
-      return self
-    end
-
-    def to_s
-      @value ||= ""
-      return @value
-    end
-  end
-
-  class Binary < Element
-    def read(ef)
-      @value = ef.io.read(@data_size)
-      raise EOFError if (@value.size < @data_size)
-
-      return self
-    end
-
-    def to_s
-      @value ||= Array.new
-      return "[Binary size " + @data_size.to_s + "]"
-    end
-  end
-
-  class Float < Element
-    def read(ef)
-      ins = ef.io.read(@data_size)
-      raise EOFError if (ins.size < @data_size)
-      @value = ins.unpack(@data_size == 4 ? "g" : "G")[0]
-
-      return self
-    end
-
-    def to_f
-      @value ||= 0.0
-      return @value
-    end
-
-    def to_s
-      return sprintf("%f", to_f)
-    end
-  end
-
-  class Date < UInteger
-    def to_s
-      @value ||= 0
-      return Time.at(@value / 1000000000).to_s
-    end
-  end
-
-  class Master < Element
-    attr_reader :children
-
-    def initialize(id, pos, data_size, size_length)
-      super(id, pos, data_size, size_length)
-      @children = Array.new
-    end
-
-    def read(ef)
-      @children = Array.new
-
-      size_left = data_size
-      while (size_left > 0)
-        e = ef.read_element_head
-        if (e.class != NilClass)
-          e.read(ef)
-          @children.push(e)
-          size_left -= e.element_size
-          e.skip(ef)
-        else
-          break
-        end
-      end
-
-      return self
-    end
-
-    def each(filter = Id.none)
-      @children.each do |c|
-        yield(c) if ((filter == Id.none) or (filter == c.id))
-      end
-    end
-
-    def find(filter)
-      id = nil
-      if (filter.class == Class)
-        id = filter.global_id
-      else
-        id = filter
-      end
-      @children.each { |c| return c if (c.id == id) }
-      return nil
-    end
-
-    def to_s
-      s = "{" + self.class.name + " " + super + " "
-      @children.each { |c| s += c.to_s + " " }
-      return s + "}"
-    end
-  end
-
-  class File
-    attr_reader :io
-
-    def initialize(io)
-      @io = io
-    end
-
-    def entity_size?(byte, mask_bits = false)
-      size = 0
-      mask = 0
-      if ((byte & 0x80) == 0x80)
-        size = 1
-        mask = 0x7f
-      elsif ((byte & 0xc0) == 0x40)
-        size = 2
-        mask = 0x3f
-      elsif ((byte & 0xe0) == 0x20)
-        size = 3
-        mask = 0x1f
-      elsif ((byte & 0xf0) == 0x10)
-        size = 4
-        mask = 0x0f
-      elsif ((byte & 0xf8) == 0x08)
-        size = 5
-        mask = 0x07
-      elsif ((byte & 0xfc) == 0x04)
-        size = 6
-        mask = 0x03
-      elsif ((byte & 0xfe) == 0x02)
-        size = 7
-        mask = 0x01
-      elsif (byte == 1)
-        size = 8
-        mask = 0x00
-      else
-        raise DataError
-      end
-      byte = byte & mask if (mask_bits)
-      return [size, byte]
-    end
-
-    def read_id
-      id = 0
-
-      byte = @io.getc
-      return nil if (byte == nil)
-
-      esize = entity_size?(byte)
-      raise IdError if (esize[0] > 4)
-      id = esize[1]
-
-      1.upto(esize[0] - 1) do |i|
-        byte = @io.readchar
-        id = id * 256 + byte
-      end
-      return Id.new(id, esize[0])
-    end
-
-    def read_size
-      byte = @io.getc
-      return nil if (byte == nil)
-
-      esize = entity_size?(byte, true)
-
-      size = esize[1]
-      1.upto(esize[0] - 1) do
-        byte = @io.readchar
-        size = size * 256 + byte
-      end
-      return [size, esize[0]]
-    end
-
-    def read_element_head
-      pos = @io.pos
-      id = read_id
-      return nil if (id.class == NilClass)
-      size = read_size
-      return nil if (size.class == NilClass)
-      return find_class_for_id(id).new(id, pos, size[0], size[1])
-    end
-
-    def map_ids(namespace = nil)
-      @id_mapping = Hash.new
-
-      rebml = Regexp.new("^Ebml::")
-      rns = nil
-      if (namespace != nil)
-        require namespace.downcase
-        rns = Regexp.new("^#{namespace}::")
-      end
-      ObjectSpace.each_object(Class) do |c|
-        if (((rebml =~ c.name) or ((rns != nil) and (rns =~ c.name))) and
-           (c.respond_to?("global_id")))
-          @id_mapping[c.global_id.value] = c
-        end
-      end
-    end
-
-    def find_class_for_id(id)
-      raise Error, "IDs not mapped" unless (@id_mapping)
-      return @id_mapping[id.value] if (@id_mapping.has_key?(id.value))
-      return Element
-    end
-  end                           # class EFile
-
-
   class Head < Master
     def Head.global_id
       @@id ||= Id.new(0x1a45dfa3, 4)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.