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