Debian Codegraph @

src/pspp-portableStream-for-R.c

raw symbols SCIP
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
9
10 /** Code translation **/
11 /*
12 The following translation table appears under the name 'spss2ascii' in
13 file 'pfm-read.c' of the foreign packge. The orginal copyright is:
14 """
15 PSPP - computes sample statistics.
16 Copyright (C) 1997-9, 2000, 2001 Free Software Foundation, Inc.
17 Written by Ben Pfaff <[email protected]>.
18 Modified for R foreign library by Saikat DebRoy <[email protected]>.
19
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of the
23 License, or (at your option) any later version.
24 """
25 */
26 static const unsigned char latin1_tab[256] = {
27 " "
28 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
29 "<(+|&[]!$*);^-/|,%_>?`:$@'=\" ~- 0123456789 -() {}\\ "
30 " "
31 };
32
33 // 0,0,0,0,0, 0,0,0,0,0, /* 0-9: control and reserved codes */
34 // 0,0,0,0,0, 0,0,0,0,0, /* 10-19: control codes */
35 // 0,0,0,0,0, 0,0,0,0,0, /* 20-29: control codes */
36 // 0,0,0,0,0, 0,0,0,0,0, /* 30-39: control codes */
37 // 0,0,0,0,0, 0,0,0,0,0, /* 40-49: control codes */
38 // 0,0,0,0,0, 0,0,0,0,0, /* 50-59: control codes */
39 // 0,0,0,0, /* 60: control code, 61-63: `reserved' */
40 // '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', /* 64-73: digits */
41 // 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 74-99: captial letters A-Z*/
42 // 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
43 // 'U', 'V', 'W', 'X', 'Y', 'Z',
44 // 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 100-25: lowercase letters a-z*/
45 // 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
46 // 'u', 'v', 'w', 'x', 'y', 'z',
47 // 32, /* 126: space*/
48 // 46, 60, 40, 43, /* 127-130: symbols .<(+ */
49 // 124, /* 131: solid vertical pipe */
50 // 38, 91, 93, 33, 36, 42, 41, 59, 94, 45, 47, /* 132-142: symbols &[]!$*);^-/ */
51 // 166, /* 143: broken vertical pipe */
52 // 44, 37, 95, 62, 63, 96, 58, /* 144-150: symbols ,%_>?`: */
53 // 163, /* 151: British pound symbol */
54 // 64, 39, 61, 34, /* 152-155: @'="*/
55 // /** Symbols beyond 155 do usually not make sense in latin1 **/
56 // 32, /* 156: Less than or equal symbol*/
57 // 32, /* 157: Empty box */
58 // 177, /* 158: Plus or minus */
59 // 32, /* 159: Filled box */
60 // 176, /* 160: Degree symbol */
61 // 32, /* 161: Dagger */
62 // 126, /* 162: Tilde */
63 // 32, /* 163: En dash */
64 // 32 ,/* 164: Lower left corner box draw */
65 // 32, /* 165: Upper left corner box draw */
66 // 32, /* 166: Greater than or equal symbol */
67 // 32,32,32,32,32, 32,32,32,32,32, /* 167-176: Superscript 0 through 9 */
68 // 32, /* 177: Lower right corner box draw */
69 // 32, /* 178: Upper right corner box draw */
70 // 32, /* 179: Not equal symbol */
71 // 32, /* 180: Em dash */
72 // 32, /* 181: Superscript ( */
73 // 32, /* 182: Superscript ) */
74 // 32, /* 183: Horizontal dagger (?) */
75 // 123,125,92, /* 184-186: Symbols {}\ */
76 // 162, /* 187: Cents symbol */
77 // 183, /* 188: Centered dot/bullet */
78 // 0,
79 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 189-255: Reserved */
80 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 // 0, 0, 0, 0, 0, 0
86 // };
87
88 /** Number translation **/
89
90
91 static const unsigned char Por2int_tab[256] =
92 {
93 255,255,255,255,255,255,255,255,255,255, /*10*/
94 255,255,255,255,255,255,255,255,255,255, /*10*/
95 255,255,255,255,255,255,255,255,255,255, /*10*/
96 255,255,255,255,255,255,255,255,255,255, /*10*/
97 255,255,255,255,255,255,255,255, /*8*/
98 0,1,2,3,4,5,6,7,8,9, /*10*/
99 255,255,255,255,255,255,255, /*7*/
100 10,11,12,13,14,15,16,17,18,19,20, /*11*/
101 21,22,23,24,25,26,27,28,29, /*9 sum=85*/
102 255,255,255,255,255, /*5*/
103 255,255,255,255,255,255,255,255,255,255, /*10*/
104 255,255,255,255,255,255,255,255,255,255, /*10*/
105 255,255,255,255,255,255,255,255,255,255, /*10*/
106 255,255,255,255,255,255,255,255,255,255, /*10*/
107 255,255,255,255,255,255,255,255,255,255, /*10*/
108 255,255,255,255,255,255,255,255,255,255, /*10*/
109 255,255,255,255,255,255,255,255,255,255, /*10*/
110 255,255,255,255,255,255,255,255,255,255, /*10*/
111 255,255,255,255,255,255,255,255,255,255, /*10*/
112 255,255,255,255,255,255,255,255,255,255, /*10*/
113 255,255,255,255,255,255,255,255,255,255, /*10*/
114 255,255,255,255,255,255,255,255,255,255, /*10*/
115 255,255,255,255,255,255,255,255,255,255, /*10*/
116 255,255,255,255,255,255,255,255,255,255, /*10*/
117 255,255,255,255,255,255,255,255,255,255, /*10*/
118 255,255,255,255,255,255,255,255,255,255, /*10*/
119 255,255,255,255,255,255
120 };
121
122 /* #define DEBUG */
123 double Por2int(int len, char* text){
124 int i, stop, sign;
125 double curr, target;
126 sign = 1;
127 curr = 1;
128 target = 0;
129 stop = 0;
130 #ifdef DEBUG
131 Rprintf("\nPor2int ----------------------------");
132 Rprintf("\n input = %s\n",text);
133 #endif
134 if(*text=='-'){
135 sign = -1;
136 stop = 1;
137 }
138 for(i = len - 1; i >=stop ; i--){
139 #ifdef DEBUG
140 Rprintf("Source: %c\t",text[i]);
141 Rprintf("Digit: %d\n",Por2int_tab[(int)text[i]]);
142 Rprintf("Current: %f\n",curr);
143 Rprintf("Update: %f\n",curr*(int)Por2int_tab[(int)text[i]]);
144 #endif
145 target += curr*(int)Por2int_tab[(int)text[i]];
146 #ifdef DEBUG
147 Rprintf("Target: %f\t",target);
148 #endif
149 curr *= 30;
150 }
151 return sign*target;
152 }
153
154 double Por2mantissa(int len, char* text){
155 int i;
156 double curr = 1./30., target;
157 target = 0;
158 #ifdef DEBUG
159 Rprintf("\nPor2mantissa ----------------------------");
160 Rprintf("\n input = %s\n",text);
161 #endif
162 for(i = 0; i < len ; i++){
163 #ifdef DEBUG
164 Rprintf("Source: %c\t",text[i]);
165 Rprintf("Digit: %d\n",Por2int_tab[(int)text[i]]);
166 #endif
167 target += curr*(double)Por2int_tab[(int)text[i]];
168 #ifdef DEBUG
169 Rprintf("Update: %f\n",curr*(double)Por2int_tab[(int)text[i]]);
170 Rprintf("Target: %f\n",target);
171 #endif
172 curr /= 30.;
173 }
174 return target;
175 }
176
177 double Por2double(int len, char* text){
178 int sign=1;
179 int exp_sign = 0;
180 int exponent=0;
181 int l_charact = len;
182 char *t_mant = NULL;
183 int l_mant = 0;
184 char *t_exp = NULL;
185 int l_exp = 0;
186 char *end = text + len;
187 char *tmp = text;
188 double result = 0;
189 #ifdef DEBUG
190 Rprintf("\nPor2double ----------------------------");
191 Rprintf("\n input = %s\n",text);
192 #endif
193 if(*text == '*') return NA_REAL;
194 if(*text == '+') {
195 text++;
196 l_charact--;
197 }
198 if(*text == '-'){
199 sign = -1;
200 text++;
201 l_charact--;
202 }
203 for(tmp = text; tmp < end; tmp++){
204 if(*tmp == '.'){
205 l_charact = (int)(tmp - text);
206 tmp++;
207 t_mant = tmp;
208 l_mant = (int)(end - tmp);
209 break;
210 }
211 if(*tmp == '+' || *tmp == '-'){
212 l_charact = (int)(tmp - text);
213 if(*tmp == '+')
214 exp_sign = 1;
215 if(*tmp == '-')
216 exp_sign = -1;
217 tmp++;
218 t_exp = tmp;
219 l_exp = (int)(end - tmp);
220 exponent = Por2int(l_exp,t_exp);
221 if(exp_sign == -1){ /** "un-normalize" **/
222 if(exponent >= l_charact){
223 l_mant = l_charact;
224 l_charact = 0;
225 exponent -= l_mant;
226 exponent = -exponent;
227 t_mant = text;
228 }
229 else {
230 l_mant = exponent;
231 l_charact -= exponent;
232 t_mant = text + l_charact;
233 exponent = 0;
234 }
235 }
236 break;
237 }
238 }
239 if(l_charact)
240 result += (double)Por2int(l_charact,text);
241 if(l_mant){
242 result += Por2mantissa(l_mant, t_mant);
243 }
244 if(exponent != 0){
245 #ifdef DEBUG
246 Rprintf("\n ####### Por2double ");
247 Rprintf(" input = %s",text);
248 Rprintf(" exponent = %d",exponent);
249 Rprintf(" result = %f",result*R_pow_di(30,exponent));
250 #endif
251 result *= R_pow_di(30,exponent);
252 }
253
254 #ifdef DEBUG
255 Rprintf("\nresult = %f",result);
256 #endif
257
258 if(sign == -1)
259 return -result;
260 else
261 return result;
262 }
263 /** porStreamBuf **/
264 #undef DEBUG
265
266 #define BUFSIZE 85
267
268
269
270
271 typedef struct {
272 FILE *f;
273 unsigned char buf[BUFSIZE];
274 int pos;
275 int line;
276 int line_size;
277 int nl_size; /* Number of characters that represent a newline (1 or 2)*/
278 unsigned char translate[256];
279 Rboolean at_end;
280 } porStreamBuf;
281
282 void initPorStreamBuf(porStreamBuf *b){
283 #ifdef DEBUG
284 Rprintf("\ninitPorStreamBuf");
285 Rprintf("\nb = %d",b);
286 #endif
287 int i;
288 for(i = 0; i < 256; i++) b->translate[i] = (unsigned char)i;
289 b->pos = 0;
290 b->line = 0;
291 b->at_end = FALSE;
292 b->line_size = 82; /* 80 chars + newline character(s) */
293 b->nl_size = 2; /* Number of characters that represent a newline (1 or 2)*/
294 }
295
296 /*
297 in: codes as in the file
298 */
299
300 void por_make_trans(porStreamBuf *b, const char *in){
301 unsigned char i;
302 unsigned char j;
303 /*for(i = 64; i <= 188; i++){*/
304 for(i = 188; i >= 64; i--){ /* Sometimes symbol translations are buggy,
305 so the translation of chars and numbers comes later and thus
306 maintains priority ... */
307 j = (unsigned char)in[i];
308 if(i <= 155 || i==126 || (i >= 184 && i<= 186) ){
309 b->translate[j] = latin1_tab[i];
310 #ifdef DEBUG
311 Rprintf("\ni=%d, in: '%c' out: '%c' ",i,in[i],latin1_tab[i]);
312 #endif
313 }
314 }
315 }
316 // #undef DEBUG
317
318 int fillPorStreamBuf(porStreamBuf *b) {
319 #ifdef DEBUG
320 Rprintf("\n\n\n == fillPorStreamBuf ===");
321 Rprintf("\nb = %d",b);
322 Rprintf("\nb->buf = %d",b->buf);
323 #endif
324 memset(b->buf,0,BUFSIZE);
325 #ifdef DEBUG
326 Rprintf("\nfile = %d",b->f);
327 #endif
328 if(feof(b->f)){
329 b->pos = 0;
330 b->at_end = TRUE;
331 return 0;
332 }
333 int fpos = ftell32(b->f);
334 char *dummy = fgets((char *)b->buf,BUFSIZE,b->f);
335 if(!dummy) {
336 fseek(b->f,fpos,SEEK_SET);
337 int ret = fread((char *)b->buf,1,BUFSIZE,b->f);
338
339 error("cannot read from file at pos %d (fread result = <%s>)",fpos,b->buf);
340 }
341
342 #ifdef DEBUG
343 Rprintf("\nline no = %d",b->line);
344 Rprintf("\nbuffer = |%s|",b->buf);
345 #endif
346 int i, len = (int)strlen((char *)b->buf);
347 for(i = 0; i < len; i++)
348 b->buf[i] = b->translate[(int)b->buf[i]];
349
350 /* The following is for buggy portable files with short lines */
351 if(len < b->line_size){
352 for(i = len; i < 80; i++)
353 b->buf[i] = 32; /* The code for space */
354 }
355
356 #ifdef DEBUG
357 Rprintf("\nreported length = %d",len);
358 #endif
359 for(i = 80; i < BUFSIZE; i++)
360 b->buf[i] = 0; /* Inefficient, but better safe than sorry ... */
361 #ifdef DEBUG
362 Rprintf("\ncorrected length = %d",strlen((char *)b->buf));
363 #endif
364 #ifdef DEBUG
365 /* Rprintf("\nline no = %d",b->line); */
366 Rprintf("\ncooked buffer = |%s|\nlength = %d");
367 /* if(prtlen < 80) {
368 * Rprintf("\nShort line encountered, length =%d at line %d",prtlen,b->line);
369 * } */
370 #endif
371 #undef DEBUG
372 b->pos = 0;
373 b->line++;
374 return strlen((char *)b->buf);
375 }
376
377 /** Internal functions to read from a porStreamBuf **/
378
379
380 void printPorStreamBuf(porStreamBuf *b){
381 Rprintf("\nbuffer = |%s|",b->buf);
382 }
383
384 char readOnePushbackPorStream1(porStreamBuf *b) {
385 #ifdef DEBUG
386 Rprintf("\nreadOnePushbackPorStream1");
387 Rprintf("\nb = %d",b);
388 Rprintf("\nb->buf = %d",b->buf);
389 Rprintf("\nbuffer = |%s|",b->buf);
390 #endif
391 if(b->pos < 80){
392 #ifdef DEBUG
393 Rprintf("\nans=%c",b->buf[b->pos]);
394 #endif
395 return b->buf[b->pos];
396 }
397 else {
398 fillPorStreamBuf(b);
399 #ifdef DEBUG
400 Rprintf("\nans=%c",b->buf[0]);
401 #endif
402 return b->buf[0];
403 }
404 }
405
406 char readOnePorStream1(porStreamBuf *b) {
407 #ifdef DEBUG
408 Rprintf("\nreadOnePorStream1");
409 Rprintf("\nb = %d",b);
410 Rprintf("\nb->buf = %d",b->buf);
411 Rprintf("\nbuffer = |%s|",b->buf);
412 #endif
413 char ans;
414 if(b->pos < 80)
415 ans = b->buf[b->pos];
416 else {
417 fillPorStreamBuf(b);
418 b->pos = 0;
419 ans = b->buf[0];
420 }
421 b->pos++;
422 #ifdef DEBUG
423 Rprintf("\nans=%c",ans);
424 #endif
425 return ans;
426 }
427
428 Rboolean atEndPorStream(porStreamBuf *b){
429 /*char test =*/ readOnePushbackPorStream1(b);
430 return b->at_end/* || test == 'Z'*/;
431 }
432
433
434 #define HardMaxRead 5*82
435
436 char *readPorStream1(porStreamBuf *b, int n) {
437 #ifdef DEBUG
438 Rprintf("\nreadPorStream1");
439 Rprintf("\nb = %d",b);
440 Rprintf("\nb->buf = %d",b->buf);
441 Rprintf("\nbuffer = |%s|",b->buf);
442 Rprintf("\nn = %d",n);
443 #endif
444 if(n > HardMaxRead || n < 0) n = HardMaxRead;
445 if(b->pos == 80) fillPorStreamBuf(b);
446 char *ans = S_alloc(n+1,1);
447 if(b->pos + n <= 80){
448 memcpy(ans, b->buf + b->pos, n);
449 b->pos += n;
450 #ifdef DEBUG
451 Rprintf("\nans = %s",ans);
452 Rprintf("\nEND readPorStream1");
453 #endif
454 return ans;
455 }
456 /* else */
457 char *tmp = ans;
458 int nread;
459 if(80 - b->pos > 0){
460 nread = 80 - b->pos;
461 memcpy(ans,b->buf + b->pos, nread);
462 n -= nread;
463 tmp += nread;
464 b->pos = 0;
465 fillPorStreamBuf(b);
466 }
467 int i, nlines = n/80, remainder = n%80;
468 for(i = 0; i < nlines; i++){
469 memcpy(tmp,b->buf,80);
470 tmp += 80;
471 fillPorStreamBuf(b);
472 }
473 if(remainder > 0)
474 memcpy(tmp,b->buf,remainder);
475 b->pos = remainder;
476 #ifdef DEBUG
477 Rprintf("\nans = %s",ans);
478 Rprintf("\nEND readPorStream1");
479 #endif
480 return ans;
481 }
482
483 int readPorStreamTo(porStreamBuf *b, char *target, int n) {
484 if(n > HardMaxRead || n < 0) n = HardMaxRead;
485 if(b->pos == 80) fillPorStreamBuf(b);
486 if(b->pos + n <= 80){
487 memcpy(target, b->buf + b->pos, n);
488 b->pos += n;
489 return n;
490 }
491 /* else */
492 char *tmp = target;
493 int nread;
494 if(80 - b->pos > 0){
495 nread = 80 - b->pos;
496 memcpy(target,b->buf + b->pos, nread);
497 n -= nread;
498 tmp += nread;
499 b->pos = 0;
500 fillPorStreamBuf(b);
501 }
502 int i, nlines = n/80, remainder = n%80;
503 for(i = 0; i < nlines; i++){
504 memcpy(tmp,b->buf,80);
505 tmp += 80;
506 fillPorStreamBuf(b);
507 }
508 if(remainder > 0)
509 memcpy(tmp,b->buf,remainder);
510 b->pos = remainder;
511 #ifdef DEBUG
512 Rprintf("\ntarget = %s",target);
513 Rprintf("\nEND readPorStreamTo");
514 #endif
515 return n;
516 }
517
518 int seekPorStream1(porStreamBuf *b, int pos) {
519 fseek(b->f,0,SEEK_SET);
520 b->line = 0;
521 int i, nlines = pos/80, remainder = pos%80;
522 #ifdef DEBUG
523 Rprintf("\nseekPorStream1");
524 Rprintf("\npos = %d",pos);
525 Rprintf("\nb = %d",b);
526 Rprintf("\nb->buf = %d",b->buf);
527 Rprintf("\nbuffer = |%s|",b->buf);
528 Rprintf("\nremainder = %d",remainder);
529 Rprintf("\n#################################################");
530 #endif
531 for(i = 0; i < nlines; i++){
532 fillPorStreamBuf(b);
533 #ifdef DEBUG
534 Rprintf("\ni=%d, buffer = |%s|",i,b->buf);
535 #endif
536 }
537 fillPorStreamBuf(b);
538 b->pos = remainder;
539 #ifdef DEBUG
540 Rprintf("\ni=%d, buffer = |%s|",i,b->buf);
541 Rprintf("\nbuffer remainder = |%s|",b->buf+remainder);
542 #endif
543 return remainder;
544 }
545
546 int slashpos(unsigned char* str, size_t n){
547 int i;
548 for(i = 0; i < n; i++){
549 if(str[i] == '/' || str[i] == '*')
550 return i+1;
551 }
552 return 0;
553 }
554
555
556 int readToSlashPorStream1(porStreamBuf *b, char *ans, int n){
557 #ifdef DEBUG
558 Rprintf("\nreadToSlashPorStream1");
559 Rprintf("\nbuffer = |%s|",b->buf);
560 Rprintf("\nb->pos = %d",b->pos);
561 Rprintf("\nn = %d",n);
562 #endif
563 if(n > HardMaxRead) n = HardMaxRead;
564 if(b->pos == 80) fillPorStreamBuf(b);
565 int len, slashp = slashpos(b->buf+b->pos,80 - b->pos);
566 if(slashp){
567 memcpy(ans,b->buf+b->pos,slashp);
568 b->pos += slashp;
569 #ifdef DEBUG
570 Rprintf("\nb->pos = %d",b->pos);
571 Rprintf("\nans = %s",ans);
572 #endif
573 return slashp;
574 }
575 else {
576 len = 80 - b->pos;
577 memcpy(ans,b->buf + b->pos,len);
578 int i, maxlines = (n + b->pos)/80+1 ;
579 #ifdef DEBUG
580 Rprintf("\nmaxlines = %d",maxlines);
581 #endif
582 for(i = 0; i < maxlines; i++){
583 #ifdef DEBUG
584 Rprintf("\ncurrent = %s",ans);
585 #endif
586 fillPorStreamBuf(b);
587 slashp = slashpos(b->buf,80);
588 #ifdef DEBUG
589 Rprintf("\nslashp = %d",slashp);
590 #endif
591 if(slashp){
592 memcpy(ans+len,b->buf,slashp);
593 b->pos = slashp;
594 #ifdef DEBUG
595 Rprintf("\nans = %s",ans);
596 #endif
597 return slashp;
598 } else {
599 int remainder = (n - len >= 80 ? 80 : n - len);
600 memcpy(ans+len,b->buf,remainder);
601 len+=80;
602 if(len > n) break;
603 }
604 }
605 // #ifdef DEBUG
606 Rprintf("\nWARNING: slash not found");
607 Rprintf("\nans = %s",ans);
608 // #endif
609 return -1;
610 }
611 }
612
613 int readIntPorStream1 (porStreamBuf *b){
614 #ifdef DEBUG
615 Rprintf("\nreadIntPorStream1");
616 Rprintf("\nb = %d",b);
617 Rprintf("\nb->buf = %d",b->buf);
618 Rprintf("\nbuffer = |%s|",b->buf);
619 #endif
620 /* Check for missing value */
621 if(readOnePushbackPorStream1(b) == '*'){
622 readOnePorStream1(b);
623 if(readOnePorStream1(b) != '.') warning("malformed sysmis entry");
624 return NA_INTEGER;
625 }
626 char ans[80];
627 memset(ans,0,80);
628 readToSlashPorStream1 (b,ans,80);
629 #ifdef DEBUG
630 Rprintf("\nans = |%s|",ans);
631 #endif
632 ans[strlen(ans)-1] = '\0'; /* chop off '/'*/
633 #ifdef DEBUG
634 Rprintf("\nans = %s",ans);
635 #endif
636 int len = (int)strlen(ans);
637 #ifdef DEBUG
638 Rprintf("\nresult = %d",(int)Por2int(len,ans));
639 #endif
640
641 return (int)Por2int(len,ans);
642 }
643
644
645 double readDoublePorStream1 (porStreamBuf *b){
646 #ifdef DEBUG
647 Rprintf("\nreadDoublePorStream1");
648 Rprintf("\nb = %d",b);
649 Rprintf("\nb->buf = %d",b->buf);
650 Rprintf("\nbuffer = |%s|",b->buf);
651 #endif
652 /* Check for missing value */
653 if(readOnePushbackPorStream1(b) == '*'){
654 readOnePorStream1(b);
655 if(readOnePorStream1(b) != '.') warning("malformed sysmis entry");
656 return NA_REAL;
657 }
658
659 char ans[80];
660 memset(ans,0,80);
661 readToSlashPorStream1 (b,ans,80);
662 #ifdef DEBUG
663 Rprintf("\nans=|%s|",ans);
664 #endif
665
666 /*if(ans[strlen(ans)-1] == '*') return NA_REAL;*/
667 ans[strlen(ans)-1] = '\0'; /* chop off '/'*/
668 int len = (int)strlen(ans);
669
670 #ifdef DEBUG
671 Rprintf("\tresult = |%f|",Por2double(len,ans));
672 #endif
673 return Por2double(len,ans);
674 }
675
676
677 char* readStringPorStream1(porStreamBuf *b){
678 #ifdef DEBUG
679 Rprintf("\nreadStringPorStream1");
680 Rprintf("\nb = %d",b);
681 Rprintf("\nb->buf = %d",b->buf);
682 Rprintf("\nbuffer = |%s|",b->buf);
683 #endif
684 int len = readIntPorStream1 (b);
685 #ifdef DEBUG
686 Rprintf("\nLength: %d",len);
687 #endif
688 char *ans = readPorStream1(b,len);
689 #ifdef DEBUG
690 Rprintf("\nString: '%s'",ans);
691 #endif
692 return ans;
693 }
694 #undef DEBUG
695
696
697
698 /** *******************************************************/
699
700 /** PorStream objects **/
701
702 SEXP closePorStream (SEXP);
703
704 SEXP NewPorStream (SEXP name){
705 #ifdef DEBUG
706 Rprintf("\nNewPorStream");
707 #endif
708 PROTECT(name = coerceVector(name,STRSXP));
709 // porStreamBuf *b = (porStreamBuf *)S_alloc(1,sizeof(porStreamBuf));
710 porStreamBuf *b = R_Calloc(1,porStreamBuf);
711 initPorStreamBuf(b);
712 b->f = fopen(CHAR(STRING_ELT(name, 0)),"rb");
713 #ifdef DEBUG
714 Rprintf("\nfile = %d",b->f);
715 #endif
716 if(b->f == NULL){
717 R_Free(b);
718 UNPROTECT(1);
719 return R_NilValue;
720 }
721 else {
722 fillPorStreamBuf(b); /* The first line must not be short */
723 b->line_size = strlen((char *)b->buf);
724 b->nl_size = b->line_size - 80;
725
726 SEXP ans = R_MakeExternalPtr(b, install("porStreamBuf"), R_NilValue);
727 PROTECT(ans);
728 R_RegisterCFinalizer(ans, (R_CFinalizer_t) closePorStream);
729 setAttrib(ans,install("file.name"),name);
730 UNPROTECT(2);
731 return ans;
732 }
733 }
734
735 porStreamBuf *get_porStreamBuf(SEXP porStream){
736 if(TYPEOF(porStream) != EXTPTRSXP || R_ExternalPtrTag(porStream) != install("porStreamBuf"))
737 error("not a porStream");
738 porStreamBuf *b = R_ExternalPtrAddr(porStream);
739 if (b == NULL){
740 error("external pointer is NULL, you need to recreate this object");
741 /* b = R_Calloc(1,porStreamBuf);
742 * R_SetExternalPtrAddr(porStream,b);
743 * initPorStreamBuf(b);
744 * SEXP name = getAttrib(porStream,install("file.name"));
745 * if(name == R_NilValue || name == NULL){
746 * R_SetExternalPtrAddr(porStream,NULL);
747 * R_Free(b);
748 * error("need filename to reopen file");
749 * }
750 * b->f = fopen(CHAR(STRING_ELT(name, 0)),"rb");
751 * if(b->f == NULL){
752 * R_SetExternalPtrAddr(porStream,NULL);
753 * R_Free(b);
754 * error("cannot reopen file -- does it still exist?");
755 * }
756 * Rprintf("File '%s' reopened\n",CHAR(STRING_ELT(name, 0))); */
757 }
758 /* if (b == NULL) error("something strange happened here!?"); */
759 return(b);
760 }
761
762 SEXP closePorStream (SEXP porStream){
763 if(TYPEOF(porStream) != EXTPTRSXP || R_ExternalPtrTag(porStream) != install("porStreamBuf"))
764 error("not a porStream");
765 porStreamBuf *b = R_ExternalPtrAddr(porStream);
766 if (b != NULL) {
767 fclose(b->f);
768 R_Free(b);
769 /*Rprintf("\n'portable' file closed\n");*/
770 R_ClearExternalPtr(porStream);
771 }
772 return R_NilValue;
773 }
774
775 /** R interface and routines **/
776
777 SEXP tellPorStream (SEXP porStream){
778 porStreamBuf *b = get_porStreamBuf(porStream);
779 return ScalarInteger(80*(b->line-1)+b->pos);
780 }
781
782 SEXP lineTellPorStream (SEXP porStream){
783 porStreamBuf *b = get_porStreamBuf(porStream);
784 return ScalarInteger(b->line);
785 }
786
787 SEXP offsetTellPorStream (SEXP porStream){
788 porStreamBuf *b = get_porStreamBuf(porStream);
789 return ScalarInteger(b->pos);
790 }
791
792
793 SEXP readOnePushbackPorStream(SEXP porStream){
794 porStreamBuf *b = get_porStreamBuf(porStream);
795 char ans[2];
796 memset(ans,0,2);
797 ans[0] = readOnePushbackPorStream1(b);
798 return mkString(ans);
799 }
800
801 SEXP readOnePorStream(SEXP porStream){
802 porStreamBuf *b = get_porStreamBuf(porStream);
803 char ans[2];
804 memset(ans,0,2);
805 ans[0] = readOnePorStream1(b);
806 return mkString(ans);
807 }
808
809 SEXP readPorStream(SEXP porStream, SEXP s_n){
810 porStreamBuf *b = get_porStreamBuf(porStream);
811 int n = asInteger(s_n);
812 return mkString(readPorStream1(b,n));
813 }
814
815 SEXP seekPorStream(SEXP porStream, SEXP s_pos){
816 porStreamBuf *b = get_porStreamBuf(porStream);
817 int pos = asInteger(s_pos);
818 return ScalarInteger(seekPorStream1(b,pos));
819 }
820
821 SEXP readToSlashPorStream(SEXP porStream, SEXP s_n){
822 porStreamBuf *b = get_porStreamBuf(porStream);
823 int n = asInteger(s_n);
824 char *ans = S_alloc(n,1);
825 readToSlashPorStream1(b,ans,n);
826 return mkString(ans);
827 }
828
829 SEXP readIntPorStream(SEXP porStream){
830 porStreamBuf *b = get_porStreamBuf(porStream);
831 return ScalarInteger(readIntPorStream1(b));
832 }
833
834 SEXP readDoublePorStream(SEXP porStream){
835 porStreamBuf *b = get_porStreamBuf(porStream);
836 return ScalarReal(readDoublePorStream1(b));
837 }
838
839 char* readCHARPorStream(porStreamBuf *b,char *buf,int len){
840 memset(buf,0,len);
841 int n = readIntPorStream1 (b);
842 if(atEndPorStream(b)) return buf;
843 if(n > len) error("string has length %d but should have maximal length %d",n,len);
844 /*SEXP ans;
845 PROTECT(ans = allocString(n));
846 char *cans = CHAR(ans);*/
847 readPorStreamTo(b,buf,n);
848 buf[n] = 0;
849 /*UNPROTECT(1);*/
850 return buf;
851 }
852
853 SEXP readStringPorStream(SEXP porStream){
854 porStreamBuf *b = get_porStreamBuf(porStream);
855 return ScalarString(mkChar(readStringPorStream1(b)));
856 }
857
858 SEXP setTranslationPorStream(SEXP porStream, SEXP s_in){
859 porStreamBuf *b = get_porStreamBuf(porStream);
860 SEXP s_out;
861 PROTECT(s_out = allocVector(RAWSXP,256));
862 PROTECT(s_in = asChar(s_in));
863 const char *in = CHAR(s_in);
864 por_make_trans(b,in);
865 memcpy(RAW(s_out),&(b->translate[0]),256);
866 UNPROTECT(2);
867 return s_out;
868 }
869
870 SEXP readDataPorStream(SEXP porStream, SEXP what, SEXP s_n, SEXP s_types){
871 #ifdef DEBUG
872 Rprintf("\n############################");
873 Rprintf("\n#readDataPorStream");
874 Rprintf("\n############################");
875 #endif
876 porStreamBuf *b = get_porStreamBuf(porStream);
877 int n = asInteger(s_n);
878
879 #ifdef DEBUG
880 Rprintf("\nRequired number of cases: %d",n);
881 Rprintf("\nBuffer contents: |%s|",b->buf);
882 Rprintf("\nLine: %d",b->line);
883 Rprintf("\nPosition: %d",b->pos);
884 Rprintf("\nBuffer remainder: %s",b->buf + b->pos);
885 #endif
886 PROTECT(s_types = coerceVector(s_types,INTSXP));
887 int nvar = length(s_types);
888 int *types = INTEGER(s_types);
889
890 SEXP x, y, data;
891 char *charbuf;
892 int charbuflen = 0;
893 PROTECT(data = allocVector(VECSXP,nvar));
894 int i,j;
895 for(j = 0; j < nvar; j++){
896 if(types[j]==0)
897 SET_VECTOR_ELT(data,j,allocVector(REALSXP,n));
898 else {
899 SET_VECTOR_ELT(data,j,allocVector(STRSXP,n));
900 if(types[j] > charbuflen) charbuflen = types[j];
901 }
902 }
903 charbuf = R_alloc(charbuflen+1,sizeof(char));
904
905 #ifdef DEBUG
906 // PrintValue(data);
907 #endif
908
909 for(i = 0; i < n; i++){
910 if(atEndPorStream(b) || (b->pos < 80 && b->buf[b->pos] == 'Z')){
911 #ifdef DEBUG
912 Rprintf("\nReached end of cases at i=%d",i);
913 #endif
914 int new_length = i;
915 for(j = 0; j < nvar; j++){
916 x = VECTOR_ELT(data,j);
917 SET_VECTOR_ELT(data,j,lengthgets(x,new_length));
918 }
919 n = new_length;
920 break;
921 }
922 #ifdef DEBUG
923 Rprintf("\nCase number: %d\n",i);
924 #endif
925 for(j = 0; j < nvar; j++){
926 if(atEndPorStream(b)) {
927 printPorStreamBuf(b);
928 warning("\nPremature end of data");
929 break;
930 }
931 #ifdef DEBUG
932 PrintValue(VECTOR_ELT(data,j));
933 #endif
934 if(types[j]==0) REAL(VECTOR_ELT(data,j))[i] = readDoublePorStream1(b);
935 else SET_STRING_ELT(VECTOR_ELT(data,j), i,
936 mkChar(readCHARPorStream(b,charbuf,types[j])));
937 #ifdef DEBUG
938 if(i<3 && types[j]>0)
939 PrintValue(STRING_ELT(VECTOR_ELT(data,j),i));
940 #endif
941 }
942 }
943 for(j = 0; j < nvar; j++){
944 x = VECTOR_ELT(what,j);
945 y = VECTOR_ELT(data,j);
946 copyMostAttrib(x,y);
947 }
948 UNPROTECT(2);
949 return data;
950 }
951
952 SEXP countCasesPorStream(SEXP porStream, SEXP s_types){
953 #ifdef DEBUG
954 Rprintf("\n############################");
955 Rprintf("\n#countCasesPorStream");
956 Rprintf("\n############################");
957 #endif
958 porStreamBuf *b = get_porStreamBuf(porStream);
959
960 #ifdef DEBUG
961 Rprintf("\nBuffer contents: |%s|",b->buf);
962 Rprintf("\nLine: %d",b->line);
963 Rprintf("\nPosition: %d",b->pos);
964 Rprintf("\nBuffer remainder: %s",b->buf + b->pos);
965 #endif
966 PROTECT(s_types = AS_INTEGER(s_types));
967 int nvar = LENGTH(s_types);
968 int *types = INTEGER(s_types);
969
970
971 int i, j;
972 char *charbuf;
973 int charbuflen = 0;
974 for(j = 0; j < nvar; j++){
975 if(types[j]!=0 && types[j] > charbuflen) charbuflen = types[j];
976 }
977 charbuf = R_alloc(charbuflen+1,sizeof(char));
978
979 for(i = 0; ; i++){
980 #ifdef DEBUG
981 Rprintf("\n===================");
982 Rprintf("\nCase nr. %d",i);
983 Rprintf("\nBuffer contents: |%s|",b->buf);
984 Rprintf("\nLine: %d",b->line);
985 Rprintf("\nPosition: %d",b->pos);
986 Rprintf("\nCurrent char: '%c'",b->buf[b->pos]);
987 Rprintf("\nBuffer remainder: %s",b->buf + b->pos);
988 #endif
989 if(atEndPorStream(b) || (b->pos < 80 && b->buf[b->pos] == 'Z')){
990 #ifdef DEBUG
991 Rprintf("\nReached end of cases at i=%d",i);
992 #endif
993 break;
994 }
995 #ifdef DEBUG
996 Rprintf("\nCase number: %d nvar = %d\n",i,nvar);
997 #endif
998 for(j = 0; j < nvar; j++){
999 if(atEndPorStream(b)) {
1000 printPorStreamBuf(b);
1001 warning("\nPremature end of data");
1002 break;
1003 }
1004 #ifdef DEBUG1
1005 Rprintf("\n(j = %d)",j);
1006 if(types[j]==0) Rprintf(" %f",readDoublePorStream1(b));
1007 else Rprintf(" '%s'",readCHARPorStream(b,charbuf,types[j]));
1008 #else
1009 if(types[j]==0) readDoublePorStream1(b);
1010 else readCHARPorStream(b,charbuf,types[j]);
1011 #endif
1012 }
1013 #ifdef DEBUG
1014 Rprintf("\n");
1015 #endif
1016 }
1017 UNPROTECT(1);
1018 return ScalarInteger(i);
1019 }
1020 //#undef DEBUG
1021
1022 SEXP readSlicePorStream(SEXP porStream, SEXP what, SEXP s_vars, SEXP s_cases, SEXP s_types){
1023 porStreamBuf *b = get_porStreamBuf(porStream);
1024 PROTECT(s_vars = coerceVector(s_vars,LGLSXP));
1025 PROTECT(s_cases = coerceVector(s_cases,LGLSXP));
1026 PROTECT(s_types = coerceVector(s_types,INTSXP));
1027 int nvar = length(s_types);
1028 int ncases = length(s_cases);
1029 int *types = INTEGER(s_types);
1030 if(LENGTH(s_vars)!=nvar) error("\'s_vars\' argument has wrong length");
1031
1032 int ii,i,j,k, m=0, n = 0;
1033 for(j = 0; j < nvar; j++) m+=LOGICAL(s_vars)[j];
1034 for(i = 0; i < ncases; i++) n+=LOGICAL(s_cases)[i];
1035
1036 SEXP x, y, data;
1037 char *charbuf;
1038 int charbuflen = 0;
1039 PROTECT(data = allocVector(VECSXP,m));
1040 k = 0;
1041 for(j = 0; j < nvar; j++){
1042 if(types[j] > charbuflen) charbuflen = types[j];
1043 if(LOGICAL(s_vars)[j]){
1044 if(types[j]==0)
1045 SET_VECTOR_ELT(data,k,allocVector(REALSXP,n));
1046 else {
1047 SET_VECTOR_ELT(data,k,allocVector(STRSXP,n));
1048 }
1049 k++;
1050 }
1051 }
1052 charbuf = R_alloc(charbuflen+1,sizeof(char));
1053 ii = 0;
1054 for(i = 0; i < ncases; i++){
1055 if(atEndPorStream(b) || (b->pos < 80 && b->buf[b->pos] == 'Z')){
1056 int new_length = ii;
1057 for(j = 0; j < m; j++){
1058 x = VECTOR_ELT(data,j);
1059 SET_VECTOR_ELT(data,j,lengthgets(x,new_length));
1060 }
1061 n = new_length;
1062 break;
1063 }
1064 if(LOGICAL(s_cases)[i]){
1065 k = 0;
1066 for(j = 0; j < nvar; j++){
1067 if(atEndPorStream(b)) {
1068 printPorStreamBuf(b);
1069 warning("\nPremature end of data");
1070 }
1071 if(types[j]==0){
1072 if(LOGICAL(s_vars)[j]){
1073 REAL(VECTOR_ELT(data,k))[ii] = readDoublePorStream1(b);
1074 k++;
1075 }
1076 else {
1077 readDoublePorStream1(b);
1078 }
1079 }
1080 else {
1081 if(LOGICAL(s_vars)[j]){
1082 SET_STRING_ELT(VECTOR_ELT(data,k), ii,
1083 mkChar(readCHARPorStream(b,charbuf,types[j])));
1084 k++;
1085 }
1086 else {
1087 readCHARPorStream(b,charbuf,types[j]);
1088 }
1089 }
1090 }
1091 ii++;
1092 }
1093 else {
1094 for(j = 0; j < nvar; j++){
1095 if(atEndPorStream(b)) {
1096 printPorStreamBuf(b);
1097 error("\nPremature end of data");
1098 }
1099 if(types[j]==0) readDoublePorStream1(b);
1100 else readCHARPorStream(b,charbuf,types[j]);
1101 }
1102 }
1103 }
1104 k = 0;
1105 for(j = 0; j < nvar; j++){
1106 if(LOGICAL(s_vars)[j]){
1107 x = VECTOR_ELT(what,j);
1108 y = VECTOR_ELT(data,k);
1109 copyMostAttrib(x,y);
1110 k++;
1111 }
1112 }
1113
1114 UNPROTECT(4);
1115 return data;
1116 }
1117
1118
1119
1120
1121 SEXP readChunkPorStream(SEXP porStream, SEXP what,
1122 SEXP s_vars, SEXP s_ncases, SEXP s_types){
1123 porStreamBuf *b = get_porStreamBuf(porStream);
1124 PROTECT(s_vars = coerceVector(s_vars,LGLSXP));
1125 PROTECT(s_ncases = coerceVector(s_ncases,INTSXP));
1126 PROTECT(s_types = coerceVector(s_types,INTSXP));
1127 int nvar = length(s_types);
1128 int ncases = INTEGER(s_ncases)[0];
1129 int *types = INTEGER(s_types);
1130 // if(LENGTH(s_vars)!=nvar) error("\'s_vars\' argument has wrong length");
1131
1132 int ii,i,j,k, m=0;
1133 for(j = 0; j < nvar; j++) m+=LOGICAL(s_vars)[j];
1134
1135 SEXP x, y, data;
1136 char *charbuf;
1137 int charbuflen = 0;
1138 PROTECT(data = allocVector(VECSXP,m));
1139 k = 0;
1140 for(j = 0; j < nvar; j++){
1141 if(types[j] > charbuflen) charbuflen = types[j];
1142 if(LOGICAL(s_vars)[j]){
1143 if(types[j]==0)
1144 SET_VECTOR_ELT(data,k,allocVector(REALSXP,ncases));
1145 else {
1146 SET_VECTOR_ELT(data,k,allocVector(STRSXP,ncases));
1147 }
1148 k++;
1149 }
1150 }
1151 charbuf = R_alloc(charbuflen+1,sizeof(char));
1152 ii = 0;
1153 for(i = 0; i < ncases; i++){
1154 if(atEndPorStream(b) || (b->pos < 80 && b->buf[b->pos] == 'Z')){
1155 int new_length = ii;
1156 for(j = 0; j < m; j++){
1157 x = VECTOR_ELT(data,j);
1158 SET_VECTOR_ELT(data,j,lengthgets(x,new_length));
1159 }
1160 break;
1161 }
1162 k = 0;
1163 for(j = 0; j < nvar; j++){
1164 if(atEndPorStream(b)) {
1165 printPorStreamBuf(b);
1166 warning("\nPremature end of data");
1167 }
1168 if(types[j]==0){
1169 if(LOGICAL(s_vars)[j]){
1170 REAL(VECTOR_ELT(data,k))[ii] = readDoublePorStream1(b);
1171 k++;
1172 }
1173 else {
1174 readDoublePorStream1(b);
1175 }
1176 }
1177 else {
1178 if(LOGICAL(s_vars)[j]){
1179 SET_STRING_ELT(VECTOR_ELT(data,k), ii,
1180 mkChar(readCHARPorStream(b,charbuf,types[j])));
1181 k++;
1182 }
1183 else {
1184 readCHARPorStream(b,charbuf,types[j]);
1185 }
1186 }
1187 }
1188 ii++;
1189 }
1190 k = 0;
1191 for(j = 0; j < nvar; j++){
1192 if(LOGICAL(s_vars)[j]){
1193 x = VECTOR_ELT(what,j);
1194 y = VECTOR_ELT(data,k);
1195 copyMostAttrib(x,y);
1196 k++;
1197 }
1198 }
1199
1200 UNPROTECT(4);
1201 return data;
1202 }