Removed Eet::File#list.
[ruby-eet.git] / lib / eet.rb
1 #--
2 # Copyright (c) 2005-2007 Tilman Sauerbeck (tilman at code-monkey de)
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 require "eet_ext"
24
25 class Object
26         def to_eet_chunks(tag, type = nil) # :nodoc:
27                 [Eet::Chunk.new(tag, to_eet)]
28         end
29
30         protected
31
32         # :call-seq:
33         #  object.to_eet_name -> string
34         #
35         # Returns the tag that's stored with the data for _object_.
36         # If your class doesn't override this method, the class name will be
37         # used.
38         def to_eet_name
39                 self.class.name
40         end
41
42         # :call-seq:
43         #  object.to_eet_properties -> hash
44         #
45         # Returns a hash that contains the properties that are stored for
46         # _object_.
47         # If your class doesn't override this method, all instance variables
48         # of _object_ will be stored.
49         def to_eet_properties
50                 instance_variables.inject({}) do |h, var|
51                         h[var[1..-1]] = [instance_variable_get(var)]
52                         h
53                 end
54         end
55 end
56
57 class String # :nodoc:
58         def to_eet_chunks(tag, type = nil)
59                 [Eet::Chunk.new(tag, self + "\0")]
60         end
61 end
62
63 class TrueClass # :nodoc:
64         def to_eet_chunks(tag, type = nil)
65                 [Eet::Chunk.new(tag, "\1")]
66         end
67 end
68
69 class FalseClass # :nodoc:
70         def to_eet_chunks(tag, type = nil)
71                 [Eet::Chunk.new(tag, "\0")]
72         end
73 end
74
75 class Array # :nodoc:
76         def to_eet_chunks(tag, type = nil)
77                 case type
78                 when :sub
79                         [Eet::Chunk.new(tag, self.to_eet)]
80                 else
81                         # lists always hold subtypes
82                         map { |item| Eet::Chunk.new(tag, item.to_eet) }
83                 end
84         end
85 end
86
87 class Hash # :nodoc:
88         def to_eet_chunks(tag, type = nil)
89                 # lists always hold subtypes
90                 map { |(key, value)| Eet::Chunk.new(tag, value.to_eet) }
91         end
92 end
93
94 module Eet
95         VERSION = "0.1.3"
96
97         class ChunkError < EetError; end
98
99         class Stream # :nodoc:
100                 def initialize(chunk = nil)
101                         super(chunk.nil? ? 0 : 1, chunk)
102                 end
103
104                 def Stream.deserialize(data)
105                         if data.to_str.empty?
106                                 raise(ArgumentError, "buffer is empty")
107                         end
108
109                         s = Stream.new
110                         offset = 0
111
112                         while offset < data.length
113                                 c, bytes = Chunk.deserialize(data[offset..-1])
114
115                                 s << c
116                                 offset += bytes
117                         end
118
119                         s
120                 end
121         end
122
123         class Chunk # :nodoc:
124                 def Chunk.deserialize(data)
125                         if data.to_str.empty?
126                                 raise(ArgumentError, "buffer is empty")
127                         end
128
129                         if data.length < 8 || data[0, 4] != "CHnK"
130                                 raise(ChunkError, "invalid data")
131                         end
132
133                         size = data[4, 4].unpack("V").first
134                         if size >= (1 << 31) || size > data.length - 8
135                                 raise(ChunkError, "invalid chunk size")
136                         end
137
138                         unless data[8, size].include?(0)
139                                 raise(ChunkError, "invalid chunk data")
140                         end
141
142                         c = Chunk.new(*data[8, size].split("\0", 2))
143
144                         [c, 8 + size]
145                 end
146         end
147 end