Removed Eet::File#list.
[ruby-eet.git] / lib / eet.rb
index abf8814f5c849809ec787b6fb70db6b4076bf339..ca2815c7bbb15b4ea2b434c734b3ff05ab43ebca 100644 (file)
@@ -1,7 +1,5 @@
 #--
-# $Id: eet.rb 1 2005-03-26 01:45:38Z tilman $
-#
-# Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
+# Copyright (c) 2005-2007 Tilman Sauerbeck (tilman at code-monkey de)
 #
 # Permission is hereby granted, free of charge, to any person obtaining
 # a copy of this software and associated documentation files (the
 require "eet_ext"
 
 class Object
-       # :call-seq:
-       #  object.to_eet -> string
-       #
-       # Serializes the receiver to EET format.
-       def to_eet
-               props = to_eet_properties
-
-               unless props.is_a?(Hash) && !props.empty?
-                       raise(Eet::PropertyError, "invalid EET properties")
-               end
-
-               eet_name = to_eet_name
-
-               if eet_name.to_str.length < 1 || eet_name.to_str.include?(0)
-                       raise(Eet::NameError, "invalid EET name")
-               end
-
-               stream = Eet::Stream.new
-
-               props.each_pair do |tag, arg|
-                       unless arg.is_a?(Array)
-                               raise(Eet::PropertyError, "hash value not an array")
-                       end
-
-                       value, type = arg
-                       next if value.nil?
-
-                       stream.push(*value.to_eet_chunks(tag, type))
-               end
-
-               chunk = Eet::Chunk.new(eet_name, stream.serialize)
-               Eet::Stream.new(chunk).serialize
-       end
-
        def to_eet_chunks(tag, type = nil) # :nodoc:
                [Eet::Chunk.new(tag, to_eet)]
        end
@@ -90,32 +54,6 @@ class Object
        end
 end
 
-class Integer # :nodoc:
-       def to_eet_chunks(tag, type = nil)
-               fmt = case type
-               when :char: "c"
-               when :short: "v"
-               when :long_long: "q"
-               else "V"
-               end
-
-               data = [self].pack(fmt)
-               [Eet::Chunk.new(tag, data)]
-       end
-end
-
-class Float # :nodoc:
-       def to_eet_chunks(tag, type = nil)
-               fmt = case type
-               when :double: "%32.32f"
-               else "%16.16f"
-               end
-
-               data = fmt % self
-               [Eet::Chunk.new(tag, data + "\0")]
-       end
-end
-
 class String # :nodoc:
        def to_eet_chunks(tag, type = nil)
                [Eet::Chunk.new(tag, self + "\0")]
@@ -124,13 +62,13 @@ end
 
 class TrueClass # :nodoc:
        def to_eet_chunks(tag, type = nil)
-               [Eet::Chunk.new(tag, [1].pack("c"))]
+               [Eet::Chunk.new(tag, "\1")]
        end
 end
 
 class FalseClass # :nodoc:
        def to_eet_chunks(tag, type = nil)
-               [Eet::Chunk.new(tag, [0].pack("c"))]
+               [Eet::Chunk.new(tag, "\0")]
        end
 end
 
@@ -154,28 +92,28 @@ class Hash # :nodoc:
 end
 
 module Eet
-       VERSION = "0.0.1"
+       VERSION = "0.1.3"
 
-       class EetError < StandardError; end
-       class NameError < EetError; end
-       class PropertyError < EetError; end
        class ChunkError < EetError; end
 
-       class Stream < Array # :nodoc:
+       class Stream # :nodoc:
                def initialize(chunk = nil)
                        super(chunk.nil? ? 0 : 1, chunk)
                end
 
-               def serialize
-                       inject("") { |a, c| a << c.serialize }
-               end
-
                def Stream.deserialize(data)
-                       data = data.to_str.dup
+                       if data.to_str.empty?
+                               raise(ArgumentError, "buffer is empty")
+                       end
+
                        s = Stream.new
+                       offset = 0
+
+                       while offset < data.length
+                               c, bytes = Chunk.deserialize(data[offset..-1])
 
-                       while data.length > 0
-                               s << Chunk.deserialize(data)
+                               s << c
+                               offset += bytes
                        end
 
                        s
@@ -183,33 +121,11 @@ module Eet
        end
 
        class Chunk # :nodoc:
-               attr_reader :tag, :data
-
-               def initialize(tag, data)
-                       if tag.to_str.include?(0)
-                               raise(ArgumentError,
-                                     "tag must not contain binary zeroes")
+               def Chunk.deserialize(data)
+                       if data.to_str.empty?
+                               raise(ArgumentError, "buffer is empty")
                        end
 
-                       @tag = tag.to_str.dup.freeze
-                       @data = data.to_str.dup.freeze
-
-                       @size = @tag.length + 1 + @data.length
-
-                       # libeet uses a signed 32bit integer to store the
-                       # chunk size, so make sure we don't overflow it
-                       if @size >= (1 << 31)
-                               raise(ArgumentError, "tag or data too long")
-                       end
-               end
-
-               def serialize
-                       buf = "CHnK"
-                       buf << [@size].pack("V")
-                       buf << @tag << "\0" << @data
-               end
-
-               def Chunk.deserialize(data)
                        if data.length < 8 || data[0, 4] != "CHnK"
                                raise(ChunkError, "invalid data")
                        end
@@ -225,9 +141,7 @@ module Eet
 
                        c = Chunk.new(*data[8, size].split("\0", 2))
 
-                       data.replace(data[8 + size..-1] || "")
-
-                       c
+                       [c, 8 + size]
                end
        end
 end