| 1 |
#include <R.h> |
| 2 |
#include <Rdefines.h> |
| 3 |
#include <string.h> |
| 4 |
#include <Rmath.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <ctype.h> |
| 7 |
#include <Rinternals.h> |
| 8 |
#include "memisc.h" |
| 9 |
#include "rofile.h" |
| 10 |
|
| 11 |
int _R_atoi(char *text){ |
| 12 |
char *end_ptr; |
| 13 |
if(!strlen(text)) return NA_INTEGER; |
| 14 |
int result = (int)strtol(text,&end_ptr,10); |
| 15 |
if(!isdigit(end_ptr[-1])) return NA_INTEGER; |
| 16 |
else return result; |
| 17 |
} |
| 18 |
|
| 19 |
double _R_atof(char *text){ |
| 20 |
char *end_ptr; |
| 21 |
if(!strlen(text)) return NA_REAL; |
| 22 |
double result = strtod(text,&end_ptr); |
| 23 |
if(!isdigit(end_ptr[-1]) && isdigit(end_ptr[-1]) != '.') return NA_REAL; |
| 24 |
else return result; |
| 25 |
} |
| 26 |
|
| 27 |
size_t Rgetline(char **lineptr, size_t *lenptr, FILE *file){ |
| 28 |
|
| 29 |
char *line = *lineptr; |
| 30 |
size_t len = *lenptr; |
| 31 |
|
| 32 |
if (file == NULL) { |
| 33 |
error("File pointer is null."); |
| 34 |
} |
| 35 |
int chunk_size = 128; |
| 36 |
if(len < chunk_size) len = chunk_size; |
| 37 |
|
| 38 |
if(line == NULL){ |
| 39 |
line = (char *)R_alloc(len,sizeof(char)); |
| 40 |
if(line == NULL) |
| 41 |
error("Could not allocate line pointer"); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
char ch = getc(file); |
| 46 |
size_t n = 0; |
| 47 |
|
| 48 |
while((ch != '\n') && (ch != '\r') && (ch != EOF)){ |
| 49 |
|
| 50 |
|
| 51 |
if(n == len){ |
| 52 |
|
| 53 |
char *tmp = (char *)R_alloc(len+chunk_size,sizeof(char)); |
| 54 |
if(tmp == NULL) |
| 55 |
error("Could not extend buffer size"); |
| 56 |
memcpy(tmp,line,len); |
| 57 |
line = tmp; |
| 58 |
len += chunk_size; |
| 59 |
|
| 60 |
} |
| 61 |
line[n] = ch; |
| 62 |
n++; |
| 63 |
|
| 64 |
ch = getc(file); |
| 65 |
} |
| 66 |
while((ch == '\n') || (ch == '\r')) ch = getc(file); |
| 67 |
if((ch != '\n') && (ch != '\r') && (ch != EOF)) ungetc(ch,file); |
| 68 |
|
| 69 |
line[n] = '\0'; |
| 70 |
*lineptr = line; |
| 71 |
*lenptr = len; |
| 72 |
return n; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
SEXP readfixed(SEXP s_file, SEXP what, SEXP s_nlines, SEXP s_start, SEXP s_stop){ |
| 77 |
PROTECT(s_start = coerceVector(s_start,INTSXP)); |
| 78 |
PROTECT(s_stop = coerceVector(s_stop,INTSXP)); |
| 79 |
FILE *f = rofile_FILE(s_file); |
| 80 |
if(LENGTH(s_start) != LENGTH(s_stop)) error("start and stop must have equal length"); |
| 81 |
int n = asInteger(s_nlines); |
| 82 |
int nvar = LENGTH(s_start); |
| 83 |
int *start = INTEGER(s_start); |
| 84 |
int *stop = INTEGER(s_stop); |
| 85 |
size_t max_lenline = 0; |
| 86 |
size_t cur_len; |
| 87 |
char *buffer = NULL; |
| 88 |
char *item, *currdata; |
| 89 |
SEXP data; |
| 90 |
PROTECT(data=allocVector(VECSXP,nvar)); |
| 91 |
int i,j; |
| 92 |
int *length = (int *) R_alloc(nvar,sizeof(int)); |
| 93 |
int maxlen = 0; |
| 94 |
SEXP x,y; |
| 95 |
for(j = 0; j < nvar; j++){ |
| 96 |
length[j] = stop[j] - start[j] + 1; |
| 97 |
if(maxlen < length[j]) maxlen = length[j]; |
| 98 |
x = VECTOR_ELT(what,j); |
| 99 |
SET_VECTOR_ELT(data,j,lengthgets(x,n)); |
| 100 |
} |
| 101 |
item = R_alloc(maxlen+1,1); |
| 102 |
#ifdef DEBUG |
| 103 |
Rprintf("Requested number of lines: %d\n",n); |
| 104 |
#endif |
| 105 |
for(i = 0; i < n; i++){ |
| 106 |
cur_len = Rgetline(&buffer,&max_lenline,f); |
| 107 |
#ifdef DEBUG |
| 108 |
Rprintf("Requested line length: %d\n",max_lenline); |
| 109 |
Rprintf("Actual line length: %d\n",strlen(buffer)); |
| 110 |
if(i < 3) |
| 111 |
Rprintf("Buffer: >>%s<<\n",buffer); |
| 112 |
#endif |
| 113 |
currdata = buffer; |
| 114 |
for(j = 0; j < nvar; j++){ |
| 115 |
x = VECTOR_ELT(data,j); |
| 116 |
currdata = buffer + start[j]-1; |
| 117 |
memset(item,0,maxlen+1); |
| 118 |
memcpy(item,currdata,length[j]); |
| 119 |
trim(item,length[j]); |
| 120 |
#undef DEBUG |
| 121 |
if(TYPEOF(x)==INTSXP) |
| 122 |
INTEGER(x)[i] = _R_atoi(item); |
| 123 |
else if (TYPEOF(x)==REALSXP) |
| 124 |
REAL(x)[i] = _R_atof(item); |
| 125 |
else |
| 126 |
SET_STRING_ELT(x,i,mkChar(item)); |
| 127 |
} |
| 128 |
} |
| 129 |
for(j = 0; j < nvar; j++){ |
| 130 |
x = VECTOR_ELT(what,j); |
| 131 |
y = VECTOR_ELT(data,j); |
| 132 |
copyMostAttrib(x,y); |
| 133 |
} |
| 134 |
UNPROTECT(3); |
| 135 |
return data; |
| 136 |
} |
| 137 |
|
| 138 |
SEXP countlines(SEXP s_file){ |
| 139 |
FILE *f = rofile_FILE(s_file); |
| 140 |
size_t max_lenline = 0; |
| 141 |
size_t cur_len; |
| 142 |
char *buffer = NULL; |
| 143 |
int i, n; |
| 144 |
|
| 145 |
for(i = 0;; i++){ |
| 146 |
cur_len = Rgetline(&buffer,&max_lenline,f); |
| 147 |
#ifdef DEBUG |
| 148 |
Rprintf("Line: %d\n",i); |
| 149 |
#endif |
| 150 |
if(feof(f)) { |
| 151 |
#ifdef DEBUG |
| 152 |
Rprintf("Requested line length: %d\n",max_lenline); |
| 153 |
Rprintf("Actual line length: %d\n",strlen(buffer)); |
| 154 |
Rprintf("Buffer: >>%s<<\n",buffer); |
| 155 |
#endif |
| 156 |
if(cur_len > 0) |
| 157 |
n = i + 1; |
| 158 |
else |
| 159 |
n = i; |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
return ScalarInteger(n); |
| 164 |
} |
| 165 |
|
| 166 |
SEXP readfixedslice(SEXP s_file, SEXP what, SEXP s_vars, SEXP s_cases, SEXP s_start, SEXP s_stop){ |
| 167 |
FILE *f = rofile_FILE(s_file); |
| 168 |
PROTECT(s_vars = coerceVector(s_vars,LGLSXP)); |
| 169 |
PROTECT(s_cases = coerceVector(s_cases,LGLSXP)); |
| 170 |
PROTECT(s_start = coerceVector(s_start,INTSXP)); |
| 171 |
PROTECT(s_stop = coerceVector(s_stop,INTSXP)); |
| 172 |
if(LENGTH(s_start) != LENGTH(s_stop)) error("start and stop must have equal length"); |
| 173 |
if(LENGTH(s_vars) != LENGTH(s_stop)) error("vars argument has wrong length"); |
| 174 |
int m = 0, n = 0; |
| 175 |
int nvar = LENGTH(what); |
| 176 |
int ncases = LENGTH(s_cases); |
| 177 |
int ii,i,j,k; |
| 178 |
for(i = 0; i < LENGTH(s_cases); i++) n += LOGICAL(s_cases)[i]; |
| 179 |
for(j = 0; j < LENGTH(s_vars); j++) m += LOGICAL(s_vars)[j]; |
| 180 |
int *start = INTEGER(s_start); |
| 181 |
int *stop = INTEGER(s_stop); |
| 182 |
size_t max_lenline = 0; |
| 183 |
size_t cur_len; |
| 184 |
char *buffer = NULL; |
| 185 |
char *item, *currdata; |
| 186 |
|
| 187 |
SEXP data; |
| 188 |
PROTECT(data = allocVector(VECSXP,m)); |
| 189 |
SEXP x, y; |
| 190 |
int *length = (int *) R_alloc(nvar,sizeof(int)); |
| 191 |
int maxlen = 0; |
| 192 |
k = 0; |
| 193 |
for(j = 0; j < nvar; j++){ |
| 194 |
length[j] = stop[j] - start[j] + 1; |
| 195 |
if(LOGICAL(s_vars)[j]){ |
| 196 |
if(maxlen < length[j]) maxlen = length[j]; |
| 197 |
x = VECTOR_ELT(what,j); |
| 198 |
SET_VECTOR_ELT(data,k,lengthgets(x,n)); |
| 199 |
k++; |
| 200 |
} |
| 201 |
} |
| 202 |
item = R_alloc(maxlen+1,1); |
| 203 |
ii = 0; |
| 204 |
for(i = 0; i < ncases; i++){ |
| 205 |
cur_len = Rgetline(&buffer,&max_lenline,f); |
| 206 |
#ifdef DEBUG |
| 207 |
Rprintf("Requested line length: %d\n",max_lenline); |
| 208 |
Rprintf("Actual line length: %d\n",strlen(buffer)); |
| 209 |
Rprintf("Buffer: >>%s<<\n",buffer); |
| 210 |
#endif |
| 211 |
if(LOGICAL(s_cases)[i]){ |
| 212 |
currdata = buffer; |
| 213 |
k = 0; |
| 214 |
for(j = 0; j < nvar; j++){ |
| 215 |
currdata = buffer + start[j]-1; |
| 216 |
if(LOGICAL(s_vars)[j]){ |
| 217 |
x = VECTOR_ELT(data,k); |
| 218 |
memset(item,0,maxlen+1); |
| 219 |
memcpy(item,currdata,length[j]); |
| 220 |
trim(item,length[j]); |
| 221 |
#ifdef DEBUG |
| 222 |
Rprintf("Item: >>%s<<\n",item); |
| 223 |
#endif |
| 224 |
#undef DEBUG |
| 225 |
if(TYPEOF(x)==INTSXP) |
| 226 |
INTEGER(x)[ii] = _R_atoi(item); |
| 227 |
else if (TYPEOF(x)==REALSXP) |
| 228 |
REAL(x)[ii] = _R_atof(item); |
| 229 |
else |
| 230 |
SET_STRING_ELT(x,ii,mkChar(item)); |
| 231 |
k++; |
| 232 |
} |
| 233 |
} |
| 234 |
ii++; |
| 235 |
} |
| 236 |
} |
| 237 |
k = 0; |
| 238 |
for(j = 0; j < nvar; j++){ |
| 239 |
if(LOGICAL(s_vars)[j]){ |
| 240 |
x = VECTOR_ELT(what,j); |
| 241 |
y = VECTOR_ELT(data,k); |
| 242 |
copyMostAttrib(x,y); |
| 243 |
k++; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
UNPROTECT(5); |
| 248 |
return data; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
SEXP readfixedchunk(SEXP s_file, SEXP what, SEXP s_vars, SEXP s_nlines, SEXP s_start, SEXP s_stop){ |
| 253 |
PROTECT(s_vars = coerceVector(s_vars,LGLSXP)); |
| 254 |
PROTECT(s_start = coerceVector(s_start,INTSXP)); |
| 255 |
PROTECT(s_stop = coerceVector(s_stop,INTSXP)); |
| 256 |
FILE *f = rofile_FILE(s_file); |
| 257 |
if(LENGTH(s_start) != LENGTH(s_stop)) error("start and stop must have equal length"); |
| 258 |
int m = 0; |
| 259 |
int n = asInteger(s_nlines); |
| 260 |
int nvar = LENGTH(what); |
| 261 |
int i,j,k; |
| 262 |
for(j = 0; j < LENGTH(s_vars); j++) m += LOGICAL(s_vars)[j]; |
| 263 |
int *start = INTEGER(s_start); |
| 264 |
int *stop = INTEGER(s_stop); |
| 265 |
size_t max_lenline = 0; |
| 266 |
size_t cur_len; |
| 267 |
char *buffer = NULL; |
| 268 |
char *item, *currdata; |
| 269 |
SEXP data; |
| 270 |
PROTECT(data=allocVector(VECSXP,m)); |
| 271 |
int *length = (int *) R_alloc(nvar,sizeof(int)); |
| 272 |
int maxlen = 0; |
| 273 |
SEXP x,y; |
| 274 |
k = 0; |
| 275 |
for(j = 0; j < nvar; j++){ |
| 276 |
length[j] = stop[j] - start[j] + 1; |
| 277 |
if(LOGICAL(s_vars)[j]){ |
| 278 |
if(maxlen < length[j]) maxlen = length[j]; |
| 279 |
x = VECTOR_ELT(what,j); |
| 280 |
SET_VECTOR_ELT(data,k,lengthgets(x,n)); |
| 281 |
k++; |
| 282 |
} |
| 283 |
} |
| 284 |
item = R_alloc(maxlen+1,1); |
| 285 |
#undef DEBUG |
| 286 |
#ifdef DEBUG |
| 287 |
Rprintf("Requested number of lines: %d\n",n); |
| 288 |
#endif |
| 289 |
for(i = 0; i < n; i++){ |
| 290 |
cur_len = Rgetline(&buffer,&max_lenline,f); |
| 291 |
#ifdef DEBUG |
| 292 |
Rprintf("Requested line length: %d\n",max_lenline); |
| 293 |
Rprintf("Actual line length: %d\n",strlen(buffer)); |
| 294 |
if(i == 0) |
| 295 |
Rprintf("Buffer: >>%s<<\n",buffer); |
| 296 |
#endif |
| 297 |
currdata = buffer; |
| 298 |
k = 0; |
| 299 |
for(j = 0; j < nvar; j++){ |
| 300 |
currdata = buffer + start[j]-1; |
| 301 |
if(LOGICAL(s_vars)[j]){ |
| 302 |
x = VECTOR_ELT(data,k); |
| 303 |
memset(item,0,maxlen+1); |
| 304 |
memcpy(item,currdata,length[j]); |
| 305 |
trim(item,length[j]); |
| 306 |
#ifdef DEBUG |
| 307 |
Rprintf("Item: >>%s<<\n",item); |
| 308 |
#endif |
| 309 |
#undef DEBUG |
| 310 |
if(TYPEOF(x)==INTSXP) |
| 311 |
INTEGER(x)[i] = _R_atoi(item); |
| 312 |
else if (TYPEOF(x)==REALSXP) |
| 313 |
REAL(x)[i] = _R_atof(item); |
| 314 |
else |
| 315 |
SET_STRING_ELT(x,i,mkChar(item)); |
| 316 |
k++; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
k = 0; |
| 321 |
for(j = 0; j < nvar; j++){ |
| 322 |
if(LOGICAL(s_vars)[j]){ |
| 323 |
x = VECTOR_ELT(what,j); |
| 324 |
y = VECTOR_ELT(data,k); |
| 325 |
copyMostAttrib(x,y); |
| 326 |
k++; |
| 327 |
} |
| 328 |
} |
| 329 |
UNPROTECT(4); |
| 330 |
return data; |
| 331 |
} |