codec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/codec.gemspec +22 -0
- data/lib/codec/base.rb +52 -0
- data/lib/codec/exceptions.rb +13 -0
- data/lib/codec/field.rb +82 -0
- data/lib/codec/protocols/core.rb +0 -0
- data/lib/codec/version.rb +3 -0
- data/lib/codec.rb +29 -0
- data/test/lib/codec/version_test.rb +10 -0
- data/test/test_helper.rb +4 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Bernard Rodier
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Codec
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'codec'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install codec
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/codec.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'codec/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.authors = ["Bernard Rodier"]
|
8
|
+
spec.email = ["bernard.rodier@gmail.com"]
|
9
|
+
spec.description = %q{Generic Coder Decoder Tool}
|
10
|
+
spec.summary = %q{This Gem provide class that permit to instantiate Codec to parse or build any protocol}
|
11
|
+
spec.homepage = ""
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.name = "codec"
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
spec.version = Codec::VERSION
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/lib/codec/base.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Codec
|
2
|
+
class Base
|
3
|
+
attr_reader :id
|
4
|
+
def initialize(id,length)
|
5
|
+
@length = length.to_i
|
6
|
+
@id = id
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_field
|
10
|
+
f = Field.new(@id)
|
11
|
+
f.set_value(@data)
|
12
|
+
return f
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse_with_length(buf,length)
|
16
|
+
init_data(buf,length)
|
17
|
+
return build_field,@remain
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse(buf)
|
21
|
+
init_data(buf,@length)
|
22
|
+
return build_field,@remain
|
23
|
+
end
|
24
|
+
|
25
|
+
def init_data(buf,length)
|
26
|
+
length = 0 if length.nil?
|
27
|
+
if(length != 0)
|
28
|
+
if buf.length < length
|
29
|
+
raise BufferUnderflow, "Not enough data for parsing #{@id} (#{length}/#{buf.length})"
|
30
|
+
end
|
31
|
+
@data = buf[0,length]
|
32
|
+
@remain = buf[length,buf.length]
|
33
|
+
else
|
34
|
+
@data = buf
|
35
|
+
@remain = ""
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_subparser(id_field,parser)
|
40
|
+
if parser.nil?
|
41
|
+
raise InitializeException, "Invalid codec reference in subparser #{id_field} for codec #{@id}"
|
42
|
+
end
|
43
|
+
if @subParsers.kind_of? Hash
|
44
|
+
@subParsers[id_field] = parser
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_sub_parsers
|
49
|
+
return @subParsers
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/codec/field.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Codec
|
2
|
+
|
3
|
+
class NilField
|
4
|
+
def nil? ; return true; end
|
5
|
+
def get_id ; "" ; end
|
6
|
+
def get_value ; "" ;end
|
7
|
+
def get_sf_recursivly id ; return self ; end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Field
|
11
|
+
def initialize(id="*")
|
12
|
+
@id = (id.nil? ? "*" : id)
|
13
|
+
@value = ""
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_id ; @id; end
|
17
|
+
|
18
|
+
def set_id id ; @id = id ; end
|
19
|
+
|
20
|
+
def get_value ; @value; end
|
21
|
+
|
22
|
+
def set_value(value)
|
23
|
+
raise "Error can not set value that is instance of Array" if value.kind_of? Array
|
24
|
+
@value = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_sub_field(sf)
|
28
|
+
@value = [] if @value == ""
|
29
|
+
raise "Add impossible on not Array valued field" unless @value.kind_of? Array
|
30
|
+
@value << [sf.get_id,sf]
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_sf_recursivly(ids)
|
34
|
+
if(ids.size == 1 && @value.kind_of?(Array))
|
35
|
+
return get_sub_field(ids.first)
|
36
|
+
elsif (ids.size > 1 && @value.kind_of?(Array))
|
37
|
+
id = ids.slice!(0)
|
38
|
+
return get_sub_field(id).get_sf_recursivly(ids)
|
39
|
+
else
|
40
|
+
return NilField.new
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_deep_field(path,separator='.')
|
45
|
+
get_sf_recursivly(path.split(separator))
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_sub_field(id)
|
49
|
+
sf_rec = get_sub_fields(id)
|
50
|
+
if sf_rec.nil?
|
51
|
+
return NilField.new
|
52
|
+
elsif sf_rec.size > 1
|
53
|
+
raise MultipleFieldError
|
54
|
+
else
|
55
|
+
return sf_rec.first
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_sub_fields(id)
|
60
|
+
raise "Error No Subfield" unless @value.kind_of? Array
|
61
|
+
sf_rec = @value.select{|v| v.first == id}.collect{|v| v.last}
|
62
|
+
if sf_rec == []
|
63
|
+
return NilField.new
|
64
|
+
else
|
65
|
+
return sf_rec
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_yaml(tab="")
|
70
|
+
if @value.kind_of? Array
|
71
|
+
s = tab + @id +": \n"
|
72
|
+
tab += " "
|
73
|
+
@value.each{|v|
|
74
|
+
s += v.last.to_yaml(tab)
|
75
|
+
}
|
76
|
+
return s
|
77
|
+
else
|
78
|
+
tab + @id + ": " + @value.to_s + "\n"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
File without changes
|
data/lib/codec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
require 'codec/version'
|
3
|
+
require 'codec/fields'
|
4
|
+
require 'codec/exceptions'
|
5
|
+
require 'codec/base'
|
6
|
+
# require 'codec/fix'
|
7
|
+
# require 'codec/packed'
|
8
|
+
# require 'codec/prefixed'
|
9
|
+
# require 'codec/composed'
|
10
|
+
|
11
|
+
|
12
|
+
module Codec
|
13
|
+
|
14
|
+
Logger = Log4r::Logger.new 'parserLog'
|
15
|
+
Logger.outputters = Log4r::Outputter.stderr
|
16
|
+
Logger.level=Log4r::INFO
|
17
|
+
protocols = []
|
18
|
+
|
19
|
+
if defined?(CODEC_CONST).nil?
|
20
|
+
CODEC_CONST = true
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def self.register_protocol(ProtocolClass)
|
25
|
+
protocols << ProtocolClass
|
26
|
+
return protocols
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bernard Rodier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Generic Coder Decoder Tool
|
47
|
+
email:
|
48
|
+
- bernard.rodier@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- codec.gemspec
|
59
|
+
- lib/codec.rb
|
60
|
+
- lib/codec/base.rb
|
61
|
+
- lib/codec/exceptions.rb
|
62
|
+
- lib/codec/field.rb
|
63
|
+
- lib/codec/protocols/core.rb
|
64
|
+
- lib/codec/version.rb
|
65
|
+
- test/lib/codec/version_test.rb
|
66
|
+
- test/test_helper.rb
|
67
|
+
homepage: ''
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: This Gem provide class that permit to instantiate Codec to parse or build
|
92
|
+
any protocol
|
93
|
+
test_files:
|
94
|
+
- test/lib/codec/version_test.rb
|
95
|
+
- test/test_helper.rb
|