| 1 |
#include <R.h> |
| 2 |
#include <Rdefines.h> |
| 3 |
#include <string.h> |
| 4 |
#include <Rmath.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <Rinternals.h> |
| 7 |
#include "memisc.h" |
| 8 |
#include "dumbswap.h" |
| 9 |
|
| 10 |
typedef int R_int32; |
| 11 |
|
| 12 |
typedef double R_flt64; |
| 13 |
|
| 14 |
typedef struct { |
| 15 |
FILE *f; |
| 16 |
unsigned char bytes[8]; |
| 17 |
int byte_pos; |
| 18 |
int case_size; |
| 19 |
int compressed; |
| 20 |
R_flt64 bias; |
| 21 |
R_flt64 *buf; |
| 22 |
int swap_code; |
| 23 |
int data_pos; |
| 24 |
R_flt64 sysmis; |
| 25 |
R_flt64 highest; |
| 26 |
R_flt64 lowest; |
| 27 |
} sys_file; |
| 28 |
|
| 29 |
typedef union { |
| 30 |
R_flt64 as_double; |
| 31 |
char as_char[8]; |
| 32 |
} sys_word; |
| 33 |
|
| 34 |
static double second_lowest_double_val(void); |
| 35 |
#define SYSMIS -DBL_MAX |
| 36 |
#define HIGHEST DBL_MAX |
| 37 |
#define LOWEST second_lowest_double_val() |
| 38 |
|
| 39 |
#undef DEBUG |
| 40 |
|
| 41 |
|
| 42 |
void init_sys_file(sys_file *s){ |
| 43 |
#ifdef DEBUG |
| 44 |
Rprintf("\ninit_sys_file"); |
| 45 |
Rprintf("\ns = %d",s); |
| 46 |
Rprintf("\ns->f = %d",s->f); |
| 47 |
#endif |
| 48 |
memset(s->bytes,0,8); |
| 49 |
s->byte_pos = 0; |
| 50 |
s->case_size = 0; |
| 51 |
s->bias = 100.; |
| 52 |
s->swap_code = 0; |
| 53 |
s->sysmis = SYSMIS; |
| 54 |
s->highest = HIGHEST; |
| 55 |
s->lowest = LOWEST; |
| 56 |
fseek(s->f,0,SEEK_SET); |
| 57 |
} |
| 58 |
|
| 59 |
int sys_read_int(R_int32 *target, sys_file *s){ |
| 60 |
R_int32 x; |
| 61 |
int read_len = (int)fread(&x,sizeof(R_int32),1,s->f); |
| 62 |
*target = iswap(x,s->swap_code); |
| 63 |
return read_len; |
| 64 |
} |
| 65 |
|
| 66 |
int sys_read_real(R_flt64 *target, sys_file *s){ |
| 67 |
R_flt64 x; |
| 68 |
int read_len = (int)fread(&x,sizeof(R_flt64),1,s->f); |
| 69 |
*target = dswap(x,s->swap_code); |
| 70 |
return read_len; |
| 71 |
} |
| 72 |
|
| 73 |
int sys_read_octet(char *target, sys_file *s){ |
| 74 |
int read_len = (int)fread(target,8,1,s->f); |
| 75 |
return read_len; |
| 76 |
} |
| 77 |
|
| 78 |
int sys_read_string(char *target, sys_file *s){ |
| 79 |
int str_len; |
| 80 |
int read_len = (int)fread(&str_len,1,1,s->f); |
| 81 |
str_len = (str_len/8)*8+7; |
| 82 |
char *string = S_alloc(str_len+1,1); |
| 83 |
read_len = (int)fread(string,str_len,1,s->f); |
| 84 |
return read_len; |
| 85 |
} |
| 86 |
|
| 87 |
int sys_read(void *target, int n, sys_file *s){ |
| 88 |
return (int)fread(target,1,n,s->f); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
int sys_read_case(sys_file *s){ |
| 102 |
#ifdef DEBUG |
| 103 |
Rprintf("\nsys_read_case"); |
| 104 |
Rprintf("\ncase size: %d",s->case_size); |
| 105 |
#endif |
| 106 |
int read_len; |
| 107 |
if(!s->compressed) |
| 108 |
return (int)fread(s->buf,8,s->case_size,s->f); |
| 109 |
else{ |
| 110 |
int j,k = s->byte_pos; |
| 111 |
for(j = 0; j < s->case_size; j++){ |
| 112 |
for(; k < 8; k++){ |
| 113 |
if (s->bytes[k]!=0) break; |
| 114 |
} |
| 115 |
if(k >= 8) { |
| 116 |
read_len = (int)fread(s->bytes,1,8,s->f); |
| 117 |
#ifdef DEBUG |
| 118 |
Rprintf("\nread length for bytes: %d",read_len); |
| 119 |
Rprintf("\nnew bytes=(%d,%d,%d,%d,%d,%d,%d,%d)", |
| 120 |
s->bytes[0], |
| 121 |
s->bytes[1], |
| 122 |
s->bytes[2], |
| 123 |
s->bytes[3], |
| 124 |
s->bytes[4], |
| 125 |
s->bytes[5], |
| 126 |
s->bytes[6], |
| 127 |
s->bytes[7]); |
| 128 |
#endif |
| 129 |
k = 0; |
| 130 |
if(read_len == 0) return 0; |
| 131 |
if(read_len < 8) return j; |
| 132 |
} |
| 133 |
#ifdef DEBUG |
| 134 |
Rprintf("\nbytes[%d]=%d",k,s->bytes[k]); |
| 135 |
#endif |
| 136 |
|
| 137 |
if(s->bytes[k] == 252) return j; |
| 138 |
else if(s->bytes[k] == 253) { |
| 139 |
read_len = (int)fread(s->buf+j,8,1,s->f); |
| 140 |
if(!read_len) return j; |
| 141 |
} |
| 142 |
else if(s->bytes[k] == 254) { |
| 143 |
memset(s->buf+j,' ',8); |
| 144 |
} |
| 145 |
else if(s->bytes[k] == 255) { |
| 146 |
s->buf[j] = dswap(s->sysmis,s->swap_code); |
| 147 |
} |
| 148 |
else { |
| 149 |
s->buf[j] = (R_flt64)s->bytes[k] - s->bias; |
| 150 |
} |
| 151 |
k++; |
| 152 |
#ifdef DEBUG |
| 153 |
Rprintf("\ns->buf[%d]=%g",j,s->buf[j]); |
| 154 |
#endif |
| 155 |
} |
| 156 |
s->byte_pos = k; |
| 157 |
#ifdef DEBUG |
| 158 |
Rprintf("\ns->f = %d",s->f); |
| 159 |
Rprintf("\ns->byte_pos = %d",s->byte_pos); |
| 160 |
Rprintf("\ns->case_size = %d",s->case_size); |
| 161 |
Rprintf("\nFile pos = %d",ftell(s->f)); |
| 162 |
#endif |
| 163 |
return j; |
| 164 |
} |
| 165 |
return 0; |
| 166 |
#undef DEBUG |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
SEXP closeSysFile (SEXP); |
| 174 |
|
| 175 |
SEXP NewSysFile (SEXP name){ |
| 176 |
#ifdef DEBUG |
| 177 |
Rprintf("\nNewSysFile"); |
| 178 |
#endif |
| 179 |
PROTECT(name = coerceVector(name,STRSXP)); |
| 180 |
|
| 181 |
sys_file *s = R_Calloc(1,sys_file); |
| 182 |
s->f = fopen(CHAR(STRING_ELT(name, 0)),"rb"); |
| 183 |
#ifdef DEBUG |
| 184 |
Rprintf("\nfile = %d",s->f); |
| 185 |
#endif |
| 186 |
if(s->f == NULL){ |
| 187 |
R_Free(s); |
| 188 |
UNPROTECT(1); |
| 189 |
return R_NilValue; |
| 190 |
} |
| 191 |
else { |
| 192 |
init_sys_file(s); |
| 193 |
SEXP ans = R_MakeExternalPtr(s, install("sys_file"), R_NilValue); |
| 194 |
PROTECT(ans); |
| 195 |
R_RegisterCFinalizer(ans, (R_CFinalizer_t) closeSysFile); |
| 196 |
setAttrib(ans,install("file.name"),name); |
| 197 |
UNPROTECT(2); |
| 198 |
return ans; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
SEXP sys_file_restore_from_attrib(SEXP SysFile, sys_file *s,const char* attribname){ |
| 204 |
SEXP ans = getAttrib(SysFile,install(attribname)); |
| 205 |
if(ans == R_NilValue || ans == NULL) { |
| 206 |
fclose(s->f); |
| 207 |
R_SetExternalPtrAddr(SysFile,NULL); |
| 208 |
R_Free(s); |
| 209 |
error("cannot restore sysfile data: missing '%s' attribute",attribname); |
| 210 |
} |
| 211 |
return ans; |
| 212 |
} |
| 213 |
|
| 214 |
sys_file *get_sys_file(SEXP SysFile){ |
| 215 |
if(TYPEOF(SysFile) != EXTPTRSXP || R_ExternalPtrTag(SysFile) != install("sys_file")) |
| 216 |
error("not a SysFile"); |
| 217 |
sys_file *s = R_ExternalPtrAddr(SysFile); |
| 218 |
if (s == NULL){ |
| 219 |
error("external pointer is NULL, you need to recreate this object"); |
| 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 |
if(s== NULL || s->f == NULL) |
| 249 |
error("file pointer is NULL"); |
| 250 |
return(s); |
| 251 |
} |
| 252 |
|
| 253 |
SEXP closeSysFile (SEXP SysFile){ |
| 254 |
if(TYPEOF(SysFile) != EXTPTRSXP || R_ExternalPtrTag(SysFile) != install("sys_file")) |
| 255 |
error("not a SysFile"); |
| 256 |
sys_file *s = R_ExternalPtrAddr(SysFile); |
| 257 |
if (s != NULL) { |
| 258 |
fclose(s->f); |
| 259 |
R_Free(s->buf); |
| 260 |
R_ClearExternalPtr(SysFile); |
| 261 |
} |
| 262 |
return R_NilValue; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
struct sysfile_header |
| 269 |
{ |
| 270 |
char rec_type[4+1]; |
| 271 |
char prod_name[60+1]; |
| 272 |
R_int32 layout_code; |
| 273 |
R_int32 case_size; |
| 274 |
R_int32 compressed; |
| 275 |
R_int32 weight_index; |
| 276 |
R_int32 ncases; |
| 277 |
R_flt64 bias; |
| 278 |
char creation_date[9+1]; |
| 279 |
char creation_time[8+1]; |
| 280 |
char file_label[64+1]; |
| 281 |
char padding[3]; |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
SEXP read_sysfile_header(SEXP SysFile){ |
| 286 |
#ifdef DEBUG |
| 287 |
Rprintf("\nread_sysfile_header"); |
| 288 |
#endif |
| 289 |
|
| 290 |
sys_file *s = get_sys_file(SysFile); |
| 291 |
|
| 292 |
struct sysfile_header h; |
| 293 |
|
| 294 |
memset(h.rec_type,0,4+1); |
| 295 |
memset(h.prod_name,0,6+1); |
| 296 |
memset(h.creation_date,0,9+1); |
| 297 |
memset(h.creation_time,0,8+1); |
| 298 |
memset(h.file_label,0,64+1); |
| 299 |
sys_read(h.rec_type,4,s); |
| 300 |
if(strncmp(h.rec_type,"$FL2",4)!=0) error("not a sysfile"); |
| 301 |
sys_read(h.prod_name,60,s); |
| 302 |
sys_read_int(&h.layout_code,s); |
| 303 |
sys_read_int(&h.case_size,s); |
| 304 |
sys_read_int(&h.compressed,s); |
| 305 |
sys_read_int(&h.weight_index,s); |
| 306 |
sys_read_int(&h.ncases,s); |
| 307 |
sys_read_real(&h.bias,s); |
| 308 |
sys_read(h.creation_date,9,s); |
| 309 |
sys_read(h.creation_time,8,s); |
| 310 |
sys_read(h.file_label,64,s); |
| 311 |
sys_read(h.padding,3,s); |
| 312 |
|
| 313 |
SEXP ans; |
| 314 |
PROTECT(ans = allocVector(VECSXP,12)); |
| 315 |
int protectcounter = 1; |
| 316 |
|
| 317 |
#ifdef DEBUG |
| 318 |
Rprintf("\nh.rec_type = %s",h.rec_type); |
| 319 |
Rprintf("\nh.case_size = %x",h.case_size); |
| 320 |
Rprintf("\nh.case_size = %d",h.case_size); |
| 321 |
Rprintf("\nh.layout_code = %x",h.layout_code); |
| 322 |
Rprintf("\nh.layout_code = %d",h.layout_code); |
| 323 |
#endif |
| 324 |
if(h.layout_code == 2) |
| 325 |
s->swap_code = 0; |
| 326 |
else |
| 327 |
s->swap_code = 1; |
| 328 |
s->compressed = iswap(h.compressed,s->swap_code); |
| 329 |
s->case_size = iswap(h.case_size,s->swap_code); |
| 330 |
s->bias = dswap(h.bias,s->swap_code); |
| 331 |
s->buf = R_Calloc(s->case_size,R_flt64); |
| 332 |
|
| 333 |
SET_VECTOR_ELT(ans,0,mkString(h.prod_name)); |
| 334 |
SET_VECTOR_ELT(ans,1,ScalarInteger(iswap(h.layout_code,s->swap_code))); |
| 335 |
SET_VECTOR_ELT(ans,2,ScalarInteger(iswap(h.case_size,s->swap_code))); |
| 336 |
SET_VECTOR_ELT(ans,3,ScalarInteger(iswap(h.compressed,s->swap_code))); |
| 337 |
SET_VECTOR_ELT(ans,4,ScalarInteger(iswap(h.weight_index,s->swap_code))); |
| 338 |
SET_VECTOR_ELT(ans,5,ScalarInteger(iswap(h.ncases,s->swap_code))); |
| 339 |
SET_VECTOR_ELT(ans,6,ScalarReal(dswap(h.bias,s->swap_code))); |
| 340 |
SET_VECTOR_ELT(ans,7,mkString(h.creation_date)); |
| 341 |
SET_VECTOR_ELT(ans,8,mkString(h.creation_time)); |
| 342 |
SET_VECTOR_ELT(ans,9,mkString(h.file_label)); |
| 343 |
SET_VECTOR_ELT(ans,10,ScalarInteger(s->swap_code)); |
| 344 |
SET_VECTOR_ELT(ans,11,mkString(h.rec_type)); |
| 345 |
|
| 346 |
|
| 347 |
SEXP names; |
| 348 |
PROTECT(names = allocVector(STRSXP,12)); |
| 349 |
protectcounter++; |
| 350 |
SET_STRING_ELT(names,0,mkChar("prod_name")); |
| 351 |
SET_STRING_ELT(names,1,mkChar("layout_code")); |
| 352 |
SET_STRING_ELT(names,2,mkChar("case_size")); |
| 353 |
SET_STRING_ELT(names,3,mkChar("compressed")); |
| 354 |
SET_STRING_ELT(names,4,mkChar("weight_index")); |
| 355 |
SET_STRING_ELT(names,5,mkChar("ncases")); |
| 356 |
SET_STRING_ELT(names,6,mkChar("bias")); |
| 357 |
SET_STRING_ELT(names,7,mkChar("creation_date")); |
| 358 |
SET_STRING_ELT(names,8,mkChar("creation_time")); |
| 359 |
SET_STRING_ELT(names,9,mkChar("file_label")); |
| 360 |
SET_STRING_ELT(names,10,mkChar("swap_code")); |
| 361 |
SET_STRING_ELT(names,11,mkChar("rec_type")); |
| 362 |
SET_NAMES(ans,names); |
| 363 |
|
| 364 |
UNPROTECT(protectcounter); |
| 365 |
#ifdef DEBUG |
| 366 |
PrintValue(ans); |
| 367 |
#endif |
| 368 |
return ans; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
struct sysfile_variable |
| 374 |
{ |
| 375 |
R_int32 rec_type; |
| 376 |
R_int32 type; |
| 377 |
|
| 378 |
R_int32 has_var_label; |
| 379 |
R_int32 n_missing_values; |
| 380 |
R_int32 print; |
| 381 |
R_int32 write; |
| 382 |
char name[8+1]; |
| 383 |
R_int32 label_len; |
| 384 |
char* label; |
| 385 |
R_flt64 missing_values[3]; |
| 386 |
|
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
SEXP read_sysfile_var(SEXP SysFile){ |
| 391 |
#ifdef DEBUG |
| 392 |
Rprintf("\nread_sysfile_var"); |
| 393 |
#endif |
| 394 |
sys_file *s = get_sys_file(SysFile); |
| 395 |
int protectcounter = 0; |
| 396 |
|
| 397 |
SEXP var, vnames; |
| 398 |
int i ; |
| 399 |
struct sysfile_variable curr_var; |
| 400 |
memset(curr_var.name,0,8+1); |
| 401 |
sys_read_int(&curr_var.rec_type,s); |
| 402 |
#ifdef DEBUG |
| 403 |
Rprintf("\ntag: %d",curr_var.rec_type); |
| 404 |
#endif |
| 405 |
if(curr_var.rec_type != 2) { |
| 406 |
#ifdef DEBUG |
| 407 |
Rprintf("%d\n",curr_var.rec_type); |
| 408 |
Rprintf("%s\n",&curr_var.rec_type); |
| 409 |
#endif |
| 410 |
|
| 411 |
fseek(s->f,-4,SEEK_CUR); |
| 412 |
return R_NilValue; |
| 413 |
} |
| 414 |
sys_read_int(&curr_var.type,s); |
| 415 |
sys_read_int(&curr_var.has_var_label,s); |
| 416 |
sys_read_int(&curr_var.n_missing_values,s); |
| 417 |
sys_read_int(&curr_var.print,s); |
| 418 |
sys_read_int(&curr_var.write,s); |
| 419 |
sys_read_octet(curr_var.name,s); |
| 420 |
trim(curr_var.name,8); |
| 421 |
|
| 422 |
long pos; |
| 423 |
if(curr_var.has_var_label){ |
| 424 |
sys_read_int(&curr_var.label_len,s); |
| 425 |
curr_var.label = R_alloc(curr_var.label_len+1,1); |
| 426 |
memset(curr_var.label,0,curr_var.label_len+1); |
| 427 |
sys_read(curr_var.label,curr_var.label_len,s); |
| 428 |
trim(curr_var.label,curr_var.label_len); |
| 429 |
pos = ftell(s->f); |
| 430 |
if(pos%4 != 0) fseek(s->f,(pos/4+1)*4,SEEK_SET); |
| 431 |
} |
| 432 |
int nmiss = 0; |
| 433 |
if(curr_var.n_missing_values!=0){ |
| 434 |
nmiss = curr_var.n_missing_values; |
| 435 |
for(i = 0; i < abs(nmiss); i++) |
| 436 |
sys_read_real(curr_var.missing_values+i,s); |
| 437 |
#ifdef DEBUG |
| 438 |
Rprintf("\n"); |
| 439 |
for(i = 0; i < abs(nmiss); i++){ |
| 440 |
Rprintf("\nR_PosInf = (as hex)%llx",R_PosInf); |
| 441 |
Rprintf("\n%d missval:",i); |
| 442 |
Rprintf("\nas char = %s",(char *)(curr_var.missing_values)+i); |
| 443 |
Rprintf("\nas hex = %llx",curr_var.missing_values[i]); |
| 444 |
Rprintf("\nas hex = %.9a",curr_var.missing_values[i]); |
| 445 |
Rprintf("\nas real = %.9e",curr_var.missing_values[i]); |
| 446 |
} |
| 447 |
#endif |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
PROTECT(var = allocVector(VECSXP,8)); |
| 453 |
protectcounter++; |
| 454 |
|
| 455 |
SET_VECTOR_ELT(var,0,mkString(curr_var.name)); |
| 456 |
SET_VECTOR_ELT(var,1,ScalarInteger(curr_var.type)); |
| 457 |
SET_VECTOR_ELT(var,2,ScalarInteger(curr_var.has_var_label)); |
| 458 |
SET_VECTOR_ELT(var,3,ScalarInteger(curr_var.n_missing_values)); |
| 459 |
SEXP curr_print, curr_write; |
| 460 |
PROTECT(curr_print = allocVector(INTSXP,4)); |
| 461 |
PROTECT(curr_write = allocVector(INTSXP,4)); |
| 462 |
protectcounter+=2; |
| 463 |
char *tmp; |
| 464 |
tmp = (char *)&curr_var.print; |
| 465 |
for(i = 0; i < 4; i++) |
| 466 |
INTEGER(curr_print)[i] = (int)tmp[i]; |
| 467 |
tmp = (char *)&curr_var.write; |
| 468 |
for(i = 0; i < 4; i++) |
| 469 |
INTEGER(curr_write)[i] = (int)tmp[i]; |
| 470 |
SET_VECTOR_ELT(var,4,curr_print); |
| 471 |
SET_VECTOR_ELT(var,5,curr_write); |
| 472 |
|
| 473 |
if(curr_var.has_var_label){ |
| 474 |
SET_VECTOR_ELT(var,6,mkString(curr_var.label)); |
| 475 |
} |
| 476 |
else SET_VECTOR_ELT(var,6,R_NilValue); |
| 477 |
|
| 478 |
SEXP s_missings; |
| 479 |
if(curr_var.n_missing_values!=0){ |
| 480 |
PROTECT(s_missings = allocVector(REALSXP,abs(nmiss))); |
| 481 |
protectcounter++; |
| 482 |
for(i = 0; i < abs(nmiss); i++) |
| 483 |
REAL(s_missings)[i] = curr_var.missing_values[i]; |
| 484 |
SET_VECTOR_ELT(var,7,s_missings); |
| 485 |
} |
| 486 |
else SET_VECTOR_ELT(var,7,R_NilValue); |
| 487 |
|
| 488 |
PROTECT(vnames = allocVector(STRSXP,8)); |
| 489 |
protectcounter++; |
| 490 |
SET_STRING_ELT(vnames,0,mkChar("name")); |
| 491 |
SET_STRING_ELT(vnames,1,mkChar("type")); |
| 492 |
SET_STRING_ELT(vnames,2,mkChar("has_var_label")); |
| 493 |
SET_STRING_ELT(vnames,3,mkChar("n_missing_values")); |
| 494 |
SET_STRING_ELT(vnames,4,mkChar("print")); |
| 495 |
SET_STRING_ELT(vnames,5,mkChar("write")); |
| 496 |
SET_STRING_ELT(vnames,6,mkChar("label")); |
| 497 |
SET_STRING_ELT(vnames,7,mkChar("missings")); |
| 498 |
SET_NAMES(var,vnames); |
| 499 |
#ifdef DEBUG |
| 500 |
Rprintf("\nCurrent variable: %s",curr_var.name); |
| 501 |
PrintValue(var); |
| 502 |
Rprintf("\nPosition: %f",ftell(s->f)); |
| 503 |
#endif |
| 504 |
|
| 505 |
UNPROTECT(protectcounter); |
| 506 |
return var; |
| 507 |
} |
| 508 |
#undef DEBUG |
| 509 |
|
| 510 |
SEXP test_sysfile_int32(SEXP SysFile){ |
| 511 |
#ifdef DEBUG |
| 512 |
Rprintf("\ntest_sysfile_int32"); |
| 513 |
#endif |
| 514 |
sys_file *s = get_sys_file(SysFile); |
| 515 |
R_int32 number; |
| 516 |
sys_read_int(&number,s); |
| 517 |
fseek(s->f,-4,SEEK_CUR); |
| 518 |
#ifdef DEBUG |
| 519 |
Rprintf("\nnumber = %x",number); |
| 520 |
#endif |
| 521 |
return ScalarInteger(number); |
| 522 |
} |
| 523 |
|
| 524 |
SEXP read_sysfile_value_labels (SEXP SysFile){ |
| 525 |
#ifdef DEBUG |
| 526 |
Rprintf("\nread_sysfile_value_labels"); |
| 527 |
#endif |
| 528 |
sys_file *s = get_sys_file(SysFile); |
| 529 |
int protectcounter = 0; |
| 530 |
R_int32 rec_type; |
| 531 |
R_int32 nlabels; |
| 532 |
sys_read_int(&rec_type,s); |
| 533 |
if(rec_type != 3) error("expecting a value label record"); |
| 534 |
sys_read_int(&nlabels,s); |
| 535 |
#ifdef DEBUG |
| 536 |
Rprintf("\nnlabels=%d",nlabels); |
| 537 |
#endif |
| 538 |
SEXP labels, values; |
| 539 |
PROTECT(labels=allocVector(STRSXP,nlabels)); |
| 540 |
PROTECT(values=allocVector(REALSXP,nlabels)); |
| 541 |
protectcounter += 2; |
| 542 |
int i; |
| 543 |
double value; |
| 544 |
|
| 545 |
char *labbuf = S_alloc(256,1); |
| 546 |
|
| 547 |
for(i = 0; i < nlabels; i++){ |
| 548 |
sys_read_real(&value,s); |
| 549 |
REAL(values)[i] = value; |
| 550 |
unsigned char lablen, readlen; |
| 551 |
sys_read(&lablen,1,s); |
| 552 |
readlen = (unsigned char)((lablen/8)*8+7); |
| 553 |
sys_read(labbuf,readlen,s); |
| 554 |
labbuf[lablen] = 0; |
| 555 |
#ifdef DEBUG |
| 556 |
Rprintf("\nvalues[%d]=%g",i,value); |
| 557 |
Rprintf("\nlabels[%d]=|%s|",i,labbuf); |
| 558 |
Rprintf("\nlength(labels[%d])=%d",i,lablen); |
| 559 |
#endif |
| 560 |
trim(labbuf,lablen); |
| 561 |
SET_STRING_ELT(labels,i,mkChar(labbuf)); |
| 562 |
} |
| 563 |
SET_NAMES(values,labels); |
| 564 |
|
| 565 |
|
| 566 |
sys_read_int(&rec_type,s); |
| 567 |
if(rec_type != 4) error("expecting a value label variables record, found instead a record tagged %d",rec_type); |
| 568 |
R_int32 nvars; |
| 569 |
sys_read_int(&nvars,s); |
| 570 |
R_int32 var; |
| 571 |
SEXP variables; |
| 572 |
PROTECT(variables = allocVector(INTSXP,nvars)); |
| 573 |
protectcounter++; |
| 574 |
for(i = 0; i < nvars; i++){ |
| 575 |
sys_read_int(&var,s); |
| 576 |
INTEGER(variables)[i] = var; |
| 577 |
} |
| 578 |
SEXP ans, names; |
| 579 |
PROTECT(ans = allocVector(VECSXP,2)); |
| 580 |
PROTECT(names = allocVector(STRSXP,2)); |
| 581 |
protectcounter+=2; |
| 582 |
SET_VECTOR_ELT(ans,0,values); |
| 583 |
SET_STRING_ELT(names,0,mkChar("labels")); |
| 584 |
SET_VECTOR_ELT(ans,1,variables); |
| 585 |
SET_STRING_ELT(names,1,mkChar("variables")); |
| 586 |
SET_NAMES(ans,names); |
| 587 |
UNPROTECT(protectcounter); |
| 588 |
#ifdef DEBUG |
| 589 |
PrintValue(ans); |
| 590 |
#endif |
| 591 |
return ans; |
| 592 |
} |
| 593 |
|
| 594 |
#define DEBUG |
| 595 |
SEXP num_to_string8(SEXP num_values){ |
| 596 |
char tmp_char[9]; |
| 597 |
int nlabels = length(num_values); |
| 598 |
R_flt64 tmp_real; |
| 599 |
int i; |
| 600 |
SEXP ans; |
| 601 |
PROTECT(ans = allocVector(STRSXP,nlabels)); |
| 602 |
memset(tmp_char,0,9); |
| 603 |
for(i = 0; i < nlabels; i++){ |
| 604 |
tmp_real = REAL(num_values)[i]; |
| 605 |
memcpy(tmp_char,&tmp_real,8); |
| 606 |
SET_STRING_ELT(ans,i,mkChar(tmp_char)); |
| 607 |
} |
| 608 |
UNPROTECT(1); |
| 609 |
return ans; |
| 610 |
} |
| 611 |
#undef DEBUG |
| 612 |
|
| 613 |
SEXP read_sysfile_document(SEXP SysFile){ |
| 614 |
#ifdef DEBUG |
| 615 |
Rprintf("\nread_sysfile_document"); |
| 616 |
#endif |
| 617 |
sys_file *s = get_sys_file(SysFile); |
| 618 |
int protectcounter = 0; |
| 619 |
R_int32 rec_type; |
| 620 |
sys_read_int(&rec_type,s); |
| 621 |
if(rec_type != 6) error("expecting a document record"); |
| 622 |
R_int32 nlines; |
| 623 |
sys_read_int(&nlines,s); |
| 624 |
SEXP lines; |
| 625 |
PROTECT(lines = allocVector(STRSXP,nlines)); |
| 626 |
protectcounter++; |
| 627 |
char buf[81]; |
| 628 |
memset(buf,0,81); |
| 629 |
int i; |
| 630 |
for(i = 0; i < nlines; i++){ |
| 631 |
sys_read(buf,80,s); |
| 632 |
SET_STRING_ELT(lines,i,mkChar(buf)); |
| 633 |
} |
| 634 |
UNPROTECT(protectcounter); |
| 635 |
return lines; |
| 636 |
} |
| 637 |
|
| 638 |
static const int info_int32 = 3; |
| 639 |
static const int info_flt64 = 4; |
| 640 |
static const int aux_var = 11; |
| 641 |
static const int long_var_names = 13; |
| 642 |
static const int aux_enc = 20; |
| 643 |
|
| 644 |
SEXP read_sysfile_aux(SEXP SysFile){ |
| 645 |
#ifdef DEBUG |
| 646 |
Rprintf("\nread_sysfile_aux"); |
| 647 |
#endif |
| 648 |
sys_file *s = get_sys_file(SysFile); |
| 649 |
int protectcounter = 0; |
| 650 |
R_int32 rec_type, subtype; |
| 651 |
sys_read_int(&rec_type,s); |
| 652 |
if(rec_type != 7) error("expecting a machine info record"); |
| 653 |
sys_read_int(&subtype,s); |
| 654 |
SEXP ans, names, data, datanames; |
| 655 |
R_int32 size, count; |
| 656 |
int i, j; |
| 657 |
if(subtype == info_int32){ |
| 658 |
#ifdef DEBUG |
| 659 |
Rprintf("\nsubtype == info_int32"); |
| 660 |
#endif |
| 661 |
PROTECT(ans = allocVector(VECSXP,2)); |
| 662 |
PROTECT(names = allocVector(STRSXP,2)); |
| 663 |
protectcounter+=2; |
| 664 |
SET_VECTOR_ELT(ans,0,mkString("info_int32")); |
| 665 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 666 |
sys_read_int(&size,s); |
| 667 |
sys_read_int(&count,s); |
| 668 |
#ifdef DEBUG |
| 669 |
Rprintf("\nsize = %d",size); |
| 670 |
Rprintf("\nsize = %x",size); |
| 671 |
Rprintf("\ncount = %d",count); |
| 672 |
#endif |
| 673 |
if(size != 4) error("we're in trouble here: size != 4"); |
| 674 |
PROTECT(data = allocVector(INTSXP,8)); |
| 675 |
PROTECT(datanames = allocVector(STRSXP,8)); |
| 676 |
protectcounter+=2; |
| 677 |
int x; |
| 678 |
for(i = 0; i < 8; i++){ |
| 679 |
sys_read_int(&x,s); |
| 680 |
INTEGER(data)[i] = x; |
| 681 |
} |
| 682 |
static const char* int32names[] = { |
| 683 |
"version_major", |
| 684 |
"version_minor", |
| 685 |
"version_revision", |
| 686 |
"machine_code", |
| 687 |
"floating_point_rep", |
| 688 |
"compression_code", |
| 689 |
"endianness", |
| 690 |
"character_code" |
| 691 |
}; |
| 692 |
for(i = 0; i < 8; i++){ |
| 693 |
SET_STRING_ELT(datanames,i, mkChar(int32names[i])); |
| 694 |
} |
| 695 |
SET_NAMES(data,datanames); |
| 696 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 697 |
SET_VECTOR_ELT(ans,1,data); |
| 698 |
SET_NAMES(ans,names); |
| 699 |
UNPROTECT(protectcounter); |
| 700 |
#ifdef DEBUG |
| 701 |
PrintValue(ans); |
| 702 |
#endif |
| 703 |
return ans; |
| 704 |
} |
| 705 |
else if (subtype == info_flt64){ |
| 706 |
#ifdef DEBUG |
| 707 |
Rprintf("\nsubtype == info_flt64"); |
| 708 |
#endif |
| 709 |
PROTECT(ans = allocVector(VECSXP,2)); |
| 710 |
PROTECT(names = allocVector(STRSXP,2)); |
| 711 |
protectcounter+=2; |
| 712 |
SET_VECTOR_ELT(ans,0,mkString("info_flt64")); |
| 713 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 714 |
sys_read_int(&size,s); |
| 715 |
sys_read_int(&count,s); |
| 716 |
#ifdef DEBUG |
| 717 |
Rprintf("\nsize = %d",size); |
| 718 |
Rprintf("\nsize = %x",size); |
| 719 |
Rprintf("\ncount = %d",count); |
| 720 |
#endif |
| 721 |
if(size != 8) error("we're in trouble here: size != 8"); |
| 722 |
PROTECT(data = allocVector(REALSXP,3)); |
| 723 |
PROTECT(datanames = allocVector(STRSXP,3)); |
| 724 |
protectcounter+=2; |
| 725 |
for(i = 0; i < count; i++) |
| 726 |
sys_read_real(REAL(data)+i,s); |
| 727 |
s->sysmis = REAL(data)[0]; |
| 728 |
s->highest = REAL(data)[1]; |
| 729 |
s->lowest = REAL(data)[2]; |
| 730 |
static const char* flt64names[] = { |
| 731 |
"sysmis", |
| 732 |
"highest", |
| 733 |
"lowest" |
| 734 |
}; |
| 735 |
for(i = 0; i < 3; i++){ |
| 736 |
SET_STRING_ELT(datanames,i,mkChar(flt64names[i])); |
| 737 |
} |
| 738 |
SET_NAMES(data,datanames); |
| 739 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 740 |
SET_VECTOR_ELT(ans,1,data); |
| 741 |
SET_NAMES(ans,names); |
| 742 |
UNPROTECT(protectcounter); |
| 743 |
#ifdef DEBUG |
| 744 |
PrintValue(ans); |
| 745 |
#endif |
| 746 |
return ans; |
| 747 |
} |
| 748 |
else if (subtype == aux_var){ |
| 749 |
#ifdef DEBUG |
| 750 |
Rprintf("\nsubtype == aux_var"); |
| 751 |
#endif |
| 752 |
PROTECT(ans = allocVector(VECSXP,2)); |
| 753 |
PROTECT(names = allocVector(STRSXP,2)); |
| 754 |
protectcounter+=2; |
| 755 |
SET_VECTOR_ELT(ans,0,mkString("aux_var")); |
| 756 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 757 |
sys_read_int(&size,s); |
| 758 |
sys_read_int(&count,s); |
| 759 |
#ifdef DEBUG |
| 760 |
Rprintf("\nsize = %d",size); |
| 761 |
Rprintf("\nsize = %x",size); |
| 762 |
Rprintf("\ncount = %d",count); |
| 763 |
#endif |
| 764 |
if(size != 4) error("we're in trouble here: size != 4"); |
| 765 |
int aux_nvars = count/3; |
| 766 |
PROTECT(data = allocVector(VECSXP,aux_nvars)); |
| 767 |
protectcounter++; |
| 768 |
SEXP auxdata, auxnames; |
| 769 |
for(i = 0; i < aux_nvars; i++){ |
| 770 |
char *_auxnames[]={ |
| 771 |
"measure", |
| 772 |
"width", |
| 773 |
"alignment" |
| 774 |
}; |
| 775 |
PROTECT(auxdata = allocVector(INTSXP,3)); |
| 776 |
PROTECT(auxnames = allocVector(STRSXP,3)); |
| 777 |
for(j = 0; j < 3; j++){ |
| 778 |
sys_read_int(INTEGER(auxdata)+j,s); |
| 779 |
SET_STRING_ELT(auxnames,j,mkChar(_auxnames[j])); |
| 780 |
} |
| 781 |
SET_NAMES(auxdata,auxnames); |
| 782 |
SET_VECTOR_ELT(data,i,auxdata); |
| 783 |
UNPROTECT(2); |
| 784 |
} |
| 785 |
SET_VECTOR_ELT(ans,1,data); |
| 786 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 787 |
SET_NAMES(ans,names); |
| 788 |
UNPROTECT(protectcounter); |
| 789 |
#ifdef DEBUG |
| 790 |
PrintValue(ans); |
| 791 |
#endif |
| 792 |
return ans; |
| 793 |
} |
| 794 |
else if(subtype == long_var_names){ |
| 795 |
#ifdef DEBUG |
| 796 |
Rprintf("\nsubtype == long_var_names"); |
| 797 |
#endif |
| 798 |
PROTECT(ans = NEW_LIST(2)); |
| 799 |
PROTECT(names = allocVector(STRSXP,2)); |
| 800 |
protectcounter+=2; |
| 801 |
SET_VECTOR_ELT(ans,0,mkString("longVariableNames")); |
| 802 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 803 |
sys_read_int(&size,s); |
| 804 |
sys_read_int(&count,s); |
| 805 |
#ifdef DEBUG |
| 806 |
Rprintf("\nsize = %d",size); |
| 807 |
Rprintf("\nsize = %x",size); |
| 808 |
Rprintf("\ncount = %d",count); |
| 809 |
#endif |
| 810 |
|
| 811 |
SEXP longnames; |
| 812 |
PROTECT(longnames = allocVector(STRSXP,1)); |
| 813 |
protectcounter++; |
| 814 |
char *longnames_data; |
| 815 |
longnames_data = R_alloc(count+1,1); |
| 816 |
memset(longnames_data,0,count+1); |
| 817 |
sys_read(longnames_data,count,s); |
| 818 |
SET_STRING_ELT(longnames,0,mkChar(longnames_data)); |
| 819 |
SET_VECTOR_ELT(ans,1,longnames); |
| 820 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 821 |
SET_NAMES(ans,names); |
| 822 |
|
| 823 |
UNPROTECT(protectcounter); |
| 824 |
#ifdef DEBUG |
| 825 |
PrintValue(ans); |
| 826 |
#endif |
| 827 |
return ans; |
| 828 |
} |
| 829 |
else if(subtype == aux_enc) { |
| 830 |
#ifdef DEBUG |
| 831 |
Rprintf("\nsubtype == aux_enc"); |
| 832 |
#endif |
| 833 |
PROTECT(ans = NEW_LIST(2)); |
| 834 |
PROTECT(names = allocVector(STRSXP,2)); |
| 835 |
protectcounter+=2; |
| 836 |
SET_VECTOR_ELT(ans,0,mkString("aux_enc")); |
| 837 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 838 |
sys_read_int(&size,s); |
| 839 |
sys_read_int(&count,s); |
| 840 |
|
| 841 |
SEXP AuxEnc; |
| 842 |
PROTECT(AuxEnc = allocVector(STRSXP,1)); |
| 843 |
protectcounter++; |
| 844 |
char *AuxEnc_data; |
| 845 |
AuxEnc_data = R_alloc(count+1,1); |
| 846 |
memset(AuxEnc_data,0,count+1); |
| 847 |
sys_read(AuxEnc_data,count,s); |
| 848 |
SET_STRING_ELT(AuxEnc,0,mkChar(AuxEnc_data)); |
| 849 |
SET_VECTOR_ELT(ans,1,AuxEnc); |
| 850 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 851 |
SET_NAMES(ans,names); |
| 852 |
UNPROTECT(protectcounter); |
| 853 |
#ifdef DEBUG |
| 854 |
PrintValue(ans); |
| 855 |
#endif |
| 856 |
return ans; |
| 857 |
} |
| 858 |
else { |
| 859 |
#ifdef DEBUG |
| 860 |
Rprintf("\nsubtype == misc"); |
| 861 |
#endif |
| 862 |
PROTECT(ans = NEW_LIST(2)); |
| 863 |
PROTECT(names = allocVector(STRSXP,2)); |
| 864 |
protectcounter+=2; |
| 865 |
SET_VECTOR_ELT(ans,0,mkString("misc")); |
| 866 |
SET_STRING_ELT(names,0,mkChar("type")); |
| 867 |
sys_read_int(&size,s); |
| 868 |
sys_read_int(&count,s); |
| 869 |
PROTECT(data = allocVector(RAWSXP,size*count)); |
| 870 |
protectcounter++; |
| 871 |
sys_read(RAW(data),size*count,s); |
| 872 |
SET_VECTOR_ELT(ans,1,data); |
| 873 |
SET_STRING_ELT(names,1,mkChar("data")); |
| 874 |
SET_NAMES(ans,names); |
| 875 |
UNPROTECT(protectcounter); |
| 876 |
#ifdef DEBUG |
| 877 |
PrintValue(ans); |
| 878 |
#endif |
| 879 |
return ans; |
| 880 |
} |
| 881 |
error("something's wrong here"); |
| 882 |
return R_NilValue; |
| 883 |
} |
| 884 |
|
| 885 |
SEXP dflt_info_flt64 (SEXP SysFile){ |
| 886 |
|
| 887 |
sys_file *s = get_sys_file(SysFile); |
| 888 |
s->sysmis = SYSMIS; |
| 889 |
s->highest = HIGHEST; |
| 890 |
s->lowest = LOWEST; |
| 891 |
|
| 892 |
SEXP data, datanames; |
| 893 |
int i; |
| 894 |
|
| 895 |
PROTECT(data = allocVector(REALSXP,3)); |
| 896 |
PROTECT(datanames = allocVector(STRSXP,3)); |
| 897 |
REAL(data)[0] = s->sysmis; |
| 898 |
REAL(data)[1] = s->highest; |
| 899 |
REAL(data)[2] = s->lowest; |
| 900 |
static const char* flt64names[] = { |
| 901 |
"sysmis", |
| 902 |
"highest", |
| 903 |
"lowest" |
| 904 |
}; |
| 905 |
for(i = 0; i < 3; i++){ |
| 906 |
SET_STRING_ELT(datanames,i,mkChar(flt64names[i])); |
| 907 |
} |
| 908 |
SET_NAMES(data,datanames); |
| 909 |
UNPROTECT(2); |
| 910 |
|
| 911 |
return data; |
| 912 |
} |
| 913 |
|
| 914 |
SEXP read_sysfile_dict_term (SEXP SysFile){ |
| 915 |
#ifdef DEBUG |
| 916 |
Rprintf("\nread_sysfile_dict_term"); |
| 917 |
#endif |
| 918 |
sys_file *s = get_sys_file(SysFile); |
| 919 |
R_int32 rec_type, filler; |
| 920 |
sys_read_int(&rec_type,s); |
| 921 |
if(rec_type != 999) error("expecting a dictionary termination record"); |
| 922 |
sys_read_int(&filler,s); |
| 923 |
s->data_pos = ftell32(s->f); |
| 924 |
return ScalarInteger(s->data_pos); |
| 925 |
} |
| 926 |
|
| 927 |
SEXP rewind_sysfile(SEXP SysFile){ |
| 928 |
int ret; |
| 929 |
#ifdef DEBUG |
| 930 |
Rprintf("\nrewind_sysfile"); |
| 931 |
#endif |
| 932 |
sys_file *s = get_sys_file(SysFile); |
| 933 |
#ifdef DEBUG |
| 934 |
Rprintf("\ns address: %x",s); |
| 935 |
Rprintf("\nFile: %x",s->f); |
| 936 |
Rprintf("\nData pos: %d",s->data_pos); |
| 937 |
int a; |
| 938 |
ret = fseek(s->f,0,SEEK_SET); |
| 939 |
a = getc(s->f); |
| 940 |
Rprintf("\nRead num at 0: %d",a); |
| 941 |
#endif |
| 942 |
ret = fseek(s->f,s->data_pos,SEEK_SET); |
| 943 |
#ifdef DEBUG |
| 944 |
Rprintf("\ns address: %x",s); |
| 945 |
Rprintf("\nFile after rewind: %x",s->f); |
| 946 |
Rprintf("\nFile pos: %d",ftell(s->f)); |
| 947 |
a = getc(s->f); |
| 948 |
Rprintf("\nRead num: %d",a); |
| 949 |
a = getc(s->f); |
| 950 |
Rprintf("\nRead num: %d",a); |
| 951 |
ret = fseek(s->f,s->data_pos,SEEK_SET); |
| 952 |
#endif |
| 953 |
#undef DEBUG |
| 954 |
if(ret) error("error in sys_rewind"); |
| 955 |
memset(s->bytes,0,8); |
| 956 |
s->byte_pos = 0; |
| 957 |
return ScalarInteger(s->data_pos); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
#define STRMAX 256 |
| 962 |
SEXP read_sysfile_data (SEXP SysFile, SEXP what, |
| 963 |
SEXP s_ncases, SEXP s_types){ |
| 964 |
sys_file *s = get_sys_file(SysFile); |
| 965 |
#ifdef DEBUG |
| 966 |
Rprintf("\nread_sysfile_data"); |
| 967 |
PrintValue(s_types); |
| 968 |
PrintValue(s_ncases); |
| 969 |
#endif |
| 970 |
if(s->case_size == 0) error("case size is zero -- why??"); |
| 971 |
PROTECT(s_types = coerceVector(s_types,INTSXP)); |
| 972 |
PROTECT(s_ncases = coerceVector(s_ncases,INTSXP)); |
| 973 |
int nvar = 0; |
| 974 |
int *types = INTEGER(s_types); |
| 975 |
int ncases = INTEGER(s_ncases)[0]; |
| 976 |
int i, j, k, new_ncases; |
| 977 |
SEXP data, x, y; |
| 978 |
for(j = 0; j < s->case_size; j++) |
| 979 |
if(types[j] >= 0) nvar++; |
| 980 |
|
| 981 |
PROTECT(data = allocVector(VECSXP,nvar)); |
| 982 |
if(s->case_size == 0) error("case size is zero after argument decoding -- why??"); |
| 983 |
k = 0; |
| 984 |
for(j = 0; j < s->case_size; j++){ |
| 985 |
if(types[j] == 0){ |
| 986 |
SET_VECTOR_ELT(data,k,allocVector(REALSXP,ncases)); |
| 987 |
k++; |
| 988 |
} |
| 989 |
else if(types[j] >= 1) { |
| 990 |
SET_VECTOR_ELT(data,k,allocVector(STRSXP,ncases)); |
| 991 |
k++; |
| 992 |
} |
| 993 |
} |
| 994 |
int str_len = 0; |
| 995 |
int str_count = 0; |
| 996 |
int read_len; |
| 997 |
char char_buf[STRMAX]; |
| 998 |
if(s->case_size == 0) error("case size is zero after buffer allocation -- why??"); |
| 999 |
R_flt64 swapped_sysmis = dswap(s->sysmis,s->swap_code); |
| 1000 |
for(i = 0; i < ncases; i++){ |
| 1001 |
read_len = sys_read_case(s); |
| 1002 |
#ifdef DEBUG |
| 1003 |
if(i == 0){ |
| 1004 |
Rprintf("\ns address = %d",s); |
| 1005 |
Rprintf("\nbuf address = %d",s->buf); |
| 1006 |
Rprintf("\ncase_size = %d",s->case_size); |
| 1007 |
Rprintf("\nbuf = "); |
| 1008 |
int jj; |
| 1009 |
for(jj = 0; jj < s->case_size; jj++) |
| 1010 |
Rprintf("%g ",s->buf[jj]); |
| 1011 |
Rprintf("\nFile = %d",s->f); |
| 1012 |
Rprintf("\n file pos = %d",ftell(s->f)); |
| 1013 |
Rprintf("\n"); |
| 1014 |
} |
| 1015 |
#endif |
| 1016 |
#undef DEBUG |
| 1017 |
if(read_len == 0){ |
| 1018 |
new_ncases = i; |
| 1019 |
for(j = 0; j < nvar; j++){ |
| 1020 |
x = VECTOR_ELT(data,j); |
| 1021 |
SET_VECTOR_ELT(data,j,lengthgets(x,new_ncases)); |
| 1022 |
} |
| 1023 |
#ifdef DEBUG |
| 1024 |
Rprintf("\nReached end of cases"); |
| 1025 |
#endif |
| 1026 |
ncases = new_ncases; |
| 1027 |
break; |
| 1028 |
} |
| 1029 |
if(read_len < s->case_size) { |
| 1030 |
warning("end of file in unfinished case, i=%d, read length=%d",i,read_len); |
| 1031 |
new_ncases = i; |
| 1032 |
ncases = new_ncases; |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
#ifdef DEBUG |
| 1036 |
Rprintf("\nbuf = |%s|",buf); |
| 1037 |
#endif |
| 1038 |
k = 0; |
| 1039 |
for(j = 0; j < s->case_size; j++){ |
| 1040 |
#ifdef DEBUG |
| 1041 |
Rprintf("\nj=%d var=%d",j,var); |
| 1042 |
Rprintf(" name: %s",CHAR(STRING_ELT(names,var))); |
| 1043 |
Rprintf(" types[j]=%d ",types[j]); |
| 1044 |
#endif |
| 1045 |
if(types[j] == 0){ |
| 1046 |
x = VECTOR_ELT(data,k); |
| 1047 |
if(s->buf[j] == swapped_sysmis) REAL(x)[i] = NA_REAL; |
| 1048 |
else REAL(x)[i] = dswap(s->buf[j],s->swap_code); |
| 1049 |
k++; |
| 1050 |
} |
| 1051 |
else if(types[j] >= 1) { |
| 1052 |
memset(char_buf,0,STRMAX); |
| 1053 |
memcpy(char_buf,s->buf+j,8); |
| 1054 |
if(types[j]<=8){ |
| 1055 |
trim(char_buf,(int)strlen(char_buf)); |
| 1056 |
#ifdef DEBUG |
| 1057 |
Rprintf("\nchar_buf=|%s|",char_buf); |
| 1058 |
#endif |
| 1059 |
x = VECTOR_ELT(data,k); |
| 1060 |
SET_STRING_ELT(x,i,mkChar(char_buf)); |
| 1061 |
k++; |
| 1062 |
} else { |
| 1063 |
str_len = types[j]; |
| 1064 |
str_count = 1; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
else if(types[j] == -1){ |
| 1068 |
if(8*str_count > STRMAX-8) error("str_count out of bounds, 8*str_count = %d, STRMAX = %d",8*str_count,STRMAX); |
| 1069 |
memcpy(&char_buf[8*str_count],s->buf+j,8); |
| 1070 |
#ifdef DEBUG |
| 1071 |
Rprintf("\n8*str_count=%d str_len=%d",8*str_count,str_len); |
| 1072 |
#endif |
| 1073 |
str_count++; |
| 1074 |
if(8*str_count >= str_len){ |
| 1075 |
trim(char_buf,(int)strlen(char_buf)); |
| 1076 |
#ifdef DEBUG |
| 1077 |
Rprintf("\nchar_buf=|%s|",char_buf); |
| 1078 |
#endif |
| 1079 |
x = VECTOR_ELT(data,k); |
| 1080 |
SET_STRING_ELT(x,i,mkChar(char_buf)); |
| 1081 |
str_count = 0; |
| 1082 |
k++; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
else error("invalid type specifier"); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
#ifdef DEBUG |
| 1089 |
Rprintf("\ns address = %d",s); |
| 1090 |
Rprintf("\nbuf address = %d",s->buf); |
| 1091 |
Rprintf("\ncase_size = %d",s->case_size); |
| 1092 |
Rprintf("\nFile = %d",s->f); |
| 1093 |
Rprintf("\n"); |
| 1094 |
#endif |
| 1095 |
|
| 1096 |
for(j = 0; j < nvar; j++){ |
| 1097 |
x = VECTOR_ELT(what,j); |
| 1098 |
y = VECTOR_ELT(data,j); |
| 1099 |
copyMostAttrib(x,y); |
| 1100 |
} |
| 1101 |
UNPROTECT(3); |
| 1102 |
return data; |
| 1103 |
} |
| 1104 |
#undef DEBUG |
| 1105 |
|
| 1106 |
SEXP count_cases_sysfile (SEXP SysFile){ |
| 1107 |
sys_file *s = get_sys_file(SysFile); |
| 1108 |
#ifdef DEBUG |
| 1109 |
Rprintf("\ncount_cases_sysfile"); |
| 1110 |
#endif |
| 1111 |
if(s->case_size == 0) error("case size is zero -- why??"); |
| 1112 |
int ncases = 0; |
| 1113 |
int i; |
| 1114 |
int read_len; |
| 1115 |
for(i = 0; ; i++){ |
| 1116 |
read_len = sys_read_case(s); |
| 1117 |
if(read_len == 0){ |
| 1118 |
ncases = i; |
| 1119 |
#ifdef DEBUG |
| 1120 |
Rprintf("\nReached end of cases"); |
| 1121 |
#endif |
| 1122 |
break; |
| 1123 |
} |
| 1124 |
if(read_len < s->case_size) { |
| 1125 |
warning("end of file in unfinished case, i=%d, read length=%d",i,read_len); |
| 1126 |
ncases = i; |
| 1127 |
break; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
return ScalarInteger(ncases); |
| 1132 |
} |
| 1133 |
#undef DEBUG |
| 1134 |
|
| 1135 |
SEXP read_sysfile_slice (SEXP SysFile, SEXP what, |
| 1136 |
SEXP s_vars, SEXP s_cases, SEXP s_types){ |
| 1137 |
PROTECT(SysFile); |
| 1138 |
sys_file *s = get_sys_file(SysFile); |
| 1139 |
if(s->case_size == 0) error("case size is zero -- why??"); |
| 1140 |
|
| 1141 |
PROTECT(s_vars = coerceVector(s_vars,LGLSXP)); |
| 1142 |
PROTECT(s_cases = coerceVector(s_cases,LGLSXP)); |
| 1143 |
PROTECT(s_types = coerceVector(s_types,INTSXP)); |
| 1144 |
int nvar = length(s_vars); |
| 1145 |
int new_ncases, ncases = length(s_cases); |
| 1146 |
int *types = INTEGER(s_types); |
| 1147 |
|
| 1148 |
|
| 1149 |
int ii,i,j,k,l, m=0, n = 0; |
| 1150 |
for(j = 0; j < nvar; j++) m+=LOGICAL(s_vars)[j]; |
| 1151 |
for(i = 0; i < ncases; i++) n+=LOGICAL(s_cases)[i]; |
| 1152 |
SEXP x, y, data; |
| 1153 |
|
| 1154 |
|
| 1155 |
PROTECT(data = allocVector(VECSXP,m)); |
| 1156 |
k = 0; |
| 1157 |
l = 0; |
| 1158 |
for(j = 0; j < s->case_size; j++){ |
| 1159 |
if(types[j] == 0){ |
| 1160 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1161 |
if(LOGICAL(s_vars)[k]){ |
| 1162 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1163 |
SET_VECTOR_ELT(data,l,allocVector(REALSXP,n)); |
| 1164 |
l++; |
| 1165 |
} |
| 1166 |
k++; |
| 1167 |
} |
| 1168 |
else if(types[j] >= 1) { |
| 1169 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1170 |
if(LOGICAL(s_vars)[k]){ |
| 1171 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1172 |
SET_VECTOR_ELT(data,l,allocVector(STRSXP,n)); |
| 1173 |
l++; |
| 1174 |
} |
| 1175 |
k++; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
int str_len = 0; |
| 1180 |
int str_count = 0; |
| 1181 |
int read_len; |
| 1182 |
char char_buf[STRMAX]; |
| 1183 |
if(s->case_size == 0) error("case size is zero after buffer allocation -- why??"); |
| 1184 |
R_flt64 swapped_sysmis = dswap(s->sysmis,s->swap_code); |
| 1185 |
#ifdef DEBUG |
| 1186 |
Rprintf("\nswapped_sysmis = %g",swapped_sysmis); |
| 1187 |
#endif |
| 1188 |
ii = 0; |
| 1189 |
for(i = 0; i < ncases; i++){ |
| 1190 |
read_len = sys_read_case(s); |
| 1191 |
if(read_len == 0){ |
| 1192 |
new_ncases = i; |
| 1193 |
for(j = 0; j < m; j++){ |
| 1194 |
x = VECTOR_ELT(data,j); |
| 1195 |
SET_VECTOR_ELT(data,j,lengthgets(x,new_ncases)); |
| 1196 |
} |
| 1197 |
ncases = new_ncases; |
| 1198 |
break; |
| 1199 |
} |
| 1200 |
if(read_len < s->case_size) { |
| 1201 |
warning("end of file in unfinished case, i=%d, read length=%d",i,read_len); |
| 1202 |
new_ncases = i; |
| 1203 |
ncases = new_ncases; |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
if(LOGICAL(s_cases)[i]){ |
| 1207 |
k = 0; |
| 1208 |
l = 0; |
| 1209 |
for(j = 0; j < s->case_size; j++){ |
| 1210 |
|
| 1211 |
if(types[j] == 0){ |
| 1212 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1213 |
if(LOGICAL(s_vars)[k]){ |
| 1214 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1215 |
x = VECTOR_ELT(data,l); |
| 1216 |
if(s->buf[j] == swapped_sysmis) REAL(x)[ii] = NA_REAL; |
| 1217 |
else REAL(x)[ii] = dswap(s->buf[j],s->swap_code); |
| 1218 |
l++; |
| 1219 |
} |
| 1220 |
k++; |
| 1221 |
} |
| 1222 |
else if(types[j] >= 1) { |
| 1223 |
memset(char_buf,0,STRMAX); |
| 1224 |
memcpy(char_buf,s->buf+j,8); |
| 1225 |
if(types[j]<=8){ |
| 1226 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1227 |
if(LOGICAL(s_vars)[k]){ |
| 1228 |
trim(char_buf,(int)strlen(char_buf)); |
| 1229 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1230 |
x = VECTOR_ELT(data,l); |
| 1231 |
SET_STRING_ELT(x,ii,mkChar(char_buf)); |
| 1232 |
l++; |
| 1233 |
} |
| 1234 |
k++; |
| 1235 |
} else { |
| 1236 |
str_len = types[j]; |
| 1237 |
str_count = 1; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
else if(types[j] == -1){ |
| 1241 |
if(8*str_count > STRMAX-8) error("str_count out of bounds, 8*str_count = %d, STRMAX = %d",8*str_count,STRMAX); |
| 1242 |
memcpy(&char_buf[8*str_count],s->buf+j,8); |
| 1243 |
str_count++; |
| 1244 |
if(8*str_count >= str_len){ |
| 1245 |
trim(char_buf,(int)strlen(char_buf)); |
| 1246 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1247 |
if(LOGICAL(s_vars)[k]){ |
| 1248 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1249 |
x = VECTOR_ELT(data,l); |
| 1250 |
SET_STRING_ELT(x,ii,mkChar(char_buf)); |
| 1251 |
l++; |
| 1252 |
} |
| 1253 |
str_count = 0; |
| 1254 |
k++; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
else error("invalid type specifier"); |
| 1258 |
} |
| 1259 |
ii++; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
k = 0; |
| 1263 |
for(j = 0; j < nvar; j++){ |
| 1264 |
if(LOGICAL(s_vars)[j]){ |
| 1265 |
x = VECTOR_ELT(what,j); |
| 1266 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1267 |
y = VECTOR_ELT(data,k); |
| 1268 |
copyMostAttrib(x,y); |
| 1269 |
k++; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
UNPROTECT(5); |
| 1274 |
return data; |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
SEXP check_pointer(SEXP ptr){ |
| 1279 |
if(TYPEOF(ptr) != EXTPTRSXP) return ScalarLogical(0); |
| 1280 |
if(!R_ExternalPtrAddr(ptr)) return ScalarLogical(0); |
| 1281 |
return ScalarLogical(1); |
| 1282 |
} |
| 1283 |
|
| 1284 |
#define FPREP_IEEE754 754 |
| 1285 |
#define FPREP FPREP_IEEE754 |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
static double |
| 1291 |
second_lowest_double_val(void) |
| 1292 |
{ |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
#if FPREP == FPREP_IEEE754 |
| 1297 |
#ifdef WORDS_BIGENDIAN |
| 1298 |
union { |
| 1299 |
unsigned char c[8]; |
| 1300 |
double d; |
| 1301 |
} second_lowest = {{0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}}; |
| 1302 |
#else |
| 1303 |
union { |
| 1304 |
unsigned char c[8]; |
| 1305 |
double d; |
| 1306 |
} second_lowest = {{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff}}; |
| 1307 |
#endif |
| 1308 |
return second_lowest.d; |
| 1309 |
#else |
| 1310 |
#error Unknown floating-point representation. |
| 1311 |
#endif |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
SEXP read_sysfile_chunk (SEXP SysFile, SEXP what, |
| 1316 |
SEXP s_vars, SEXP s_ncases, SEXP s_types){ |
| 1317 |
PROTECT(SysFile); |
| 1318 |
sys_file *s = get_sys_file(SysFile); |
| 1319 |
if(s->case_size == 0) error("case size is zero -- why??"); |
| 1320 |
|
| 1321 |
PROTECT(s_vars = coerceVector(s_vars,LGLSXP)); |
| 1322 |
PROTECT(s_ncases = coerceVector(s_ncases,INTSXP)); |
| 1323 |
PROTECT(s_types = coerceVector(s_types,INTSXP)); |
| 1324 |
int nvar = length(s_vars); |
| 1325 |
int new_ncases, ncases = INTEGER(s_ncases)[0]; |
| 1326 |
int *types = INTEGER(s_types); |
| 1327 |
|
| 1328 |
|
| 1329 |
int ii,i,j,k,l, m=0; |
| 1330 |
for(j = 0; j < nvar; j++) m+=LOGICAL(s_vars)[j]; |
| 1331 |
|
| 1332 |
SEXP x, y, data; |
| 1333 |
|
| 1334 |
|
| 1335 |
PROTECT(data = allocVector(VECSXP,m)); |
| 1336 |
k = 0; |
| 1337 |
l = 0; |
| 1338 |
for(j = 0; j < s->case_size; j++){ |
| 1339 |
if(types[j] == 0){ |
| 1340 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1341 |
if(LOGICAL(s_vars)[k]){ |
| 1342 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1343 |
SET_VECTOR_ELT(data,l,allocVector(REALSXP,ncases)); |
| 1344 |
l++; |
| 1345 |
} |
| 1346 |
k++; |
| 1347 |
} |
| 1348 |
else if(types[j] >= 1) { |
| 1349 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1350 |
if(LOGICAL(s_vars)[k]){ |
| 1351 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1352 |
SET_VECTOR_ELT(data,l,allocVector(STRSXP,ncases)); |
| 1353 |
l++; |
| 1354 |
} |
| 1355 |
k++; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
int str_len = 0; |
| 1360 |
int str_count = 0; |
| 1361 |
int read_len; |
| 1362 |
char char_buf[STRMAX]; |
| 1363 |
if(s->case_size == 0) error("case size is zero after buffer allocation -- why??"); |
| 1364 |
R_flt64 swapped_sysmis = dswap(s->sysmis,s->swap_code); |
| 1365 |
#ifdef DEBUG |
| 1366 |
Rprintf("\nswapped_sysmis = %g",swapped_sysmis); |
| 1367 |
#endif |
| 1368 |
ii = 0; |
| 1369 |
for(i = 0; i < ncases; i++){ |
| 1370 |
read_len = sys_read_case(s); |
| 1371 |
if(read_len == 0){ |
| 1372 |
new_ncases = i; |
| 1373 |
for(j = 0; j < m; j++){ |
| 1374 |
x = VECTOR_ELT(data,j); |
| 1375 |
SET_VECTOR_ELT(data,j,lengthgets(x,new_ncases)); |
| 1376 |
} |
| 1377 |
ncases = new_ncases; |
| 1378 |
break; |
| 1379 |
} |
| 1380 |
if(read_len < s->case_size) { |
| 1381 |
warning("end of file in unfinished case, i=%d, read length=%d",i,read_len); |
| 1382 |
new_ncases = i; |
| 1383 |
ncases = new_ncases; |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
k = 0; |
| 1387 |
l = 0; |
| 1388 |
for(j = 0; j < s->case_size; j++){ |
| 1389 |
|
| 1390 |
if(types[j] == 0){ |
| 1391 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1392 |
if(LOGICAL(s_vars)[k]){ |
| 1393 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1394 |
x = VECTOR_ELT(data,l); |
| 1395 |
if(s->buf[j] == swapped_sysmis) REAL(x)[ii] = NA_REAL; |
| 1396 |
else REAL(x)[ii] = dswap(s->buf[j],s->swap_code); |
| 1397 |
l++; |
| 1398 |
} |
| 1399 |
k++; |
| 1400 |
} |
| 1401 |
else if(types[j] >= 1) { |
| 1402 |
memset(char_buf,0,STRMAX); |
| 1403 |
memcpy(char_buf,s->buf+j,8); |
| 1404 |
if(types[j]<=8){ |
| 1405 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1406 |
if(LOGICAL(s_vars)[k]){ |
| 1407 |
trim(char_buf,(int)strlen(char_buf)); |
| 1408 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1409 |
x = VECTOR_ELT(data,l); |
| 1410 |
SET_STRING_ELT(x,ii,mkChar(char_buf)); |
| 1411 |
l++; |
| 1412 |
} |
| 1413 |
k++; |
| 1414 |
} else { |
| 1415 |
str_len = types[j]; |
| 1416 |
str_count = 1; |
| 1417 |
} |
| 1418 |
} |
| 1419 |
else if(types[j] == -1){ |
| 1420 |
if(8*str_count > STRMAX-8) error("str_count out of bounds, 8*str_count = %d, STRMAX = %d",8*str_count,STRMAX); |
| 1421 |
memcpy(&char_buf[8*str_count],s->buf+j,8); |
| 1422 |
str_count++; |
| 1423 |
if(8*str_count >= str_len){ |
| 1424 |
trim(char_buf,(int)strlen(char_buf)); |
| 1425 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1426 |
if(LOGICAL(s_vars)[k]){ |
| 1427 |
if(l >= m) error("index l out of bounds, l = %d, m = %d",l,m); |
| 1428 |
x = VECTOR_ELT(data,l); |
| 1429 |
SET_STRING_ELT(x,ii,mkChar(char_buf)); |
| 1430 |
l++; |
| 1431 |
} |
| 1432 |
str_count = 0; |
| 1433 |
k++; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
else error("invalid type specifier"); |
| 1437 |
} |
| 1438 |
ii++; |
| 1439 |
} |
| 1440 |
k = 0; |
| 1441 |
for(j = 0; j < nvar; j++){ |
| 1442 |
if(LOGICAL(s_vars)[j]){ |
| 1443 |
x = VECTOR_ELT(what,j); |
| 1444 |
if(k >= nvar) error("index k out of bounds, k = %d, nvar = %d",k,m); |
| 1445 |
y = VECTOR_ELT(data,k); |
| 1446 |
copyMostAttrib(x,y); |
| 1447 |
k++; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
UNPROTECT(5); |
| 1452 |
return data; |
| 1453 |
} |