Class: Excelsior::Reader

Inherits:
Object
  • Object
show all
Defined in:
ext/excelsior_reader/excelsior_reader.c

Class Method Summary collapse

Class Method Details

.rows(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'ext/excelsior_reader/excelsior_reader.c', line 127

VALUE e_rows(int argc, VALUE *argv, VALUE self) {
  
  int cs, act, have = 0, nread = 0, curline = 1, text = 0;
  char *ts = 0, *te = 0, *buf = NULL, *eof = NULL;
	char *p, *pe;
  int buffer_size = BUFSIZE;
  
  has_found = 0;
	VALUE arr;
  VALUE io;
  VALUE format;
  int is_io = 0;
  int done = 0;
	int first_run = 1;
  arr = rb_ary_new();
  rb_scan_args(argc, argv, "11", &io, &format);
	if(NIL_P(format)) format = rb_str_new2(",");

	
#line 147 "excelsior_reader.c"
	{
	cs = excelsior_scan_start;
	ts = 0;
	te = 0;
	act = 0;
	}

#line 91 "excelsior_reader.rl"
  
  is_io = rb_respond_to(io, s_read);
  buf = (char *) malloc(buffer_size); //ALLOC_N(char, buffer_size); <= This caused problems
	  
  while(!done) {
  
    int len, space = buffer_size - have;
    VALUE str;
    p = buf + have;
  
    if(is_io) {
			str = rb_funcall(io, s_read, 1, INT2FIX(space));
			if(first_run) {
				str = rb_str_append(format, str);
				first_run = 0;
			} 
      len = RSTRING_LEN(str);
			p = memcpy(p, StringValuePtr(str), len);
    } else { 
      // Going to assume it's a string and already in memory
      //str = io;
			io = rb_str_append(format, io);
	  	p = RSTRING_PTR(io);
      len = RSTRING_LEN(io);
      pe = p + len;
	  	eof = pe;
	  	done = 1;
    }
    if(len < space) {
      done = 1;
      //p[len++] = 0; can't seem to get it to work with this
      pe = p + len;
      eof = pe;
    } else {
      pe = p + len; 
    }
  	
    
#line 194 "excelsior_reader.c"
	{
	int _klen;
	unsigned int _trans;
	const char *_acts;
	unsigned int _nacts;
	const char *_keys;

	if ( p == pe )
		goto _test_eof;
	if ( cs == 0 )
		goto _out;
_resume:
	_acts = _excelsior_scan_actions + _excelsior_scan_from_state_actions[cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 ) {
		switch ( *_acts++ ) {
	case 2:
#line 1 "excelsior_reader.rl"
	{ts = p;}
	break;
#line 215 "excelsior_reader.c"
		}
	}

	_keys = _excelsior_scan_trans_keys + _excelsior_scan_key_offsets[cs];
	_trans = _excelsior_scan_index_offsets[cs];

	_klen = _excelsior_scan_single_lengths[cs];
	if ( _klen > 0 ) {
		const char *_lower = _keys;
		const char *_mid;
		const char *_upper = _keys + _klen - 1;
		while (1) {
			if ( _upper < _lower )
				break;

			_mid = _lower + ((_upper-_lower) >> 1);
			if ( (*p) < *_mid )
				_upper = _mid - 1;
			else if ( (*p) > *_mid )
				_lower = _mid + 1;
			else {
				_trans += (_mid - _keys);
				goto _match;
			}
		}
		_keys += _klen;
		_trans += _klen;
	}

	_klen = _excelsior_scan_range_lengths[cs];
	if ( _klen > 0 ) {
		const char *_lower = _keys;
		const char *_mid;
		const char *_upper = _keys + (_klen<<1) - 2;
		while (1) {
			if ( _upper < _lower )
				break;

			_mid = _lower + (((_upper-_lower) >> 1) & ~1);
			if ( (*p) < _mid[0] )
				_upper = _mid - 2;
			else if ( (*p) > _mid[1] )
				_lower = _mid + 2;
			else {
				_trans += ((_mid - _keys)>>1);
				goto _match;
			}
		}
		_trans += _klen;
	}

_match:
_eof_trans:
	cs = _excelsior_scan_trans_targs[_trans];

	if ( _excelsior_scan_trans_actions[_trans] == 0 )
		goto _again;

	_acts = _excelsior_scan_actions + _excelsior_scan_trans_actions[_trans];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 )
	{
		switch ( *_acts++ )
		{
	case 3:
#line 1 "excelsior_reader.rl"
	{te = p+1;}
	break;
	case 4:
#line 32 "excelsior_reader.rl"
	{te = p+1;{ {cs = 6; goto _again;} }}
	break;
	case 5:
#line 33 "excelsior_reader.rl"
	{te = p+1;{ {cs = 10; goto _again;} }}
	break;
	case 6:
#line 34 "excelsior_reader.rl"
	{te = p+1;{ {cs = 14; goto _again;} }}
	break;
	case 7:
#line 35 "excelsior_reader.rl"
	{te = p+1;{ {cs = 18; goto _again;} }}
	break;
	case 8:
#line 40 "excelsior_reader.rl"
	{act = 6;}
	break;
	case 9:
#line 41 "excelsior_reader.rl"
	{act = 7;}
	break;
	case 10:
#line 42 "excelsior_reader.rl"
	{act = 8;}
	break;
	case 11:
#line 39 "excelsior_reader.rl"
	{te = p+1;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 12:
#line 43 "excelsior_reader.rl"
	{te = p+1;{ if(has_found == 0) rb_ary_push(arr, Qnil); has_found = 0;}}
	break;
	case 13:
#line 39 "excelsior_reader.rl"
	{te = p;p--;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 14:
#line 42 "excelsior_reader.rl"
	{te = p;p--;{ rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}}
	break;
	case 15:
#line 1 "excelsior_reader.rl"
	{	switch( act ) {
	case 0:
	{{cs = 0; goto _again;}}
	break;
	case 7:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}
	break;
	case 8:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}
	break;
	default:
	{{p = ((te))-1;}}
	break;
	}
	}
	break;
	case 16:
#line 47 "excelsior_reader.rl"
	{act = 11;}
	break;
	case 17:
#line 48 "excelsior_reader.rl"
	{act = 12;}
	break;
	case 18:
#line 49 "excelsior_reader.rl"
	{act = 13;}
	break;
	case 19:
#line 46 "excelsior_reader.rl"
	{te = p+1;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 20:
#line 47 "excelsior_reader.rl"
	{te = p+1;}
	break;
	case 21:
#line 46 "excelsior_reader.rl"
	{te = p;p--;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 22:
#line 49 "excelsior_reader.rl"
	{te = p;p--;{ rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}}
	break;
	case 23:
#line 1 "excelsior_reader.rl"
	{	switch( act ) {
	case 0:
	{{cs = 0; goto _again;}}
	break;
	case 12:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}
	break;
	case 13:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}
	break;
	default:
	{{p = ((te))-1;}}
	break;
	}
	}
	break;
	case 24:
#line 54 "excelsior_reader.rl"
	{act = 16;}
	break;
	case 25:
#line 55 "excelsior_reader.rl"
	{act = 17;}
	break;
	case 26:
#line 56 "excelsior_reader.rl"
	{act = 18;}
	break;
	case 27:
#line 53 "excelsior_reader.rl"
	{te = p+1;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 28:
#line 57 "excelsior_reader.rl"
	{te = p+1;{ if(has_found == 0) rb_ary_push(arr, Qnil); has_found = 0;}}
	break;
	case 29:
#line 53 "excelsior_reader.rl"
	{te = p;p--;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 30:
#line 56 "excelsior_reader.rl"
	{te = p;p--;{ rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}}
	break;
	case 31:
#line 1 "excelsior_reader.rl"
	{	switch( act ) {
	case 0:
	{{cs = 0; goto _again;}}
	break;
	case 17:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}
	break;
	case 18:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}
	break;
	default:
	{{p = ((te))-1;}}
	break;
	}
	}
	break;
	case 32:
#line 61 "excelsior_reader.rl"
	{act = 21;}
	break;
	case 33:
#line 62 "excelsior_reader.rl"
	{act = 22;}
	break;
	case 34:
#line 63 "excelsior_reader.rl"
	{act = 23;}
	break;
	case 35:
#line 60 "excelsior_reader.rl"
	{te = p+1;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 36:
#line 64 "excelsior_reader.rl"
	{te = p+1;{ if(has_found == 0) rb_ary_push(arr, Qnil); has_found = 0;}}
	break;
	case 37:
#line 60 "excelsior_reader.rl"
	{te = p;p--;{ if(has_found ==0) rb_ary_push(arr, Qnil); rb_yield(arr); arr = rb_ary_new(); has_found = 0; }}
	break;
	case 38:
#line 63 "excelsior_reader.rl"
	{te = p;p--;{ rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}}
	break;
	case 39:
#line 1 "excelsior_reader.rl"
	{	switch( act ) {
	case 0:
	{{cs = 0; goto _again;}}
	break;
	case 22:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts, te - ts)); has_found = 1;}
	break;
	case 23:
	{{p = ((te))-1;} rb_ary_push(arr, rb_str_new(ts + 1, te - ts - 2)); has_found = 1;}
	break;
	default:
	{{p = ((te))-1;}}
	break;
	}
	}
	break;
#line 484 "excelsior_reader.c"
		}
	}

_again:
	_acts = _excelsior_scan_actions + _excelsior_scan_to_state_actions[cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 ) {
		switch ( *_acts++ ) {
	case 0:
#line 1 "excelsior_reader.rl"
	{ts = 0;}
	break;
	case 1:
#line 1 "excelsior_reader.rl"
	{act = 0;}
	break;
#line 501 "excelsior_reader.c"
		}
	}

	if ( cs == 0 )
		goto _out;
	if ( ++p != pe )
		goto _resume;
	_test_eof: {}
	if ( p == eof )
	{
	if ( _excelsior_scan_eof_trans[cs] > 0 ) {
		_trans = _excelsior_scan_eof_trans[cs] - 1;
		goto _eof_trans;
	}
	}

	_out: {}
	}

#line 129 "excelsior_reader.rl"
		
		
    if(ts != 0) { // we are not at the end
			have = pe - ts; //so copy stuff back in
			memmove(buf, ts, have);
			te = buf + (te - ts);
			ts = buf;
		}   
    
  }
  
  if(RARRAY_LEN(arr) > 0) { // have a last array to yield
    rb_yield(arr);
  }
  
  return Qnil;
}