protobuffer 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Danopia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = protobuffer
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Daniel Danopia. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "protobuffer"
8
+ gem.summary = %Q{Parser/encoder for Google's ProtoBuffer system, with DSL-ish syntax}
9
+ gem.description = %Q{ProtoBuffer::Converter can be inherited by a class and then multiple `structure` blocks define each structure, and trees can be made. For a sample usage, look at http://wiki.github.com/danopia/protobuffer}
10
+ gem.email = "me.github@danopia.net"
11
+ gem.homepage = "http://github.com/danopia/protobuffer"
12
+ gem.authors = ["Daniel Danopia"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "protobuffer #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,221 @@
1
+ require 'stringio'
2
+
3
+ # Custom Ruby ProtoBuffer implementation with a DSL-like structure syntax. I'll
4
+ # document it later.
5
+ module ProtoBuffer
6
+ class Converter
7
+
8
+ ####################
9
+ # Decoding
10
+ ####################
11
+
12
+ def self.parse(structure, data)
13
+ data = StringIO.new(data)
14
+ hash = {}
15
+ structure = structures[structure] if structure.is_a? Symbol
16
+
17
+ parse_field hash, data, structure until data.eof?
18
+
19
+ hash
20
+ end
21
+
22
+ def self.parse_field(parent_args, data, structure)
23
+ key = data.getc / 8
24
+ label = nil
25
+ value = nil
26
+
27
+ type = substructure = structure[key]
28
+ type = type.first if type.is_a? Array
29
+
30
+ unless type
31
+ puts "UNEXPECTED KEY: #{key} (expected one of #{structure.keys.join(', ')})"
32
+ return
33
+ end
34
+
35
+ if type.is_a? Param
36
+ label = type.label
37
+ type = type.type
38
+ end
39
+
40
+ if type == :varint
41
+ value = read_varint(data)
42
+
43
+ elsif type == :boolean
44
+ value = read_varint(data) == 1
45
+
46
+ elsif type == :string
47
+ value = read_string(data)
48
+
49
+ elsif type.is_a?(Hash) || type.is_a?(Symbol)
50
+ if type.is_a? Symbol
51
+ label ||= type
52
+ type = structures[type]
53
+ end
54
+
55
+ value = {}
56
+ raw = StringIO.new(read_string(data))
57
+ parse_field value, raw, type until raw.eof?
58
+
59
+ else
60
+ puts "Unknown type: #{type}"
61
+ return
62
+ end
63
+
64
+ label ||= key
65
+ if substructure.is_a? Array
66
+ parent_args[label] ||= []
67
+ parent_args[label] << value
68
+ else
69
+ puts "Overwritting a key!" if parent_args[key]
70
+ parent_args[label] = value
71
+ end
72
+ end
73
+
74
+ def self.read_varint(io)
75
+ index = 0
76
+ value = 0
77
+ while true
78
+ byte = io.getc
79
+ if byte & 0x80 > 0
80
+ value |= (byte & 0x7F) << index
81
+ index += 7
82
+ else
83
+ return value | byte << index
84
+ end
85
+ end
86
+ end
87
+
88
+ def self.read_string(io)
89
+ io.read read_varint(io)
90
+ end
91
+
92
+ ####################
93
+ # Encoding
94
+ ####################
95
+
96
+ def self.encode(structure, hash)
97
+ structure = reverse_structures[structure] if structure.is_a? Symbol
98
+
99
+ hash.to_a.map do |pair|
100
+ output = ''
101
+ type, value = pair
102
+ value = [value] unless value.is_a? Array
103
+ key = type
104
+
105
+ substructure = structure[type]
106
+ substructure = substructure.first if substructure.is_a? Array
107
+
108
+ if substructure.is_a? Param
109
+ key = substructure.index
110
+ substructure = substructure.type
111
+ end
112
+
113
+ if substructure.is_a?(Symbol) && !([:varint, :string, :boolean].include?(substructure))
114
+ substructure = reverse_structures[substructure]
115
+ end
116
+
117
+ value.each do |arg|
118
+ if substructure == :varint || arg.is_a?(Fixnum) || arg.is_a?(Bignum)
119
+ output << (key*8).chr
120
+ output << write_varint(arg.to_i)
121
+
122
+ elsif substructure == :boolean || arg.is_a?(TrueClass) || arg.is_a?(FalseClass)
123
+ output << (key*8).chr
124
+ output << write_varint(arg ? 1 : 0)
125
+
126
+ elsif substructure == :string || arg.is_a?(String)
127
+ output << (key*8+2).chr
128
+ write_string output, arg
129
+
130
+ elsif substructure.is_a?(Hash) || substructure.is_a?(Symbol)
131
+ if substructure.is_a? Symbol
132
+ substructure = structures[substructure]
133
+ end
134
+
135
+ output << (key*8+2).chr
136
+ write_string output, encode(substructure, arg)
137
+
138
+ else
139
+ puts "Unknown type: #{type}"
140
+ return
141
+ end
142
+ end
143
+ output
144
+ end.sort{|a, b| a[0] <=> b[0]}.join('')
145
+ end
146
+
147
+ def self.write_varint(value)
148
+ bytes = ''
149
+ while value > 0x7F
150
+ bytes << ((value & 0x7F) | 0x80).chr
151
+ value >>= 7
152
+ end
153
+ bytes << value.chr
154
+ end
155
+
156
+ def self.write_string(io, string)
157
+ io << write_varint(string.size) << string
158
+ end
159
+
160
+ ####################
161
+ # Structure helpers
162
+ ####################
163
+
164
+ class Param
165
+ attr_accessor :type, :label, :index
166
+
167
+ def initialize(type, label, index=nil)
168
+ @type = type
169
+ @label = label
170
+ @index = index
171
+ end
172
+ end
173
+
174
+ def self.structures
175
+ @structures ||= {}
176
+ end
177
+
178
+ def self.reverse_structures
179
+ @reverse_structures ||= {}
180
+ end
181
+
182
+ # Yay ugly
183
+ def self.structure(key, structure)
184
+ structure.each_pair do |index, type|
185
+ type = type.first if type.is_a? Array
186
+ type.index = index if type.is_a? Param
187
+ end
188
+
189
+ structures[key] = structure
190
+
191
+ reverse = {}
192
+ structure.each_pair do |key2, type|
193
+ reverse[key2] = type # so that indexes still work
194
+
195
+ type2 = type
196
+ type2 = type.first if type.is_a? Array
197
+
198
+ label = key2
199
+ if type2.is_a? Param
200
+ if type2.label
201
+ label = type2.label
202
+ elsif type.type.is_a? Symbol
203
+ label = type2.type
204
+ end
205
+ elsif type2.is_a?(Symbol) && !([:varint, :string, :boolean].include?(type2))
206
+ label = type2
207
+ end
208
+
209
+ reverse[label] = type
210
+ end
211
+
212
+ reverse_structures[key] = reverse
213
+ end
214
+
215
+ def self.method_missing(type, label=nil)
216
+ label ||= type if type.is_a? Symbol
217
+ Param.new(type, label)
218
+ end
219
+
220
+ end
221
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'protobuffer'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,8 @@
1
+ require 'helper'
2
+
3
+ class TestProtobuffer < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ #pass
6
+ # flunk "hey buddy, you should probably rename this file and start testing for real"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: protobuffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Danopia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: ProtoBuffer::Converter can be inherited by a class and then multiple `structure` blocks define each structure, and trees can be made. For a sample usage, look at http://wiki.github.com/danopia/protobuffer
26
+ email: me.github@danopia.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/protobuffer.rb
42
+ - test/helper.rb
43
+ - test/test_protobuffer.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/danopia/protobuffer
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Parser/encoder for Google's ProtoBuffer system, with DSL-ish syntax
72
+ test_files:
73
+ - test/helper.rb
74
+ - test/test_protobuffer.rb