jls-grok 0.2.3104 → 0.3.3209

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/grok.gemspec CHANGED
@@ -6,22 +6,15 @@ Gem::Specification.new do |spec|
6
6
  examples/grok-web.rb
7
7
  examples/pattern-discovery.rb
8
8
  examples/test.rb
9
- ext
10
- ext/extconf.rb
11
- ext/rgrok.h
12
- ext/ruby_grok.c
13
- ext/ruby_grokdiscover.c
14
- ext/ruby_grokmatch.c
15
- ext/ruby_grokmatch.h
16
9
  grok.gemspec
17
10
  lib
18
11
  lib/grok
19
12
  lib/grok.rb
13
+ lib/grok/match.rb
20
14
  lib/grok/pile.rb
21
15
  test
22
16
  test/Makefile
23
17
  test/alltests.rb
24
- test/core
25
18
  test/general
26
19
  test/general/basic_test.rb
27
20
  test/general/captures_test.rb
@@ -45,16 +38,16 @@ Gem::Specification.new do |spec|
45
38
 
46
39
  svnrev = %x{svn info}.split("\n").grep(/Revision:/).first.split(" ").last.to_i
47
40
  spec.name = "jls-grok"
48
- spec.version = "0.2.#{svnrev}"
41
+ spec.version = "0.3.#{svnrev}"
49
42
 
50
43
  spec.summary = "grok bindings for ruby"
51
44
  spec.description = "Grok ruby bindings - pattern match/extraction tool"
52
45
  spec.files = files
53
- spec.require_paths << "ext"
54
- spec.extensions = ["ext/extconf.rb"]
55
46
 
56
- spec.author = "Jordan Sissel"
57
- spec.email = "jls@semicomplete.com"
47
+ spec.add_dependency("ffi", ">= 1.0.5")
48
+
49
+ spec.authors = ["Jordan Sissel", "Pete Fritchman"]
50
+ spec.email = ["jls@semicomplete.com", "petef@databits.net"]
58
51
  spec.homepage = "http://code.google.com/p/semicomplete/wiki/Grok"
59
52
  end
60
53
 
data/lib/grok/match.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "rubygems"
2
+ require "ffi"
3
+ require "grok"
4
+
5
+ class Grok::Match < FFI::Struct
6
+ module CGrokMatch
7
+ extend FFI::Library
8
+ ffi_lib "libgrok.so"
9
+
10
+ attach_function :grok_match_get_named_substring,
11
+ [:pointer, :pointer], :pointer
12
+ attach_function :grok_match_walk_init, [:pointer], :void
13
+ attach_function :grok_match_walk_next,
14
+ [:pointer, :pointer, :pointer, :pointer, :pointer], :int
15
+ attach_function :grok_match_walk_end, [:pointer], :void
16
+ end
17
+
18
+ include CGrokMatch
19
+ layout :grok, :pointer,
20
+ :subject, :string,
21
+ :start, :int,
22
+ :end, :int
23
+
24
+ public
25
+ def initialize
26
+ super
27
+
28
+ @captures = nil
29
+ end
30
+
31
+ private
32
+ def _get_captures
33
+ @captures = Hash.new { |h, k| h[k] = Array.new }
34
+ grok_match_walk_init(self)
35
+ name_ptr = FFI::MemoryPointer.new(:pointer)
36
+ namelen_ptr = FFI::MemoryPointer.new(:int)
37
+ data_ptr = FFI::MemoryPointer.new(:pointer)
38
+ datalen_ptr = FFI::MemoryPointer.new(:int)
39
+ while grok_match_walk_next(self, name_ptr, namelen_ptr, data_ptr, datalen_ptr) == Grok::GROK_OK
40
+ namelen = namelen_ptr.read_int
41
+ name = name_ptr.get_pointer(0).get_string(0, namelen)
42
+ datalen = datalen_ptr.read_int
43
+ data = data_ptr.get_pointer(0).get_string(0, datalen)
44
+ @captures[name] << data
45
+ end
46
+ grok_match_walk_end(self)
47
+ end
48
+
49
+ public
50
+ def captures
51
+ _get_captures if @captures.nil?
52
+ return @captures
53
+ end
54
+
55
+ public
56
+ def start
57
+ return self[:start]
58
+ end
59
+
60
+ public
61
+ def end
62
+ return self[:end]
63
+ end
64
+
65
+ public
66
+ def subject
67
+ return self[:subject]
68
+ end
69
+
70
+ public
71
+ def each_capture
72
+ _get_captures if @captures.nil?
73
+ @captures.each { |k, v| yield([k, v]) }
74
+ end
75
+ end # Grok::Match
data/lib/grok/pile.rb CHANGED
@@ -2,53 +2,55 @@ require "grok"
2
2
 
3
3
  # A grok pile is an easy way to have multiple patterns together so
4
4
  # that you can try to match against each one.
5
- # The API provided should be similar to the normal Grok
5
+ # The API provided should be similar to the normal Grok
6
6
  # interface, but you can compile multiple patterns and match will
7
7
  # try each one until a match is found.
8
- class Grok::Pile
9
- def initialize
10
- @groks = []
11
- @patterns = {}
12
- @pattern_files = []
13
- end # def initialize
8
+ class Grok
9
+ class Pile
10
+ def initialize
11
+ @groks = []
12
+ @patterns = {}
13
+ @pattern_files = []
14
+ end # def initialize
14
15
 
15
- # see Grok#add_pattern
16
- def add_pattern(name, string)
17
- @patterns[name] = string
18
- end # def add_pattern
16
+ # see Grok#add_pattern
17
+ def add_pattern(name, string)
18
+ @patterns[name] = string
19
+ end # def add_pattern
19
20
 
20
- # see Grok#add_patterns_from_file
21
- def add_patterns_from_file(path)
22
- if !File.exists?(path)
23
- raise "File does not exist: #{path}"
24
- end
25
- @pattern_files << path
26
- end # def add_patterns_from_file
21
+ # see Grok#add_patterns_from_file
22
+ def add_patterns_from_file(path)
23
+ if !File.exists?(path)
24
+ raise "File does not exist: #{path}"
25
+ end
26
+ @pattern_files << path
27
+ end # def add_patterns_from_file
27
28
 
28
- # see Grok#compile
29
- def compile(pattern)
30
- grok = Grok.new
31
- @patterns.each do |name, value|
32
- grok.add_patterne(name, value)
33
- end
34
- @pattern_files.each do |path|
35
- grok.add_patterns_from_file(path)
36
- end
37
- grok.compile(pattern)
38
- @groks << grok
39
- end # def compile
29
+ # see Grok#compile
30
+ def compile(pattern)
31
+ grok = Grok.new
32
+ @patterns.each do |name, value|
33
+ grok.add_patterne(name, value)
34
+ end
35
+ @pattern_files.each do |path|
36
+ grok.add_patterns_from_file(path)
37
+ end
38
+ grok.compile(pattern)
39
+ @groks << grok
40
+ end # def compile
40
41
 
41
- # Slight difference from Grok#match in that it returns
42
- # the Grok instance that matched successfully in addition
43
- # to the GrokMatch result.
44
- # See also: Grok#match
45
- def match(string)
46
- @groks.each do |grok|
47
- match = grok.match(string)
48
- if match
49
- return [grok, match]
42
+ # Slight difference from Grok#match in that it returns
43
+ # the Grok instance that matched successfully in addition
44
+ # to the GrokMatch result.
45
+ # See also: Grok#match
46
+ def match(string)
47
+ @groks.each do |grok|
48
+ match = grok.match(string)
49
+ if match
50
+ return [grok, match]
51
+ end
50
52
  end
51
- end
52
- return false
53
- end # def match
54
- end # class Grok::Pile
53
+ return false
54
+ end # def match
55
+ end # class Pile
56
+ end # class Grok
data/lib/grok.rb CHANGED
@@ -1,8 +1,107 @@
1
- require "Grok"
2
- require "grok/pile"
1
+ require "rubygems"
2
+ require "ffi"
3
+
4
+ class Grok < FFI::Struct
5
+ module CGrok
6
+ extend FFI::Library
7
+ ffi_lib "libgrok.so"
8
+
9
+ attach_function :grok_new, [], :pointer
10
+ attach_function :grok_compilen, [:pointer, :pointer, :int], :int
11
+ attach_function :grok_pattern_add,
12
+ [:pointer, :pointer, :int, :pointer, :int], :int
13
+ attach_function :grok_patterns_import_from_file, [:pointer, :pointer], :int
14
+ attach_function :grok_execn, [:pointer, :pointer, :int, :pointer], :int
15
+ end
16
+
17
+ include CGrok
18
+ layout :pattern, :string,
19
+ :pattern_len, :int,
20
+ :full_pattern, :string,
21
+ :full_pattern_len, :int,
22
+ :__patterns, :pointer, # TCTREE*, technically
23
+ :__re, :pointer, # pcre*
24
+ :__pcre_capture_vector, :pointer, # int*
25
+ :__pcre_num_captures, :int,
26
+ :__captures_by_id, :pointer, # TCTREE*
27
+ :__captures_by_name, :pointer, # TCTREE*
28
+ :__captures_by_subname, :pointer, # TCTREE*
29
+ :__captures_by_capture_number, :pointer, # TCTREE*
30
+ :__max_capture_num, :int,
31
+ :pcre_errptr, :string,
32
+ :pcre_erroffset, :int,
33
+ :pcre_errno, :int,
34
+ :logmask, :uint,
35
+ :logdepth, :uint,
36
+ :errstr, :string
37
+
38
+ GROK_OK = 0
39
+ GROK_ERROR_FILE_NOT_ACCESSIBLE = 1
40
+ GROK_ERROR_PATTERN_NOT_FOUND = 2
41
+ GROK_ERROR_UNEXPECTED_READ_SIZE = 3
42
+ GROK_ERROR_COMPILE_FAILED = 4
43
+ GROK_ERROR_UNINITIALIZED = 5
44
+ GROK_ERROR_PCRE_ERROR = 6
45
+ GROK_ERROR_NOMATCH = 7
46
+
47
+ public
48
+ def initialize
49
+ super(grok_new)
50
+ end
51
+
52
+ public
53
+ def add_pattern(name, pattern)
54
+ name_c = FFI::MemoryPointer.from_string(name)
55
+ pattern_c = FFI::MemoryPointer.from_string(pattern)
56
+ grok_pattern_add(self, name_c, name.length, pattern_c, pattern.length)
57
+ return nil
58
+ end
59
+
60
+ public
61
+ def add_patterns_from_file(path)
62
+ path_c = FFI::MemoryPointer.from_string(path)
63
+ ret = grok_patterns_import_from_file(self, path_c)
64
+ if ret != GROK_OK
65
+ raise ArgumentError, "Failed to add patterns from file #{path}"
66
+ end
67
+ return nil
68
+ end
69
+
70
+ public
71
+ def pattern
72
+ return self[:pattern]
73
+ end
74
+
75
+ public
76
+ def expanded_pattern
77
+ return self[:full_pattern]
78
+ end
79
+
80
+ public
81
+ def compile(pattern)
82
+ pattern_c = FFI::MemoryPointer.from_string(pattern)
83
+ ret = grok_compilen(self, pattern_c, pattern.length)
84
+ if ret != GROK_OK
85
+ raise ArgumentError, "Compile failed: #{self[:errstr]})"
86
+ end
87
+ return ret
88
+ end
89
+
90
+ public
91
+ def match(text)
92
+ match = Grok::Match.new
93
+ text_c = FFI::MemoryPointer.from_string(text)
94
+ rc = grok_execn(self, text_c, text.size, match)
95
+ case rc
96
+ when GROK_OK
97
+ return match
98
+ when GROK_ERROR_NOMATCH
99
+ return false
100
+ end
101
+
102
+ raise ValueError, "unknown return from grok_execn: #{rc}"
103
+ end
3
104
 
4
- # extend Grok to add simpler access to the discover feature.
5
- class Grok
6
105
  public
7
106
  def discover(input)
8
107
  init_discover if @discover == nil
@@ -15,4 +114,7 @@ class Grok
15
114
  @discover = GrokDiscover.new(self)
16
115
  @discover.logmask = logmask
17
116
  end
18
- end
117
+ end # Grok
118
+
119
+ require "grok/match"
120
+ require "grok/pile"
data/test/Makefile CHANGED
@@ -2,6 +2,8 @@
2
2
  .PHONY: test
3
3
  test:
4
4
  $(MAKE) -C ../../ libgrok.so
5
- [ -f "../Makefile" ] || (cd ../ext; ruby extconf.rb)
6
- $(MAKE) -C ../ext Grok.so
7
- LD_LIBRARY_PATH="$${LD_LIBRARY_PATH}:$$PWD/../../" RUBYLIB="$$PWD/../ext" ruby alltests.rb
5
+ LD_LIBRARY_PATH="$${LD_LIBRARY_PATH}:$$PWD/../../" RUBYLIB="$$PWD/../lib" ruby alltests.rb
6
+
7
+ test_jruby:
8
+ $(MAKE) -C ../../ libgrok.so
9
+ LD_LIBRARY_PATH="$${LD_LIBRARY_PATH}:$$PWD/../../" RUBYLIB="$$PWD/../lib" jruby alltests.rb
@@ -24,7 +24,7 @@ class GrokPatternCapturingTests < Test::Unit::TestCase
24
24
  input = "hello world"
25
25
  match = @grok.match(input)
26
26
  assert_equal("(?<0000>.*)", @grok.expanded_pattern)
27
- assert_kind_of(GrokMatch, match)
27
+ assert_kind_of(Grok::Match, match)
28
28
  assert_kind_of(Hash, match.captures)
29
29
  assert_equal(match.captures.length, 1)
30
30
  assert_kind_of(Array, match.captures["foo"])
metadata CHANGED
@@ -1,30 +1,48 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jls-grok
3
3
  version: !ruby/object:Gem::Version
4
- hash: 6231
5
- prerelease: false
4
+ hash: 6401
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 3104
10
- version: 0.2.3104
8
+ - 3
9
+ - 3209
10
+ version: 0.3.3209
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jordan Sissel
14
+ - Pete Fritchman
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-11-15 00:00:00 -08:00
19
+ date: 2011-02-14 00:00:00 -08:00
19
20
  default_executable:
20
- dependencies: []
21
-
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: ffi
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 29
31
+ segments:
32
+ - 1
33
+ - 0
34
+ - 5
35
+ version: 1.0.5
36
+ type: :runtime
37
+ version_requirements: *id001
22
38
  description: Grok ruby bindings - pattern match/extraction tool
23
- email: jls@semicomplete.com
39
+ email:
40
+ - jls@semicomplete.com
41
+ - petef@databits.net
24
42
  executables: []
25
43
 
26
- extensions:
27
- - ext/extconf.rb
44
+ extensions: []
45
+
28
46
  extra_rdoc_files: []
29
47
 
30
48
  files:
@@ -33,18 +51,12 @@ files:
33
51
  - examples/grok-web.rb
34
52
  - examples/pattern-discovery.rb
35
53
  - examples/test.rb
36
- - ext/extconf.rb
37
- - ext/rgrok.h
38
- - ext/ruby_grok.c
39
- - ext/ruby_grokdiscover.c
40
- - ext/ruby_grokmatch.c
41
- - ext/ruby_grokmatch.h
42
54
  - grok.gemspec
43
55
  - lib/grok.rb
56
+ - lib/grok/match.rb
44
57
  - lib/grok/pile.rb
45
58
  - test/Makefile
46
59
  - test/alltests.rb
47
- - test/core
48
60
  - test/general/basic_test.rb
49
61
  - test/general/captures_test.rb
50
62
  - test/patterns/day.rb
@@ -69,7 +81,6 @@ rdoc_options: []
69
81
 
70
82
  require_paths:
71
83
  - lib
72
- - ext
73
84
  required_ruby_version: !ruby/object:Gem::Requirement
74
85
  none: false
75
86
  requirements:
@@ -91,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
102
  requirements: []
92
103
 
93
104
  rubyforge_project:
94
- rubygems_version: 1.3.7
105
+ rubygems_version: 1.5.1
95
106
  signing_key:
96
107
  specification_version: 3
97
108
  summary: grok bindings for ruby
data/ext/extconf.rb DELETED
@@ -1,21 +0,0 @@
1
- require "mkmf"
2
-
3
- # TODO(sissel): I don't think we need these two headers.
4
- #if !find_header("tcutil.h", "/usr/local/include")
5
- #if !find_header("pcre.h", "/usr/local/include")
6
-
7
- if !find_header("grok.h", "/usr/local/include", "../../", "/usr/include")
8
- raise "Could not find grok.h"
9
- end
10
-
11
- if !find_library("grok", "grok_init", "../", "../../", "/usr/local/lib", "/usr/lib")
12
- raise "Could not find libgrok, is it installed?"
13
- end
14
-
15
- create_makefile("Grok")
16
-
17
- #with_cflags("-g") do
18
- #with_ldflags("-g") do
19
- #create_makefile("Grok")
20
- #end
21
- #end
data/ext/rgrok.h DELETED
@@ -1,9 +0,0 @@
1
- #ifndef _RGROK_H_
2
- #define _RGROK_H_
3
- #include <ruby.h>
4
- #include <grok.h>
5
-
6
- extern VALUE rGrok_new(VALUE klass);
7
- extern VALUE rGrok_new_from_grok(grok_t *grok);
8
-
9
- #endif /* _RGROK_H_ */
data/ext/ruby_grok.c DELETED
@@ -1,257 +0,0 @@
1
- #include <ruby.h>
2
- #include <grok.h>
3
- #include "rgrok.h"
4
- #include "ruby_grokmatch.h"
5
-
6
- VALUE cGrok; /* Grok class object */
7
-
8
- /*
9
- * Document-class: Grok
10
- * Grok ruby bindings.
11
- *
12
- * For an intro to grok, see:
13
- * See http://code.google.com/p/semicomplete/wiki/Grok
14
- * and http://code.google.com/p/semicomplete/wiki/GrokRuby
15
- *
16
- * General usage of grok is as follows:
17
- *
18
- * grok = Grok.new
19
- * grok.add_patterns_from_file("/path/to/patterns")
20
- * grok.add_pattern("WORD", "\b\w+\b")
21
- * grok.compile("%{WORD} world!")
22
- * m = grok.match("Hello world!")
23
- * if m
24
- * puts m.captures["WORD"]
25
- * else
26
- * puts "No match."
27
- * fi
28
- *
29
- * Grok autodiscovery:
30
- * grok = Grok.new
31
- * grok.add_patterns_from_file("/path/to/patterns")
32
- *
33
- * # Assuming you loaded the default pattern set
34
- * grok.discover("2009-04-18")
35
- * #=> "\\Q\\E%{DATE_EU}\\Q\\E"
36
- * grok.discover("Visit http://www.google.com/")
37
- * #=> "\\QVisit \\E%{URI}\\Q\\E"
38
- *
39
- */
40
-
41
- extern VALUE cGrokMatch;
42
- extern VALUE cGrokDiscover;
43
- extern void Init_GrokMatch();
44
- extern void Init_GrokDiscover();
45
-
46
- static VALUE rGrok_initialize(VALUE self) {
47
- /* empty */
48
- return Qnil;
49
- }
50
-
51
- void rGrok_free(void *p) {
52
- grok_t *grok = (grok_t *)p;
53
-
54
- /* we strdup our pattern from ruby and rb_str2cstr */
55
- /* typecast to ignore warnings about freeing a const...
56
- * TODO(sissel): Fix the constness */
57
- free((char *)grok->pattern);
58
-
59
- grok_free(grok);
60
- free(grok);
61
- }
62
-
63
- VALUE rGrok_new(VALUE klass) {
64
- VALUE rgrok;
65
- grok_t *grok = ALLOC(grok_t);
66
- grok_init(grok);
67
- //grok->logmask = ~0;
68
- rgrok = Data_Wrap_Struct(klass, 0, rGrok_free, grok);
69
- rb_obj_call_init(rgrok, 0, 0);
70
- return rgrok;
71
- }
72
-
73
- VALUE rGrok_new_from_grok(grok_t *grok) {
74
- VALUE rgrok;
75
- rgrok = Data_Wrap_Struct(cGrok, 0, rGrok_free, grok);
76
- rb_obj_call_init(rgrok, 0, 0);
77
- return rgrok;
78
- }
79
-
80
- /*
81
- * Compiles an expression.
82
- *
83
- * call-seq:
84
- * grok.compile(pattern)
85
- *
86
- */
87
- VALUE rGrok_compile(VALUE self, VALUE pattern) {
88
- grok_t *grok;
89
- char *c_pattern = NULL;
90
- char *str = NULL;
91
- long len = 0;
92
- int ret = 0;
93
- Data_Get_Struct(self, grok_t, grok);
94
-
95
- /* Need strdup here in case 'pattern' object is deleted later in
96
- * the ruby code
97
- * TODO(sissel): Really, just mark that we are using this string
98
- * rather than strduping. */
99
- str = rb_str2cstr(pattern, &len);
100
- c_pattern = malloc(len);
101
- memcpy(c_pattern, str, len);
102
-
103
- if (grok->pattern != NULL) {
104
- /* if we've already called compile, let's free up the string we
105
- * allocated last time */
106
-
107
- /* typecast to break constness. TODO(sissel): fix const */
108
- free((char *)grok->pattern);
109
- }
110
-
111
- ret = grok_compilen(grok, c_pattern, (int)len);
112
- if (ret) {
113
- rb_raise(rb_eArgError, "Compile failed: %s", grok->errstr);
114
- }
115
-
116
- return Qnil;
117
- }
118
-
119
- VALUE rGrok_match(VALUE self, VALUE input) {
120
- grok_t *grok = NULL;
121
- grok_match_t gm;
122
- char *c_input = NULL;
123
- long len = 0;
124
- int ret = 0;
125
-
126
- Data_Get_Struct(self, grok_t, grok);
127
- c_input = rb_str2cstr(input, &len);
128
- ret = grok_execn(grok, c_input, (int)len, &gm);
129
-
130
- VALUE rgm = Qnil;
131
-
132
- //fprintf(stderr, "%d\n", ret);
133
- switch (ret) {
134
- case GROK_ERROR_NOMATCH:
135
- rgm = Qfalse;
136
- break;
137
- case GROK_OK:
138
- rgm = rGrokMatch_new_from_grok_match(&gm);
139
- break;
140
- default:
141
- rb_raise(rb_eArgError, "Error from grok_execn: %d", ret);
142
- rgm = Qfalse;
143
- }
144
-
145
- return rgm;
146
- }
147
-
148
- VALUE rGrok_add_pattern(VALUE self, VALUE name, VALUE pattern) {
149
- grok_t *grok = NULL;
150
- char *c_name= NULL, *c_pattern = NULL;
151
- long namelen = 0, patternlen = 0;
152
-
153
- /* Don't need strdup here, since grok_pattern_add will store a copy
154
- * if the data */
155
- c_name = rb_str2cstr(name, &namelen);
156
- c_pattern = rb_str2cstr(pattern, &patternlen);
157
- Data_Get_Struct(self, grok_t, grok);
158
-
159
- grok_pattern_add(grok, c_name, namelen, c_pattern, patternlen);
160
- return Qnil;
161
- }
162
-
163
- VALUE rGrok_add_patterns_from_file(VALUE self, VALUE path) {
164
- grok_t *grok = NULL;
165
- int ret = 0;
166
- char *c_path = NULL;
167
- long pathlen = 0;
168
-
169
- /* don't need strdup here, since we don't store 'path' long-term */
170
- c_path = rb_str2cstr(path, &pathlen);
171
- Data_Get_Struct(self, grok_t, grok);
172
-
173
- ret = grok_patterns_import_from_file(grok, c_path);
174
-
175
- if (ret != GROK_OK) {
176
- rb_raise(rb_eArgError, "Failed to add patterns from file %s", c_path);
177
- }
178
-
179
- return Qnil;
180
- }
181
-
182
- VALUE rGrok_expanded_pattern(VALUE self) {
183
- grok_t *grok = NULL;
184
- VALUE expanded_pattern;
185
-
186
- Data_Get_Struct(self, grok_t, grok);
187
- expanded_pattern = rb_str_new(grok->full_pattern, grok->full_pattern_len);
188
- return expanded_pattern;
189
- }
190
-
191
- VALUE rGrok_pattern(VALUE self) {
192
- grok_t *grok = NULL;
193
- VALUE pattern;
194
-
195
- Data_Get_Struct(self, grok_t, grok);
196
- pattern = rb_str_new(grok->pattern, grok->pattern_len);
197
- return pattern;
198
- }
199
-
200
- VALUE rGrok_patterns(VALUE self) {
201
- VALUE patternmap = rb_hash_new();
202
- TCLIST *names = NULL;
203
- grok_t *grok = NULL;
204
- int i = 0, len = 0;
205
-
206
- Data_Get_Struct(self, grok_t, grok);
207
- names = grok_pattern_name_list(grok);
208
-
209
- len = tclistnum(names);
210
- for (i = 0; i < len; i++) {
211
- int namelen = 0;
212
- const char *name = tclistval(names, i, &namelen);
213
- size_t regexplen = 0;
214
- const char *regexp = NULL;
215
- grok_pattern_find(grok, name, namelen, &regexp, &regexplen);
216
-
217
- VALUE key;
218
- VALUE value;
219
- key = rb_tainted_str_new(name, namelen);
220
- value = rb_tainted_str_new(regexp, regexplen);
221
- rb_hash_aset(patternmap, key, value);
222
- }
223
- tclistdel(names);
224
- return patternmap;
225
- }
226
-
227
- VALUE rGrok_get_logmask(VALUE self) {
228
- grok_t *grok;
229
- Data_Get_Struct(self, grok_t, grok);
230
- return INT2FIX(grok->logmask);
231
- }
232
-
233
- VALUE rGrok_set_logmask(VALUE self, VALUE mask) {
234
- grok_t *grok;
235
- Data_Get_Struct(self, grok_t, grok);
236
- grok->logmask = FIX2INT(mask);
237
- return Qnil;
238
- }
239
-
240
- void Init_Grok() {
241
- cGrok = rb_define_class("Grok", rb_cObject);
242
- rb_define_singleton_method(cGrok, "new", rGrok_new, 0);
243
- rb_define_method(cGrok, "initialize", rGrok_initialize, 0);
244
- rb_define_method(cGrok, "compile", rGrok_compile, 1);
245
- rb_define_method(cGrok, "match", rGrok_match, 1);
246
- rb_define_method(cGrok, "expanded_pattern", rGrok_expanded_pattern, 0);
247
- rb_define_method(cGrok, "pattern", rGrok_pattern, 0);
248
- rb_define_method(cGrok, "add_pattern", rGrok_add_pattern, 2);
249
- rb_define_method(cGrok, "add_patterns_from_file",
250
- rGrok_add_patterns_from_file, 1);
251
- rb_define_method(cGrok, "patterns", rGrok_patterns, 0);
252
- rb_define_method(cGrok, "logmask=", rGrok_set_logmask, 1);
253
- rb_define_method(cGrok, "logmask", rGrok_get_logmask, 0);
254
-
255
- Init_GrokMatch();
256
- Init_GrokDiscover();
257
- }
@@ -1,69 +0,0 @@
1
- #include "rgrok.h"
2
- #include <grok.h>
3
-
4
- VALUE cGrokDiscover;
5
- extern VALUE cGrok;
6
-
7
- static void rGrokDiscover_free(void *p);
8
-
9
- VALUE rGrokDiscover_new(VALUE klass, VALUE grok) {
10
- VALUE rgd;
11
- grok_discover_t *gdt = ALLOC(grok_discover_t); //grok_discover_new();
12
- rgd = Data_Wrap_Struct(klass, 0, rGrokDiscover_free, gdt);
13
-
14
- VALUE initargs[1] = { grok };
15
- rb_obj_call_init(rgd, 1, initargs);
16
- return (VALUE)rgd;
17
- }
18
-
19
- static void rGrokDiscover_free(void *p) {
20
- grok_discover_t *gdt = p;
21
- grok_discover_free(gdt);
22
- }
23
-
24
- VALUE rGrokDiscover_initialize(VALUE self, VALUE rb_grok) {
25
- grok_discover_t *gdt;
26
- grok_t *grok;
27
- Data_Get_Struct(self, grok_discover_t, gdt);
28
- Data_Get_Struct(rb_grok, grok_t, grok);
29
-
30
- grok_discover_init(gdt, grok);
31
- return Qnil;
32
- }
33
-
34
-
35
- VALUE rGrokDiscover_discover(VALUE self, VALUE input) {
36
- char *cstr_discovery;
37
- char *cstr_input;
38
- long unused_input_len;
39
- int discovery_len;
40
- grok_discover_t *gdt;
41
- grok_t *grok;
42
-
43
- Data_Get_Struct(self, grok_discover_t, gdt);
44
- cstr_input = rb_str2cstr(input, &unused_input_len);
45
- grok_discover(gdt, cstr_input, &cstr_discovery, &discovery_len);
46
- return rb_str_new(cstr_discovery, discovery_len);
47
- }
48
-
49
- VALUE rGrokDiscover_get_logmask(VALUE self) {
50
- grok_discover_t *gdt;
51
- Data_Get_Struct(self, grok_discover_t, gdt);
52
- return INT2FIX(gdt->logmask);
53
- }
54
-
55
- VALUE rGrokDiscover_set_logmask(VALUE self, VALUE mask) {
56
- grok_discover_t *gdt;
57
- Data_Get_Struct(self, grok_discover_t, gdt);
58
- gdt->logmask = FIX2INT(mask);
59
- return Qnil;
60
- }
61
-
62
- void Init_GrokDiscover() {
63
- cGrokDiscover = rb_define_class("GrokDiscover", rb_cObject);
64
- rb_define_singleton_method(cGrokDiscover, "new", rGrokDiscover_new, 1);
65
- rb_define_method(cGrokDiscover, "initialize", rGrokDiscover_initialize, 1);
66
- rb_define_method(cGrokDiscover, "discover", rGrokDiscover_discover, 1);
67
- rb_define_method(cGrokDiscover, "logmask=", rGrokDiscover_set_logmask, 1);
68
- rb_define_method(cGrokDiscover, "logmask", rGrokDiscover_get_logmask, 0);
69
- }
data/ext/ruby_grokmatch.c DELETED
@@ -1,220 +0,0 @@
1
- #include "rgrok.h"
2
- #include <grok.h>
3
-
4
- VALUE cGrokMatch;
5
- extern VALUE cGrok;
6
-
7
- static ID id_atend;
8
- static ID id_atstart;
9
- static ID id_atcaptures;
10
- static ID id_atsubject;
11
-
12
- static ID id_length;
13
- static ID id_has_key_p;
14
-
15
- static void rGrokMatch_free(void *p);
16
-
17
- VALUE rGrokMatch_new(VALUE klass) {
18
- NEWOBJ(rgm, grok_match_t);
19
- OBJSETUP(rgm, klass, T_OBJECT);
20
-
21
- return (VALUE)rgm;
22
- }
23
-
24
- VALUE rGrokMatch_new_from_grok_match(grok_match_t *gm) {
25
- VALUE rgrokmatch;
26
- grok_match_t *my_gm = NULL;
27
- rgrokmatch = Data_Make_Struct(cGrokMatch, grok_match_t, 0,
28
- rGrokMatch_free, my_gm);
29
- memcpy(my_gm, gm, sizeof(grok_match_t));
30
-
31
- /* We must strdup here, otherwise ruby GC may come and re-arrange
32
- * rb_str2cstr() can give a pointer to an object that is later deleted. When
33
- * deleted, we won't know about it, and we'll get a garbage string from that
34
- * pointer.
35
- *
36
- * TODO(sissel): make a GrokMatch 'ruby' friendly struct
37
- * that uses VALUE types for subject.
38
- */
39
- my_gm->subject = strdup(gm->subject);
40
- rb_obj_call_init(rgrokmatch, 0, 0);
41
- return rgrokmatch;
42
- }
43
-
44
- VALUE rGrokMatch_initialize(VALUE self) {
45
- grok_match_t *gm;
46
- VALUE captures;
47
-
48
- Data_Get_Struct(self, grok_match_t, gm);
49
-
50
- /* Set the @captures variable to a hash of array of strings */
51
- captures = rb_hash_new();
52
- //captures = rb_eval_string("Hash.new { |h,k| h[k] = Array.new }");
53
- rb_iv_set(self, "@captures", captures);
54
-
55
- return Qtrue;
56
- }
57
-
58
- VALUE rGrokMatch_each_capture(VALUE self) {
59
- char *name;
60
- const char *data;
61
- int namelen, datalen;
62
- grok_match_t *gm;
63
- VALUE captures;
64
-
65
- Data_Get_Struct(self, grok_match_t, gm);
66
- captures = rb_iv_get(self, "@captures");
67
- grok_match_walk_init(gm);
68
- while (grok_match_walk_next(gm, &name, &namelen, &data, &datalen) == 0) {
69
- VALUE key, value;
70
-
71
- #ifdef _TRIM_KEY_EXCESS_IN_C_
72
- /* This section will skip captures of %{FOO} and rename captures of
73
- * %{FOO:bar} to just 'bar' */
74
- size_t koff = 0;
75
- /* there is no 'strcspn' that takes a length, so do it ourselves */
76
- while (koff < namelen && name[koff] != ':' && name[koff] != '\0') {
77
- koff++;
78
- }
79
-
80
- /* Skip captures that aren't named specially */
81
- if (koff == namelen) {
82
- //printf("Skipping %.*s\n", namelen, name);
83
- continue;
84
- }
85
-
86
- koff++;
87
-
88
- key = rb_tainted_str_new(name + koff, namelen - koff);
89
- #else
90
- key = rb_tainted_str_new(name, namelen);
91
- #endif
92
- value = rb_tainted_str_new(data, datalen);
93
-
94
- // Yield [key, value]
95
- rb_yield(rb_ary_new3(2, key, value));
96
- //rb_ary_push(ary, value);
97
- }
98
-
99
- grok_match_walk_end(gm);
100
- return Qtrue;
101
- }
102
-
103
- VALUE rGrokMatch_captures(VALUE self) {
104
- char *name;
105
- const char *data;
106
- int namelen, datalen;
107
- grok_match_t *gm;
108
- VALUE captures;
109
-
110
- Data_Get_Struct(self, grok_match_t, gm);
111
- captures = rb_iv_get(self, "@captures");
112
-
113
- if (captures == Qnil) {
114
- captures = rb_hash_new();
115
- }
116
-
117
- if (FIX2INT(rb_funcall(captures, id_length, 0)) > 0) {
118
- //if (FIX2NUM(rb_hash_size(captures)) > 0) {
119
- return captures;
120
- }
121
-
122
- grok_match_walk_init(gm);
123
- while (grok_match_walk_next(gm, &name, &namelen, &data, &datalen) == 0) {
124
- VALUE key = Qnil;
125
- VALUE value = Qnil;
126
-
127
- #ifdef _TRIM_KEY_EXCESS_IN_C_
128
- /* This section will skip captures of %{FOO} and rename captures of
129
- * %{FOO:bar} to just 'bar' */
130
- size_t koff = 0;
131
- /* there is no 'strcspn' that takes a length, so do it ourselves */
132
- while (koff < namelen && name[koff] != ':' && name[koff] != '\0') {
133
- koff++;
134
- }
135
-
136
- /* Skip captures that aren't named specially */
137
- if (koff == namelen) {
138
- //printf("Skipping %.*s\n", namelen, name);
139
- continue;
140
- }
141
-
142
- koff++;
143
-
144
- key = rb_tainted_str_new(name + koff, namelen - koff);
145
- #else
146
- key = rb_tainted_str_new(name, namelen);
147
- #endif
148
- value = rb_tainted_str_new(data, datalen);
149
-
150
- VALUE array;
151
- //if (rb_hash_has_key(captures, key) == Qfalse) {
152
- if (rb_funcall(captures, id_has_key_p, 1, key) == Qfalse) {
153
- array = rb_hash_aset(captures, key, rb_ary_new());
154
- } else {
155
- array = rb_hash_aref(captures, key);
156
- }
157
- rb_ary_push(array, value);
158
- }
159
-
160
- grok_match_walk_end(gm);
161
- return captures;
162
- }
163
-
164
- VALUE rGrokMatch_start(VALUE self) {
165
- grok_match_t *gm;
166
- Data_Get_Struct(self, grok_match_t, gm);
167
- return INT2FIX(gm->start);
168
- }
169
-
170
- VALUE rGrokMatch_end(VALUE self) {
171
- grok_match_t *gm;
172
- Data_Get_Struct(self, grok_match_t, gm);
173
- VALUE ret = Qnil;
174
- ret = rb_iv_get(self, "@end");
175
- if (ret == Qnil) {
176
- ret = rb_iv_set(self, "@end", INT2FIX(gm->end));
177
- }
178
- return INT2FIX(gm->end);
179
- }
180
-
181
- VALUE rGrokMatch_subject(VALUE self) {
182
- grok_match_t *gm;
183
- Data_Get_Struct(self, grok_match_t, gm);
184
- return rb_tainted_str_new2(gm->subject);
185
- }
186
-
187
- VALUE rGrokMatch_debug(VALUE self) {
188
- grok_match_t *gm;
189
- Data_Get_Struct(self, grok_match_t, gm);
190
- printf("match subject: %s\n", gm->subject);
191
- printf("match start: %d\n", gm->start);
192
- printf("match end: %d\n", gm->end);
193
- printf("grok pattern: %s\n", gm->grok->pattern);
194
- return Qnil;
195
- }
196
-
197
- void rGrokMatch_free(void *p) {
198
- grok_match_t *gm = (grok_match_t *)p;
199
- free((char *)gm->subject);
200
- }
201
-
202
- void Init_GrokMatch() {
203
- cGrokMatch = rb_define_class("GrokMatch", rb_cObject);
204
- rb_define_singleton_method(cGrokMatch, "new", rGrokMatch_new, 0);
205
- rb_define_method(cGrokMatch, "initialize", rGrokMatch_initialize, 0);
206
- rb_define_method(cGrokMatch, "captures", rGrokMatch_captures, 0);
207
- rb_define_method(cGrokMatch, "start", rGrokMatch_start, 0);
208
- rb_define_method(cGrokMatch, "end", rGrokMatch_end, 0);
209
- rb_define_method(cGrokMatch, "subject", rGrokMatch_subject, 0);
210
- rb_define_method(cGrokMatch, "each_capture", rGrokMatch_each_capture, 0);
211
-
212
- //rb_define_method(cGrokMatch, "debug", rGrokMatch_debug, 0);
213
-
214
- id_atend = rb_intern("@end");
215
- id_atstart = rb_intern("@start");
216
- id_atcaptures = rb_intern("@captures");
217
- id_atsubject = rb_intern("@subject");
218
- id_length = rb_intern("length");
219
- id_has_key_p = rb_intern("has_key?");
220
- }
data/ext/ruby_grokmatch.h DELETED
@@ -1,14 +0,0 @@
1
- #ifndef _RUBY_GROKMATCH_H_
2
- #define _RUBY_GROKMATCH_H_
3
-
4
- #include "rgrok.h"
5
- #include <grok.h>
6
-
7
- #ifdef _IS_RUBY_GROKMATCH_
8
- #define CONDEXTERN
9
- #else
10
- #define CONDEXTERN extern
11
- #endif
12
-
13
- CONDEXTERN VALUE rGrokMatch_new_from_grok_match(grok_match_t *gm);
14
- #endif /* _RUBY_GROKMATCH_H_ */
data/test/core DELETED
Binary file