split/Dtable/dtable.c in tioga-1.5 vs split/Dtable/dtable.c in tioga-1.6
- old
+ new
@@ -1529,13 +1529,14 @@
PRIVATE
/*======================================================================*/ VALUE Read_Dtable(VALUE dest, char *filename, int skip_lines) {
FILE *file = NULL;
long num_cols, num_rows;
- int i, j, k;
+ int i, j, k, len;
const int buff_len = 10000;
- char c, buff[buff_len], *p, *pend;
+ const int err_len = 100;
+ char c, buff[buff_len], *p, *pend, err_str[err_len];
double *data, **ptr = Dtable_Ptr(dest, &num_cols, &num_rows);
if ((file=fopen(filename,"r")) == NULL)
rb_raise(rb_eArgError, "failed to open %s", filename);
for (i = 0; i < skip_lines; i++) { /* skip over initial lines */
if (fgets(buff, buff_len, file)==NULL) {
@@ -1569,13 +1570,16 @@
data[j] = strtod(buff,&pend);
}
}
if (!is_okay_number(data[j])) {
fclose(file);
+ len = (pend-buff < err_len-1)? pend-buff : err_len-1;
+ printf("len %i\n", len);
+ strncpy(err_str,buff,len);
rb_raise(rb_eArgError,
- "reached end of file before reading requested amount of data in %s (asked for %i xs and %i ys; found only %i and %i)",
- filename, num_cols, num_rows, i+1, j);
+ "failed to read requested amount of data in %s (asked for %i xs and %i ys; found only %i and %i). last attempt to read got %g from string starting with: %s",
+ filename, num_cols, num_rows, i+1, j, data[j], err_str);
}
}
}
fclose(file);
return dest;
@@ -1662,13 +1666,14 @@
double * col;
long target_len = 1 /* first signature byte */
+ 8 /* 2 * length */
+ cols * rows * 8 ;
unsigned u_len;
- VALUE str = rb_str_buf_new(target_len);
+ VALUE str = rb_str_new2("");
+ rb_str_resize(str,target_len); /* This seems to do the trick */
/* \begin{playing with ruby's internals} */
- unsigned char * ptr = (unsigned char *) RSTRING(str)->ptr;
+ unsigned char * ptr = (unsigned char *) RSTRING_PTR(str);
/* signature byte */
(*ptr++) = DTABLE_DUMP_VERSION;
u_len = (unsigned) rows; /* limits to 4 billions rows */
STORE_UNSIGNED(u_len, ptr); /* destroys u_len */
u_len = (unsigned) cols; /* limits to 4 billions columns */
@@ -1680,11 +1685,11 @@
{
store_double(*(col++), ptr);
ptr += 8;
}
}
- RSTRING(str)->len = target_len;
+ /* RSTRING_LEN(str) = target_len;*/
return str;
/* \end{playing with ruby's internals} */
}
PRIVATE
@@ -1695,10 +1700,10 @@
VALUE dtable_load(VALUE klass, VALUE str)
{
VALUE ret = Qnil;
VALUE s = StringValue(str);
unsigned char * buf = (unsigned char *) StringValuePtr(s);
- unsigned char * dest = buf + RSTRING(s)->len;
+ unsigned char * dest = buf + RSTRING_LEN(s);
unsigned i; /* for GET_UNSIGNED */
unsigned tmp = 0;
long rows, cols;
long x,y;
double ** data;