toastyapps-excelsior 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +26 -0
- data/ext/excelsior_reader/excelsior_reader.c +261 -0
- data/ext/excelsior_reader/excelsior_reader.rl +52 -0
- data/ext/excelsior_reader/extconf.rb +5 -0
- data/lib/excelsior.rb +7 -0
- metadata +60 -0
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
PKG_FILES = %w(Rakefile) +
|
5
|
+
Dir.glob("{lib}/**/*") +
|
6
|
+
Dir.glob("ext/**/*.{c,rb,rl}") +
|
7
|
+
%w[ext/excelsior_reader/excelsior_reader.c] # needed because they are generated later
|
8
|
+
|
9
|
+
gem_spec = Gem::Specification.new do |gem_spec|
|
10
|
+
gem_spec.name = 'excelsior'
|
11
|
+
gem_spec.version = '0.0.1'
|
12
|
+
gem_spec.summary = 'A Ruby gem that uses C bindings to read CSV files superfast. I\'m totally serial!'
|
13
|
+
gem_spec.description = 'A Ruby gem that uses C bindings to read CSV files superfast. I\'m totally serial!'
|
14
|
+
gem_spec.email = 'matt@toastyapps.com'
|
15
|
+
gem_spec.homepage = 'http://github.com/toastyapps/excelsior'
|
16
|
+
gem_spec.authors = ["Matthew Mongeau"]
|
17
|
+
gem_spec.files = PKG_FILES
|
18
|
+
gem_spec.extensions = FileList["ext/**/extconf.rb"].to_a
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Generate a gemspec file"
|
22
|
+
task :gemspec do
|
23
|
+
File.open("#{gem_spec.name}.gemspec", "w") do |f|
|
24
|
+
f.write gem_spec.to_yaml
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
|
2
|
+
#line 1 "excelsior_reader.rl"
|
3
|
+
#include "ruby.h"
|
4
|
+
|
5
|
+
VALUE e_parse(VALUE self, VALUE data) {
|
6
|
+
int cs, act, curline = 1, len = 0;
|
7
|
+
char *ts = 0, *te = 0, *buf = NULL, *eof = NULL;
|
8
|
+
char *p, *pe;
|
9
|
+
VALUE rows;
|
10
|
+
VALUE arr;
|
11
|
+
rows = rb_ary_new();
|
12
|
+
arr = rb_ary_new();
|
13
|
+
len = RSTRING(data)->len;
|
14
|
+
p = RSTRING(data)->ptr;
|
15
|
+
pe = p + len;
|
16
|
+
eof = pe;
|
17
|
+
int has_found = 0;
|
18
|
+
|
19
|
+
|
20
|
+
#line 34 "excelsior_reader.rl"
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
#line 25 "excelsior_reader.c"
|
25
|
+
static const char _excelsior_scan_actions[] = {
|
26
|
+
0, 1, 0, 1, 1, 1, 2, 1,
|
27
|
+
3, 1, 4, 1, 5, 1, 6, 1,
|
28
|
+
7, 1, 8, 1, 9
|
29
|
+
};
|
30
|
+
|
31
|
+
static const char _excelsior_scan_key_offsets[] = {
|
32
|
+
0, 6, 7, 8, 16, 22
|
33
|
+
};
|
34
|
+
|
35
|
+
static const char _excelsior_scan_trans_keys[] = {
|
36
|
+
32, 34, 39, 44, 9, 13, 34, 39,
|
37
|
+
10, 13, 32, 34, 39, 44, 9, 12,
|
38
|
+
32, 34, 39, 44, 9, 13, 10, 0
|
39
|
+
};
|
40
|
+
|
41
|
+
static const char _excelsior_scan_single_lengths[] = {
|
42
|
+
4, 1, 1, 6, 4, 1
|
43
|
+
};
|
44
|
+
|
45
|
+
static const char _excelsior_scan_range_lengths[] = {
|
46
|
+
1, 0, 0, 1, 1, 0
|
47
|
+
};
|
48
|
+
|
49
|
+
static const char _excelsior_scan_index_offsets[] = {
|
50
|
+
0, 6, 8, 10, 18, 24
|
51
|
+
};
|
52
|
+
|
53
|
+
static const char _excelsior_scan_indicies[] = {
|
54
|
+
0, 0, 0, 0, 0, 1, 3, 2,
|
55
|
+
3, 4, 6, 7, 5, 2, 4, 8,
|
56
|
+
5, 1, 10, 9, 9, 9, 9, 1,
|
57
|
+
6, 11, 0
|
58
|
+
};
|
59
|
+
|
60
|
+
static const char _excelsior_scan_trans_targs[] = {
|
61
|
+
3, 4, 1, 3, 2, 3, 3, 5,
|
62
|
+
3, 3, 0, 3
|
63
|
+
};
|
64
|
+
|
65
|
+
static const char _excelsior_scan_trans_actions[] = {
|
66
|
+
19, 5, 0, 11, 0, 9, 7, 0,
|
67
|
+
13, 17, 0, 15
|
68
|
+
};
|
69
|
+
|
70
|
+
static const char _excelsior_scan_to_state_actions[] = {
|
71
|
+
0, 0, 0, 1, 0, 0
|
72
|
+
};
|
73
|
+
|
74
|
+
static const char _excelsior_scan_from_state_actions[] = {
|
75
|
+
0, 0, 0, 3, 0, 0
|
76
|
+
};
|
77
|
+
|
78
|
+
static const char _excelsior_scan_eof_trans[] = {
|
79
|
+
1, 0, 0, 0, 10, 12
|
80
|
+
};
|
81
|
+
|
82
|
+
static const int excelsior_scan_start = 3;
|
83
|
+
static const int excelsior_scan_error = -1;
|
84
|
+
|
85
|
+
static const int excelsior_scan_en_main = 3;
|
86
|
+
|
87
|
+
|
88
|
+
#line 37 "excelsior_reader.rl"
|
89
|
+
|
90
|
+
#line 91 "excelsior_reader.c"
|
91
|
+
{
|
92
|
+
cs = excelsior_scan_start;
|
93
|
+
ts = 0;
|
94
|
+
te = 0;
|
95
|
+
act = 0;
|
96
|
+
}
|
97
|
+
|
98
|
+
#line 38 "excelsior_reader.rl"
|
99
|
+
|
100
|
+
#line 101 "excelsior_reader.c"
|
101
|
+
{
|
102
|
+
int _klen;
|
103
|
+
unsigned int _trans;
|
104
|
+
const char *_acts;
|
105
|
+
unsigned int _nacts;
|
106
|
+
const char *_keys;
|
107
|
+
|
108
|
+
if ( p == pe )
|
109
|
+
goto _test_eof;
|
110
|
+
_resume:
|
111
|
+
_acts = _excelsior_scan_actions + _excelsior_scan_from_state_actions[cs];
|
112
|
+
_nacts = (unsigned int) *_acts++;
|
113
|
+
while ( _nacts-- > 0 ) {
|
114
|
+
switch ( *_acts++ ) {
|
115
|
+
case 1:
|
116
|
+
#line 1 "excelsior_reader.rl"
|
117
|
+
{ts = p;}
|
118
|
+
break;
|
119
|
+
#line 120 "excelsior_reader.c"
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
_keys = _excelsior_scan_trans_keys + _excelsior_scan_key_offsets[cs];
|
124
|
+
_trans = _excelsior_scan_index_offsets[cs];
|
125
|
+
|
126
|
+
_klen = _excelsior_scan_single_lengths[cs];
|
127
|
+
if ( _klen > 0 ) {
|
128
|
+
const char *_lower = _keys;
|
129
|
+
const char *_mid;
|
130
|
+
const char *_upper = _keys + _klen - 1;
|
131
|
+
while (1) {
|
132
|
+
if ( _upper < _lower )
|
133
|
+
break;
|
134
|
+
|
135
|
+
_mid = _lower + ((_upper-_lower) >> 1);
|
136
|
+
if ( (*p) < *_mid )
|
137
|
+
_upper = _mid - 1;
|
138
|
+
else if ( (*p) > *_mid )
|
139
|
+
_lower = _mid + 1;
|
140
|
+
else {
|
141
|
+
_trans += (_mid - _keys);
|
142
|
+
goto _match;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
_keys += _klen;
|
146
|
+
_trans += _klen;
|
147
|
+
}
|
148
|
+
|
149
|
+
_klen = _excelsior_scan_range_lengths[cs];
|
150
|
+
if ( _klen > 0 ) {
|
151
|
+
const char *_lower = _keys;
|
152
|
+
const char *_mid;
|
153
|
+
const char *_upper = _keys + (_klen<<1) - 2;
|
154
|
+
while (1) {
|
155
|
+
if ( _upper < _lower )
|
156
|
+
break;
|
157
|
+
|
158
|
+
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
|
159
|
+
if ( (*p) < _mid[0] )
|
160
|
+
_upper = _mid - 2;
|
161
|
+
else if ( (*p) > _mid[1] )
|
162
|
+
_lower = _mid + 2;
|
163
|
+
else {
|
164
|
+
_trans += ((_mid - _keys)>>1);
|
165
|
+
goto _match;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
_trans += _klen;
|
169
|
+
}
|
170
|
+
|
171
|
+
_match:
|
172
|
+
_trans = _excelsior_scan_indicies[_trans];
|
173
|
+
_eof_trans:
|
174
|
+
cs = _excelsior_scan_trans_targs[_trans];
|
175
|
+
|
176
|
+
if ( _excelsior_scan_trans_actions[_trans] == 0 )
|
177
|
+
goto _again;
|
178
|
+
|
179
|
+
_acts = _excelsior_scan_actions + _excelsior_scan_trans_actions[_trans];
|
180
|
+
_nacts = (unsigned int) *_acts++;
|
181
|
+
while ( _nacts-- > 0 )
|
182
|
+
{
|
183
|
+
switch ( *_acts++ )
|
184
|
+
{
|
185
|
+
case 2:
|
186
|
+
#line 1 "excelsior_reader.rl"
|
187
|
+
{te = p+1;}
|
188
|
+
break;
|
189
|
+
case 3:
|
190
|
+
#line 28 "excelsior_reader.rl"
|
191
|
+
{te = p+1;{ rb_ary_push(rows, arr); arr = rb_ary_new(); }}
|
192
|
+
break;
|
193
|
+
case 4:
|
194
|
+
#line 29 "excelsior_reader.rl"
|
195
|
+
{te = p+1;}
|
196
|
+
break;
|
197
|
+
case 5:
|
198
|
+
#line 31 "excelsior_reader.rl"
|
199
|
+
{te = p+1;{ rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}}
|
200
|
+
break;
|
201
|
+
case 6:
|
202
|
+
#line 32 "excelsior_reader.rl"
|
203
|
+
{te = p+1;{ if(has_found == 0) rb_ary_push(arr, Qnil); has_found = 0;}}
|
204
|
+
break;
|
205
|
+
case 7:
|
206
|
+
#line 28 "excelsior_reader.rl"
|
207
|
+
{te = p;p--;{ rb_ary_push(rows, arr); arr = rb_ary_new(); }}
|
208
|
+
break;
|
209
|
+
case 8:
|
210
|
+
#line 30 "excelsior_reader.rl"
|
211
|
+
{te = p;p--;{ rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}}
|
212
|
+
break;
|
213
|
+
case 9:
|
214
|
+
#line 30 "excelsior_reader.rl"
|
215
|
+
{{p = ((te))-1;}{ rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}}
|
216
|
+
break;
|
217
|
+
#line 218 "excelsior_reader.c"
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
221
|
+
_again:
|
222
|
+
_acts = _excelsior_scan_actions + _excelsior_scan_to_state_actions[cs];
|
223
|
+
_nacts = (unsigned int) *_acts++;
|
224
|
+
while ( _nacts-- > 0 ) {
|
225
|
+
switch ( *_acts++ ) {
|
226
|
+
case 0:
|
227
|
+
#line 1 "excelsior_reader.rl"
|
228
|
+
{ts = 0;}
|
229
|
+
break;
|
230
|
+
#line 231 "excelsior_reader.c"
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
if ( ++p != pe )
|
235
|
+
goto _resume;
|
236
|
+
_test_eof: {}
|
237
|
+
if ( p == eof )
|
238
|
+
{
|
239
|
+
if ( _excelsior_scan_eof_trans[cs] > 0 ) {
|
240
|
+
_trans = _excelsior_scan_eof_trans[cs] - 1;
|
241
|
+
goto _eof_trans;
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
#line 39 "excelsior_reader.rl"
|
248
|
+
if(RARRAY_LEN(arr) > 0) {
|
249
|
+
rb_ary_push(rows, arr);
|
250
|
+
}
|
251
|
+
return rows;
|
252
|
+
}
|
253
|
+
|
254
|
+
VALUE mExcelsior;
|
255
|
+
VALUE cReader;
|
256
|
+
|
257
|
+
void Init_excelsior_reader() {
|
258
|
+
mExcelsior = rb_define_module("Excelsior");
|
259
|
+
cReader = rb_define_class_under(mExcelsior, "Reader", rb_cObject);
|
260
|
+
rb_define_singleton_method(cReader, "parse", e_parse, 1);
|
261
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
VALUE e_parse(VALUE self, VALUE data) {
|
4
|
+
int cs, act, curline = 1, len = 0;
|
5
|
+
char *ts = 0, *te = 0, *buf = NULL, *eof = NULL;
|
6
|
+
char *p, *pe;
|
7
|
+
VALUE rows;
|
8
|
+
VALUE arr;
|
9
|
+
rows = rb_ary_new();
|
10
|
+
arr = rb_ary_new();
|
11
|
+
len = RSTRING(data)->len;
|
12
|
+
p = RSTRING(data)->ptr;
|
13
|
+
pe = p + len;
|
14
|
+
eof = pe;
|
15
|
+
int has_found = 0;
|
16
|
+
|
17
|
+
%%{
|
18
|
+
machine excelsior_scan;
|
19
|
+
delimeter = ",";
|
20
|
+
newline = "\r"? "\n" | "\r" | "\n";
|
21
|
+
schar1 = any - '"';
|
22
|
+
schar2 = any - "'";
|
23
|
+
letter = any - delimeter - space - '"' - "'";
|
24
|
+
string = '"' schar1* '"' | "'" schar2* "'";
|
25
|
+
word = letter+;
|
26
|
+
value = word (" " word)*;
|
27
|
+
main := |*
|
28
|
+
newline { rb_ary_push(rows, arr); arr = rb_ary_new(); };
|
29
|
+
space;
|
30
|
+
value { rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;};
|
31
|
+
string { rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;};
|
32
|
+
delimeter { if(has_found == 0) rb_ary_push(arr, Qnil); has_found = 0;};
|
33
|
+
*|;
|
34
|
+
}%%
|
35
|
+
|
36
|
+
%% write data nofinal;
|
37
|
+
%% write init;
|
38
|
+
%% write exec;
|
39
|
+
if(RARRAY_LEN(arr) > 0) {
|
40
|
+
rb_ary_push(rows, arr);
|
41
|
+
}
|
42
|
+
return rows;
|
43
|
+
}
|
44
|
+
|
45
|
+
VALUE mExcelsior;
|
46
|
+
VALUE cReader;
|
47
|
+
|
48
|
+
void Init_excelsior_reader() {
|
49
|
+
mExcelsior = rb_define_module("Excelsior");
|
50
|
+
cReader = rb_define_class_under(mExcelsior, "Reader", rb_cObject);
|
51
|
+
rb_define_singleton_method(cReader, "parse", e_parse, 1);
|
52
|
+
}
|
data/lib/excelsior.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toastyapps-excelsior
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Mongeau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-16 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Ruby gem that uses C bindings to read CSV files superfast. I'm totally serial!
|
17
|
+
email: matt@toastyapps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/excelsior_reader/extconf.rb
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- lib/excelsior.rb
|
27
|
+
- ext/excelsior_reader/excelsior_reader.c
|
28
|
+
- ext/excelsior_reader/extconf.rb
|
29
|
+
- ext/excelsior_reader/excelsior_reader.rl
|
30
|
+
- ext/excelsior_reader/excelsior_reader.c
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/toastyapps/excelsior
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: A Ruby gem that uses C bindings to read CSV files superfast. I'm totally serial!
|
59
|
+
test_files: []
|
60
|
+
|