QI_MAKE 1-SEP-1993 10:03:22 VAX C V3.2-044 Page 1 V1.0 30-AUG-1993 16:39:05 X$SRC:[CSO]QI_MAKE.C;2 (1) 1 /* qi_make - convert a sequential file to a format suitable for a qi build */ 2 3 /* 1993/08/30 release for beta testing */ 4 5 6 #include stdio 268 #include string 314 #include ctype 377 #include "qi.h" 473 474 #define MAX_PARAMS 100 475 476 struct param { 477 int pos; 478 int size; 479 int field; 480 int cap; 481 int bz; 482 char *pre; 483 char *post; 484 } params[MAX_PARAMS + 1]; 485 486 void cap(char *); 487 void trim(char *); 488 489 490 main(int argc, char *argv[]) 491 { 492 1 493 1 FILE *in, *out; 494 1 char record[512], field[128]; 495 1 char filename[256], temp[256], *cp; 496 1 int pidx, index; 497 1 498 1 if (argc < 3) { 499 2 printf("Usage: make input output [base]\n"); 500 2 exit(); 501 2 } 502 1 503 1 strcpy(filename, argv[1]); 504 1 if (cp = strchr(filename, '.')) *cp = '\0'; 505 1 strcat(filename, ".qi_make"); 506 1 507 1 if (read_params(filename) == FALSE) { 508 2 sprintf(temp, "can't open parameter file %s", filename); 509 2 perror(temp); 510 2 exit(); 511 2 } 512 1 513 1 if ((in = fopen(argv[1], "r")) == NULL) { 514 2 perror("Can't open input file"); 515 2 exit(); 516 2 } 517 1 if ((out = fopen(argv[2], "w")) == NULL) { 518 2 perror("Can't open output file"); QI_MAKE 1-SEP-1993 10:03:22 VAX C V3.2-044 Page 2 V1.0 30-AUG-1993 16:39:05 X$SRC:[CSO]QI_MAKE.C;2 (1) 519 2 exit(); 520 2 } 521 1 if (argc == 4) 522 1 index = atoi(argv[3]); 523 1 else 524 1 index = 0; 525 1 printf("ID base set to %d\n", index); 526 1 527 1 while (fgets(record, sizeof(record), in)) { 528 2 index++; /* it really starts at 1 */ 529 2 if ((index % 100) == 0) printf("%d\n", index); 530 2 for (pidx = 0; params[pidx].size >= 0; pidx++) { 531 3 strncpy(field, "", sizeof(field)); 532 3 strncpy(field, record + params[pidx].pos, params[pidx].size); 533 3 if (params[pidx].cap) cap(field); 534 3 trim(field); 535 3 if ((params[pidx].size == 0) || 536 3 ((strlen(field) > 0) && 537 3 (!params[pidx].bz || 538 3 (atoi(field) > 0)))) 539 3 fprintf(out, "%0*d%0*d%0*d%0*d%s%s%s\n", 540 3 ID_SIZE, index, 541 3 FIELD_SIZE, params[pidx].field, 542 3 SEQ_SIZE, 0, ATTR_SIZE, 0, 543 3 params[pidx].pre, field, params[pidx].post); 544 3 } 545 2 } 546 1 fclose(in); 547 1 fclose(out); 548 1 } 549 550 551 int read_params(char *name) 552 { 553 1 FILE *fp; 554 1 int ind = 0; 555 1 char *cp, *cp2, val[10], line[256]; 556 1 557 1 fp = fopen(name, "r"); 558 1 559 1 if (fp == NULL) return (FALSE); 560 1 561 1 while (fgets(line, sizeof(line), fp)) { 562 2 if (line[0] == '#') continue; 563 2 cp = line; 564 2 cp2 = strchr(cp,','); 565 2 strncpy(val, "", sizeof(val)); 566 2 strncpy(val, cp, cp2 - cp); 567 2 params[ind].pos = atoi(val) - 1; /* origin 0 into field */ 568 2 cp = cp2 + 1; 569 2 570 2 cp2 = strchr(cp,','); 571 2 strncpy(val, "", sizeof(val)); 572 2 strncpy(val, cp, cp2 - cp); 573 2 params[ind].size = atoi(val); 574 2 cp = cp2 + 1; 575 2 QI_MAKE 1-SEP-1993 10:03:22 VAX C V3.2-044 Page 3 V1.0 30-AUG-1993 16:39:05 X$SRC:[CSO]QI_MAKE.C;2 (1) 576 2 cp2 = strchr(cp,','); 577 2 strncpy(val, "", sizeof(val)); 578 2 strncpy(val, cp, cp2 - cp); 579 2 params[ind].field = atoi(val); 580 2 cp = cp2 + 1; 581 2 582 2 cp2 = strchr(cp,','); 583 2 if (cp2 == NULL) 584 2 cp2 = strchr(cp, '\0'); 585 2 strncpy(val, "", sizeof(val)); 586 2 strncpy(val, cp, cp2 - cp); 587 2 params[ind].cap = atoi(val); 588 2 cp = cp2 + 1; 589 2 590 2 cp2 = strchr(cp,','); 591 2 if (cp2 == NULL) 592 2 cp2 = strchr(cp, '\0'); 593 2 strncpy(val, "", sizeof(val)); 594 2 strncpy(val, cp, cp2 - cp); 595 2 params[ind].bz = atoi(val); 596 2 cp = cp2 + 1; 597 2 598 2 cp2 = strchr(cp,','); 599 2 if (cp2 == NULL) 600 2 cp2 = strchr(cp, '\0'); 601 2 params[ind].pre = calloc((cp2 - cp) + 1, sizeof(char)); 602 2 strncpy(params[ind].pre, cp, cp2 - cp); 603 2 cp = cp2 + 1; 604 2 605 2 cp2 = strchr(cp,','); 606 2 if (cp2 == NULL) 607 2 cp2 = strchr(cp, '\0'); 608 2 params[ind].post = calloc((cp2 - cp) + 1, sizeof(char)); 609 2 strncpy(params[ind].post, cp, cp2 - cp); 610 2 cp = cp2 + 1; 611 2 if (++ind == MAX_PARAMS) { 612 3 printf("Parameter file contains more than %d rules\n", MAX_PARAMS); 613 3 break; 614 3 } 615 2 } 616 1 params[ind].size = -1; 617 1 618 1 fclose(fp); 619 1 return (TRUE); 620 1 } 621 622 void trim(char *s) 623 { 624 1 while ((strlen(s) > 0) && (s[strlen(s)-1] == ' ')) 625 1 s[strlen(s)-1] = '\0'; 626 1 } 627 628 void cap(char *s) 629 { 630 1 char *ptr; 631 1 632 1 *s = _toupper(*s); QI_MAKE 1-SEP-1993 10:03:22 VAX C V3.2-044 Page 4 V1.0 30-AUG-1993 16:39:05 X$SRC:[CSO]QI_MAKE.C;2 (1) 633 1 for (ptr = s + 1; *ptr; ptr++) { 634 2 *ptr = _tolower(*ptr); 635 2 if (*ptr == ',') *ptr = ' '; 636 2 if (!isalnum(*(ptr - 1))) 637 2 *ptr = _toupper(*ptr); 638 2 if ((*(ptr - 1) == 'I') && (*ptr == 'i')) 639 2 *ptr = _toupper(*ptr); 640 2 } 641 1 } 642 Command Line ------------ CC/DEBUG/NOOP/LIST QI_MAKE .