Class: SimpleCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecsv/version.rb,
ext/simplecsv/rb_simplecsv.c

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION Classes: Error, InvalidError, NoMemoryError, ParseError, TooBigError

Constant Summary collapse

STRICT =
INT2FIX(CSV_STRICT)
REPALL_NL =
INT2FIX(CSV_REPALL_NL)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

:nodoc:



304
305
306
307
# File 'ext/simplecsv/rb_simplecsv.c', line 304

static VALUE simplecsv_initialize(VALUE self)
{
    return Qnil;
}

Class Method Details

.new(options = nil) ⇒ Object

Create a SimpleCSV instance object.

Parameters

options

Interger : parser options. You can specify SimpleCSV::STRICT and/or SimpleCSV::REPALL_NL.

Exceptions

TypeError

options isn’t Fixnum / nil.

SimpleCSV::Error

failed to initialize csv parser.



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
# File 'ext/simplecsv/rb_simplecsv.c', line 274

static VALUE simplecsv_s_new(int argc, VALUE* argv, VALUE klass)
{
    VALUE new_obj;
    VALUE opts;
    struct csv_parser_object* parser_obj;
    struct csv_parser* parser;
    int options;

    rb_scan_args(argc, argv, "01", &opts);

    Set_Options(opts, options);

    new_obj = Data_Make_Struct(klass, struct csv_parser_object, NULL, free_parser_object, parser_obj);

    if (csv_init(&parser, options) != 0) {
        rb_raise(eCSVError, "failed to initialize csv parser");
    }

    parser_obj->_parser = parser;

    rb_obj_call_init(new_obj, argc, argv);

    rb_ivar_set(new_obj, rb_intern("'row'"), rb_ary_new());

    return new_obj;
}

.parse(str, options = nil) {|row| ... } ⇒ Object

Parse csv string.

some examples below:

parse csv data

SimpleCSV.parse("1,2,3\n\"a\", \"b\", \"c\"\n") do |row|
  p row
end
#=> ["1", "2", "3"]
#=> ["a", "b", "c"]

srtict mode

Raise SimpleCSV::ParseError exception while parsing incorrect quoted string.

SimpleCSV.parse("\"ab\"c\"\n", SimpleCSV::STRICT) do |row|
  p row
end
#=> `parse': error while parsing by malformed data (SimpleCSV::ParseError)

blank line

SimpleCSV.parse pass blank line by default. You can get [] when processing blank line with SimpleCSV::REPALL_NL option.

SimpleCSV.parse("\n") do |row|
  p row
end
#=> nothing

SimpleCSV.parse("\n", SimpleCSV::REPALL_NL) do |row|
  p row
end
#=> []

strict and yield each blank line

You can use ‘bit or’ to specify both options.

SimpleCSV.parse(str, SimpleCSV::STRICT | SimpleCSV::REPALL_NL)

Parameters

str

String : csv string.

options

Interger : parser options. You can specify SimpleCSV::STRICT and/or SimpleCSV::REPALL_NL.

row

Array of Strings : parsed data.

Exceptions

TypeError

options isn’t Fixnum / nil.

SimpleCSV::Error

failed to initialize csv parser.

SimpleCSV::ParseError

failed to parse.

LocalJumpError

You must call this method with block.

Yields:

  • (row)


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
# File 'ext/simplecsv/rb_simplecsv.c', line 156

static VALUE simplecsv_s_parse(int argc, VALUE* argv, VALUE klass)
{
    VALUE str, opts;
    struct csv_parser* parser;
    int options;
    int len;

    rb_scan_args(argc, argv, "11", &str, &opts);

    Check_Type(str, T_STRING);
    Set_Options(opts, options);

    if (csv_init(&parser, options) != 0) {
        rb_raise(eCSVError, "failed to initialize csv parser");
    }

    rb_ivar_set(klass, rb_intern("'row'"), rb_ary_new());

    len = RSTRING(str)->len;
    if (csv_parse(parser, StringValuePtr(str), len, default_field_callback, default_row_callback, (void*)klass) != len) {
        switch (csv_error(parser)) {
            case CSV_EPARSE:
                rb_raise(eCSVParseError, "error while parsing by malformed data");
                break;
            case CSV_ENOMEM:
                rb_raise(eCSVNoMemError, "no memory");
                break;
            case CSV_ETOOBIG:
                rb_raise(eCSVTooBigError, "too large field data");
                break;
            case CSV_EINVALID:
                rb_raise(eCSVInvalid, csv_strerror(csv_error(parser)));
                break;
            default:
                rb_raise(eCSVError, "failed to parse by unknown reason");
        }
    }

    csv_fini(parser, default_field_callback, default_row_callback, (void*)klass);
    csv_free(parser);

    return Qnil;
}

.write(array) ⇒ Object

Returns quoted csv string.

examples

puts SimpleCSV.write(['1', '2', '3'])
#=> "1","2","3"

puts SimpleCSV.write(['"']
#=> """"

Parameters

array

Array of Strings: source data

Returns

csv-string

String : quoted csv string.

Exceptions

TypeError

array isn’t Array or each content of array isn’t String.



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
# File 'ext/simplecsv/rb_simplecsv.c', line 225

static VALUE simplecsv_s_write(VALUE klass, VALUE array)
{
    int i; 
    VALUE ret, str;
    char buf[255];
    char* buf2;
    size_t len;

    Check_Type(array, T_ARRAY);

    ret = rb_str_new2("");
    for (i = 0; i < RARRAY(array)->len; i++) {
        str = RARRAY(array)->ptr[i];
        Check_Type(str, T_STRING);
        /* because SimpleCSV.write(['"'*127]).size #=> 256 */
        if (RSTRING(str)->len < 127) {
            /* use fixed size buffer when short string */
            len = csv_write(buf, 255, StringValuePtr(str), RSTRING(str)->len);
            rb_str_cat(ret, buf, len);
        } else {
            /* allocate csv_write processed size buffer when long string */
            len = csv_write(NULL, 0, StringValuePtr(str), RSTRING(str)->len);
            buf2 = ALLOCA_N(char, len);
            len = csv_write(buf2, len, StringValuePtr(str), RSTRING(str)->len);
            rb_str_cat(ret, buf2, len);
        }
        if (i != RARRAY(array)->len - 1) {
            rb_str_cat(ret, ",", 1);
        }
    }

    return ret;
}

Instance Method Details

#on_field(str, pr) ⇒ Object

This method is called when processing each field. You can write “pr.call(some_val)” for pass value to block of ‘parse’ method.

Parameters

str

String : field text.

pr

Proc : a block, passing by ‘parse’ method



431
432
433
434
435
436
437
438
439
# File 'ext/simplecsv/rb_simplecsv.c', line 431

static VALUE simplecsv_on_field(VALUE self, VALUE str, VALUE proc)
{
    VALUE row;

    row = rb_ivar_get(self, rb_intern("'row'"));
    rb_ary_push(row, str);

    return Qnil;
}

#on_row(str, pr) ⇒ Object

This method is called when processing each row. You can write “pr.call(some_val)” for pass value to block of ‘parse’ method.

Parameters

str

String : row delimitor (n).

pr

Proc : a block, passing by ‘parse’ method



454
455
456
457
458
459
460
461
462
463
# File 'ext/simplecsv/rb_simplecsv.c', line 454

static VALUE simplecsv_on_row(VALUE self, VALUE str, VALUE proc)
{
    VALUE row;

    row = rb_ivar_get(self, rb_intern("'row'"));
    rb_funcall(proc, rb_intern("call"), 1, row);
    rb_ivar_set(self, rb_intern("'row'"), rb_ary_new());

    return Qnil;
}

#optionsObject

options -> options

Get option values.

Returns

options

Interger : parser options. SimpleCSV::STRICT and/or SimpleCSVREPALL_NL.



319
320
321
322
323
324
325
326
327
# File 'ext/simplecsv/rb_simplecsv.c', line 319

static VALUE simplecsv_get_options(VALUE self)
{
    struct csv_parser_object* parser_object;
    struct csv_parser* parser;

    Get_Parser(self, parser_object, parser);

    return INT2FIX(parser->options);
}

#options=(new_options) ⇒ Object

Set option values.

Parameters

options

Interger : parser options. SimpleCSV::STRICT and/or SimpleCSVREPALL_NL. You can specify nil to clear options.



340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'ext/simplecsv/rb_simplecsv.c', line 340

static VALUE simplecsv_set_options(VALUE self, VALUE opts)
{
    struct csv_parser_object* parser_object;
    struct csv_parser* parser;
    int options;

    Get_Parser(self, parser_object, parser);
    Set_Options(opts, options);

    csv_opts(parser, options);

    return Qnil;
}

#parse(str, options = nil) {|row| ... } ⇒ Object

Parse csv string. Same as SimpleCSV.parse.

Parameters

str

String : csv string.

options

Interger : parser options. You can specify SimpleCSV::STRICT and/or SimpleCSVREPALL_NL.

row

Array of Strings : parsed data.

Exceptions

TypeError

options isn’t Fixnum/nil.

SimpleCSV::Error

failed to initialize csv parser.

SimpleCSV::ParseError

failed to parse.

LocalJumpError

You must call this method with block.

Yields:

  • (row)


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
# File 'ext/simplecsv/rb_simplecsv.c', line 374

static VALUE simplecsv_parse(int argc, VALUE* argv, VALUE self)
{
    VALUE str, proc;
    struct csv_parser_object* parser_object;
    struct csv_parser* parser;
    int len;

    rb_scan_args(argc, argv, "1&", &str, &proc);

    Check_Type(str, T_STRING);

    if (CLASS_OF(self) == cSimpleCSV) {
        rb_need_block();
    }

    rb_ivar_set(self, rb_intern("'proc'"), proc);

    Get_Parser(self, parser_object, parser);

    len = RSTRING(str)->len;
    if (csv_parse(parser, StringValuePtr(str), len, field_callback, row_callback, (void*)self) != len) {
        switch (csv_error(parser)) {
            case CSV_EPARSE:
                rb_raise(eCSVParseError, "error while parsing by malformed data");
                break;
            case CSV_ENOMEM:
                rb_raise(eCSVNoMemError, "no memory");
                break;
            case CSV_ETOOBIG:
                rb_raise(eCSVTooBigError, "too large field data");
                break;
            case CSV_EINVALID:
                rb_raise(eCSVInvalid, csv_strerror(csv_error(parser)));
                break;
            default:
                rb_raise(eCSVError, "failed to parse by unknown reason");
        }
    }

    csv_fini(parser, field_callback, row_callback, (void*)self);

    return Qnil;
}

#write(array) ⇒ Object

Returns quoted csv string.

examples

puts SimpleCSV.write(['1', '2', '3'])
#=> "1","2","3"

puts SimpleCSV.write(['"']
#=> """"

Parameters

array

Array of Strings: source data

Returns

csv-string

String : quoted csv string.

Exceptions

TypeError

array isn’t Array or each content of array isn’t String.



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
# File 'ext/simplecsv/rb_simplecsv.c', line 225

static VALUE simplecsv_s_write(VALUE klass, VALUE array)
{
    int i; 
    VALUE ret, str;
    char buf[255];
    char* buf2;
    size_t len;

    Check_Type(array, T_ARRAY);

    ret = rb_str_new2("");
    for (i = 0; i < RARRAY(array)->len; i++) {
        str = RARRAY(array)->ptr[i];
        Check_Type(str, T_STRING);
        /* because SimpleCSV.write(['"'*127]).size #=> 256 */
        if (RSTRING(str)->len < 127) {
            /* use fixed size buffer when short string */
            len = csv_write(buf, 255, StringValuePtr(str), RSTRING(str)->len);
            rb_str_cat(ret, buf, len);
        } else {
            /* allocate csv_write processed size buffer when long string */
            len = csv_write(NULL, 0, StringValuePtr(str), RSTRING(str)->len);
            buf2 = ALLOCA_N(char, len);
            len = csv_write(buf2, len, StringValuePtr(str), RSTRING(str)->len);
            rb_str_cat(ret, buf2, len);
        }
        if (i != RARRAY(array)->len - 1) {
            rb_str_cat(ret, ",", 1);
        }
    }

    return ret;
}