Re-implemented Object#to_eet in C.
[ruby-eet.git] / lib / eet.rb
1 #--
2 # $Id: eet.rb 49 2005-05-30 19:52: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 Integer # :nodoc:
60         def to_eet_chunks(tag, type = nil)
61                 fmt = case type
62                 when :char: "c"
63                 when :short: "v"
64                 when :long_long: "q"
65                 else "V"
66                 end
67
68                 data = [self].pack(fmt)
69                 [Eet::Chunk.new(tag, data)]
70         end
71 end
72
73 class Float # :nodoc:
74         def to_eet_chunks(tag, type = nil)
75                 fmt = case type
76                 when :double: "%32.32f"
77                 else "%16.16f"
78                 end
79
80                 data = fmt % self
81                 [Eet::Chunk.new(tag, data + "\0")]
82         end
83 end
84
85 class String # :nodoc:
86         def to_eet_chunks(tag, type = nil)
87                 [Eet::Chunk.new(tag, self + "\0")]
88         end
89 end
90
91 class TrueClass # :nodoc:
92         def to_eet_chunks(tag, type = nil)
93                 [Eet::Chunk.new(tag, "\1")]
94         end
95 end
96
97 class FalseClass # :nodoc:
98         def to_eet_chunks(tag, type = nil)
99                 [Eet::Chunk.new(tag, "\0")]
100         end
101 end
102
103 class Array # :nodoc:
104         def to_eet_chunks(tag, type = nil)
105                 case type
106                 when :sub
107                         [Eet::Chunk.new(tag, self.to_eet)]
108                 else
109                         # lists always hold subtypes
110                         map { |item| Eet::Chunk.new(tag, item.to_eet) }
111                 end
112         end
113 end
114
115 class Hash # :nodoc:
116         def to_eet_chunks(tag, type = nil)
117                 # lists always hold subtypes
118                 map { |(key, value)| Eet::Chunk.new(tag, value.to_eet) }
119         end
120 end
121
122 module Eet
123         VERSION = "0.1.2"
124
125         class ChunkError < EetError; end
126
127         class Stream # :nodoc:
128                 def initialize(chunk = nil)
129                         super(chunk.nil? ? 0 : 1, chunk)
130                 end
131
132                 def Stream.deserialize(data)
133                         if data.to_str.empty?
134                                 raise(ArgumentError, "buffer is empty")
135                         end
136
137                         s = Stream.new
138                         offset = 0
139
140                         while offset < data.length
141                                 c, bytes = Chunk.deserialize(data[offset..-1])
142
143                                 s << c
144                                 offset += bytes
145                         end
146
147                         s
148                 end
149         end
150
151         class Chunk # :nodoc:
152                 def Chunk.deserialize(data)
153                         if data.to_str.empty?
154                                 raise(ArgumentError, "buffer is empty")
155                         end
156
157                         if data.length < 8 || data[0, 4] != "CHnK"
158                                 raise(ChunkError, "invalid data")
159                         end
160
161                         size = data[4, 4].unpack("V").first
162                         if size >= (1 << 31) || size > data.length - 8
163                                 raise(ChunkError, "invalid chunk size")
164                         end
165
166                         unless data[8, size].include?(0)
167                                 raise(ChunkError, "invalid chunk data")
168                         end
169
170                         c = Chunk.new(*data[8, size].split("\0", 2))
171
172                         [c, 8 + size]
173                 end
174         end
175 end