rxraw-lineparser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rxraw-lineparser.rb +89 -0
  2. metadata +56 -0
@@ -0,0 +1,89 @@
1
+ #�!/usr/bin/env ruby
2
+
3
+ # file: rxraw-lineparser.rb
4
+
5
+ class RXRawLineParser
6
+
7
+ def self.parse(format_mask, line)
8
+
9
+ field_names = format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
10
+
11
+ patterns = possible_patterns(format_mask)
12
+ pattern = patterns.detect {|x| line.match(/#{x.join}/)}.join
13
+ field_values = line.match(/#{pattern}/).captures
14
+
15
+ found_quotes = find_qpattern(pattern)
16
+
17
+ if found_quotes then
18
+ found_quotes.each {|i| field_values[i] = field_values[i][1..-2]}
19
+ end
20
+
21
+ field_values += [''] * (field_names.length - field_values.length)
22
+
23
+ [field_names, field_values]
24
+ end
25
+
26
+ def possible_patterns(format_mask)
27
+
28
+ tot_fields = format_mask.scan(/\[!\w+\]/).length
29
+ return [['(.*)']] if tot_fields <= 1
30
+ main_fields = tot_fields - 1
31
+ qpattern = %q{(["'][^"']+["'])}
32
+
33
+ a = fmask_delimiters(format_mask)
34
+ r = diminishing_permutation(main_fields)
35
+
36
+ if r.length > 2 then
37
+ itemx = [r.slice!(-2)]
38
+ r2 = r[0..-3] + itemx + r[-2..-1]
39
+ else
40
+ r2 = r
41
+ end
42
+
43
+ rr = r2.map do |x|
44
+ x.each_with_index.map do |item, i|
45
+ d = a[i]
46
+ case item
47
+ when 1
48
+ i > 0 ? d + qpattern : qpattern
49
+ when 0
50
+ s = "([^%s]+)" % d
51
+ i > 0 ? d + s : s
52
+ end
53
+ end
54
+ end
55
+
56
+ count = 2**main_fields
57
+ rr2 = rr.take(count).map {|x| x + [a[-1] + '(.*)']}
58
+ wild_r = rr2.slice!(-1)
59
+
60
+ rrr = rr2 + rr[0..count-2] + [wild_r] + rr[count-1..-1]
61
+
62
+ end
63
+
64
+ def diminishing_permutation(max_fields)
65
+ result = max_fields.times.inject([]) do |r,i|
66
+ r + [1,0].repeated_permutation(max_fields-i).to_a
67
+ end
68
+ end
69
+
70
+ def find_qpattern(s)
71
+ s.split(/(?=\([^\)]+\))/).map.with_index\
72
+ .select{|x,i| x[/\["'\]\[\^"'\]\+\["'\]/] }.map(&:last)
73
+ end
74
+
75
+ def fmask_delimiters(f)
76
+ a = f.split(/(?=\[!\w+\])/)[0..-2].map do |x|
77
+
78
+ aa = x.split(/(?=[^\]]+$)/)
79
+
80
+ if aa.length == 2 and aa.first[/\[!\w+\]/] then
81
+ field, delimiter = *aa
82
+ delimiter ||= '$'
83
+ d = delimiter[0]
84
+ d
85
+ end
86
+ end
87
+ end
88
+
89
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rxraw-lineparser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - James Robertson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-04-18 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/rxraw-lineparser.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.5.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: rxraw-lineparser
55
+ test_files: []
56
+