464 {
465 INTDBG(
"ENTER: original: %p, *original: %s, insertion: %s, replaces: %d, start_index: %d\n", original, *original, insertion, replaces, start_index);
466
467 int orig_len=0;
468 int ins_len=0;
469 char *subtoken;
470 char *workBuf;
471 char *workPtr;
472 char *tok_save_ptr=NULL;
473 char *newOriginal;
474 char *newInsertion;
475 char *newFormula;
476 int insert_events;
477
478 if (*original != NULL) {
479 orig_len = strlen(*original);
480 }
481 if (insertion != NULL) {
482 ins_len = strlen(insertion);
483 }
484 newFormula =
papi_calloc (orig_len + ins_len + 40, 1);
485
486
487 if (insertion == NULL) {
488
489 workPtr = *original;
490 if (workPtr[0] == '|') {
491 strcpy(newFormula, &workPtr[1]);
492 } else {
493 strcpy(newFormula, workPtr);
494 }
495
497 *original = newFormula;
498 INTDBG(
"EXIT: newFormula: %s\n", *original);
499 return;
500 }
501
502
503
504 insert_events = 0;
508 subtoken = strtok_r(workPtr, "|", &tok_save_ptr);
509 while ( subtoken != NULL) {
510
511 if ((subtoken[0] == 'N') && (isdigit(subtoken[1]))) {
512 insert_events++;
513 int val = atoi(&subtoken[1]);
514 val += start_index;
515 subtoken[1] = '\0';
516 sprintf (workBuf, "N%d", val);
517 } else {
518 strcpy(workBuf, subtoken);
519 }
520 strcat (newInsertion, workBuf);
521 strcat (newInsertion, "|");
522 subtoken = strtok_r(NULL, "|", &tok_save_ptr);
523 }
526 INTDBG(
"newInsertion: %s\n", newInsertion);
527
528
529
530 if (*original == NULL) {
532 INTDBG(
"EXIT: newFormula: %s\n", newInsertion);
535 return;
536 }
537
538
539 if ((replaces < 0)) {
542 INTDBG(
"EXIT: Invalid value for token in original formula to be replaced\n");
543 return;
544 }
545
546
547
551
552 subtoken = strtok_r(workPtr, "|", &tok_save_ptr);
553 while ( subtoken != NULL) {
554
555
556 strcpy(workBuf, subtoken);
557 if ((subtoken[0] == 'N') && (isdigit(subtoken[1]))) {
558 int val = atoi(&subtoken[1]);
559 if (val > replaces) {
560 val += insert_events-1;
561 subtoken[1] = '\0';
562 sprintf (workBuf, "N%d", val);
563 }
564 }
565
566 strcat (newOriginal, workBuf);
567 strcat (newOriginal, "|");
568 subtoken = strtok_r(NULL, "|", &tok_save_ptr);
569 }
572 INTDBG(
"newOriginal: %s\n", newOriginal);
573
574
575 newFormula[0] = '\0';
576 workPtr = newOriginal;
577 subtoken = strtok_r(workPtr, "|", &tok_save_ptr);
578 while ( subtoken != NULL) {
579
580
581 if ((subtoken[0] == 'N') && (isdigit(subtoken[1])) && (replaces == atoi(&subtoken[1]))) {
582
583 strcat(newFormula, newInsertion);
584 } else {
585
586 strcat(newFormula, subtoken);
587 strcat(newFormula, "|");
588 }
589 subtoken = strtok_r(NULL, "|", &tok_save_ptr);
590 }
593
594
596 *original = newFormula;
597 INTDBG(
"EXIT: newFormula: %s\n", newFormula);
598 return;
599}
#define papi_calloc(a, b)