Sha256: a5078a26277812fe568500b1b5e4effc871a1cd0ba573f9be64923cd1d4f5229

Contents?: true

Size: 925 Bytes

Versions: 1

Compression:

Stored size: 925 Bytes

Contents

#include "ccsv.h"

static VALUE rb_cC;

static VALUE foreach(VALUE self, VALUE filename) {

  FILE *file = fopen(StringValueCStr(filename), "r");
  if (file == NULL)
    rb_raise(rb_eRuntimeError, "File not found");

  char *line = NULL;
  size_t len = 0;
  char *token;
  int idx;

  VALUE ary;
  
  while (getline(&line, &len, file) != -1) {
    ary = rb_ary_new();
    token = strtok(line, DELIMITERS);
    idx = 0;

    while (token != NULL) {
      rb_ary_store(ary, idx, rb_str_new(token, strlen(token)));
      idx ++;
      token = strtok(NULL, DELIMITERS);
    }

    /* OBJ_FREEZE(ary); */
    rb_yield(ary);
    /* FL_UNSET((ary), FL_FREEZE); */

    /* for(idx = 0; idx < RARRAY_LEN(ary); idx ++) {
      rb_ary_store(ary, idx, Qnil);
    } */

  }

  fclose(file);

  return Qnil;
}

void
Init_ccsv()
{
  rb_cC = rb_define_class("Ccsv", rb_cObject);
  rb_define_singleton_method(rb_cC, "foreach", foreach, 1);
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccsv-0.1.2 ext/ccsv.c