| 583 | |
| 584 | |
| 585 | ResultType input_type::SetMatchList(LPTSTR aMatchList, size_t aMatchList_length) |
| 586 | { |
| 587 | LPTSTR *realloc_temp; // Needed since realloc returns NULL on failure but leaves original block allocated. |
| 588 | MatchCount = 0; // Set default. |
| 589 | if (*aMatchList) |
| 590 | { |
| 591 | // If needed, create the array of pointers that points into MatchBuf to each match phrase: |
| 592 | if (!match) |
| 593 | { |
| 594 | if ( !(match = (LPTSTR *)malloc(INPUT_ARRAY_BLOCK_SIZE * sizeof(LPTSTR))) ) |
| 595 | return MemoryError(); // Short msg. since so rare. |
| 596 | MatchCountMax = INPUT_ARRAY_BLOCK_SIZE; |
| 597 | } |
| 598 | // If needed, create or enlarge the buffer that contains all the match phrases: |
| 599 | size_t space_needed = aMatchList_length + 1; // +1 for the final zero terminator. |
| 600 | if (space_needed > MatchBufSize) |
| 601 | { |
| 602 | MatchBufSize = (UINT)(space_needed > 4096 ? space_needed : 4096); |
| 603 | if (MatchBuf) // free the old one since it's too small. |
| 604 | free(MatchBuf); |
| 605 | if ( !(MatchBuf = tmalloc(MatchBufSize)) ) |
| 606 | { |
| 607 | MatchBufSize = 0; |
| 608 | return MemoryError(); // Short msg. since so rare. |
| 609 | } |
| 610 | } |
| 611 | // Copy aMatchList into the match buffer: |
| 612 | LPTSTR source, dest; |
| 613 | for (source = aMatchList, dest = match[MatchCount] = MatchBuf |
| 614 | ; *source; ++source) |
| 615 | { |
| 616 | if (*source != ',') // Not a comma, so just copy it over. |
| 617 | { |
| 618 | *dest++ = *source; |
| 619 | continue; |
| 620 | } |
| 621 | // Otherwise: it's a comma, which becomes the terminator of the previous key phrase unless |
| 622 | // it's a double comma, in which case it's considered to be part of the previous phrase |
| 623 | // rather than the next. |
| 624 | if (*(source + 1) == ',') // double comma |
| 625 | { |
| 626 | *dest++ = *source; |
| 627 | ++source; // Omit the second comma of the pair, i.e. each pair becomes a single literal comma. |
| 628 | continue; |
| 629 | } |
| 630 | // Otherwise, this is a delimiting comma. |
| 631 | *dest = '\0'; |
| 632 | // If the previous item is blank -- which I think can only happen now if the MatchList |
| 633 | // begins with an orphaned comma (since two adjacent commas resolve to one literal comma) |
| 634 | // -- don't add it to the match list: |
| 635 | if (*match[MatchCount]) |
| 636 | { |
| 637 | ++MatchCount; |
| 638 | match[MatchCount] = ++dest; |
| 639 | *dest = '\0'; // Init to prevent crash on orphaned comma such as "btw,otoh," |
| 640 | } |
| 641 | if (*(source + 1)) // There is a next element. |
| 642 | { |
nothing calls this directly
no test coverage detected