mawk 0.1.0

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.
Files changed (6) hide show
  1. data/README +30 -0
  2. data/ext/extconf.rb +7 -0
  3. data/ext/ruby-mawk.c +82 -0
  4. data/ext/ruby-mawk.h +19 -0
  5. data/lib/mawk.rb +48 -0
  6. metadata +59 -0
data/README ADDED
@@ -0,0 +1,30 @@
1
+ = ruby-mawk
2
+
3
+ == Description
4
+
5
+ Ruby binding for libmawk
6
+
7
+ see http://repo.hu/projects/libmawk/
8
+
9
+ == Install
10
+
11
+ gem install mawk
12
+
13
+ == Example
14
+
15
+ #!/usr/bin/env ruby
16
+
17
+ require 'mawk'
18
+
19
+ open('file.txt') do |f|
20
+ f.awk('/London/ {print}')
21
+ end
22
+
23
+ str = <<-EOS
24
+ London Bridge is broken down,
25
+ Broken down, bloken down.
26
+ London Bridge is bloken down,
27
+ My fair lady.
28
+ EOS
29
+
30
+ puts str.sawk('{print $2}')
data/ext/extconf.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ $CFLAGS += ' -Wall '
4
+
5
+ if have_header('libmawk.h') and have_library('mawk')
6
+ create_makefile('mawkc')
7
+ end
data/ext/ruby-mawk.c ADDED
@@ -0,0 +1,82 @@
1
+ #include "ruby-mawk.h"
2
+
3
+ static VALUE rb_cMAWK;
4
+ static VALUE rb_eMAWKError;
5
+
6
+ static void rb_mawk_free(struct mawk *p) {
7
+ xfree(p);
8
+ }
9
+
10
+ static VALUE rb_mawk_alloc(VALUE klass) {
11
+ struct mawk *p = ALLOC(struct mawk);
12
+ return Data_Wrap_Struct(klass, 0, rb_mawk_free, p);
13
+ }
14
+
15
+ static VALUE rd_mawk_initialize(VALUE self, VALUE script) {
16
+ struct mawk *p;
17
+ int argc = 2;
18
+ char *argv[] = {"awk", StringValuePtr(script)};
19
+
20
+ Data_Get_Struct(self, struct mawk, p);
21
+
22
+ p->m = libmawk_initialize_stage1(argc, argv);
23
+
24
+ if (p->m == NULL) {
25
+ rb_raise(rb_eMAWKError, "Init failed");
26
+ }
27
+
28
+ p->m = libmawk_initialize_stage2(p->m);
29
+
30
+ if (p->m->compile_error_count != 0) {
31
+ libmawk_uninitialize(p->m);
32
+ rb_raise(rb_eMAWKError, "Compile error");
33
+ }
34
+
35
+ if (p->m->do_exit) {
36
+ libmawk_uninitialize(p->m);
37
+ rb_raise(rb_eMAWKError, "Do exit");
38
+ }
39
+
40
+ p->m = libmawk_initialize_stage3(p->m);
41
+
42
+ return Qnil;
43
+ }
44
+
45
+ static VALUE rd_mawk_uninitialize(VALUE self) {
46
+ struct mawk *p;
47
+
48
+ Data_Get_Struct(self, struct mawk, p);
49
+ libmawk_uninitialize(p->m);
50
+
51
+ return Qnil;
52
+ }
53
+
54
+ static VALUE rd_mawk_append_input(VALUE self, VALUE input) {
55
+ struct mawk *p;
56
+
57
+ Data_Get_Struct(self, struct mawk, p);
58
+ libmawk_append_input(p->m, StringValuePtr(input));
59
+
60
+ return Qnil;
61
+ }
62
+
63
+ static VALUE rd_mawk_run_main(VALUE self) {
64
+ struct mawk *p;
65
+
66
+ Data_Get_Struct(self, struct mawk, p);
67
+ libmawk_run_main(p->m);
68
+
69
+ return Qnil;
70
+ }
71
+
72
+ void Init_mawkc() {
73
+ rb_cMAWK = rb_define_class("MAWK", rb_cObject);
74
+ rb_define_alloc_func(rb_cMAWK, rb_mawk_alloc);
75
+ rb_define_private_method(rb_cMAWK, "initialize", rd_mawk_initialize, 1);
76
+ rb_define_method(rb_cMAWK, "uninitialize", rd_mawk_uninitialize, 0);
77
+ rb_define_method(rb_cMAWK, "append_input", rd_mawk_append_input, 1);
78
+ rb_define_method(rb_cMAWK, "<<", rd_mawk_append_input, 1);
79
+ rb_define_method(rb_cMAWK, "run_main", rd_mawk_run_main, 0);
80
+
81
+ rb_eMAWKError = rb_define_class_under(rb_cMAWK, "Error", rb_eStandardError);
82
+ }
data/ext/ruby-mawk.h ADDED
@@ -0,0 +1,19 @@
1
+ #ifndef __RUBY_MAWK_H__
2
+ #define __RUBY_MAWK_H__
3
+
4
+ #include <stdlib.h>
5
+ #include <libmawk.h>
6
+ #include <ruby.h>
7
+
8
+ #ifndef RSTRING_PTR
9
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
10
+ #endif
11
+ #ifndef RSTRING_LEN
12
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
13
+ #endif
14
+
15
+ struct mawk {
16
+ mawk_state_t *m;
17
+ };
18
+
19
+ #endif // __RUBY_MAWK_H__
data/lib/mawk.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'mawkc'
2
+ require 'tempfile'
3
+ require 'stringio'
4
+
5
+ module AWK
6
+ def awk(script)
7
+ m = MAWK.new(script)
8
+
9
+ self.each_line do |line|
10
+ line.concat("\n") if line !~ /\n\Z/
11
+ m << line
12
+ end
13
+
14
+ m.run_main
15
+ m.uninitialize
16
+ end
17
+
18
+ def sawk(script)
19
+ file = Tempfile.new("mawk.#{$$}.#{Time.now.strftime '%Y%m%d%H%M%S'}")
20
+ out = nil
21
+
22
+ begin
23
+ stdout = $stdout.clone
24
+ $stdout.reopen(file)
25
+ awk(script)
26
+ file.rewind
27
+ out = file.read
28
+ ensure
29
+ $stdout.reopen(stdout)
30
+ file.close
31
+ file.unlink
32
+ end
33
+
34
+ return out
35
+ end
36
+ end
37
+
38
+ class IO
39
+ include AWK
40
+ end
41
+
42
+ class String
43
+ include AWK
44
+ end
45
+
46
+ class StringIO
47
+ include AWK
48
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mawk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2011-04-11 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: sgwr_dts@yahoo.co.jp
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - ext/ruby-mawk.c
26
+ - ext/ruby-mawk.h
27
+ - ext/extconf.rb
28
+ - README
29
+ - lib/mawk.rb
30
+ has_rdoc: true
31
+ homepage: https://bitbucket.org/winebarrel/ruby-mawk
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Ruby binding for libmawk
58
+ test_files: []
59
+