| 1 |
#include <ctype.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <QvInput.h> |
| 5 |
#include <QvDebugError.h> |
| 6 |
#include <QvReadError.h> |
| 7 |
#include <QvNode.h> |
| 8 |
|
| 9 |
#define CURVERSION 1.0 // Current version of file format |
| 10 |
#define COMMENT_CHAR '#' |
| 11 |
#define COMPRESS_CHAR 0x1F /* First byte of compressed or gzipped file */ |
| 12 |
|
| 13 |
static const int numValidASCIIHeaders = 1; |
| 14 |
struct headerStorage |
| 15 |
{ |
| 16 |
const char *string; |
| 17 |
float version; |
| 18 |
}; |
| 19 |
|
| 20 |
static const headerStorage ASCIIHeader[1] = { |
| 21 |
{ "#VRML V1.0 ascii", /* 20 chars */ 1.0 }, |
| 22 |
}; |
| 23 |
|
| 24 |
float |
| 25 |
QvInput::isASCIIHeader(const char *string) |
| 26 |
{ |
| 27 |
for (int i = 0; i < numValidASCIIHeaders; i++) { |
| 28 |
if (strcmp(ASCIIHeader[i].string, string) == 0) |
| 29 |
return ASCIIHeader[i].version; |
| 30 |
} |
| 31 |
return FALSE; |
| 32 |
} |
| 33 |
|
| 34 |
QvInput::QvInput() |
| 35 |
{ |
| 36 |
setFilePointer(stdin); |
| 37 |
} |
| 38 |
|
| 39 |
QvInput::~QvInput() |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
void |
| 44 |
QvInput::setFilePointer(FILE *newFP) |
| 45 |
{ |
| 46 |
fp = newFP; |
| 47 |
lineNum = 1; |
| 48 |
version = CURVERSION; |
| 49 |
readHeader = FALSE; |
| 50 |
headerOk = TRUE; |
| 51 |
backBufIndex = -1; |
| 52 |
} |
| 53 |
|
| 54 |
float |
| 55 |
QvInput::getVersion() |
| 56 |
{ |
| 57 |
if (! readHeader) |
| 58 |
(void) checkHeader(); |
| 59 |
|
| 60 |
return version; |
| 61 |
} |
| 62 |
|
| 63 |
QvBool |
| 64 |
QvInput::get(char &c) |
| 65 |
{ |
| 66 |
QvBool ret; |
| 67 |
|
| 68 |
if (backBufIndex >= 0) { |
| 69 |
c = backBuf.getString()[backBufIndex++]; |
| 70 |
|
| 71 |
if (c != '\0') |
| 72 |
return TRUE; |
| 73 |
|
| 74 |
backBuf.makeEmpty(); |
| 75 |
backBufIndex = -1; |
| 76 |
} |
| 77 |
|
| 78 |
if (! readHeader && ! checkHeader()) |
| 79 |
return FALSE; |
| 80 |
|
| 81 |
if (eof()) { |
| 82 |
c = (char)EOF; |
| 83 |
ret = FALSE; |
| 84 |
} |
| 85 |
|
| 86 |
else { |
| 87 |
int i = getc(fp); |
| 88 |
|
| 89 |
if (i == EOF) { |
| 90 |
c = (char)EOF; |
| 91 |
ret = FALSE; |
| 92 |
} |
| 93 |
else { |
| 94 |
c = (char) i; |
| 95 |
ret = TRUE; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return ret; |
| 100 |
} |
| 101 |
|
| 102 |
QvBool |
| 103 |
QvInput::read(char &c) |
| 104 |
{ |
| 105 |
return (skipWhiteSpace() && get(c)); |
| 106 |
} |
| 107 |
|
| 108 |
QvBool |
| 109 |
QvInput::read(QvString &s) |
| 110 |
{ |
| 111 |
if (! skipWhiteSpace()) |
| 112 |
return FALSE; |
| 113 |
|
| 114 |
QvBool quoted; |
| 115 |
char c; |
| 116 |
char bufStore[256]; |
| 117 |
char *buf; |
| 118 |
int bytesLeft; |
| 119 |
|
| 120 |
s.makeEmpty(); |
| 121 |
|
| 122 |
if (! get(c)) |
| 123 |
return FALSE; |
| 124 |
|
| 125 |
quoted = (c == '\"'); |
| 126 |
if (! quoted) |
| 127 |
putBack(c); |
| 128 |
|
| 129 |
do { |
| 130 |
buf = bufStore; |
| 131 |
bytesLeft = sizeof(bufStore) - 1; |
| 132 |
|
| 133 |
while (bytesLeft > 0) { |
| 134 |
|
| 135 |
if (! get(*buf)) |
| 136 |
break; |
| 137 |
|
| 138 |
if (quoted) { |
| 139 |
if (*buf == '\"') |
| 140 |
break; |
| 141 |
|
| 142 |
if (*buf == '\\') { |
| 143 |
if ((get(c)) && c == '\"') |
| 144 |
*buf = '\"'; |
| 145 |
else |
| 146 |
putBack(c); |
| 147 |
} |
| 148 |
|
| 149 |
if (*buf == '\n') |
| 150 |
lineNum++; |
| 151 |
} |
| 152 |
|
| 153 |
else if (isspace(*buf)) { |
| 154 |
putBack(*buf); |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
buf++; |
| 159 |
bytesLeft--; |
| 160 |
} |
| 161 |
*buf = '\0'; |
| 162 |
|
| 163 |
s += bufStore; |
| 164 |
|
| 165 |
} while (bytesLeft == 0); |
| 166 |
|
| 167 |
return TRUE; |
| 168 |
} |
| 169 |
|
| 170 |
QvBool |
| 171 |
QvInput::read(QvName &n, QvBool validIdent) |
| 172 |
{ |
| 173 |
QvBool gotChar; |
| 174 |
|
| 175 |
if (! skipWhiteSpace()) |
| 176 |
return FALSE; |
| 177 |
|
| 178 |
if (! validIdent) { |
| 179 |
QvString s; |
| 180 |
|
| 181 |
if (! read(s)) |
| 182 |
return FALSE; |
| 183 |
|
| 184 |
n = s; |
| 185 |
} |
| 186 |
|
| 187 |
else { |
| 188 |
char buf[256]; |
| 189 |
char *b = buf; |
| 190 |
char c; |
| 191 |
|
| 192 |
if ((gotChar = get(c)) && QvName::isIdentStartChar(c)) { |
| 193 |
*b++ = c; |
| 194 |
|
| 195 |
while ((gotChar = get(c)) && QvName::isIdentChar(c)) { |
| 196 |
if (b - buf < 255) |
| 197 |
*b++ = c; |
| 198 |
} |
| 199 |
} |
| 200 |
*b = '\0'; |
| 201 |
|
| 202 |
if (gotChar) |
| 203 |
putBack(c); |
| 204 |
|
| 205 |
n = buf; |
| 206 |
} |
| 207 |
|
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
|
| 211 |
#define READ_NUM(reader, readType, num, type) \ |
| 212 |
QvBool ok; \ |
| 213 |
if (! skipWhiteSpace()) \ |
| 214 |
ok = FALSE; \ |
| 215 |
else { \ |
| 216 |
readType _tmp; \ |
| 217 |
ok = reader(_tmp); \ |
| 218 |
if (ok) \ |
| 219 |
num = (type) _tmp; \ |
| 220 |
} \ |
| 221 |
return ok |
| 222 |
|
| 223 |
#define READ_INTEGER(num, type) \ |
| 224 |
READ_NUM(readInteger, long, num, type) |
| 225 |
|
| 226 |
#define READ_UNSIGNED_INTEGER(num, type) \ |
| 227 |
READ_NUM(readUnsignedInteger, unsigned long, num, type) |
| 228 |
|
| 229 |
#define READ_REAL(num, type) \ |
| 230 |
READ_NUM(readReal, double, num, type) |
| 231 |
|
| 232 |
QvBool |
| 233 |
QvInput::read(int &i) |
| 234 |
{ |
| 235 |
READ_INTEGER(i, int); |
| 236 |
} |
| 237 |
|
| 238 |
QvBool |
| 239 |
QvInput::read(unsigned int &i) |
| 240 |
{ |
| 241 |
READ_UNSIGNED_INTEGER(i, unsigned int); |
| 242 |
} |
| 243 |
|
| 244 |
QvBool |
| 245 |
QvInput::read(short &s) |
| 246 |
{ |
| 247 |
READ_INTEGER(s, short); |
| 248 |
} |
| 249 |
|
| 250 |
QvBool |
| 251 |
QvInput::read(unsigned short &s) |
| 252 |
{ |
| 253 |
READ_UNSIGNED_INTEGER(s, unsigned short); |
| 254 |
} |
| 255 |
|
| 256 |
QvBool |
| 257 |
QvInput::read(long &l) |
| 258 |
{ |
| 259 |
READ_INTEGER(l, long); |
| 260 |
} |
| 261 |
|
| 262 |
QvBool |
| 263 |
QvInput::read(unsigned long &l) |
| 264 |
{ |
| 265 |
READ_UNSIGNED_INTEGER(l, unsigned long); |
| 266 |
} |
| 267 |
|
| 268 |
QvBool |
| 269 |
QvInput::read(float &f) |
| 270 |
{ |
| 271 |
READ_REAL(f, float); |
| 272 |
} |
| 273 |
|
| 274 |
QvBool |
| 275 |
QvInput::read(double &d) |
| 276 |
{ |
| 277 |
READ_REAL(d, double); |
| 278 |
} |
| 279 |
|
| 280 |
QvBool |
| 281 |
QvInput::eof() const |
| 282 |
{ |
| 283 |
return feof(fp); |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
QvInput::getLocationString(QvString &string) const |
| 288 |
{ |
| 289 |
char buf[128]; |
| 290 |
sprintf(buf, "\tOccurred at line %3d", lineNum); |
| 291 |
string = buf; |
| 292 |
} |
| 293 |
|
| 294 |
void |
| 295 |
QvInput::putBack(char c) |
| 296 |
{ |
| 297 |
if (c == (char) EOF) |
| 298 |
return; |
| 299 |
|
| 300 |
if (backBufIndex >= 0) |
| 301 |
--backBufIndex; |
| 302 |
else |
| 303 |
ungetc(c, fp); |
| 304 |
} |
| 305 |
|
| 306 |
void |
| 307 |
QvInput::putBack(const char *string) |
| 308 |
{ |
| 309 |
backBuf = string; |
| 310 |
backBufIndex = 0; |
| 311 |
} |
| 312 |
|
| 313 |
#if defined(unix) || defined(__unix) |
| 314 |
# include <unistd.h> |
| 315 |
|
| 316 |
static FILE * |
| 317 |
spliceDecompress(QvInput *in, FILE *fp, char c) |
| 318 |
{ |
| 319 |
int pfd[2], zpfd[2]; |
| 320 |
int k = 0; |
| 321 |
FILE *togz = (FILE *)NULL; |
| 322 |
|
| 323 |
/* Build a pipeline: |
| 324 |
* 0x1F+"in"data->zpfd[1] => zpfd[0]->gzip->pfd[1] => pfd[0]->parser |
| 325 |
*/ |
| 326 |
if(pipe(pfd) < 0 || pipe(zpfd) < 0) { |
| 327 |
QvReadError::post(in, "Can't make pipe to read compressed VRML"); |
| 328 |
return NULL; |
| 329 |
} |
| 330 |
switch(fork()) { |
| 331 |
case -1: |
| 332 |
QvReadError::post(in, "Can't fork subprocess to decompress VRML"); |
| 333 |
return NULL; |
| 334 |
|
| 335 |
case 0: |
| 336 |
close(pfd[0]); |
| 337 |
|
| 338 |
switch(fork()) { |
| 339 |
case -1: |
| 340 |
QvReadError::post(in, "Can't fork subprocess to decompress VRML"); |
| 341 |
_exit(1); |
| 342 |
|
| 343 |
case 0: |
| 344 |
int ch; |
| 345 |
togz = fdopen(zpfd[1], "w"); |
| 346 |
close(pfd[1]); |
| 347 |
close(zpfd[0]); |
| 348 |
putc(c, togz); |
| 349 |
while((ch = getc(fp)) != EOF) { |
| 350 |
putc(ch, togz); |
| 351 |
k++; |
| 352 |
} |
| 353 |
fflush(togz); |
| 354 |
_exit(0); |
| 355 |
|
| 356 |
default: |
| 357 |
close(zpfd[1]); |
| 358 |
if(zpfd[0] != 0) { |
| 359 |
dup2(zpfd[0], 0); |
| 360 |
close(zpfd[0]); |
| 361 |
} |
| 362 |
if(pfd[1] != 1) { |
| 363 |
dup2(pfd[1], 1); |
| 364 |
close(pfd[1]); |
| 365 |
} |
| 366 |
execlp("gzip", "gzip", "-d", NULL); |
| 367 |
execlp("compress", "compress", "-d", NULL); |
| 368 |
QvReadError::post(in, "Can't exec gzip nor compress to read compressed VRML"); |
| 369 |
_exit(1); |
| 370 |
} |
| 371 |
|
| 372 |
default: |
| 373 |
close(pfd[1]); |
| 374 |
close(zpfd[0]); |
| 375 |
close(zpfd[1]); |
| 376 |
return fdopen(pfd[0], "r"); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
#endif /* unix */ |
| 381 |
|
| 382 |
|
| 383 |
QvBool |
| 384 |
QvInput::checkHeader() |
| 385 |
{ |
| 386 |
char c; |
| 387 |
|
| 388 |
readHeader = TRUE; |
| 389 |
|
| 390 |
/* Read first character cautiously, in case we need to reroute input. |
| 391 |
* Ugh. Is there no clean way to do this? |
| 392 |
*/ |
| 393 |
|
| 394 |
int got; |
| 395 |
|
| 396 |
#if defined(unix) || defined(__unix) |
| 397 |
got = ::read(fileno(fp), &c, 1) > 0; |
| 398 |
if(got && c == COMPRESS_CHAR) { |
| 399 |
if((fp = spliceDecompress(this, fp, c)) == NULL) { |
| 400 |
headerOk = FALSE; |
| 401 |
return FALSE; |
| 402 |
} |
| 403 |
got = get(c); |
| 404 |
} |
| 405 |
#else |
| 406 |
got = get(c); |
| 407 |
#endif |
| 408 |
|
| 409 |
if(got) { |
| 410 |
if (c == COMMENT_CHAR) { |
| 411 |
char buf[256]; |
| 412 |
int i = 0; |
| 413 |
|
| 414 |
buf[i++] = c; |
| 415 |
while (get(c) && c != '\n') |
| 416 |
buf[i++] = c; |
| 417 |
if(i>0 && buf[i-1] == '\r') |
| 418 |
i--; |
| 419 |
buf[i] = '\0'; |
| 420 |
if (c == '\n') |
| 421 |
lineNum++; |
| 422 |
|
| 423 |
if ((version = isASCIIHeader(buf))) |
| 424 |
return TRUE; |
| 425 |
} |
| 426 |
else |
| 427 |
putBack(c); |
| 428 |
} |
| 429 |
|
| 430 |
QvReadError::post(this, "File does not have a valid header string"); |
| 431 |
headerOk = FALSE; |
| 432 |
return FALSE; |
| 433 |
} |
| 434 |
|
| 435 |
QvBool |
| 436 |
QvInput::skipWhiteSpace() |
| 437 |
{ |
| 438 |
char c; |
| 439 |
QvBool gotChar; |
| 440 |
|
| 441 |
if (! readHeader && ! checkHeader()) |
| 442 |
return FALSE; |
| 443 |
|
| 444 |
while (TRUE) { |
| 445 |
|
| 446 |
while ((gotChar = get(c)) && isspace(c)) |
| 447 |
if (c == '\n') |
| 448 |
lineNum++; |
| 449 |
|
| 450 |
if (! gotChar) |
| 451 |
break; |
| 452 |
|
| 453 |
if (c == COMMENT_CHAR) { |
| 454 |
while (get(c) && c != '\n') |
| 455 |
; |
| 456 |
|
| 457 |
if (eof()) |
| 458 |
QvReadError::post(this, "EOF reached before end of comment"); |
| 459 |
else |
| 460 |
lineNum++; |
| 461 |
} |
| 462 |
else { |
| 463 |
putBack(c); |
| 464 |
break; } |
| 465 |
} |
| 466 |
|
| 467 |
return TRUE; |
| 468 |
} |
| 469 |
|
| 470 |
QvBool |
| 471 |
QvInput::readInteger(long &l) |
| 472 |
{ |
| 473 |
char str[32]; |
| 474 |
char *s = str; |
| 475 |
|
| 476 |
if (readChar(s, '-') || readChar(s, '+')) |
| 477 |
s++; |
| 478 |
|
| 479 |
if (! readUnsignedIntegerString(s)) |
| 480 |
return FALSE; |
| 481 |
|
| 482 |
l = strtol(str, NULL, 0); |
| 483 |
|
| 484 |
return TRUE; |
| 485 |
} |
| 486 |
|
| 487 |
QvBool |
| 488 |
QvInput::readUnsignedInteger(unsigned long &l) |
| 489 |
{ |
| 490 |
char str[32]; |
| 491 |
if (! readUnsignedIntegerString(str)) |
| 492 |
return FALSE; |
| 493 |
#ifdef sun |
| 494 |
l = atol(str); |
| 495 |
#else |
| 496 |
l = strtoul(str, NULL, 0); |
| 497 |
#endif |
| 498 |
|
| 499 |
return TRUE; |
| 500 |
} |
| 501 |
|
| 502 |
QvBool |
| 503 |
QvInput::readUnsignedIntegerString(char *str) |
| 504 |
{ |
| 505 |
int minSize = 1; |
| 506 |
char *s = str; |
| 507 |
|
| 508 |
if (readChar(s, '0')) { |
| 509 |
|
| 510 |
if (readChar(s + 1, 'x')) { |
| 511 |
s += 2 + readHexDigits(s + 2); |
| 512 |
minSize = 3; |
| 513 |
} |
| 514 |
|
| 515 |
else |
| 516 |
s += 1 + readDigits(s + 1); |
| 517 |
} |
| 518 |
|
| 519 |
else |
| 520 |
s += readDigits(s); |
| 521 |
|
| 522 |
if (s - str < minSize) |
| 523 |
return FALSE; |
| 524 |
|
| 525 |
*s = '\0'; |
| 526 |
|
| 527 |
return TRUE; |
| 528 |
} |
| 529 |
|
| 530 |
QvBool |
| 531 |
QvInput::readReal(double &d) |
| 532 |
{ |
| 533 |
char str[32]; |
| 534 |
int n; |
| 535 |
char *s = str; |
| 536 |
QvBool gotNum = FALSE; |
| 537 |
|
| 538 |
n = readChar(s, '-'); |
| 539 |
if (n == 0) |
| 540 |
n = readChar(s, '+'); |
| 541 |
s += n; |
| 542 |
|
| 543 |
if ((n = readDigits(s)) > 0) { |
| 544 |
gotNum = TRUE; |
| 545 |
s += n; |
| 546 |
} |
| 547 |
|
| 548 |
if (readChar(s, '.') > 0) { |
| 549 |
s++; |
| 550 |
|
| 551 |
if ((n = readDigits(s)) > 0) { |
| 552 |
gotNum = TRUE; |
| 553 |
s += n; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
if (! gotNum) |
| 558 |
return FALSE; |
| 559 |
|
| 560 |
n = readChar(s, 'e'); |
| 561 |
if (n == 0) |
| 562 |
n = readChar(s, 'E'); |
| 563 |
|
| 564 |
if (n > 0) { |
| 565 |
s += n; |
| 566 |
|
| 567 |
n = readChar(s, '-'); |
| 568 |
if (n == 0) |
| 569 |
n = readChar(s, '+'); |
| 570 |
s += n; |
| 571 |
|
| 572 |
if ((n = readDigits(s)) > 0) |
| 573 |
s += n; |
| 574 |
|
| 575 |
else |
| 576 |
return FALSE; } |
| 577 |
|
| 578 |
*s = '\0'; |
| 579 |
|
| 580 |
d = atof(str); |
| 581 |
|
| 582 |
return TRUE; |
| 583 |
} |
| 584 |
|
| 585 |
int |
| 586 |
QvInput::readDigits(char *string) |
| 587 |
{ |
| 588 |
char c, *s = string; |
| 589 |
|
| 590 |
while (get(c)) { |
| 591 |
|
| 592 |
if (isdigit(c)) |
| 593 |
*s++ = c; |
| 594 |
|
| 595 |
else { |
| 596 |
putBack(c); |
| 597 |
break; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
return s - string; |
| 602 |
} |
| 603 |
|
| 604 |
int |
| 605 |
QvInput::readHexDigits(char *string) |
| 606 |
{ |
| 607 |
char c, *s = string; |
| 608 |
|
| 609 |
while (get(c)) { |
| 610 |
|
| 611 |
if (isxdigit(c)) |
| 612 |
*s++ = c; |
| 613 |
|
| 614 |
else { |
| 615 |
putBack(c); |
| 616 |
break; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
return s - string; |
| 621 |
} |
| 622 |
|
| 623 |
int |
| 624 |
QvInput::readChar(char *string, char charToRead) |
| 625 |
{ |
| 626 |
char c; |
| 627 |
int ret; |
| 628 |
|
| 629 |
if (! get(c)) |
| 630 |
ret = 0; |
| 631 |
|
| 632 |
else if (c == charToRead) { |
| 633 |
*string = c; |
| 634 |
ret = 1; |
| 635 |
} |
| 636 |
|
| 637 |
else { |
| 638 |
putBack(c); |
| 639 |
ret = 0; |
| 640 |
} |
| 641 |
|
| 642 |
return ret; |
| 643 |
} |
| 644 |
|
| 645 |
void |
| 646 |
QvInput::addReference(const QvName &name, QvNode *node) |
| 647 |
{ |
| 648 |
refDict.enter((u_long) name.getString(), (void *) node); |
| 649 |
|
| 650 |
node->setName(name); |
| 651 |
} |
| 652 |
|
| 653 |
QvNode * |
| 654 |
QvInput::findReference(const QvName &name) const |
| 655 |
{ |
| 656 |
void *node; |
| 657 |
|
| 658 |
if (refDict.find((u_long) name.getString(), node)) |
| 659 |
return (QvNode *) node; |
| 660 |
|
| 661 |
return NULL; |
| 662 |
} |