Module: CSVScan

Defined in:
ext/csvscan/csvscan.c

Defined Under Namespace

Classes: ParseError

Class Method Summary collapse

Class Method Details

.scan(port) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'ext/csvscan/csvscan.c', line 20

VALUE csv_scan(VALUE self, VALUE port) {
    int cs, act, have = 0, nread = 0, curline = 1;
    unsigned char *tokstart = NULL, *tokend = NULL, *buf;
    VALUE row, coldata;
    VALUE bufsize = Qnil;
    int done=0, buffer_size;

    if ( !rb_respond_to( port, s_read ) ) {
        if ( rb_respond_to( port, s_to_str ) ) {
            port = rb_funcall( port, s_to_str, 0 );
            StringValue(port);
        } else {
            rb_raise( rb_eArgError, "bad argument, String or IO only please." );
        }
    }

    buffer_size = BUFSIZE;
    if (rb_ivar_defined(self, rb_intern("@buffer_size")) == Qtrue) {
        bufsize = rb_ivar_get(self, rb_intern("@buffer_size"));
        if (!NIL_P(bufsize)) {
            buffer_size = NUM2INT(bufsize);
        }
    }
    buf = ALLOC_N(unsigned char, buffer_size);

    
#line 47 "csvscan.c"
	{
	cs = csv_scan_start;
	tokstart = 0;
	tokend = 0;
	act = 0;
	}
#line 102 "csvscan.rl"

    row = rb_ary_new();
    coldata = Qnil;

    while( !done ) {
        VALUE str;
        unsigned char *p = buf + have, *pe;
        int len, space = buffer_size - have;

        if ( space == 0 ) {
            rb_raise(rb_eCSVParseError, "ran out of buffer on line %d.", curline);
        }

        if ( rb_respond_to( port, s_read ) ) {
            str = rb_funcall( port, s_read, 1, INT2FIX(space) );
        } else {
            str = rb_str_substr( port, nread, space );
        }

        StringValue(str);
        memcpy( p, RSTRING_PTR(str), RSTRING_LEN(str) );
        len = RSTRING_LEN(str);
        nread += len;

        /* If this is the last buffer, tack on an EOF. */
        if ( len < space ) {
            p[len++] = 0;
            done = 1;
        }

        pe = p + len;
        
#line 87 "csvscan.c"
	{
	if ( p == pe )
		goto _out;
	switch ( cs )
	{
tr0:
#line 19 "csvscan.rl"
	{tokend = p;{p = ((tokend))-1;}}
	goto st2;
tr1:
#line 10 "csvscan.rl"
	{
        curline += 1;
    }
#line 20 "csvscan.rl"
	{
          rb_ary_push(row, coldata);
          rb_yield(row);
          coldata = Qnil;
          row = rb_ary_new();
      }
#line 20 "csvscan.rl"
	{tokend = p+1;{p = ((tokend))-1;}}
	goto st2;
tr2:
#line 49 "csvscan.rl"
	{tokend = p;{
          unsigned char ch, *start_p, *wptr, *rptr;
          int rest, datalen;
          start_p = wptr = tokstart;
          rptr = tokstart + 1;
          rest = tokend - tokstart - 2;
          datalen = 0;
          while(rest>0) {
              ch = *rptr++;
              if (ch=='"') {
                rptr++;
                rest--;
              }
              *wptr++ = ch;
              datalen++;
              rest--;
          }
          coldata = rb_str_new( start_p, datalen );
      }{p = ((tokend))-1;}}
	goto st2;
tr5:
#line 1 "csvscan.rl"
	{	switch( act ) {
	case 0: tokend = tokstart; {goto st1;}
	case 4:
	{
          unsigned char ch, *endp;
          int datalen;
          datalen = tokend - tokstart;
          endp = tokend - 1;
          while(datalen>0) {
              ch = *endp--;
              if (ch==' ' || ch=='\t') {
                  datalen--;
              } else {
                  break;
              }
          }
          if (datalen==0) {
              coldata = Qnil;
          } else {
              coldata = rb_str_new(tokstart, datalen);
          }
      }
	break;
	case 5:
	{
          unsigned char ch, *start_p, *wptr, *rptr;
          int rest, datalen;
          start_p = wptr = tokstart;
          rptr = tokstart + 1;
          rest = tokend - tokstart - 2;
          datalen = 0;
          while(rest>0) {
              ch = *rptr++;
              if (ch=='"') {
                rptr++;
                rest--;
              }
              *wptr++ = ch;
              datalen++;
              rest--;
          }
          coldata = rb_str_new( start_p, datalen );
      }
	break;
	default: break;
	}
	{p = ((tokend))-1;}}
	goto st2;
tr6:
#line 19 "csvscan.rl"
	{tokend = p+1;{p = ((tokend))-1;}}
	goto st2;
tr7:
#line 19 "csvscan.rl"
	{tokend = p+1;{p = ((tokend))-1;}}
#line 10 "csvscan.rl"
	{
        curline += 1;
    }
#line 20 "csvscan.rl"
	{
          rb_ary_push(row, coldata);
          rb_yield(row);
          coldata = Qnil;
          row = rb_ary_new();
      }
	goto st2;
tr10:
#line 26 "csvscan.rl"
	{tokend = p+1;{
          rb_ary_push(row, coldata);
          coldata = Qnil;
      }{p = ((tokend))-1;}}
	goto st2;
st2:
#line 1 "csvscan.rl"
	{tokstart = 0;}
#line 1 "csvscan.rl"
	{act = 0;}
	if ( ++p == pe )
		goto _out2;
case 2:
#line 1 "csvscan.rl"
	{tokstart = p;}
#line 220 "csvscan.c"
	switch( (*p) ) {
		case 9u: goto tr6;
		case 10u: goto tr7;
		case 13u: goto st4;
		case 32u: goto tr6;
		case 34u: goto st0;
		case 44u: goto tr10;
	}
	if ( 11u <= (*p) && (*p) <= 12u )
		goto tr8;
	goto tr4;
tr4:
#line 1 "csvscan.rl"
	{tokend = p+1;}
#line 30 "csvscan.rl"
	{act = 4;}
	goto st3;
tr8:
#line 1 "csvscan.rl"
	{tokend = p+1;}
#line 19 "csvscan.rl"
	{act = 1;}
	goto st3;
st3:
	if ( ++p == pe )
		goto _out3;
case 3:
#line 248 "csvscan.c"
	switch( (*p) ) {
		case 10u: goto tr5;
		case 13u: goto tr5;
		case 34u: goto tr5;
		case 44u: goto tr5;
	}
	goto tr4;
st4:
	if ( ++p == pe )
		goto _out4;
case 4:
	if ( (*p) == 10u )
		goto tr1;
	goto tr0;
tr11:
#line 10 "csvscan.rl"
	{
        curline += 1;
    }
	goto st0;
st0:
	if ( ++p == pe )
		goto _out0;
case 0:
#line 273 "csvscan.c"
	switch( (*p) ) {
		case 10u: goto tr11;
		case 34u: goto tr12;
	}
	goto st0;
tr12:
#line 1 "csvscan.rl"
	{tokend = p+1;}
#line 49 "csvscan.rl"
	{act = 5;}
	goto st5;
st5:
	if ( ++p == pe )
		goto _out5;
case 5:
#line 289 "csvscan.c"
	if ( (*p) == 34u )
		goto st0;
	goto tr2;
st1:
	goto _out1;
	}
	_out2: cs = 2; goto _out; 
	_out3: cs = 3; goto _out; 
	_out4: cs = 4; goto _out; 
	_out0: cs = 0; goto _out; 
	_out5: cs = 5; goto _out; 
	_out1: cs = 1; goto _out; 

	_out: {}
	}
#line 134 "csvscan.rl"

        if ( cs == csv_scan_error ) {
            free(buf);
            rb_raise(rb_eCSVParseError, "parse error on line %d.", curline);
        }

        if ( tokstart == 0 ) {
            have = 0;
        } else {
            have = pe - tokstart;
            memmove( buf, tokstart, have );
            tokend = buf + (tokend - tokstart);
            tokstart = buf;
        }
    }
    free(buf);
    return Qnil;
}